chado_feature__residues.inc 26 KB

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