tripal_feature.theme.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. }
  25. /**
  26. *
  27. *
  28. * @ingroup tripal_feature
  29. */
  30. function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
  31. // we want to provide a new variable that contains the matched features.
  32. $feature = $variables['node']->feature;
  33. if (!property_exists($feature, 'all_relationships')) {
  34. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  35. }
  36. }
  37. /**
  38. *
  39. *
  40. * @ingroup tripal_feature
  41. */
  42. function tripal_feature_preprocess_tripal_feature_proteins(&$variables) {
  43. // we want to provide a new variable that contains the matched features.
  44. $feature = $variables['node']->feature;
  45. if (!property_exists($feature, 'all_relationships')) {
  46. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  47. }
  48. }
  49. /**
  50. *
  51. *
  52. * @ingroup tripal_feature
  53. */
  54. function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
  55. // we want to provide a new variable that contains the matched features.
  56. $feature = $variables['node']->feature;
  57. $options = array(
  58. 'return_array' => 1,
  59. 'include_fk' => array(
  60. 'srcfeature_id' => array(
  61. 'type_id' => 1,
  62. ),
  63. 'feature_id' => array(
  64. 'type_id' => 1
  65. ),
  66. )
  67. );
  68. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  69. // get alignments as child
  70. $cfeaturelocs = $feature->featureloc->feature_id;
  71. if (!$cfeaturelocs) {
  72. $cfeaturelocs = array();
  73. }
  74. // get alignment as parent
  75. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  76. if (!$pfeaturelocs) {
  77. $pfeaturelocs = array();
  78. }
  79. // get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  80. $mfeaturelocs = tripal_feature_get_matched_alignments($feature);
  81. $feature->matched_featurelocs = $mfeaturelocs;
  82. // combine all three alignments into a single array for printing together in
  83. // a single list
  84. $alignments = array();
  85. foreach ($pfeaturelocs as $featureloc) {
  86. // if type is a 'match' then ignore it. We will handle those below
  87. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  88. continue;
  89. }
  90. $alignment = new stdClass();
  91. $alignment->record = $featureloc;
  92. $alignment->name = $featureloc->feature_id->name;
  93. $alignment->type = $featureloc->feature_id->type_id->name;
  94. $alignment->fmin = $featureloc->fmin;
  95. $alignment->fmax = $featureloc->fmax;
  96. $alignment->phase = $featureloc->phase;
  97. $alignment->strand = $featureloc->strand;
  98. $alignments[] = $alignment;
  99. if (property_exists($featureloc->feature_id, 'nid')) {
  100. $alignment->nid = $featureloc->feature_id->nid;
  101. }
  102. }
  103. foreach ($cfeaturelocs as $featureloc) {
  104. // if type is a 'match' then ignore it. We will handle those below
  105. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  106. continue;
  107. }
  108. $alignment = new stdClass();
  109. $alignment->record = $featureloc;
  110. $alignment->name = $featureloc->srcfeature_id->name;
  111. $alignment->type = $featureloc->srcfeature_id->type_id->name;
  112. $alignment->fmin = $featureloc->fmin;
  113. $alignment->is_fmin_partial = $featureloc->is_fmin_partial;
  114. $alignment->fmax = $featureloc->fmax;
  115. $alignment->is_fmax_partial = $featureloc->is_fmax_partial;
  116. $alignment->phase = $featureloc->phase;
  117. $alignment->strand = $featureloc->strand;
  118. $alignments[] = $alignment;
  119. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  120. $alignment->nid = $featureloc->srcfeature_id->nid;
  121. }
  122. }
  123. // in matching features, the left feature is always the feature
  124. // provided to this function.
  125. foreach ($mfeaturelocs as $featureloc) {
  126. // get more information about the right feature
  127. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  128. $rfeature = chado_generate_var('feature', $select);
  129. // now add to the list
  130. $alignment = new stdClass();
  131. $alignment->record = $featureloc;
  132. $alignment->right_feature = $rfeature;
  133. $alignment->name = $rfeature->name;
  134. $alignment->type = $rfeature->type_id->name;
  135. $alignment->fmin = $featureloc->left_fmin;
  136. $alignment->is_fmin_partial = $featureloc->left_is_fmin_partial;
  137. $alignment->fmax = $featureloc->left_fmax;
  138. $alignment->is_fmax_partial = $featureloc->left_is_fmax_partial;
  139. $alignment->phase = $featureloc->left_phase;
  140. $alignment->strand = $featureloc->left_strand;
  141. $alignment->right_fmin = $featureloc->right_fmin;
  142. $alignment->right_is_fmin_partial = $featureloc->right_is_fmin_partial;
  143. $alignment->right_fmax = $featureloc->right_fmax;
  144. $alignment->right_is_fmax_partial = $featureloc->right_is_fmax_partial;
  145. $alignment->right_phase = $featureloc->right_phase;
  146. $alignment->right_strand = $featureloc->right_strand;
  147. $alignments[] = $alignment;
  148. if (property_exists($rfeature, 'nid')) {
  149. $alignment->nid = $rfeature->nid;
  150. }
  151. }
  152. $feature->all_featurelocs = $alignments;
  153. }
  154. /**
  155. *
  156. *
  157. * @ingroup tripal_feature
  158. */
  159. function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables, $hook) {
  160. $organism = $variables['node']->organism;
  161. $organism->feature_counts = tripal_feature_load_organism_feature_counts($organism);
  162. }
  163. /**
  164. * Using the tripal_core_expand_chado_vars function to retrieve a set
  165. * of relationships can be very slow, especialy if there are many relationships
  166. * This function is intended to help speed up the retrieval of relationships
  167. * by only retrieving the base information for the relationship and returning
  168. * an array with
  169. *
  170. * @param $feature
  171. * The feature object
  172. * @return
  173. * An array with two objects
  174. *
  175. * @ingroup tripal_feature_api
  176. */
  177. function tripal_feature_get_feature_relationships($feature) {
  178. // expand the feature object to include the feature relationships.
  179. $options = array(
  180. 'return_array' => 1,
  181. 'order_by' => array('rank' => 'ASC'),
  182. // we don't want to fully recurse we only need information about the
  183. // relationship type and the object and subject features (including feature type
  184. // and organism)
  185. 'include_fk' => array(
  186. 'type_id' => 1,
  187. 'object_id' => array(
  188. 'type_id' => 1,
  189. 'organism_id' => 1
  190. ),
  191. 'subject_id' => array(
  192. 'type_id' => 1,
  193. 'organism_id' => 1
  194. ),
  195. ),
  196. );
  197. $feature = chado_expand_var($feature, 'table', 'feature_relationship', $options);
  198. // get the subject relationships
  199. $srelationships = $feature->feature_relationship->subject_id;
  200. $orelationships = $feature->feature_relationship->object_id;
  201. // get alignment as child. The $feature->featureloc element
  202. // is already populated from the alignment preprocess function
  203. $options = array(
  204. 'include_fk' => array(
  205. 'srcfeature_id' => 1,
  206. 'feature_id' => 1,
  207. ),
  208. );
  209. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  210. $cfeaturelocs = $feature->featureloc->feature_id;
  211. if (!$cfeaturelocs) {
  212. $cfeaturelocs = array();
  213. }
  214. elseif (!is_array($cfeaturelocs)) {
  215. $cfeaturelocs = array($cfeaturelocs);
  216. }
  217. // prepare the SQL statement to get the featureloc for the
  218. // feature in the relationships.
  219. $flrels_sql = "
  220. SELECT
  221. FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
  222. FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
  223. FROM {featureloc} FL
  224. INNER JOIN {feature} F ON F.feature_id = FL.srcfeature_id
  225. WHERE FL.feature_id = :feature_id and FL.srcfeature_id = :srcfeature_id
  226. ";
  227. // combine both object and subject relationshisp into a single array
  228. $relationships = array();
  229. $relationships['object'] = array();
  230. $relationships['subject'] = array();
  231. // iterate through the object relationships
  232. if ($orelationships) {
  233. foreach ($orelationships as $relationship) {
  234. $rel = new stdClass();
  235. // get locations where the child feature and this feature overlap with the
  236. // same landmark feature.
  237. $rel->child_featurelocs = array();
  238. foreach ($cfeaturelocs as $featureloc) {
  239. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->subject_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  240. while ($loc = $res->fetchObject()) {
  241. // add in the node id of the src feature if it exists and save this location
  242. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  243. $loc->nid = $featureloc->srcfeature_id->nid;
  244. }
  245. $rel->child_featurelocs[] = $loc;
  246. }
  247. }
  248. $rel->record = $relationship;
  249. // get the relationship and child types
  250. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  251. $child_type = $relationship->subject_id->type_id->name;
  252. // get the node id of the subject
  253. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  254. $n = db_query($sql, array(':feature_id' => $relationship->subject_id->feature_id))->fetchObject();
  255. if ($n) {
  256. $rel->record->nid = $n->nid;
  257. }
  258. if (!array_key_exists($rel_type, $relationships['object'])) {
  259. $relationships['object'][$rel_type] = array();
  260. }
  261. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  262. $relationships['object'][$rel_type][$child_type] = array();
  263. }
  264. $relationships['object'][$rel_type][$child_type][] = $rel;
  265. }
  266. }
  267. // now add in the subject relationships
  268. if ($srelationships) {
  269. foreach ($srelationships as $relationship) {
  270. $rel = new stdClass();
  271. // get locations where this feature overlaps with the parent
  272. $rel->parent_featurelocs = array();
  273. foreach ($cfeaturelocs as $featureloc) {
  274. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->object_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  275. while ($loc = $res->fetchObject()) {
  276. // add in the node id of the src feature if it exists and save this location
  277. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  278. $loc->nid = $featureloc->srcfeature_id->nid;
  279. }
  280. $rel->parent_featurelocs[] = $loc;
  281. }
  282. }
  283. $rel->record = $relationship;
  284. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  285. $parent_type = $relationship->object_id->type_id->name;
  286. // get the node id of the subject
  287. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  288. $n = db_query($sql, array(':feature_id' => $relationship->object_id->feature_id))->fetchObject();
  289. if ($n) {
  290. $rel->record->nid = $n->nid;
  291. }
  292. if (!array_key_exists($rel_type, $relationships['subject'])) {
  293. $relationships['subject'][$rel_type] = array();
  294. }
  295. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  296. $relationships['subject'][$rel_type][$parent_type] = array();
  297. }
  298. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  299. }
  300. }
  301. return $relationships;
  302. }