--- get non-analysis feature of the type specified along with its children, optionally constraining by src [location]
--- WARNING: rely on soi is created in database
--- WARNING: can not use it for chromosome_arm type feature as they don't have featureloc entry

SQL:
select * FROM
(select
src.uniquename as src_seq,
f.*,
fl.fmin,
fl.fmax,
fl.strand,
fl.rank,
fl.locgroup,
fl.srcfeature_id,
q.name as type,
1 as depth,
NULL as relationship_type,
NULL as parent_id,
0 as orderrank
FROM
feature f
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 IN (&&type&&) and cv.name = 'so' [and src.uniquename = &&src&&] [and fl.fmin <= &&fmax&& and fl.fmax >= &&fmin&&]
UNION
select
src.uniquename as src_seq,
f.*,
fl.fmin,
fl.fmax,
fl.strand,
fl.rank,
fl.locgroup,
fl.srcfeature_id,
t.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
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
cvterm t ON (f.type_id = t.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 IN (&&type&&) group by c.name, c.cvterm_id) as q ON (f.type_id = q.cvterm_id)
WHERE f.is_analysis = 'f' [and src.uniquename = &&src&&] [and fl.fmin <= &&fmax&& and fl.fmax >= &&fmin&&]
) as uf order by depth, parent_id, orderrank, rank;



PROPERTY-SQL:
select
f.feature_id,
t.name as type,
fp.value,
fp.rank
FROM
feature f
INNER join
featureloc fl ON (f.feature_id = fl.feature_id)
INNER join
feature src ON (src.feature_id = fl.srcfeature_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 IN (&&type&&) group by c.name, c.cvterm_id) as q ON (f.type_id = q.cvterm_id)
INNER join
featureprop fp ON (fp.feature_id = f.feature_id)
INNER join
cvterm t ON (t.cvterm_id = fp.type_id)
WHERE f.is_analysis = 'f' [and src.name = &&src&&] [and fl.fmin <= &&fmax&& and fl.fmax >= &&fmin&&];

ONTOLOLGY-SQL:
select
f.feature_id,
gf.name,
gfx.accession,
db.name as dbname,
cv.name as cv
FROM
feature f
INNER join
featureloc fl ON (f.feature_id = fl.feature_id)
INNER join
feature src ON (src.feature_id = fl.srcfeature_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 IN (&&type&&) group by c.name, c.cvterm_id) as q ON (f.type_id = q.cvterm_id)
INNER join
feature_cvterm fcvt ON (f.feature_id = fcvt.feature_id)
INNER join
cvterm gf ON (gf.cvterm_id = fcvt.cvterm_id)
INNER join
dbxref gfx ON (gf.dbxref_id = gfx.dbxref_id)
INNER join
db ON (gfx.db_id = db.db_id)
INNER join
cv ON (gf.cv_id = cv.cv_id)
WHERE db.name = 'GO' and f.is_analysis = 'f' [and src.name = &&src&&] [and fl.fmin <= &&fmax&& and fl.fmax >= &&fmin&&] ;

DBXREF-SQL:
select
f.feature_id,
xref.accession,
db.name as dbname
FROM
feature f
INNER join
featureloc fl ON (f.feature_id = fl.feature_id)
INNER join
feature src ON (src.feature_id = fl.srcfeature_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 IN (&&type&&) group by c.name, c.cvterm_id) as q ON (f.type_id = q.cvterm_id)
INNER join
feature_dbxref fxref ON (f.feature_id = fxref.feature_id)
INNER join
dbxref xref ON (fxref.dbxref_id = xref.dbxref_id)
INNER join
db ON (db.db_id = xref.db_id)
WHERE f.is_analysis = 'f' [and src.name = &&src&&] [and fl.fmin <= &&fmax&& and fl.fmax >= &&fmin&&];

