chado_feature__residues.inc 25 KB

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