tripal_feature.theme.inc 12 KB

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