Skip to content

Commit c0b5463

Browse files
committed
fixed germplasm pedigree endpoint
1 parent 85fd77e commit c0b5463

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

initdb/113__germplasm_pedigree_get.sql

+41-29
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ CREATE OR REPLACE FUNCTION get_pedigree(
33
"germplasmDbId" text
44
)
55
RETURNS json AS $$
6+
DECLARE
7+
pedigree_node_id text;
68
BEGIN
9+
SELECT id INTO pedigree_node_id FROM pedigree_node WHERE germplasm_id = "germplasmDbId";
10+
711
RETURN (
812
SELECT json_build_object(
913
'@context', '[]'::json,
@@ -15,49 +19,57 @@ BEGIN
1519
'familyCode', node.family_code,
1620
'germplasmDbId', "germplasmDbId",
1721
'germplasmName', germ.germplasm_name,
18-
'parents', json_agg(
22+
'parents', COALESCE(
1923
(
20-
SELECT json_build_object(
21-
'germplasmDbId', parent.id,
22-
'germplasmName', parent.germplasm_name,
23-
'parentType', to_parent_type(e.parent_type)
24-
)
24+
SELECT
25+
json_agg(
26+
json_build_object(
27+
'germplasmDbId', parent.id,
28+
'germplasmName', parent.germplasm_name,
29+
'parentType', to_parent_type(e.parent_type)
30+
)
31+
)
2532
FROM
2633
pedigree_node parent_node
2734
JOIN
28-
pedigree_edge e ON parent_node.id = e.connceted_node_id AND e.edge_type = edge_type_to_int('parent') -- TODO: hardcoded EdgeType (parent=0, child=1, sibling=2)
35+
pedigree_edge e ON parent_node.id = e.connceted_node_id AND e.edge_type = edge_type_to_int('parent')
2936
JOIN
3037
germplasm parent ON parent_node.germplasm_id = parent.id
31-
WHERE e.this_node_id = (SELECT child_node.id
38+
WHERE
39+
e.this_node_id = (SELECT child_node.id
3240
FROM pedigree_node child_node
3341
WHERE child_node.germplasm_id = "germplasmDbId")
34-
)
42+
),
43+
'[]'::json
3544
),
3645
'pedigree', node.pedigree_string,
37-
'siblings', json_agg(
46+
'siblings', COALESCE(
3847
(
3948
SELECT
40-
json_build_object(
41-
'germplasmDbId', sibling.id,
42-
'germplasmName', sibling.germplasm_name
43-
)
49+
json_agg(
50+
json_build_object(
51+
'germplasmDbId', sibling.id,
52+
'germplasmName', sibling.germplasm_name
53+
)
54+
)
4455
FROM
45-
pedigree_node sibling_node
46-
JOIN
47-
pedigree_edge pe ON sibling_node.id = pe.this_node_id AND pe.edge_type = edge_type_to_int('parent')
48-
JOIN
49-
germplasm sibling ON sibling_node.germplasm_id = sibling.id
56+
pedigree_node sibling_node
57+
JOIN
58+
pedigree_edge pe ON sibling_node.id = pe.this_node_id AND pe.edge_type = edge_type_to_int('parent')
59+
JOIN
60+
germplasm sibling ON sibling_node.germplasm_id = sibling.id
5061
WHERE
51-
pe.connceted_node_id IN
52-
(
53-
SELECT parent_node.id
54-
FROM
55-
pedigree_node parent_node
56-
JOIN
57-
pedigree_edge e ON parent_node.id = e.connceted_node_id AND e.edge_type = edge_type_to_int('parent')
58-
WHERE e.this_node_id = (SELECT id FROM pedigree_node WHERE germplasm_id = "germplasmDbId")
59-
)
60-
)
62+
pe.connceted_node_id IN
63+
(
64+
SELECT parent_node.id
65+
FROM
66+
pedigree_node parent_node
67+
JOIN
68+
pedigree_edge e ON parent_node.id = e.connceted_node_id AND e.edge_type = edge_type_to_int('parent')
69+
WHERE e.this_node_id = pedigree_node_id
70+
)
71+
),
72+
'[]'::json
6173
)
6274
)
6375
FROM

server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def get_progeny(germplasmDbId: str):
204204

205205

206206
@app.get("/brapi/v2/germplasm/<germplasmDbId>/pedigree")
207-
def get_progeny(germplasmDbId: str):
207+
def get_pedigree(germplasmDbId: str):
208208
with psycopg.connect(CONN_STR) as conn:
209209
with conn.cursor() as cur:
210210
cur.execute("SELECT get_pedigree(%s);", (germplasmDbId,))

0 commit comments

Comments
 (0)