--- get gene model, need 2 arguements
--- (aspect: a GO substree root, e.g. biological_process, and term: a GO term)
--- maybe with OPTIONS section to tell SOI adapter to get auxillaries, get by default?
--- this sql relies on get_feature_ids_by_ont plpgsql function installed!!

SQL:
select * FROM
((select
f.*,
src.uniquename as src_seq,
fl.*,
q.name as type,
1 as depth,
NULL as relationship_type,
NULL as parent_id,
0 as orderrank
FROM
feature f
INNER join
(select * from get_feature_ids_by_ont_root(&&aspect&&, &&term&&)) as fc ON (f.feature_id = fc.feature_id)
INNER join
featureloc fl ON (f.feature_id = fl.feature_id)
INNER join
feature src ON (src.feature_id = fl.srcfeature_id)
INNER join
cvterm q ON (f.type_id = q.cvterm_id)
INNER join
cv ON (q.cv_id = cv.cv_id)
WHERE f.is_analysis = 'f' and q.name = 'gene' and cv.name = 'so'
)
UNION
(select
f.*,
src.uniquename as src_seq,
fl.*,
q.name as type,
q.depth,
frt.name as relationship_type,
fr.object_id as parent_id,
fr.rank as orderrank
FROM
feature f
INNER join
(select * from get_feature_ids_by_ont_root(&&aspect&&, &&term&&)) as fc ON (f.feature_id = fc.feature_id)
INNER join
featureloc fl ON (f.feature_id = fl.feature_id)
INNER join
feature src ON (src.feature_id = fl.srcfeature_id)
INNER join
feature_relationship fr ON (f.feature_id = fr.subject_id)
INNER join
cvterm frt ON (fr.type_id = frt.cvterm_id)
INNER join 
(select c.name, c.cvterm_id, max(pathdistance+1) as depth
FROM cvterm c, cvtermpath path, cvterm p, cv
WHERE c.cvterm_id = subject_id and p.cvterm_id = object_id
and path.cv_id =cv.cv_id and cv.name = 'soi'
and p.name = 'gene' group by c.name, c.cvterm_id) as q ON (f.type_id = q.cvterm_id)
WHERE f.is_analysis = 'f')) as uf order by depth, parent_id, orderrank, rank;
