tripal_feature.theme.inc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. // 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. }
  833. /**
  834. *
  835. */
  836. function tripal_feature_preprocess_tripal_feature_bar_chart_type_organism_summary(&$vars) {
  837. // Add in all the javascript/css files.
  838. tripal_add_d3js();
  839. drupal_add_css(drupal_get_path('module','tripal_feature') . '/theme/css/tripal_feature.css');
  840. drupal_add_js(drupal_get_path('module','tripal_feature') . '/theme/js/tripalFeature.adminChart.js');
  841. // Retrieve and process all the data and save it as javascript settings.
  842. //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  843. // We are using the organism_feature_count materialized view as the source for our data.
  844. // Thus grab all the records from this materialized view.
  845. $organism_feature_count = chado_select_record(
  846. 'organism_feature_count',
  847. array('*'),
  848. array(),
  849. array('order_by' => array('genus' => 'ASC', 'species' => 'ASC', 'feature_type' => 'ASC', 'num_features' => 'DESC'))
  850. );
  851. // Initialize variables.
  852. $chart = array();
  853. $type_names = array();
  854. $organism_names = array();
  855. $max_bar_height = 0;
  856. // Process each row of the materialzied view into the chart array.
  857. // Note: it's first keyed by type since each type will be a bar. Each type will have
  858. // a "bars" array with the start (y0) and end (y1) height on the bar for a given
  859. // organism. Finally we keep a record of the names of the types & organisms
  860. // for axis' and legend generation respectively.
  861. foreach ($organism_feature_count as $row) {
  862. // Build up the easy details for the current row's type. These will be overridden
  863. // multiple times but that's more efficient than checking each time.
  864. $chart[$row->cvterm_id]['cvterm_id'] = $row->cvterm_id;
  865. $chart[$row->cvterm_id]['name'] = str_replace('_', ' ', $row->feature_type);
  866. // Save the name of the type and organism into their respective arrays
  867. // for generation of axis' and legends for the chart.
  868. $type_names[$row->cvterm_id] = $row->feature_type;
  869. $organism_names[$row->organism_id] = $row->genus . ' ' . $row->species;
  870. // Save information about the current organism. This isn't actually used by the
  871. // chart but can be used to debug the bar generation to follow.
  872. $chart[$row->cvterm_id]['organisms'][] = array(
  873. 'name' => $row->genus . ' ' . $row->species,
  874. 'value' => (int) $row->num_features
  875. );
  876. // Now to build the bar array with the start (y0) and end (y1) height on the
  877. // bar for a given organism.
  878. // NOTE: we cannot assume the types are all in order so store y0 & y1 in the
  879. // $chart[type] array.
  880. // If y0 has not yet been set for this type then we're starting with the first
  881. // chunk (organism) on the bar.
  882. if (!isset($chart[$row->cvterm_id]['y0'])) {
  883. $chart[$row->cvterm_id]['y0'] = 0;
  884. $chart[$row->cvterm_id]['y1'] = $row->num_features;
  885. }
  886. // Otherwise, add the next chunk (organism) on top of the pre-existing bar.
  887. else {
  888. $chart[$row->cvterm_id]['y0'] = $chart[$row->cvterm_id]['y1'];
  889. $chart[$row->cvterm_id]['y1'] = $chart[$row->cvterm_id]['y0'] + $row->num_features;
  890. }
  891. // Now save the bar chunk we just determined.
  892. $chart[$row->cvterm_id]['bars'][] = array(
  893. 'name' => $row->genus . ' ' . $row->species,
  894. 'y0' => $chart[$row->cvterm_id]['y0'],
  895. 'y1' => $chart[$row->cvterm_id]['y1'],
  896. );
  897. // We also need to keep track of the total number of features for a single bar (Type).
  898. $chart[$row->cvterm_id]['total_features'] = (int) $chart[$row->cvterm_id]['y1'];
  899. // And the maximum "height" for all bars.
  900. if ($max_bar_height < $chart[$row->cvterm_id]['total_features']) {
  901. $max_bar_height = (int) $chart[$row->cvterm_id]['total_features'];
  902. }
  903. }
  904. // Sort based on the total number of features.
  905. // NOTE: This changes the keys so it's no longer the organism/type_id.
  906. usort($chart, 'tripal_feature_admin_summary_sort');
  907. sort($type_names);
  908. sort($organism_names);
  909. // We also need to add information about the materialized views
  910. // so that admin can update it and know how recent the data is.
  911. $mview = db_query('
  912. SELECT mview_id, name, last_update
  913. FROM tripal_mviews
  914. WHERE mv_table=:mv_table',
  915. array(':mv_table' => 'organism_feature_count')
  916. )->fetchObject();
  917. $vars['chart_details'] = array(
  918. 'summary' => $chart,
  919. 'types' => $type_names,
  920. 'organisms' => $organism_names,
  921. 'legendPosition' => 'top',
  922. 'maxBarHeight' => $max_bar_height,
  923. 'mviewUrl' => url('admin/tripal/storage/chado/mviews/update/' . $mview->mview_id),
  924. 'mviewTable' => $mview->name,
  925. 'mviewLastUpdate' => $mview->last_update ? format_date($mview->last_update) : '',
  926. );
  927. // Save everything we just determined as a Drupal JS settings so that we have access to
  928. // it in our js script.
  929. drupal_add_js(array('tripalFeature' => array('admin' => $vars['chart_details'])), 'setting');
  930. }