tripal_feature.gff_exporter.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if (array_key_exists('genus', $filters) or array_key_exists('species', $filters)) {
  22. $from .= "INNER JOIN {organism} O on F.organism_id = O.organism_id ";
  23. if (array_key_exists('genus', $filters)) {
  24. $where .= "AND O.genus = :genus ";
  25. $args[':genus'] = $filters['genus'];
  26. }
  27. if (array_key_exists('species', $filters)) {
  28. $where .= "AND O.species = :species ";
  29. $args[':species'] = $filters['species'];
  30. }
  31. }
  32. if (array_key_exists('types', $filters)) {
  33. $where .= "AND CVT.name IN (:types) ";
  34. $args[':types'] = $filters['types'];
  35. }
  36. $sql = "$select $from $where $order";
  37. // The SQL statement for feature properties.
  38. $props_sql = "
  39. SELECT CVT.name, FP.value
  40. FROM {featureprop} FP
  41. INNER JOIN {cvterm} CVT on CVT.cvterm_id = FP.type_id
  42. WHERE FP.feature_id = :feature_id
  43. ORDER BY CVT.name
  44. ";
  45. // The SQL statement for Dbxrefs
  46. $dbxref_sql = "
  47. SELECT DB.name, DBX.accession
  48. FROM {dbxref} DBX
  49. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  50. WHERE DBX.dbxref_id = :dbxref_id
  51. UNION
  52. SELECT DB.name, DBX.accession
  53. FROM {feature_dbxref} FDBX
  54. INNER JOIN {dbxref} DBX on DBX.dbxref_id = FDBX.dbxref_id
  55. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  56. WHERE FDBX.feature_id = :feature_id
  57. ";
  58. // The SQL statement for CVTerms
  59. $cvterm_sql = "
  60. SELECT CV.name as db_name, DBX.accession
  61. FROM {feature_cvterm} FCVT
  62. INNER JOIN {cvterm} CVT on CVT.cvterm_id = FCVT.cvterm_id
  63. INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
  64. INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
  65. WHERE FCVT.feature_id = :feature_id
  66. ";
  67. $results = chado_query($sql, $args);
  68. while ($line = $results->fetchObject()) {
  69. print $line->landmark_uname . "\t";
  70. print $source . "\t";
  71. print $line->type . "\t";
  72. print $line->fmin . "\t";
  73. print $line->fmax . "\t";
  74. print "." . "\t";
  75. if ($line->strand) {
  76. print $line->strand . "\t";
  77. }
  78. else {
  79. print '.' . "\t";
  80. }
  81. if ($line->phase) {
  82. print $line->phase . "\t";
  83. }
  84. else {
  85. print '.' . "\t";
  86. }
  87. print "ID=" . $line->uniquename . ";Name=" . $line->name . ";";
  88. $props = chado_query($props_sql, array(':feature_id' => $line->feature_id));
  89. $prop_name = '';
  90. while ($prop = $props->fetchobject()) {
  91. // If this is the first time we've encountered this property then
  92. // add the name=value key pair.
  93. if ($prop_name != $prop->name) {
  94. if ($prop_name) {
  95. // End the previous property
  96. print ";";
  97. }
  98. // TODO: urlencode the properties
  99. print $prop->name . "=" . $prop->value;
  100. $prop_name = $prop->name;
  101. }
  102. // If we've seen this property before then just add the value.
  103. else {
  104. print "," . $prop->value;
  105. }
  106. }
  107. // End the last property
  108. if ($prop_name) {
  109. print ";";
  110. }
  111. // Add in any DBXref records
  112. $args = array(
  113. ':feature_id' => $line->feature_id,
  114. ':dbxref_id' => $line->dbxref_id,
  115. );
  116. $dbxrefs = chado_query($dbxref_sql, $args);
  117. $xrefs = '';
  118. while ($dbxref = $dbxrefs->fetchObject()) {
  119. $xrefs .= $dbxref->name . ":" . $dbxref->accession . ",";
  120. }
  121. if ($xrefs) {
  122. print "Dbxref=" . substr($xrefs, 0, -1) . ";";
  123. }
  124. // Add in any CVTerm records
  125. $args = array(
  126. ':feature_id' => $line->feature_id,
  127. );
  128. $cvterms = chado_query($cvterm_sql, $args);
  129. $xrefs = '';
  130. while ($cvterm = $cvterms->fetchObject()) {
  131. $xrefs .= $cvterm->db_name . ":" . $cvterm->accession . ",";
  132. }
  133. if ($xrefs) {
  134. print "Ontology_term=" . substr($xrefs, 0, -1) . ";";
  135. }
  136. print "\n";
  137. }
  138. }