chado_feature__residues.inc 23 KB

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