chado_feature__residues.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. class chado_feature__residues extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Sequences'),
  9. 'description' => t('A field for managing nucleotide and protein residues.'),
  10. 'default_widget' => 'chado_feature__residues_widget',
  11. 'default_formatter' => 'chado_feature__residues_formatter',
  12. 'settings' => array(),
  13. 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
  14. 'storage' => array(
  15. 'type' => 'field_chado_storage',
  16. 'module' => 'tripal_chado',
  17. 'active' => TRUE
  18. ),
  19. );
  20. }
  21. /**
  22. * @see TripalField::can_attach()
  23. */
  24. protected function can_attach($entity_type, $bundle, $details) {
  25. $table_name = $details['chado_table'];
  26. $type_table = $details['chado_type_table'];
  27. $type_field = $details['chado_type_column'];
  28. $cv_id = $details['chado_cv_id'];
  29. $cvterm_id = $details['chado_cvterm_id'];
  30. // If this is not the feature table then we don't want to attach.
  31. if ($table_name == 'feature') {
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. /**
  37. * @see TripalField::create_info()
  38. */
  39. function create_info($entity_type, $bundle, $details) {
  40. if (!$this->can_attach($entity_type, $bundle, $details)) {
  41. return;
  42. }
  43. $table_name = $details['chado_table'];
  44. $type_table = $details['chado_type_table'];
  45. $type_field = $details['chado_type_column'];
  46. $cv_id = $details['chado_cv_id'];
  47. $cvterm_id = $details['chado_cvterm_id'];
  48. return array(
  49. 'field_name' => 'feature__residues',
  50. 'type' => 'chado_feature__residues',
  51. 'cardinality' => 1,
  52. 'locked' => FALSE,
  53. 'storage' => array(
  54. 'type' => 'field_chado_storage',
  55. ),
  56. 'settings' => array(
  57. 'chado_table' => $table_name,
  58. 'chado_column' => 'residues',
  59. 'semantic_web' => 'SO:0000110',
  60. ),
  61. );
  62. }
  63. /**
  64. * @see TripalField::create_instance_info()
  65. */
  66. function create_instance_info($entity_type, $bundle, $details) {
  67. if (!$this->can_attach($entity_type, $bundle, $details)) {
  68. return;
  69. }
  70. $table_name = $details['chado_table'];
  71. $type_table = $details['chado_type_table'];
  72. $type_field = $details['chado_type_column'];
  73. $cv_id = $details['chado_cv_id'];
  74. $cvterm_id = $details['chado_cvterm_id'];
  75. return array(
  76. 'field_name' => 'feature__residues',
  77. 'entity_type' => $entity_type,
  78. 'bundle' => $bundle->name,
  79. 'label' => 'Sequences',
  80. 'description' => 'All available sequences for this record.',
  81. 'required' => FALSE,
  82. 'settings' => array(),
  83. 'widget' => array(
  84. 'type' => 'chado_feature__residues_widget',
  85. 'settings' => array(
  86. 'display_label' => 1,
  87. ),
  88. ),
  89. 'display' => array(
  90. 'deafult' => array(
  91. 'label' => 'above',
  92. 'type' => 'chado_feature__residues_formatter',
  93. 'settings' => array(),
  94. ),
  95. ),
  96. );
  97. }
  98. /**
  99. * @see TripalField::widget_info()
  100. */
  101. function widget_info() {
  102. return array(
  103. 'label' => t('Sequence'),
  104. 'field types' => array('chado_feature__residues'),
  105. );
  106. }
  107. /**
  108. * @see TripalField::formatter_info()
  109. */
  110. function formatter_info() {
  111. return array(
  112. 'label' => t('Sequences'),
  113. 'field types' => array('chado_feature__residues'),
  114. );
  115. }
  116. /**
  117. * @see TripalField::formatter_view()
  118. */
  119. function formatter_view(&$element, $entity_type, $entity, $field,
  120. $instance, $langcode, $items, $display) {
  121. $num_bases = 50;
  122. $feature = $entity->chado_record;
  123. foreach ($items as $delta => $item) {
  124. // If there are no residues then skip this one.
  125. if (!array_key_exists('residues', $item['value'])) {
  126. continue;
  127. }
  128. $residues = $item['value']['residues'];
  129. $label = $item['value']['label'];
  130. $defline = $item['value']['defline'];
  131. $content = '<p>' . $label . '<p>';
  132. $content .= '<pre class="residues-formatter">';
  133. $content .= '>' . $defline . "<br>";
  134. $content .= wordwrap($residues, $num_bases, "<br>", TRUE);
  135. $content .= '</pre>';
  136. $element[$delta] = array(
  137. // We create a render array to produce the desired markup,
  138. '#type' => 'markup',
  139. '#markup' => $content,
  140. );
  141. }
  142. }
  143. /**
  144. * @see TripalField::widget_form()
  145. */
  146. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  147. $langcode, $items, $delta, $element) {
  148. $settings = $field['settings'];
  149. $field_name = $field['field_name'];
  150. $field_type = $field['type'];
  151. $field_table = $field['settings']['chado_table'];
  152. $field_column = $field['settings']['chado_column'];
  153. // Get the field defaults.
  154. $residues = '';
  155. if (count($items) > 0 and array_key_exists('feature__residues', $items[0])) {
  156. $residues = $items[0]['feature__residues'];
  157. }
  158. if (array_key_exists('values', $form_state)) {
  159. $residues = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__residues');
  160. }
  161. $widget['value'] = array(
  162. '#type' => 'value',
  163. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  164. );
  165. $widget['feature__residues'] = array(
  166. '#type' => 'textarea',
  167. '#title' => $element['#title'],
  168. '#description' => $element['#description'],
  169. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  170. '#default_value' => $residues,
  171. '#delta' => $delta,
  172. '#element_validate' => array('chado_feature__residues_widget_validate'),
  173. '#cols' => 30,
  174. );
  175. }
  176. /**
  177. * @see TripalField::load()
  178. */
  179. function load($field, $entity, $details) {
  180. $field_name = $field['field_name'];
  181. $feature = $details['record'];
  182. $num_seqs = 0;
  183. // We don't want to get the sequence for traditionally large types. They are
  184. // too big, bog down the web browser, take longer to load and it's not
  185. // reasonable to print them on a page.
  186. if(strcmp($feature->type_id->name,'scaffold') == 0 or
  187. strcmp($feature->type_id->name,'chromosome') == 0 or
  188. strcmp($feature->type_id->name,'supercontig') == 0 or
  189. strcmp($feature->type_id->name,'pseudomolecule') == 0) {
  190. $entity->{$field_name}['und'][$num_seqs]['value'] = array(
  191. '@type' => 'SO:0000110',
  192. 'type' => 'sequence_feature',
  193. 'label' => 'Residues',
  194. 'defline' => ">This sequence is too large for this display.",
  195. 'residues' => '',
  196. );
  197. $entity->{$field_name}['und'][$num_seqs]['feature__residues'] = '';
  198. }
  199. else {
  200. $feature = chado_expand_var($feature,'field','feature.residues');
  201. if ($feature->residues) {
  202. $entity->{$field_name}['und'][$num_seqs]['value'] = array(
  203. '@type' => 'SO:0000110',
  204. 'type' => 'sequence_feature',
  205. 'label' => 'Sequence',
  206. 'defline' => tripal_get_fasta_defline($feature, '', NULL, '', strlen($feature->residues)),
  207. 'residues' => $feature->residues,
  208. );
  209. $entity->{$field_name}['und'][$num_seqs]['feature__residues'] = $feature->residues;
  210. }
  211. else {
  212. $entity->{$field_name}['und'][$num_seqs]['value'] = array();
  213. $entity->{$field_name}['und'][$num_seqs]['feature__residues'] = '';
  214. }
  215. }
  216. $num_seqs++;
  217. // Add in the protein sequences
  218. $values = array(
  219. 'object_id' => $feature->feature_id,
  220. 'subject_id' => array(
  221. 'type_id' => array(
  222. 'name' => 'polypeptide'
  223. ),
  224. ),
  225. 'type_id' => array(
  226. 'name' => 'derives_from',
  227. ),
  228. );
  229. $options = array(
  230. 'return_array' => 1,
  231. 'include_fk' => array(
  232. 'subject_id' => 1
  233. ),
  234. );
  235. $protein_rels = chado_generate_var('feature_relationship', $values, $options);
  236. foreach ($protein_rels as $protein_rel) {
  237. $protein_rel = chado_expand_var($protein_rel, 'field', 'feature.residues');
  238. if ($protein_rel->subject_id->residues) {
  239. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  240. '@type' => 'SO:0000104',
  241. 'type' => 'polypeptide',
  242. 'label' => 'Protein Sequence',
  243. 'defline' => tripal_get_fasta_defline($protein_rel->subject_id, '', NULL, '', strlen($protein_rel->subject_id->residues)),
  244. 'residues' => $protein_rel->subject_id->residues,
  245. );
  246. }
  247. }
  248. // Add in sequences from alignments.
  249. $options = array(
  250. 'return_array' => 1,
  251. 'include_fk' => array(
  252. 'srcfeature_id' => array(
  253. 'type_id' => 1
  254. ),
  255. 'feature_id' => array(
  256. 'type_id' => 1
  257. ),
  258. ),
  259. );
  260. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  261. $featureloc_sequences = $this->get_featureloc_sequences($feature->feature_id, $feature->featureloc->feature_id);
  262. // Add in the coding sequences.
  263. $values = array(
  264. 'object_id' => $feature->feature_id,
  265. 'subject_id' => array(
  266. 'type_id' => array(
  267. 'name' => 'CDS'
  268. ),
  269. ),
  270. 'type_id' => array(
  271. 'name' => 'part_of',
  272. ),
  273. );
  274. $options = array(
  275. 'order_by' => array('rank' => 'ASC'),
  276. 'return_array' => 1,
  277. 'include_fk' => array(
  278. 'subject_id' => 1
  279. ),
  280. );
  281. $cds_rels = chado_generate_var('feature_relationship', $values, $options);
  282. $coding_seq = '';
  283. foreach ($cds_rels as $cds_rel) {
  284. $cds_rel = chado_expand_var($cds_rel, 'field', 'feature.residues');
  285. if ($cds_rel->subject_id->residues) {
  286. $coding_seq .= $cds_rel->subject_id->residues;
  287. }
  288. }
  289. if ($coding_seq) {
  290. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  291. '@type' => 'SO:0000316',
  292. 'type' => 'coding_sequence',
  293. 'label' => 'Coding sequence (CDS)',
  294. 'defline' => tripal_get_fasta_defline($feature, '', $feature->featureloc->feature_id[0], '', strlen($coding_seq)),
  295. 'residues' => $coding_seq,
  296. );
  297. }
  298. foreach($featureloc_sequences as $src => $attrs){
  299. // the $attrs array has the following keys
  300. // * id: a unique identifier combining the feature id with the cvterm id
  301. // * type: the type of sequence (e.g. mRNA, etc)
  302. // * location: the alignment location
  303. // * defline: the definition line
  304. // * formatted_seq: the formatted sequences
  305. // * featureloc: the feature object aligned to
  306. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  307. 'residues' => $attrs['residues'],
  308. '@type' => 'SO:0000110',
  309. 'type' => 'sequence_feature',
  310. 'defline' => tripal_get_fasta_defline($feature, '', $attrs['featureloc'], 'CDS', strlen($attrs['residues'])),
  311. 'label' => 'Sequence from alignment at ' . $attrs['location'],
  312. );
  313. // check to see if this alignment has any CDS. If so, generate a CDS sequence
  314. $cds_sequence = tripal_get_feature_sequences(
  315. array(
  316. 'feature_id' => $feature->feature_id,
  317. 'parent_id' => $attrs['featureloc']->srcfeature_id->feature_id,
  318. 'name' => $feature->name,
  319. 'featureloc_id' => $attrs['featureloc']->featureloc_id,
  320. ),
  321. array(
  322. 'derive_from_parent' => 1, // CDS are in parent-child relationships so we want to use the sequence from the parent
  323. 'aggregate' => 1, // we want to combine all CDS for this feature into a single sequence
  324. 'sub_feature_types' => array('CDS'), // we're looking for CDS features
  325. 'is_html' => 0
  326. )
  327. );
  328. if (count($cds_sequence) > 0) {
  329. // the tripal_get_feature_sequences() function can return multiple sequences
  330. // if a feature is aligned to multiple places. In the case of CDSs we expect
  331. // that one mRNA is only aligned to a single location on the assembly so we
  332. // can access the CDS sequence with index 0.
  333. if ($cds_sequence[0]['residues']) {
  334. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  335. 'residues' => $cds_sequence[0]['residues'],
  336. '@type' => 'SO:0000316',
  337. 'type' => 'coding_sequence',
  338. 'defline' => tripal_get_fasta_defline($feature, '', $attrs['featureloc'], 'CDS', $cds_sequence[0]['length']),
  339. 'label' => 'Coding sequence (CDS) from alignment at ' . $attrs['location'],
  340. );
  341. }
  342. }
  343. }
  344. }
  345. /**
  346. *
  347. * @param unknown $feature_id
  348. * @param unknown $featurelocs
  349. * @return multitype:|Ambigous <multitype:, an>
  350. */
  351. private function get_featureloc_sequences($feature_id, $featurelocs) {
  352. // if we don't have any featurelocs then no point in continuing
  353. if (!$featurelocs) {
  354. return array();
  355. }
  356. // get the list of relationships (including any aggregators) and iterate
  357. // through each one to find information needed to color-code the reference sequence
  358. $relationships = $this->get_aggregate_relationships($feature_id);
  359. if (!$relationships) {
  360. return array();
  361. }
  362. // iterate through each of the realtionships features and get their
  363. // locations
  364. foreach ($relationships as $rindex => $rel) {
  365. // get the featurelocs for each of the relationship features
  366. $rel_featurelocs = $this->get_featurelocs($rel->subject_id, 'as_child', 0);
  367. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  368. // keep track of this unique source feature
  369. $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
  370. // copy over the results to the relationship object. Since there can
  371. // be more than one feature location for each relationship feature we
  372. // use the '$src' variable to keep track of these.
  373. $rel->featurelocs = new stdClass();
  374. $rel->featurelocs->$src = new stdClass();
  375. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  376. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  377. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  378. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  379. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  380. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  381. // keep track of the individual parts for each relationship
  382. $start = $rel->featurelocs->$src->fmin;
  383. $end = $rel->featurelocs->$src->fmax;
  384. $type = $rel->subject_type;
  385. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  386. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  387. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  388. }
  389. }
  390. // the featurelocs array provided to the function contains the locations
  391. // where this feature is found. We want to get the sequence for each
  392. // location and then annotate it with the parts found from the relationships
  393. // locations determiend above.
  394. $floc_sequences = array();
  395. foreach ($featurelocs as $featureloc) {
  396. // build the src name so we can keep track of the different parts for each feature
  397. $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
  398. // orient the parts to the beginning of the feature sequence
  399. if (!empty($rel_locs[$src]['parts'])) {
  400. $parts = $rel_locs[$src]['parts'];
  401. $rparts = array(); // we will fill this up if we're on the reverse strand
  402. foreach ($parts as $start => $types) {
  403. foreach ($types as $type_name => $type) {
  404. if ($featureloc->strand >= 0) {
  405. // this is on the forward strand. We need to convert the start on the src feature to the
  406. // start on this feature's sequence
  407. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  408. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  409. $parts[$start][$type_name]['type'] = $type_name;
  410. }
  411. else {
  412. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  413. // begining of the reverse sequence
  414. $size = ($featureloc->fmax - $featureloc->fmin);
  415. $start_orig = $parts[$start][$type_name]['start'];
  416. $end_orig = $parts[$start][$type_name]['end'];
  417. $new_start = $size - ($end_orig - $featureloc->fmin);
  418. $new_end = $size - ($start_orig - $featureloc->fmin);
  419. $rparts[$new_start][$type_name]['start'] = $new_start;
  420. $rparts[$new_start][$type_name]['end'] = $new_end;
  421. $rparts[$new_start][$type_name]['type'] = $type_name;
  422. }
  423. }
  424. }
  425. // now sort the parts
  426. // if we're on the reverse strand we need to resort
  427. if ($featureloc->strand >= 0) {
  428. usort($parts, 'chado_feature__residues_sort_rel_parts_by_start');
  429. }
  430. else {
  431. usort($rparts, 'chado_feature__residues_sort_rel_parts_by_start');
  432. $parts = $rparts;
  433. }
  434. $floc_sequences[$src]['id'] = $src;
  435. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  436. $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
  437. $start = $featureloc->fmin + 1;
  438. $size = $featureloc->fmax - $featureloc->fmin;
  439. // TODO: fix the hard coded $start and $size
  440. // the $start and $size variables are hard-coded in the SQL statement
  441. // because the db_query function places quotes around all placeholders
  442. // (e.g. :start & :size) and screws up the substring function
  443. $sql = "
  444. SELECT substring(residues from $start for $size) as residues
  445. FROM {feature}
  446. WHERE feature_id = :feature_id
  447. ";
  448. $sequence = chado_query($sql, $args)->fetchObject();
  449. $residues = $sequence->residues;
  450. if ($featureloc->strand < 0) {
  451. $residues = tripal_reverse_compliment_sequence($residues);
  452. }
  453. $strand = '.';
  454. if ($featureloc->strand == 1) {
  455. $strand = '+';
  456. }
  457. elseif ($featureloc->strand == -1) {
  458. $strand = '-';
  459. }
  460. $floc_sequences[$src]['location'] = tripal_get_location_string($featureloc);
  461. $floc_sequences[$src]['defline'] = tripal_get_fasta_defline($featureloc->feature_id, '', $featureloc, '', strlen($residues));
  462. $floc_sequences[$src]['featureloc'] = $featureloc;
  463. $floc_sequences[$src]['residues'] = $residues;
  464. //$floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $floc_sequences[$src]['defline']);
  465. }
  466. }
  467. return $floc_sequences;
  468. }
  469. /**
  470. * Get features related to the current feature to a given depth. Recursive function.
  471. *
  472. * @param $feature_id
  473. * @param $substitute
  474. * @param $levels
  475. * @param $base_type_id
  476. * @param $depth
  477. *
  478. * @ingroup tripal_feature
  479. */
  480. private function get_aggregate_relationships($feature_id, $substitute=1,
  481. $levels=0, $base_type_id=NULL, $depth=0) {
  482. // we only want to recurse to as many levels deep as indicated by the
  483. // $levels variable, but only if this variable is > 0. If 0 then we
  484. // recurse until we reach the end of the relationships tree.
  485. if ($levels > 0 and $levels == $depth) {
  486. return NULL;
  487. }
  488. // first get the relationships for this feature
  489. return $this->get_relationships($feature_id, 'as_object');
  490. }
  491. /**
  492. * Get the relationships for a feature.
  493. *
  494. * @param $feature_id
  495. * The feature to get relationships for
  496. * @param $side
  497. * The side of the relationship this feature is (ie: 'as_subject' or 'as_object')
  498. *
  499. * @ingroup tripal_feature
  500. */
  501. private function get_relationships($feature_id, $side = 'as_subject') {
  502. // get the relationships for this feature. The query below is used for both
  503. // querying the object and subject relationships
  504. $sql = "
  505. SELECT
  506. FS.name as subject_name, FS.uniquename as subject_uniquename,
  507. CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
  508. FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
  509. CVT.name as rel_type,
  510. FO.name as object_name, FO.uniquename as object_uniquename,
  511. CVTO.name as object_type, CVTO.cvterm_id as object_type_id
  512. FROM {feature_relationship} FR
  513. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  514. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  515. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  516. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  517. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  518. ";
  519. if (strcmp($side, 'as_object')==0) {
  520. $sql .= " WHERE FR.object_id = :feature_id";
  521. }
  522. if (strcmp($side, 'as_subject')==0) {
  523. $sql .= " WHERE FR.subject_id = :feature_id";
  524. }
  525. $sql .= " ORDER BY FR.rank";
  526. // get the relationships
  527. $results = chado_query($sql, array(':feature_id' => $feature_id));
  528. // iterate through the relationships, put these in an array and add
  529. // in the Drupal node id if one exists
  530. $i=0;
  531. $esql = "
  532. SELECT entity_id
  533. FROM {chado_entity}
  534. WHERE data_table = 'feature' AND record_id = :feature_id";
  535. $relationships = array();
  536. while ($rel = $results->fetchObject()) {
  537. $entity = db_query($esql, array(':feature_id' => $rel->subject_id))->fetchObject();
  538. if ($entity) {
  539. $rel->subject_entity_id = $entity->entity_id;
  540. }
  541. $entity = db_query($esql, array(':feature_id' => $rel->object_id))->fetchObject();
  542. if ($entity) {
  543. $rel->object_entity_id = $entity->entity_id;
  544. }
  545. $relationships[$i++] = $rel;
  546. }
  547. return $relationships;
  548. }
  549. /**
  550. * Load the locations for a given feature
  551. *
  552. * @param $feature_id
  553. * The feature to look up locations for
  554. * @param $side
  555. * Whether the feature is the scrfeature, 'as_parent', or feature, 'as_child'
  556. * @param $aggregate
  557. * Whether or not to get the locations for related features
  558. *
  559. * @ingroup tripal_feature
  560. */
  561. private function get_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  562. $sql = "
  563. SELECT
  564. F.name, F.feature_id, F.uniquename,
  565. FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
  566. CVT.name as cvname, CVT.cvterm_id,
  567. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  568. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
  569. FROM {featureloc} FL
  570. INNER JOIN {feature} F ON FL.feature_id = F.feature_id
  571. INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
  572. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  573. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  574. ";
  575. if (strcmp($side, 'as_parent')==0) {
  576. $sql .= "WHERE FL.srcfeature_id = :feature_id ";
  577. }
  578. if (strcmp($side, 'as_child')==0) {
  579. $sql .= "WHERE FL.feature_id = :feature_id ";
  580. }
  581. $flresults = chado_query($sql, array(':feature_id' => $feature_id));
  582. // copy the results into an array
  583. $i=0;
  584. $featurelocs = array();
  585. while ($loc = $flresults->fetchObject()) {
  586. // if a drupal node exists for this feature then add the nid to the
  587. // results object
  588. $loc->feid = tripal_get_chado_entity_id('feature', $loc->feature_id);
  589. $loc->seid = tripal_get_chado_entity_id('feature', $loc->src_feature_id);
  590. // add the result to the array
  591. $featurelocs[$i++] = $loc;
  592. }
  593. // Add the relationship feature locs if aggregate is turned on
  594. if ($aggregate and strcmp($side, 'as_parent')==0) {
  595. // get the relationships for this feature without substituting any children
  596. // for the parent. We want all relationships
  597. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  598. foreach ($relationships as $rindex => $rel) {
  599. // get the featurelocs for each of the relationship features
  600. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  601. foreach ($rel_featurelocs as $findex => $rfloc) {
  602. $featurelocs[$i++] = $rfloc;
  603. }
  604. }
  605. }
  606. usort($featurelocs, 'chado_feature__residues_sort_locations');
  607. return $featurelocs;
  608. }
  609. }
  610. /**
  611. * Callback function for validating the chado_feature__residues_widget.
  612. */
  613. function chado_feature__residues_widget_validate($element, &$form_state) {
  614. $field_name = $element['#parents'][0];
  615. // Remove any white spaces.
  616. $residues = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__residues');
  617. if ($residues) {
  618. $residues = preg_replace('/\s/', '', $residues);
  619. tripal_chado_set_field_form_values($field_name, $form_state, $residues, 0, 'feature__residues');
  620. }
  621. }
  622. /**
  623. * Used to sort the list of relationship parts by start position
  624. *
  625. * @ingroup tripal_feature
  626. */
  627. function chado_feature__residues_sort_rel_parts_by_start($a, $b) {
  628. foreach ($a as $type_name => $details) {
  629. $astart = $a[$type_name]['start'];
  630. break;
  631. }
  632. foreach ($b as $type_name => $details) {
  633. $bstart = $b[$type_name]['start'];
  634. break;
  635. }
  636. return strnatcmp($astart, $bstart);
  637. }
  638. /**
  639. * Used to sort the feature locs by start position
  640. *
  641. * @param $a
  642. * One featureloc record (as an object)
  643. * @param $b
  644. * The other featureloc record (as an object)
  645. *
  646. * @return
  647. * Which feature location comes first
  648. *
  649. * @ingroup tripal_feature
  650. */
  651. function chado_feature__residues_sort_locations($a, $b) {
  652. return strnatcmp($a->fmin, $b->fmin);
  653. }