tripal_feature.theme.inc

File

tripal_feature/theme/tripal_feature.theme.inc
View source
  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 (!property_exists($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. // We should not assume this table is present since it is a materialized view.
  628. if (!chado_table_exists('organism_feature_count')) {
  629. return NULL;
  630. }
  631. // build the where clause for the SQL statement if we have a custom term list
  632. // we'll also keep track of the names the admin provided (if any) and the
  633. // order that the terms should appear.
  634. $is_custom = 0;
  635. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  636. $where = '';
  637. if ($temp) {
  638. $is_custom = 1;
  639. $temp = explode("\n", $temp);
  640. $i = 0;
  641. foreach ($temp as $value) {
  642. // separate the key value pairs
  643. $temp2 = explode("=", $value);
  644. $feature_type = rtrim($temp2[0]);
  645. $order[] = $feature_type; // save the order of the these terms
  646. $where .= " OFC.feature_type = :name$i OR ";
  647. $args[":name$i"] = rtrim($temp2[0]);
  648. // if the admin specified a new name then store that otherwise use the
  649. // the default sequence ontology term name
  650. if(count($temp2) == 2) {
  651. $names[] = rtrim($temp2[1]);
  652. }
  653. else {
  654. $names[] = $feature_type;
  655. }
  656. $i++;
  657. }
  658. if ($where) {
  659. $where = drupal_substr($where, 0, -4); # remove OR from the end
  660. $where = "($where) AND";
  661. }
  662. }
  663. // get the feature counts. This is dependent on a materialized view
  664. // installed with the organism module
  665. $sql = "
  666. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  667. FROM {organism_feature_count} OFC
  668. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  669. WHERE $where organism_id = :organism_id
  670. ORDER BY num_features desc
  671. ";
  672. $args[':organism_id'] = $organism->organism_id;
  673. $org_features = chado_query($sql, $args);
  674. // iterate through the types
  675. $types = array();
  676. while ($type = $org_features->fetchObject()) {
  677. $types[$type->feature_type] = $type;
  678. // if we don't have an order this means we didn't go through the loop
  679. // above to set the names, so do that now
  680. if (!$is_custom) {
  681. $names[] = $type->feature_type;
  682. $order[] = $type->feature_type;
  683. }
  684. }
  685. // now reorder the types
  686. $ordered_types = array();
  687. foreach ($order as $type) {
  688. if (array_key_exists($type, $types)) {
  689. $ordered_types[] = $types[$type];
  690. }
  691. }
  692. return array(
  693. 'types' => $ordered_types,
  694. 'names' => $names
  695. );
  696. }
  697. /**
  698. * Using the chado_expand_var function to retrieve a set
  699. * of relationships can be very slow, especialy if there are many relationships
  700. * This function is intended to help speed up the retrieval of relationships
  701. * by only retrieving the base information for the relationship and returning
  702. * an array with
  703. *
  704. * @param $feature
  705. * The feature object
  706. * @return
  707. * An array with two objects
  708. *
  709. * @ingroup tripal_feature_api
  710. */
  711. function tripal_feature_get_feature_relationships($feature) {
  712. // expand the feature object to include the feature relationships.
  713. $options = array(
  714. 'return_array' => 1,
  715. 'order_by' => array('rank' => 'ASC'),
  716. // we don't want to fully recurse we only need information about the
  717. // relationship type and the object and subject features (including feature type
  718. // and organism)
  719. 'include_fk' => array(
  720. 'type_id' => 1,
  721. 'object_id' => array(
  722. 'type_id' => 1,
  723. 'organism_id' => 1
  724. ),
  725. 'subject_id' => array(
  726. 'type_id' => 1,
  727. 'organism_id' => 1
  728. ),
  729. ),
  730. );
  731. $feature = chado_expand_var($feature, 'table', 'feature_relationship', $options);
  732. // get the subject relationships
  733. $srelationships = $feature->feature_relationship->subject_id;
  734. $orelationships = $feature->feature_relationship->object_id;
  735. // get alignment as child. The $feature->featureloc element
  736. // is already populated from the alignment preprocess function
  737. $options = array(
  738. 'include_fk' => array(
  739. 'srcfeature_id' => 1,
  740. 'feature_id' => 1,
  741. ),
  742. );
  743. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  744. $cfeaturelocs = $feature->featureloc->feature_id;
  745. if (!$cfeaturelocs) {
  746. $cfeaturelocs = array();
  747. }
  748. elseif (!is_array($cfeaturelocs)) {
  749. $cfeaturelocs = array($cfeaturelocs);
  750. }
  751. // prepare the SQL statement to get the featureloc for the
  752. // feature in the relationships.
  753. $flrels_sql = "
  754. SELECT
  755. FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
  756. FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
  757. FROM {featureloc} FL
  758. INNER JOIN {feature} F ON F.feature_id = FL.srcfeature_id
  759. WHERE FL.feature_id = :feature_id and FL.srcfeature_id = :srcfeature_id
  760. ";
  761. // combine both object and subject relationshisp into a single array
  762. $relationships = array();
  763. $relationships['object'] = array();
  764. $relationships['subject'] = array();
  765. // iterate through the object relationships
  766. if ($orelationships) {
  767. foreach ($orelationships as $relationship) {
  768. $rel = new stdClass();
  769. // get locations where the child feature and this feature overlap with the
  770. // same landmark feature.
  771. $rel->child_featurelocs = array();
  772. foreach ($cfeaturelocs as $featureloc) {
  773. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->subject_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  774. while ($loc = $res->fetchObject()) {
  775. // add in the node id of the src feature if it exists and save this location
  776. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  777. $loc->nid = $featureloc->srcfeature_id->nid;
  778. }
  779. $rel->child_featurelocs[] = $loc;
  780. }
  781. }
  782. $rel->record = $relationship;
  783. // get the relationship and child types
  784. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  785. $child_type = $relationship->subject_id->type_id->name;
  786. // get the node id of the subject
  787. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  788. $n = db_query($sql, array(':feature_id' => $relationship->subject_id->feature_id))->fetchObject();
  789. if ($n) {
  790. $rel->record->nid = $n->nid;
  791. }
  792. if (!array_key_exists($rel_type, $relationships['object'])) {
  793. $relationships['object'][$rel_type] = array();
  794. }
  795. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  796. $relationships['object'][$rel_type][$child_type] = array();
  797. }
  798. $relationships['object'][$rel_type][$child_type][] = $rel;
  799. }
  800. }
  801. // now add in the subject relationships
  802. if ($srelationships) {
  803. foreach ($srelationships as $relationship) {
  804. $rel = new stdClass();
  805. // get locations where this feature overlaps with the parent
  806. $rel->parent_featurelocs = array();
  807. foreach ($cfeaturelocs as $featureloc) {
  808. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->object_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  809. while ($loc = $res->fetchObject()) {
  810. // add in the node id of the src feature if it exists and save this location
  811. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  812. $loc->nid = $featureloc->srcfeature_id->nid;
  813. }
  814. $rel->parent_featurelocs[] = $loc;
  815. }
  816. }
  817. $rel->record = $relationship;
  818. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  819. $parent_type = $relationship->object_id->type_id->name;
  820. // get the node id of the subject
  821. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  822. $n = db_query($sql, array(':feature_id' => $relationship->object_id->feature_id))->fetchObject();
  823. if ($n) {
  824. $rel->record->nid = $n->nid;
  825. }
  826. if (!array_key_exists($rel_type, $relationships['subject'])) {
  827. $relationships['subject'][$rel_type] = array();
  828. }
  829. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  830. $relationships['subject'][$rel_type][$parent_type] = array();
  831. }
  832. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  833. }
  834. }
  835. return $relationships;
  836. }
  837. /**
  838. *
  839. */
  840. function tripal_feature_preprocess_tripal_feature_bar_chart_type_organism_summary(&$vars) {
  841. // Add in all the javascript/css files.
  842. tripal_add_d3js();
  843. drupal_add_css(drupal_get_path('module','tripal_feature') . '/theme/css/tripal_feature.css');
  844. drupal_add_js(drupal_get_path('module','tripal_feature') . '/theme/js/tripalFeature.adminChart.js');
  845. // Retrieve and process all the data and save it as javascript settings.
  846. //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  847. // We are using the organism_feature_count materialized view as the source for our data.
  848. // Thus grab all the records from this materialized view.
  849. $organism_feature_count = chado_select_record(
  850. 'organism_feature_count',
  851. array('*'),
  852. array(),
  853. array('order_by' => array('genus' => 'ASC', 'species' => 'ASC', 'feature_type' => 'ASC', 'num_features' => 'DESC'))
  854. );
  855. // Initialize variables.
  856. $chart = array();
  857. $type_names = array();
  858. $organism_names = array();
  859. $max_bar_height = 0;
  860. // Process each row of the materialzied view into the chart array.
  861. // Note: it's first keyed by type since each type will be a bar. Each type will have
  862. // a "bars" array with the start (y0) and end (y1) height on the bar for a given
  863. // organism. Finally we keep a record of the names of the types & organisms
  864. // for axis' and legend generation respectively.
  865. foreach ($organism_feature_count as $row) {
  866. // Build up the easy details for the current row's type. These will be overridden
  867. // multiple times but that's more efficient than checking each time.
  868. $chart[$row->cvterm_id]['cvterm_id'] = $row->cvterm_id;
  869. $chart[$row->cvterm_id]['name'] = str_replace('_', ' ', $row->feature_type);
  870. // Save the name of the type and organism into their respective arrays
  871. // for generation of axis' and legends for the chart.
  872. $type_names[$row->cvterm_id] = $row->feature_type;
  873. $organism_names[$row->organism_id] = $row->genus . ' ' . $row->species;
  874. // Save information about the current organism. This isn't actually used by the
  875. // chart but can be used to debug the bar generation to follow.
  876. $chart[$row->cvterm_id]['organisms'][] = array(
  877. 'name' => $row->genus . ' ' . $row->species,
  878. 'value' => (int) $row->num_features
  879. );
  880. // Now to build the bar array with the start (y0) and end (y1) height on the
  881. // bar for a given organism.
  882. // NOTE: we cannot assume the types are all in order so store y0 & y1 in the
  883. // $chart[type] array.
  884. // If y0 has not yet been set for this type then we're starting with the first
  885. // chunk (organism) on the bar.
  886. if (!isset($chart[$row->cvterm_id]['y0'])) {
  887. $chart[$row->cvterm_id]['y0'] = 0;
  888. $chart[$row->cvterm_id]['y1'] = $row->num_features;
  889. }
  890. // Otherwise, add the next chunk (organism) on top of the pre-existing bar.
  891. else {
  892. $chart[$row->cvterm_id]['y0'] = $chart[$row->cvterm_id]['y1'];
  893. $chart[$row->cvterm_id]['y1'] = $chart[$row->cvterm_id]['y0'] + $row->num_features;
  894. }
  895. // Now save the bar chunk we just determined.
  896. $chart[$row->cvterm_id]['bars'][] = array(
  897. 'name' => $row->genus . ' ' . $row->species,
  898. 'y0' => $chart[$row->cvterm_id]['y0'],
  899. 'y1' => $chart[$row->cvterm_id]['y1'],
  900. );
  901. // We also need to keep track of the total number of features for a single bar (Type).
  902. $chart[$row->cvterm_id]['total_features'] = (int) $chart[$row->cvterm_id]['y1'];
  903. // And the maximum "height" for all bars.
  904. if ($max_bar_height < $chart[$row->cvterm_id]['total_features']) {
  905. $max_bar_height = (int) $chart[$row->cvterm_id]['total_features'];
  906. }
  907. }
  908. // Sort based on the total number of features.
  909. // NOTE: This changes the keys so it's no longer the organism/type_id.
  910. usort($chart, 'tripal_feature_admin_summary_sort');
  911. sort($type_names);
  912. sort($organism_names);
  913. // We also need to add information about the materialized views
  914. // so that admin can update it and know how recent the data is.
  915. $mview = db_query('
  916. SELECT mview_id, name, last_update
  917. FROM tripal_mviews
  918. WHERE mv_table=:mv_table',
  919. array(':mv_table' => 'organism_feature_count')
  920. )->fetchObject();
  921. $vars['chart_details'] = array(
  922. 'summary' => $chart,
  923. 'types' => $type_names,
  924. 'organisms' => $organism_names,
  925. 'legendPosition' => 'top',
  926. 'maxBarHeight' => $max_bar_height,
  927. 'mviewUrl' => url('admin/tripal/schema/mviews/update/' . $mview->mview_id),
  928. 'mviewTable' => $mview->name,
  929. 'mviewLastUpdate' => $mview->last_update ? format_date($mview->last_update) : '',
  930. );
  931. // Save everything we just determined as a Drupal JS settings so that we have access to
  932. // it in our js script.
  933. drupal_add_js(array('tripalFeature' => array('admin' => $vars['chart_details'])), 'setting');
  934. }