tripal_feature.gff_exporter.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. function tripal_feature_gff3_exporter($source, $filters) {
  3. $select = "
  4. SELECT SF.uniquename as landmark_uname, SF.name as landmark_name,
  5. F.feature_id, F.dbxref_id, F.uniquename, F.name, CVT.name as type,
  6. FL.fmin, FL.fmax, FL.strand, FL.phase
  7. ";
  8. $from = "
  9. FROM {featureloc} FL
  10. INNER JOIN {feature} F on FL.feature_id = F.feature_id
  11. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  12. INNER JOIN {feature} SF on FL.srcfeature_id = SF.feature_id
  13. ";
  14. $where = "
  15. WHERE 1=1
  16. ";
  17. $order = "
  18. ORDER BY SF.uniquename, FL.fmin
  19. ";
  20. $args = array();
  21. // Filter by organism
  22. if (array_key_exists('genus', $filters) or array_key_exists('species', $filters)) {
  23. $from .= "INNER JOIN {organism} O on F.organism_id = O.organism_id ";
  24. if (array_key_exists('genus', $filters)) {
  25. $where .= "AND O.genus = :genus ";
  26. $args[':genus'] = $filters['genus'];
  27. }
  28. if (array_key_exists('species', $filters)) {
  29. $where .= "AND O.species = :species ";
  30. $args[':species'] = $filters['species'];
  31. }
  32. }
  33. // Filter by types
  34. if (array_key_exists('types', $filters)) {
  35. $where .= "AND CVT.name IN (:types) ";
  36. $args[':types'] = $filters['types'];
  37. }
  38. // Filter by exact feature_id
  39. if (array_key_exists('feature_id', $filters)) {
  40. $where .= "AND F.feature_id = :feature_id ";
  41. $args[':feature_id'] = $filters['feature_id'];
  42. }
  43. // Filter by analysis
  44. if (array_key_exists('analysis_id', $filters) or
  45. array_key_exists('analysis_name', $filters)) {
  46. $from .= "INNER JOIN {analysisfeature} AF on AF.feature_id = F.feature_id ";
  47. if (array_key_exists('analysis_id', $filters)) {
  48. $where .= "AND AF.analysis_id = :analysis_id ";
  49. $args[':analysis_id'] = $filters['analysis_id'];
  50. }
  51. if (array_key_exists('analysis_name', $filters)) {
  52. $from .= "INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id ";
  53. $where .= "AND A.name = :analysis_name ";
  54. $args[':analysis_name'] = $filters['analysis_name'];
  55. }
  56. }
  57. $sql = "$select $from $where $order";
  58. // The SQL statement for feature properties.
  59. $props_sql = "
  60. SELECT CVT.name, FP.value
  61. FROM {featureprop} FP
  62. INNER JOIN {cvterm} CVT on CVT.cvterm_id = FP.type_id
  63. WHERE FP.feature_id = :feature_id
  64. ORDER BY CVT.name
  65. ";
  66. // The SQL statement for Dbxrefs associated with this feature
  67. $dbxref_sql = "
  68. SELECT DB.name as dbname, DBX.accession
  69. FROM {dbxref} DBX
  70. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  71. WHERE DBX.dbxref_id = :dbxref_id
  72. UNION
  73. SELECT DB.name, DBX.accession
  74. FROM {feature_dbxref} FDBX
  75. INNER JOIN {dbxref} DBX on DBX.dbxref_id = FDBX.dbxref_id
  76. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  77. WHERE FDBX.feature_id = :feature_id
  78. ";
  79. // The SQL statement for CVTerms assigned to this feature.
  80. $cvterm_sql = "
  81. SELECT CV.name as db_name, DBX.accession
  82. FROM {feature_cvterm} FCVT
  83. INNER JOIN {cvterm} CVT on CVT.cvterm_id = FCVT.cvterm_id
  84. INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
  85. INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
  86. WHERE FCVT.feature_id = :feature_id
  87. ";
  88. // The SQL for finding the parents of this feature.
  89. $parent_sql = "
  90. SELECT F.name, F.uniquename, F.feature_id
  91. FROM {feature_relationship} FR
  92. INNER JOIN {cvterm} CVT on CVT.cvterm_id = FR.type_id
  93. INNER JOIN {feature} F on FR.object_id = F.feature_id
  94. WHERE CVT.name = 'part_of' AND FR.subject_id = :feature_id
  95. ";
  96. // The SQL for aliases of this feature.
  97. $alias_sql = "
  98. SELECT S.name
  99. FROM {feature_synonym} FS
  100. INNER JOIN {synonym} S on FS.synonym_id = S.synonym_id
  101. WHERE FS.feature_id = :feature_id
  102. ";
  103. // Get the score
  104. $score_sql = "
  105. SELECT rawscore as score
  106. FROM {analysisfeature} AF
  107. WHERE feature_id = :feature_id
  108. ORDER BY AF.analysis_id
  109. OFFSET 0 LIMIT 1
  110. ";
  111. $results = chado_query($sql, $args);
  112. while ($line = $results->fetchObject()) {
  113. // Get the score for this feature
  114. $score = chado_query($score_sql, array(':feature_id' => $line->feature_id))->fetchField();
  115. print $line->landmark_uname . "\t";
  116. print $source . "\t";
  117. print $line->type . "\t";
  118. print ($line->fmin + 1). "\t";
  119. print $line->fmax . "\t";
  120. if ($score) {
  121. print $score . "\t";
  122. }
  123. else {
  124. print "." . "\t";
  125. }
  126. if ($line->strand) {
  127. print $line->strand . "\t";
  128. }
  129. else {
  130. print '.' . "\t";
  131. }
  132. if ($line->phase) {
  133. print $line->phase . "\t";
  134. }
  135. else {
  136. print '.' . "\t";
  137. }
  138. print "ID=" . $line->uniquename . ";Name=" . $line->name . ";";
  139. // Look for a parent of this feature
  140. $args = array(':feature_id' => $line->feature_id);
  141. $parents = chado_query($parent_sql, $args);
  142. $attr = '';
  143. while ($parent = $parents->fetchObject()) {
  144. $attr .= $parent->uniquename . ",";
  145. }
  146. if ($attr) {
  147. print "Parent=" . substr($attr, 0, -1) . ";";
  148. }
  149. // Add in any aliases
  150. $args = array(':feature_id' => $line->feature_id);
  151. $aliases = chado_query($alias_sql, $args);
  152. $attr = '';
  153. while ($alias = $aliases->fetchObject()) {
  154. $attr .= $alias->name . ",";
  155. }
  156. if ($attr) {
  157. print "Alias=" . substr($attr, 0, -1) . ";";
  158. }
  159. $props = chado_query($props_sql, array(':feature_id' => $line->feature_id));
  160. $prop_name = '';
  161. while ($prop = $props->fetchobject()) {
  162. // If this is the first time we've encountered this property then
  163. // add the name=value key pair.
  164. if ($prop_name != $prop->name) {
  165. if ($prop_name) {
  166. // End the previous property
  167. print ";";
  168. }
  169. // TODO: urlencode the properties
  170. print $prop->name . "=" . $prop->value;
  171. $prop_name = $prop->name;
  172. }
  173. // If we've seen this property before then just add the value.
  174. else {
  175. print "," . $prop->value;
  176. }
  177. }
  178. // End the last property
  179. if ($prop_name) {
  180. print ";";
  181. }
  182. // Add in any DBXref records
  183. $args = array(
  184. ':feature_id' => $line->feature_id,
  185. ':dbxref_id' => $line->dbxref_id,
  186. );
  187. $dbxrefs = chado_query($dbxref_sql, $args);
  188. $xrefs = '';
  189. while ($dbxref = $dbxrefs->fetchObject()) {
  190. if ($dbxref->dbname = 'GFF_source') {
  191. continue;
  192. }
  193. $xrefs .= $dbxref->dbname . ":" . $dbxref->accession . ",";
  194. }
  195. if ($xrefs) {
  196. print "Dbxref=" . substr($xrefs, 0, -1) . ";";
  197. }
  198. // Add in any CVTerm records
  199. $args = array(':feature_id' => $line->feature_id);
  200. $cvterms = chado_query($cvterm_sql, $args);
  201. $xrefs = '';
  202. while ($cvterm = $cvterms->fetchObject()) {
  203. $xrefs .= $cvterm->db_name . ":" . $cvterm->accession . ",";
  204. }
  205. if ($xrefs) {
  206. print "Ontology_term=" . substr($xrefs, 0, -1) . ";";
  207. }
  208. print "\n";
  209. // Look for children of this feature and recursively add them.
  210. $children_sql = "
  211. SELECT FR.subject_id
  212. FROM {feature_relationship} FR
  213. INNER JOIN {cvterm} CVT on CVT.cvterm_id = FR.type_id
  214. WHERE CVT.name = 'part_of' AND FR.object_id = :feature_id
  215. ";
  216. $children = chado_query($children_sql, array(':feature_id' => $line->feature_id));
  217. while ($child = $children->fetchObject()) {
  218. $child_filters = array(
  219. 'feature_id' => $child->subject_id,
  220. );
  221. tripal_feature_gff3_exporter($source, $child_filters);
  222. }
  223. }
  224. }