data__sequence.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. class data__sequence extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'Sequence';
  12. // The default description for this field.
  13. public static $description = 'A field for managing nucleotide and protein residues.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = [
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'data',
  24. // The name of the term.
  25. 'term_name' => 'sequence',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '2044',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. ];
  33. // Indicates the download formats for this field. The list must be the
  34. // name of a child class of the TripalFieldDownloader.
  35. public static $download_formatters = [
  36. 'TripalTabDownloader',
  37. 'TripalCSVDownloader',
  38. 'TripalNucFASTADownloader',
  39. ];
  40. // The default widget for this field.
  41. public static $default_widget = 'data__sequence_widget';
  42. // The default formatter for this field.
  43. public static $default_formatter = 'data__sequence_formatter';
  44. /**
  45. * @see TripalField::elementInfo()
  46. */
  47. public function elementInfo() {
  48. $field_term = $this->getFieldTermID();
  49. return [
  50. $field_term => [
  51. 'operations' => [],
  52. 'sortable' => FALSE,
  53. 'searchable' => FALSE,
  54. 'type' => 'xs:string',
  55. 'readonly' => FALSE,
  56. ],
  57. ];
  58. }
  59. /**
  60. * @see TripalField::load()
  61. */
  62. public function load($entity) {
  63. $field_name = $this->field['field_name'];
  64. $feature = $entity->chado_record;
  65. $feature = chado_expand_var($feature, 'field', 'feature.residues');
  66. $entity->{$field_name}['und'][0]['value'] = $feature->residues;
  67. /* // Add in sequences from alignments.
  68. $options = array(
  69. 'return_array' => 1,
  70. 'include_fk' => array(
  71. 'srcfeature_id' => array(
  72. 'type_id' => 1
  73. ),
  74. 'feature_id' => array(
  75. 'type_id' => 1
  76. ),
  77. ),
  78. );
  79. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  80. $featureloc_sequences = $this->get_featureloc_sequences($feature->feature_id, $feature->featureloc->feature_id);
  81. // Add in the coding sequences. It's faster to provide the SQL rather than
  82. // to use chado_generate_var based on the type.
  83. $sql = "
  84. SELECT F.*
  85. FROM {feature_relationship} FR
  86. INNER JOIN {feature} F on FR.subject_id = F.feature_id
  87. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  88. INNER JOIN {cvterm} RCVT on RCVT.cvterm_id = FR.type_id
  89. INNER JOIN {featureloc} FL on FL.feature_id = F.feature_id
  90. WHERE
  91. FR.object_id = :feature_id and
  92. CVT.name = 'CDS' and
  93. RCVT.name = 'part_of'
  94. ORDER BY FR.rank ASC
  95. ";
  96. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  97. $coding_seq = '';
  98. while ($CDS = $results->fetchObject()) {
  99. if ($CDS->residues) {
  100. $coding_seq .= $CDS->residues;
  101. }
  102. }
  103. if ($coding_seq) {
  104. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  105. '@type' => 'SO:0000316',
  106. 'type' => 'coding_sequence',
  107. 'label' => 'Coding sequence (CDS)',
  108. 'defline' => chado_get_fasta_defline($feature, 'CDS', NULL, '', strlen($coding_seq)),
  109. 'residues' => $coding_seq,
  110. );
  111. }
  112. foreach($featureloc_sequences as $src => $attrs){
  113. // the $attrs array has the following keys
  114. // * id: a unique identifier combining the feature id with the cvterm id
  115. // * type: the type of sequence (e.g. mRNA, etc)
  116. // * location: the alignment location
  117. // * defline: the definition line
  118. // * formatted_seq: the formatted sequences
  119. // * featureloc: the feature object aligned to
  120. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  121. 'residues' => $attrs['residues'],
  122. '@type' => 'SO:0000110',
  123. 'type' => 'sequence_feature',
  124. 'defline' => chado_get_fasta_defline($feature, '', $attrs['featureloc'], 'CDS', strlen($attrs['residues'])),
  125. 'label' => 'Sequence from alignment at ' . $attrs['location'],
  126. );
  127. // check to see if this alignment has any CDS. If so, generate a CDS sequence
  128. $cds_sequence = chado_get_feature_sequences(
  129. array(
  130. 'feature_id' => $feature->feature_id,
  131. 'parent_id' => $attrs['featureloc']->srcfeature_id->feature_id,
  132. 'name' => $feature->name,
  133. 'featureloc_id' => $attrs['featureloc']->featureloc_id,
  134. ),
  135. array(
  136. 'derive_from_parent' => 1, // CDS are in parent-child relationships so we want to use the sequence from the parent
  137. 'aggregate' => 1, // we want to combine all CDS for this feature into a single sequence
  138. 'sub_feature_types' => array('CDS'), // we're looking for CDS features
  139. 'is_html' => 0
  140. )
  141. );
  142. if (count($cds_sequence) > 0) {
  143. // the chado_get_feature_sequences() function can return multiple sequences
  144. // if a feature is aligned to multiple places. In the case of CDSs we expect
  145. // that one mRNA is only aligned to a single location on the assembly so we
  146. // can access the CDS sequence with index 0.
  147. if ($cds_sequence[0]['residues']) {
  148. $entity->{$field_name}['und'][$num_seqs++]['value'] = array(
  149. 'residues' => $cds_sequence[0]['residues'],
  150. '@type' => 'SO:0000316',
  151. 'type' => 'coding_sequence',
  152. 'defline' => chado_get_fasta_defline($feature, '', $attrs['featureloc'], 'CDS', $cds_sequence[0]['length']),
  153. 'label' => 'Coding sequence (CDS) from alignment at ' . $attrs['location'],
  154. );
  155. }
  156. }
  157. } */
  158. }
  159. }