data__sequence.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. 'elements' => [
  57. 'data:2044' => [
  58. 'name' => 'sequence',
  59. 'searchable' => FALSE,
  60. ],
  61. // @todo add this cvterm to the db.
  62. // https://www.ebi.ac.uk/ols/ontologies/ncit/terms?iri=http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FNCIT_C47845
  63. 'NCIT:C47845' => [
  64. 'name' => 'FASTA Format',
  65. 'searchable' => FALSE,
  66. ],
  67. ],
  68. ],
  69. ];
  70. }
  71. /**
  72. * @see TripalField::load()
  73. */
  74. public function load($entity) {
  75. $field_name = $this->field['field_name'];
  76. $feature = (array) $entity->chado_record;
  77. // Retrieve all the sequence for the given feature.
  78. $sequences = chado_get_feature_sequences($feature, []);
  79. // If there are no direct residues then try to derive from the parent.
  80. if (empty($sequences) OR (sizeof($sequences) == 1 AND empty($sequences[0]['residues']))) {
  81. $sequences = chado_get_feature_sequences($feature, ['derive_from_parent' => 1]);
  82. }
  83. // Add each sequence to the field as a fasta record.
  84. // NOTE: We keep the residues separate in case the formatter wants to
  85. // determine it's own fasta record defline.
  86. // @debug dpm($sequences, 'sequences');
  87. foreach ($sequences as $k => $seq) {
  88. if (!empty($seq['residues'])) {
  89. $entity->{$field_name}['und'][$k]['value'] = [
  90. 'NCIT:C47845' => '>' . $seq['defline'] . "\n" . $seq['residues'],
  91. 'data:2044' => $seq['residues'],
  92. ];
  93. }
  94. }
  95. }
  96. }