--- get gene model, need 1 arguement, type (tRNA, etc)

--- syntax:

--- SQL:
--- select ...

--- optionally supply sql for auxillarie values of feature

--- PROPERTY-SQL:
--- select ...

--- DBXREF-SQL:
--- select ...

--- ONTOLOGY-SQL:
--- select ...

--- parameter value format: &&param&&, which later adapter replaces this with a value
--- when pass in {param=>value} arg to get_feature_by_template function

--- option parameter format: [AND sometable.colname = &&colname&&], which will be delete from sql
--- if no such parameter is supplied

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
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)
INNER join
(select * from get_feature_ids_by_type_name(&&type&&, &&name&&, 'f')) as parent ON (f.feature_id = parent.feature_id)
WHERE f.is_analysis = 'f' AND q.name = &&type&& 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
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 * from get_feature_ids_by_type_name(&&type&&, &&name&&, 'f')) as fa ON (f.feature_id = fa.feature_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 = &&type&& 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;

PROPERTY-SQL:
select
f.feature_id,
t.name as type,
fp.value,
fp.rank
FROM
feature f
INNER join
(select * from get_feature_ids_by_type_name(&&type&&, &&name&&, 'f')) as fa ON (f.feature_id = fa.feature_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';

ONTOLOLGY-SQL:
select
f.feature_id,
gf.name,
gfx.accession,
db.name as dbname,
cv.name as cv
FROM
feature f
INNER join
(select * from get_feature_ids_by_type_name(&&type&&, &&name&&, 'f')) as fa ON (f.feature_id = fa.feature_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';

DBXREF-SQL:
select
f.feature_id,
xref.accession,
db.name as dbname
FROM
feature f
INNER join
(select * from get_feature_ids_by_type_name(&&type&&, &&name&&, 'f')) as fa ON (f.feature_id = fa.feature_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';

