so__cds.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class so__cds 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 label for this field.
  11. public static $default_label = 'Coding Sequence';
  12. // The default description for this field.
  13. public static $description = 'A contiguous sequence which begins with, and includes, a start codon and ends with, and includes, a stop codon.';
  14. // Provide a list of instance specific settings. These can be accessed within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal will automatically change these settings for the instance.
  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. schema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'SO',
  24. // The name of the term.
  25. 'term_name' => 'CDS',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '0000316',
  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. // The default widget for this field.
  34. public static $default_widget = 'so__cds_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'so__cds_formatter';
  37. /**
  38. * @see TripalField::elementInfo()
  39. */
  40. public function elementInfo() {
  41. $field_term = $this->getFieldTermID();
  42. return [
  43. $field_term => [
  44. 'sortable' => FALSE,
  45. 'searchable' => FALSE,
  46. 'type' => 'xs:string',
  47. 'readonly' => TRUE,
  48. ],
  49. ];
  50. }
  51. /**
  52. * @see TripalField::load()
  53. */
  54. public function load($entity) {
  55. $field_name = $this->field['field_name'];
  56. $feature = $entity->chado_record;
  57. $num_seqs = 0;
  58. // Set some defauls for the empty record
  59. $entity->{$field_name}['und'][0] = [
  60. 'value' => '',
  61. ];
  62. // Get the featureloc records that this feature is aligned to. We use
  63. // this SQL rather than the chado_expand_var function because we don't
  64. // want the residues included from the srcfeature_id which may be huge
  65. // and overrun memory.
  66. $featurelocs_sql = "
  67. SELECT FL.featureloc_id, FL.srcfeature_id
  68. FROM {featureloc} FL
  69. WHERE FL.feature_id = :feature_id
  70. ORDER BY rank ASC
  71. ";
  72. $aligned = chado_query($featurelocs_sql, [':feature_id' => $feature->feature_id]);
  73. $index = 0;
  74. while ($featureloc = $aligned->fetchObject()) {
  75. // Generate a CDS sequence if one exsits for this feature alignment.
  76. $cds_sequence = chado_get_feature_sequences(
  77. [
  78. 'feature_id' => $feature->feature_id,
  79. 'parent_id' => $featureloc->srcfeature_id,
  80. 'name' => $feature->name,
  81. 'featureloc_id' => $featureloc->featureloc_id,
  82. ],
  83. [
  84. // CDS are in parent-child relationships so we want to use the
  85. // sequence from the parent
  86. 'derive_from_parent' => 1,
  87. // we want to combine all CDS for this feature into a single sequence
  88. 'aggregate' => 1,
  89. // we're looking for CDS features
  90. 'sub_feature_types' => ['CDS'],
  91. 'is_html' => 0,
  92. ]
  93. );
  94. if (count($cds_sequence) > 0) {
  95. // the chado_get_feature_sequences() function can return multiple sequences
  96. // if a feature is aligned to multiple places. In the case of CDSs we expect
  97. // that one mRNA is only aligned to a single location on the assembly so we
  98. // can access the CDS sequence with index 0.
  99. if ($cds_sequence[0]['residues']) {
  100. $entity->{$field_name}['und'][$num_seqs++] = [
  101. 'value' => $cds_sequence[0]['residues'],
  102. // This field was incorrelctly listed as a field in the featureprop
  103. // table, but really it is a derived field. So, we have to do this
  104. // hacky fix to get around the problem.
  105. 'chado-featureprop__featureprop_id' => NULL,
  106. 'chado-featureprop__feature_id' => NULL,
  107. 'chado-featureprop__value' => NULL,
  108. 'chado-featureprop__type_id' => NULL,
  109. 'chado-featureprop__rank' => NULL
  110. ];
  111. }
  112. }
  113. }
  114. }
  115. }