chado_feature__residues.inc 25 KB

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