tripal_feature.theme.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. /**
  3. * @ingroup tripal_feature
  4. */
  5. function tripal_feature_preprocess_tripal_feature_sequence(&$variables) {
  6. // we want to provide a new variable that contains the matched features.
  7. $feature = $variables['node']->feature;
  8. // get the featureloc src features
  9. $options = array(
  10. 'return_array' => 1,
  11. 'include_fk' => array(
  12. 'srcfeature_id' => array(
  13. 'type_id' => 1
  14. ),
  15. ),
  16. );
  17. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  18. // because there are two foriegn keys in the featureloc table with the feature table
  19. // we have to access the records for each by specifying the field name after the table name:
  20. $ffeaturelocs = $feature->featureloc->feature_id;
  21. // now extract the sequences
  22. $featureloc_sequences = tripal_feature_load_featureloc_sequences($feature->feature_id, $ffeaturelocs);
  23. $feature->featureloc_sequences = $featureloc_sequences;
  24. // if this feature has associated protein sequences (or others via relationships
  25. // then we want to make sure the relationships are added so that we can
  26. // show the protein sequences
  27. if (!$feature->all_relationships) {
  28. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  29. }
  30. }
  31. /**
  32. * Get the sequence this feature is located on
  33. *
  34. * @param $feature_id
  35. * @param $featurelocs
  36. *
  37. * @ingroup tripal_feature
  38. */
  39. function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
  40. // if we don't have any featurelocs then no point in continuing
  41. if (!$featurelocs) {
  42. return array();
  43. }
  44. // get the list of relationships (including any aggregators) and iterate
  45. // through each one to find information needed to color-code the reference sequence
  46. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  47. if (!$relationships) {
  48. return array();
  49. }
  50. // iterate through each of the realtionships features and get their
  51. // locations
  52. foreach ($relationships as $rindex => $rel) {
  53. // get the featurelocs for each of the relationship features
  54. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  55. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  56. // keep track of this unique source feature
  57. $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
  58. // copy over the results to the relationship object. Since there can
  59. // be more than one feature location for each relationship feature we
  60. // use the '$src' variable to keep track of these.
  61. $rel->featurelocs = new stdClass();
  62. $rel->featurelocs->$src = new stdClass();
  63. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  64. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  65. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  66. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  67. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  68. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  69. // keep track of the individual parts for each relationship
  70. $start = $rel->featurelocs->$src->fmin;
  71. $end = $rel->featurelocs->$src->fmax;
  72. $type = $rel->subject_type;
  73. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  74. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  75. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  76. }
  77. }
  78. // the featurelocs array provided to the function contains the locations
  79. // where this feature is found. We want to get the sequence for each
  80. // location and then annotate it with the parts found from the relationships
  81. // locations determiend above.
  82. $floc_sequences = array();
  83. foreach ($featurelocs as $featureloc) {
  84. // build the src name so we can keep track of the different parts for each feature
  85. $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
  86. // orient the parts to the beginning of the feature sequence
  87. if (!empty($rel_locs[$src]['parts'])) {
  88. $parts = $rel_locs[$src]['parts'];
  89. $rparts = array(); // we will fill this up if we're on the reverse strand
  90. foreach ($parts as $start => $types) {
  91. foreach ($types as $type_name => $type) {
  92. if ($featureloc->strand >= 0) {
  93. // this is on the forward strand. We need to convert the start on the src feature to the
  94. // start on this feature's sequence
  95. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  96. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  97. $parts[$start][$type_name]['type'] = $type_name;
  98. }
  99. else {
  100. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  101. // begining of the reverse sequence
  102. $size = ($featureloc->fmax - $featureloc->fmin);
  103. $start_orig = $parts[$start][$type_name]['start'];
  104. $end_orig = $parts[$start][$type_name]['end'];
  105. $new_start = $size - ($end_orig - $featureloc->fmin);
  106. $new_end = $size - ($start_orig - $featureloc->fmin);
  107. $rparts[$new_start][$type_name]['start'] = $new_start;
  108. $rparts[$new_start][$type_name]['end'] = $new_end;
  109. $rparts[$new_start][$type_name]['type'] = $type_name;
  110. }
  111. }
  112. }
  113. // now sort the parts
  114. // if we're on the reverse strand we need to resort
  115. if ($featureloc->strand >= 0) {
  116. usort($parts, 'tripal_feature_sort_rel_parts_by_start');
  117. }
  118. else {
  119. usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
  120. $parts = $rparts;
  121. }
  122. $floc_sequences[$src]['id'] = $src;
  123. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  124. $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
  125. $start = $featureloc->fmin + 1;
  126. $size = $featureloc->fmax - $featureloc->fmin;
  127. // TODO: fix the hard coded $start and $size
  128. // the $start and $size variables are hard-coded in the SQL statement
  129. // because the db_query function places quotes around all placeholders
  130. // (e.g. :start & :size) and screws up the substring function
  131. $sql = "
  132. SELECT substring(residues from $start for $size) as residues
  133. FROM {feature}
  134. WHERE feature_id = :feature_id
  135. ";
  136. $sequence = chado_query($sql, $args)->fetchObject();
  137. $residues = $sequence->residues;
  138. if ($featureloc->strand < 0) {
  139. $residues = tripal_reverse_compliment_sequence($residues);
  140. }
  141. $strand = '.';
  142. if ($featureloc->strand == 1) {
  143. $strand = '+';
  144. }
  145. elseif ($featureloc->strand == -1) {
  146. $strand = '-';
  147. }
  148. $floc_sequences[$src]['location'] = tripal_get_location_string($featureloc);
  149. $floc_sequences[$src]['defline'] = tripal_get_fasta_defline($featureloc->feature_id, '', $featureloc, '', strlen($residues));
  150. $floc_sequences[$src]['featureloc'] = $featureloc;
  151. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $floc_sequences[$src]['defline']);
  152. }
  153. }
  154. return $floc_sequences;
  155. }
  156. /**
  157. * Used to sort the list of relationship parts by start position
  158. *
  159. * @ingroup tripal_feature
  160. */
  161. function tripal_feature_sort_rel_parts_by_start($a, $b) {
  162. foreach ($a as $type_name => $details) {
  163. $astart = $a[$type_name]['start'];
  164. break;
  165. }
  166. foreach ($b as $type_name => $details) {
  167. $bstart = $b[$type_name]['start'];
  168. break;
  169. }
  170. return strnatcmp($astart, $bstart);
  171. }
  172. /**
  173. * Load the locations for a given feature
  174. *
  175. * @param $feature_id
  176. * The feature to look up locations for
  177. * @param $side
  178. * Whether the feature is the scrfeature, 'as_parent', or feature, 'as_child'
  179. * @param $aggregate
  180. * Whether or not to get the locations for related features
  181. *
  182. * @ingroup tripal_feature
  183. */
  184. function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  185. $sql = "
  186. SELECT
  187. F.name, F.feature_id, F.uniquename,
  188. FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
  189. CVT.name as cvname, CVT.cvterm_id,
  190. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  191. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
  192. FROM {featureloc} FL
  193. INNER JOIN {feature} F ON FL.feature_id = F.feature_id
  194. INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
  195. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  196. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  197. ";
  198. if (strcmp($side, 'as_parent')==0) {
  199. $sql .= "WHERE FL.srcfeature_id = :feature_id ";
  200. }
  201. if (strcmp($side, 'as_child')==0) {
  202. $sql .= "WHERE FL.feature_id = :feature_id ";
  203. }
  204. $flresults = chado_query($sql, array(':feature_id' => $feature_id));
  205. // copy the results into an array
  206. $i=0;
  207. $featurelocs = array();
  208. while ($loc = $flresults->fetchObject()) {
  209. // if a drupal node exists for this feature then add the nid to the
  210. // results object
  211. $loc->fnid = chado_get_nid_from_id('feature', $loc->feature_id);
  212. $loc->snid = chado_get_nid_from_id('feature', $loc->src_feature_id);
  213. // add the result to the array
  214. $featurelocs[$i++] = $loc;
  215. }
  216. // Add the relationship feature locs if aggregate is turned on
  217. if ($aggregate and strcmp($side, 'as_parent')==0) {
  218. // get the relationships for this feature without substituting any children
  219. // for the parent. We want all relationships
  220. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  221. foreach ($relationships as $rindex => $rel) {
  222. // get the featurelocs for each of the relationship features
  223. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  224. foreach ($rel_featurelocs as $findex => $rfloc) {
  225. $featurelocs[$i++] = $rfloc;
  226. }
  227. }
  228. }
  229. usort($featurelocs, 'tripal_feature_sort_locations');
  230. return $featurelocs;
  231. }
  232. /**
  233. * Used to sort the feature locs by start position
  234. *
  235. * @param $a
  236. * One featureloc record (as an object)
  237. * @param $b
  238. * The other featureloc record (as an object)
  239. *
  240. * @return
  241. * Which feature location comes first
  242. *
  243. * @ingroup tripal_feature
  244. */
  245. function tripal_feature_sort_locations($a, $b) {
  246. return strnatcmp($a->fmin, $b->fmin);
  247. }
  248. /**
  249. * Get features related to the current feature to a given depth. Recursive function.
  250. *
  251. * @param $feature_id
  252. * @param $substitute
  253. * @param $levels
  254. * @param $base_type_id
  255. * @param $depth
  256. *
  257. * @ingroup tripal_feature
  258. */
  259. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  260. $levels=0, $base_type_id=NULL, $depth=0) {
  261. // we only want to recurse to as many levels deep as indicated by the
  262. // $levels variable, but only if this variable is > 0. If 0 then we
  263. // recurse until we reach the end of the relationships tree.
  264. if ($levels > 0 and $levels == $depth) {
  265. return NULL;
  266. }
  267. // first get the relationships for this feature
  268. return tripal_feature_load_relationships($feature_id, 'as_object');
  269. }
  270. /**
  271. * Get the relationships for a feature.
  272. *
  273. * @param $feature_id
  274. * The feature to get relationships for
  275. * @param $side
  276. * The side of the relationship this feature is (ie: 'as_subject' or 'as_object')
  277. *
  278. * @ingroup tripal_feature
  279. */
  280. function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
  281. // get the relationships for this feature. The query below is used for both
  282. // querying the object and subject relationships
  283. $sql = "
  284. SELECT
  285. FS.name as subject_name, FS.uniquename as subject_uniquename,
  286. CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
  287. FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
  288. CVT.name as rel_type,
  289. FO.name as object_name, FO.uniquename as object_uniquename,
  290. CVTO.name as object_type, CVTO.cvterm_id as object_type_id
  291. FROM {feature_relationship} FR
  292. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  293. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  294. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  295. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  296. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  297. ";
  298. if (strcmp($side, 'as_object')==0) {
  299. $sql .= " WHERE FR.object_id = :feature_id";
  300. }
  301. if (strcmp($side, 'as_subject')==0) {
  302. $sql .= " WHERE FR.subject_id = :feature_id";
  303. }
  304. $sql .= " ORDER BY FR.rank";
  305. // get the relationships
  306. $results = chado_query($sql, array(':feature_id' => $feature_id));
  307. // iterate through the relationships, put these in an array and add
  308. // in the Drupal node id if one exists
  309. $i=0;
  310. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  311. $relationships = array();
  312. while ($rel = $results->fetchObject()) {
  313. $node = db_query($nodesql, array(':feature_id' => $rel->subject_id))->fetchObject();
  314. if ($node) {
  315. $rel->subject_nid = $node->nid;
  316. }
  317. $node = db_query($nodesql, array(':feature_id' => $rel->object_id))->fetchObject();
  318. if ($node) {
  319. $rel->object_nid = $node->nid;
  320. }
  321. $relationships[$i++] = $rel;
  322. }
  323. return $relationships;
  324. }
  325. /**
  326. * Returns the marked up fasta sequence for the described feature
  327. *
  328. * @param $sequence
  329. * @param $parts
  330. * @param $defline
  331. *
  332. * @ingroup tripal_feature
  333. */
  334. function tripal_feature_color_sequence($sequence, $parts, $defline) {
  335. $types = array();
  336. // first get the list of types so we can create a color legend
  337. foreach ($parts as $index => $t) {
  338. foreach ($t as $type_name => $details) {
  339. $types[$type_name] = 1;
  340. }
  341. }
  342. $newseq = "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  343. foreach ($types as $type_name => $present) {
  344. $newseq .= "<span id=\"tripal_feature-legend-$type_name\" class=\"tripal_feature-legend-item tripal_feature-featureloc_sequence-$type_name\" script=\"\">$type_name</span>";
  345. }
  346. $newseq .= "</div>Hold the cursor over a type above to highlight its positions in the sequence below.";
  347. // set the background color of the rows based on the type
  348. $pos = 0;
  349. $newseq .= "<pre class=\"tripal_feature-sequence\">";
  350. $newseq .= ">$defline<br>";
  351. // iterate through the parts. They should be in order.
  352. $starts = array(); // an array holding all of the children starting locations
  353. $ends = array(); // an array holding all of the children's ending locations
  354. $seqcount = 0;
  355. foreach ($parts as $index => $types) {
  356. // get the start for this part. All types in this part start at the
  357. // same position so we only need the first record
  358. foreach ($types as $type => $child) {
  359. $start = $child['start'];
  360. $starts[$start][] = $type;
  361. }
  362. // next, sort the parts by their end. We want the span tag to
  363. // to be added in the order the parts end.
  364. usort($types, 'tripal_feature_sort_rel_parts_by_end');
  365. // iterate through the types in order that then end and create a
  366. // span for it.
  367. foreach ($types as $type) {
  368. $end = $type['end'];
  369. $ends[$end][] = $type;
  370. }
  371. }
  372. // iterate through each nucleotide in the sequence, add a new line very
  373. // 50 characters and add the spans as we encounter them
  374. for ($i = 0; $i < strlen($sequence); $i++) {
  375. // if we are at and end of a span then close it
  376. if (array_key_exists($i, $ends)) {
  377. foreach ($ends[$i] as $index => $type) {
  378. $newseq .= "</span>";
  379. }
  380. }
  381. // if we are at and end of a span then close it
  382. if (array_key_exists($i, $starts)) {
  383. foreach ($starts[$i] as $index => $type) {
  384. $class = "tripal_feature-featureloc_sequence-" . $type;
  385. $newseq .= "<span class=\"$class\">";
  386. }
  387. }
  388. $newseq .= $sequence{$i};
  389. $seqcount++;
  390. if ($seqcount % 50 == 0) {
  391. $newseq .= "\n";
  392. }
  393. }
  394. $newseq .= "</pre>";
  395. return $newseq;
  396. }
  397. /**
  398. * Used to sort the list of relationship parts by start position
  399. *
  400. * @ingroup tripal_feature
  401. */
  402. function tripal_feature_sort_rel_parts_by_end($a, $b) {
  403. $val = strnatcmp($b['end'], $a['end']);
  404. if ($val == 0) {
  405. return strcmp($a['type'], $b['type']);
  406. }
  407. return $val;
  408. }
  409. /**
  410. *
  411. *
  412. * @ingroup tripal_feature
  413. */
  414. function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
  415. // we want to provide a new variable that contains the matched features.
  416. $feature = $variables['node']->feature;
  417. if (!property_exists($feature, 'all_relationships')) {
  418. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  419. }
  420. }
  421. /**
  422. *
  423. *
  424. * @ingroup tripal_feature
  425. */
  426. function tripal_feature_preprocess_tripal_feature_proteins(&$variables) {
  427. // we want to provide a new variable that contains the matched features.
  428. $feature = $variables['node']->feature;
  429. if (!property_exists($feature, 'all_relationships')) {
  430. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  431. }
  432. }
  433. /**
  434. *
  435. *
  436. * @ingroup tripal_feature
  437. */
  438. function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
  439. // we want to provide a new variable that contains the matched features.
  440. $feature = $variables['node']->feature;
  441. $options = array(
  442. 'return_array' => 1,
  443. 'include_fk' => array(
  444. 'srcfeature_id' => array(
  445. 'type_id' => 1,
  446. ),
  447. 'feature_id' => array(
  448. 'type_id' => 1
  449. ),
  450. )
  451. );
  452. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  453. // get alignments as child
  454. $cfeaturelocs = $feature->featureloc->feature_id;
  455. if (!$cfeaturelocs) {
  456. $cfeaturelocs = array();
  457. }
  458. // get alignment as parent
  459. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  460. if (!$pfeaturelocs) {
  461. $pfeaturelocs = array();
  462. }
  463. // get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  464. $mfeaturelocs = tripal_feature_get_matched_alignments($feature);
  465. $feature->matched_featurelocs = $mfeaturelocs;
  466. // combine all three alignments into a single array for printing together in
  467. // a single list
  468. $alignments = array();
  469. foreach ($pfeaturelocs as $featureloc) {
  470. // if type is a 'match' then ignore it. We will handle those below
  471. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  472. continue;
  473. }
  474. $alignment = new stdClass();
  475. $alignment->record = $featureloc;
  476. $alignment->name = $featureloc->feature_id->name;
  477. $alignment->type = $featureloc->feature_id->type_id->name;
  478. $alignment->fmin = $featureloc->fmin;
  479. $alignment->fmax = $featureloc->fmax;
  480. $alignment->phase = $featureloc->phase;
  481. $alignment->strand = $featureloc->strand;
  482. $alignments[] = $alignment;
  483. if (property_exists($featureloc->feature_id, 'nid')) {
  484. $alignment->nid = $featureloc->feature_id->nid;
  485. }
  486. }
  487. foreach ($cfeaturelocs as $featureloc) {
  488. // if type is a 'match' then ignore it. We will handle those below
  489. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  490. continue;
  491. }
  492. $alignment = new stdClass();
  493. $alignment->record = $featureloc;
  494. $alignment->name = $featureloc->srcfeature_id->name;
  495. $alignment->type = $featureloc->srcfeature_id->type_id->name;
  496. $alignment->fmin = $featureloc->fmin;
  497. $alignment->is_fmin_partial = $featureloc->is_fmin_partial;
  498. $alignment->fmax = $featureloc->fmax;
  499. $alignment->is_fmax_partial = $featureloc->is_fmax_partial;
  500. $alignment->phase = $featureloc->phase;
  501. $alignment->strand = $featureloc->strand;
  502. $alignments[] = $alignment;
  503. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  504. $alignment->nid = $featureloc->srcfeature_id->nid;
  505. }
  506. }
  507. // in matching features, the left feature is always the feature
  508. // provided to this function.
  509. foreach ($mfeaturelocs as $featureloc) {
  510. // get more information about the right feature
  511. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  512. $rfeature = chado_generate_var('feature', $select);
  513. // now add to the list
  514. $alignment = new stdClass();
  515. $alignment->record = $featureloc;
  516. $alignment->right_feature = $rfeature;
  517. $alignment->name = $rfeature->name;
  518. $alignment->type = $rfeature->type_id->name;
  519. $alignment->fmin = $featureloc->left_fmin;
  520. $alignment->is_fmin_partial = $featureloc->left_is_fmin_partial;
  521. $alignment->fmax = $featureloc->left_fmax;
  522. $alignment->is_fmax_partial = $featureloc->left_is_fmax_partial;
  523. $alignment->phase = $featureloc->left_phase;
  524. $alignment->strand = $featureloc->left_strand;
  525. $alignment->right_fmin = $featureloc->right_fmin;
  526. $alignment->right_is_fmin_partial = $featureloc->right_is_fmin_partial;
  527. $alignment->right_fmax = $featureloc->right_fmax;
  528. $alignment->right_is_fmax_partial = $featureloc->right_is_fmax_partial;
  529. $alignment->right_phase = $featureloc->right_phase;
  530. $alignment->right_strand = $featureloc->right_strand;
  531. $alignments[] = $alignment;
  532. if (property_exists($rfeature, 'nid')) {
  533. $alignment->nid = $rfeature->nid;
  534. }
  535. }
  536. $feature->all_featurelocs = $alignments;
  537. }
  538. /**
  539. * This function is for features that align through an intermediate such
  540. * as 'EST_match' or 'match'. This occurs in the case where two sequences
  541. * align but where one does not align perfectly. Some ESTs may be in a contig
  542. * but not all of the EST. Portions may overhang and not be included in the
  543. * consensus if quality is bad.
  544. * For example:
  545. * Feature 1: Contig --------------------
  546. * Feature 2: EST_match -------
  547. * Feature 3: EST ---------
  548. *
  549. * The feature provided to the function will always be the feature 1. The
  550. * featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  551. * alignment of feature 3 with feature 2
  552. *
  553. * Features may align to more than one feature and are not matches. We do
  554. * not want to include these, so we have to filter on the SO terms:
  555. * match, or %_match
  556. *
  557. * @ingroup tripal_feature
  558. */
  559. function tripal_feature_get_matched_alignments($feature) {
  560. $sql = "
  561. SELECT
  562. FL1.featureloc_id as left_featureloc_id,
  563. FL1.srcfeature_id as left_srcfeature_id,
  564. FL1.feature_id as left_feature_id,
  565. FL1.fmin as left_fmin,
  566. FL1.is_fmin_partial as left_is_fmin_partial,
  567. FL1.fmax as left_fmax,
  568. FL1.is_fmax_partial as left_is_fmax_partial,
  569. FL1.strand as left_strand,
  570. FL1.phase as left_phase,
  571. FL1.locgroup as left_locgroup,
  572. FL1.rank as left_rank,
  573. FL2.featureloc_id as right_featureloc_id,
  574. FL2.srcfeature_id as right_srcfeature_id,
  575. FL2.feature_id as right_feature_id,
  576. FL2.fmin as right_fmin,
  577. FL2.is_fmin_partial as right_is_fmin_partial,
  578. FL2.fmax as right_fmax,
  579. FL2.is_fmax_partial as right_is_fmax_partial,
  580. FL2.strand as right_strand,
  581. FL2.phase as right_phase,
  582. FL2.locgroup as right_locgroup,
  583. FL2.rank as right_rank
  584. FROM {feature} F1
  585. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  586. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  587. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  588. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  589. WHERE
  590. F1.feature_id = :feature_id AND
  591. (CVT2.name = 'match' or CVT2.name like '%_match')
  592. ORDER BY FL1.fmin
  593. ";
  594. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  595. // iterate through the results and add them to our featurelocs array
  596. $featurelocs = array();
  597. while ($fl = $results->fetchObject()) {
  598. // ignore featurelocs where the left and right srcfeature is the same
  599. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  600. continue;
  601. }
  602. $featurelocs[] = $fl ;
  603. }
  604. return $featurelocs;
  605. }
  606. /**
  607. *
  608. *
  609. * @ingroup tripal_feature
  610. */
  611. function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables, $hook) {
  612. $organism = $variables['node']->organism;
  613. $organism->feature_counts = tripal_feature_load_organism_feature_counts($organism);
  614. }
  615. /**
  616. * Load the arguments for the organism feature counts browser
  617. *
  618. * @param $organism
  619. * The organism of interest
  620. *
  621. * @ingroup tripal_feature
  622. */
  623. function tripal_feature_load_organism_feature_counts($organism) {
  624. $args = array();
  625. $order = array();
  626. $names = array();
  627. // build the where clause for the SQL statement if we have a custom term list
  628. // we'll also keep track of the names the admin provided (if any) and the
  629. // order that the terms should appear.
  630. $is_custom = 0;
  631. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  632. $where = '';
  633. if ($temp) {
  634. $is_custom = 1;
  635. $temp = explode("\n", $temp);
  636. $i = 0;
  637. foreach ($temp as $value) {
  638. // separate the key value pairs
  639. $temp2 = explode("=", $value);
  640. $feature_type = rtrim($temp2[0]);
  641. $order[] = $feature_type; // save the order of the these terms
  642. $where .= " OFC.feature_type = :name$i OR ";
  643. $args[":name$i"] = rtrim($temp2[0]);
  644. // if the admin specified a new name then store that otherwise use the
  645. // the default sequence ontology term name
  646. if(count($temp2) == 2) {
  647. $names[] = rtrim($temp2[1]);
  648. }
  649. else {
  650. $names[] = $feature_type;
  651. }
  652. $i++;
  653. }
  654. if ($where) {
  655. $where = drupal_substr($where, 0, -4); # remove OR from the end
  656. $where = "($where) AND";
  657. }
  658. }
  659. // get the feature counts. This is dependent on a materialized view
  660. // installed with the organism module
  661. $sql = "
  662. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  663. FROM {organism_feature_count} OFC
  664. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  665. WHERE $where organism_id = :organism_id
  666. ORDER BY num_features desc
  667. ";
  668. $args[':organism_id'] = $organism->organism_id;
  669. $org_features = chado_query($sql, $args);
  670. // iterate through the types
  671. $types = array();
  672. while ($type = $org_features->fetchObject()) {
  673. $types[$type->feature_type] = $type;
  674. // if we don't have an order this means we didn't go through the loop
  675. // above to set the names, so do that now
  676. if (!$is_custom) {
  677. $names[] = $type->feature_type;
  678. $order[] = $type->feature_type;
  679. }
  680. }
  681. // now reorder the types
  682. $ordered_types = array();
  683. foreach ($order as $type) {
  684. if (array_key_exists($type, $types)) {
  685. $ordered_types[] = $types[$type];
  686. }
  687. }
  688. return array(
  689. 'types' => $ordered_types,
  690. 'names' => $names
  691. );
  692. }
  693. /**
  694. * Using the chado_expand_var function to retrieve a set
  695. * of relationships can be very slow, especialy if there are many relationships
  696. * This function is intended to help speed up the retrieval of relationships
  697. * by only retrieving the base information for the relationship and returning
  698. * an array with
  699. *
  700. * @param $feature
  701. * The feature object
  702. * @return
  703. * An array with two objects
  704. *
  705. * @ingroup tripal_feature_api
  706. */
  707. function tripal_feature_get_feature_relationships($feature) {
  708. // expand the feature object to include the feature relationships.
  709. $options = array(
  710. 'return_array' => 1,
  711. 'order_by' => array('rank' => 'ASC'),
  712. // we don't want to fully recurse we only need information about the
  713. // relationship type and the object and subject features (including feature type
  714. // and organism)
  715. 'include_fk' => array(
  716. 'type_id' => 1,
  717. 'object_id' => array(
  718. 'type_id' => 1,
  719. 'organism_id' => 1
  720. ),
  721. 'subject_id' => array(
  722. 'type_id' => 1,
  723. 'organism_id' => 1
  724. ),
  725. ),
  726. );
  727. $feature = chado_expand_var($feature, 'table', 'feature_relationship', $options);
  728. // get the subject relationships
  729. $srelationships = $feature->feature_relationship->subject_id;
  730. $orelationships = $feature->feature_relationship->object_id;
  731. // get alignment as child. The $feature->featureloc element
  732. // is already populated from the alignment preprocess function
  733. $options = array(
  734. 'include_fk' => array(
  735. 'srcfeature_id' => 1,
  736. 'feature_id' => 1,
  737. ),
  738. );
  739. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  740. $cfeaturelocs = $feature->featureloc->feature_id;
  741. if (!$cfeaturelocs) {
  742. $cfeaturelocs = array();
  743. }
  744. elseif (!is_array($cfeaturelocs)) {
  745. $cfeaturelocs = array($cfeaturelocs);
  746. }
  747. // prepare the SQL statement to get the featureloc for the
  748. // feature in the relationships.
  749. $flrels_sql = "
  750. SELECT
  751. FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
  752. FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
  753. FROM {featureloc} FL
  754. INNER JOIN {feature} F ON F.feature_id = FL.srcfeature_id
  755. WHERE FL.feature_id = :feature_id and FL.srcfeature_id = :srcfeature_id
  756. ";
  757. // combine both object and subject relationshisp into a single array
  758. $relationships = array();
  759. $relationships['object'] = array();
  760. $relationships['subject'] = array();
  761. // iterate through the object relationships
  762. if ($orelationships) {
  763. foreach ($orelationships as $relationship) {
  764. $rel = new stdClass();
  765. // get locations where the child feature and this feature overlap with the
  766. // same landmark feature.
  767. $rel->child_featurelocs = array();
  768. foreach ($cfeaturelocs as $featureloc) {
  769. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->subject_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  770. while ($loc = $res->fetchObject()) {
  771. // add in the node id of the src feature if it exists and save this location
  772. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  773. $loc->nid = $featureloc->srcfeature_id->nid;
  774. }
  775. $rel->child_featurelocs[] = $loc;
  776. }
  777. }
  778. $rel->record = $relationship;
  779. // get the relationship and child types
  780. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  781. $child_type = $relationship->subject_id->type_id->name;
  782. // get the node id of the subject
  783. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  784. $n = db_query($sql, array(':feature_id' => $relationship->subject_id->feature_id))->fetchObject();
  785. if ($n) {
  786. $rel->record->nid = $n->nid;
  787. }
  788. if (!array_key_exists($rel_type, $relationships['object'])) {
  789. $relationships['object'][$rel_type] = array();
  790. }
  791. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  792. $relationships['object'][$rel_type][$child_type] = array();
  793. }
  794. $relationships['object'][$rel_type][$child_type][] = $rel;
  795. }
  796. }
  797. // now add in the subject relationships
  798. if ($srelationships) {
  799. foreach ($srelationships as $relationship) {
  800. $rel = new stdClass();
  801. // get locations where this feature overlaps with the parent
  802. $rel->parent_featurelocs = array();
  803. foreach ($cfeaturelocs as $featureloc) {
  804. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->object_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  805. while ($loc = $res->fetchObject()) {
  806. // add in the node id of the src feature if it exists and save this location
  807. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  808. $loc->nid = $featureloc->srcfeature_id->nid;
  809. }
  810. $rel->parent_featurelocs[] = $loc;
  811. }
  812. }
  813. $rel->record = $relationship;
  814. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  815. $parent_type = $relationship->object_id->type_id->name;
  816. // get the node id of the subject
  817. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  818. $n = db_query($sql, array(':feature_id' => $relationship->object_id->feature_id))->fetchObject();
  819. if ($n) {
  820. $rel->record->nid = $n->nid;
  821. }
  822. if (!array_key_exists($rel_type, $relationships['subject'])) {
  823. $relationships['subject'][$rel_type] = array();
  824. }
  825. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  826. $relationships['subject'][$rel_type][$parent_type] = array();
  827. }
  828. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  829. }
  830. }
  831. return $relationships;
  832. }