so__transcript.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. class so__transcript 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 = 'Transcripts';
  12. // The default description for this field.
  13. public static $description = 'An RNA synthesized on a DNA or RNA template by an RNA polymerase.';
  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' => 'transcript',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '0000673',
  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. // Contains the list of fields on the transcript child that the
  33. // user wants to have displayed for the transcript. The
  34. // `so__transcript_expanded_formatter will display these.
  35. 'transcript_fields' => []
  36. ];
  37. // The default widget for this field.
  38. public static $default_widget = 'so__transcript_widget';
  39. // The default formatter for this field.
  40. public static $default_formatter = 'so__transcript_formatter';
  41. // --------------------------------------------------------------------------
  42. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  43. // --------------------------------------------------------------------------
  44. // An array containing details about the field. The format of this array
  45. // is the same as that returned by field_info_fields()
  46. protected $field;
  47. // An array containing details about an instance of the field. A field does
  48. // not have to have an instance. But if dealing with an instance (such as
  49. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  50. protected $instance;
  51. /**
  52. * @see TripalField::elementInfo()
  53. */
  54. public function elementInfo() {
  55. $field_term = $this->getFieldTermID();
  56. return [
  57. $field_term => [
  58. 'operations' => [],
  59. 'sortable' => FALSE,
  60. 'searchable' => FALSE,
  61. 'type' => 'xs:complexType',
  62. 'readonly' => TRUE,
  63. 'elements' => [
  64. 'rdfs:type' => [
  65. 'name' => 'transcript_type',
  66. 'label' => 'Transcript Type',
  67. 'help' => 'The type of a transcript.',
  68. 'searchable' => FALSE,
  69. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  70. 'sortable' => FALSE,
  71. 'type' => 'xs:string',
  72. 'readonly' => TRUE,
  73. 'required' => FALSE,
  74. ],
  75. 'schema:name' => [
  76. 'name' => 'transcript_name',
  77. 'label' => 'Transcript Name',
  78. 'help' => 'The name of a transcript.',
  79. 'searchable' => FALSE,
  80. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  81. 'sortable' => FALSE,
  82. 'type' => 'xs:string',
  83. 'readonly' => TRUE,
  84. 'required' => FALSE,
  85. ],
  86. 'data:0842' => [
  87. 'name' => 'transcript_uniquename',
  88. 'label' => 'Transcript Identifier',
  89. 'help' => 'The unique identifier of a transcript.',
  90. 'searchable' => FALSE,
  91. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  92. 'sortable' => FALSE,
  93. 'type' => 'xs:string',
  94. 'readonly' => TRUE,
  95. 'required' => FALSE,
  96. ],
  97. 'SO:0000735' => [
  98. 'name' => 'loc',
  99. 'label' => 'Transcript Location',
  100. 'help' => 'The location of the transcript on a reference feature.',
  101. 'searchable' => FALSE,
  102. 'operations' => ['eq', 'ne', 'contains', 'starts'],
  103. 'sortable' => FALSE,
  104. 'type' => 'xs:string',
  105. 'readonly' => TRUE,
  106. 'required' => FALSE,
  107. ],
  108. 'entity' => [
  109. 'searchable' => FALSE,
  110. 'sortable' => FALSE,
  111. ],
  112. ],
  113. ],
  114. ];
  115. }
  116. /**
  117. *
  118. * @see TripalField::load()
  119. */
  120. public function load($entity) {
  121. $record = $entity->chado_record;
  122. $field_name = $this->field['field_name'];
  123. $field_type = $this->field['type'];
  124. $field_table = $this->instance['settings']['chado_table'];
  125. $field_column = $this->instance['settings']['chado_column'];
  126. // Set some defaults for the empty record.
  127. $entity->{$field_name}['und'][0] = [
  128. 'value' => '',
  129. ];
  130. if (!$record) {
  131. return;
  132. }
  133. // Get the mRNA features for this gene.
  134. $sql = "
  135. SELECT FS.name, FS.uniquename, FS.feature_id, FCVT.name as type
  136. FROM {feature_relationship} FR
  137. INNER JOIN {feature} FS on FS.feature_id = FR.subject_id
  138. INNER JOIN {cvterm} FCVT on FCVT.cvterm_id = FS.type_id
  139. INNER JOIN {cv} CV on CV.cv_id = FCVT.cv_id
  140. WHERE
  141. FR.object_id = :feature_id and
  142. FCVT.name = 'mRNA' and
  143. CV.name = 'sequence'
  144. ";
  145. $results = chado_query($sql, [':feature_id' => $record->feature_id]);
  146. $i = 0;
  147. while ($transcript = $results->fetchObject()) {
  148. // Get the location of this mRNA.
  149. $sql = "
  150. SELECT FL.*, F.name as srcfeature_name
  151. FROM {featureloc} FL
  152. INNER JOIN {feature} F on F.feature_id = FL.srcfeature_id
  153. WHERE FL.feature_id = :object_id
  154. ";
  155. $floc_results = chado_query($sql, [':object_id' => $transcript->feature_id]);
  156. $loc = "";
  157. while ($location = $floc_results->fetchObject()) {
  158. $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
  159. }
  160. $entity->{$field_name}['und'][$i]['value'] = [
  161. 'rdfs:type' => $transcript->type,
  162. 'schema:name' => $transcript->name,
  163. 'data:0842' => $transcript->uniquename,
  164. 'SO:0000735' => $loc,
  165. ];
  166. $feature = chado_generate_var('feature', ['feature_id' => $transcript->feature_id]);
  167. if (property_exists($feature, 'entity_id')) {
  168. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $feature->entity_id;
  169. }
  170. $i++;
  171. }
  172. }
  173. /**
  174. * @see TripalField::globalSettingsForm()
  175. */
  176. public function instanceSettingsForm() {
  177. $element = parent::instanceSettingsForm();
  178. $settings = $this->instance['settings'];
  179. $element['transcript_fields'] = [
  180. '#type' => 'value',
  181. '#default_value' => $settings['transcript_fields'],
  182. ];
  183. //Get the content type for the mRNA term (SO:0000234) and it's feilds.
  184. $bundle = tripal_load_bundle_entity(['accession' => 'SO:0000234']);
  185. $fields = field_info_instances('TripalEntity', $bundle->name);
  186. foreach ($fields as $field_name => $details) {
  187. $weight = $details['display']['default']['weight'];
  188. $element['field-'. $weight] = [
  189. '#type' => 'value',
  190. '#value' => $field_name,
  191. '#weight' => $weight,
  192. ];
  193. $element['field-'. $weight . '-check'] = [
  194. '#type' => 'checkbox',
  195. '#weight' => $weight,
  196. ];
  197. $element['field-'. $weight . '-label'] = [
  198. '#type' => 'markup',
  199. '#markup' => t($details['label']),
  200. '#weight' => $weight,
  201. ];
  202. $element['field-'. $weight . '-description'] = [
  203. '#type' => 'markup',
  204. '#markup' => t($details['description']),
  205. '#weight' => $weight,
  206. ];
  207. $element['field-'. $weight . '-weight'] = [
  208. '#type' => 'textfield',
  209. '#default_value' => $weight,
  210. '#weight' => $weight,
  211. '#size' => 3,
  212. '#attributes' => [
  213. 'class' => ['tripal-field-settings-table-weights'],
  214. ],
  215. ];
  216. }
  217. return $element;
  218. }
  219. /**
  220. * @see TripalField::settingsTheme()
  221. */
  222. public function settingsTheme($element) {
  223. $headers = ['', 'Field', 'Description', ''];
  224. $rows = [];
  225. foreach (element_children($element) as $child) {
  226. $matches = [];
  227. if (preg_match('/^field-(\d+)-(.+)$/', $child, $matches)) {
  228. $weight = $matches[1];
  229. switch ($matches[2]) {
  230. case 'check':
  231. $rows[$weight]['data'][0] = drupal_render($element[$child]);
  232. break;
  233. case 'label':
  234. $rows[$weight]['data'][1] = drupal_render($element[$child]);
  235. break;
  236. case 'description':
  237. $rows[$weight]['data'][2] = drupal_render($element[$child]);
  238. break;
  239. case 'weight':
  240. $rows[$weight]['data'][3] = drupal_render($element[$child]);
  241. break;
  242. }
  243. ksort($rows[$weight]['data']);
  244. $rows[$weight]['class'] = ['draggable'];
  245. }
  246. }
  247. ksort($rows);
  248. $table = [
  249. 'header' => $headers,
  250. 'rows' => $rows,
  251. 'attributes' => ["id" => 'tripal-field-settings-table'],
  252. 'sticky' => TRUE,
  253. 'caption' => t('Content Panes Available in the TOC'),
  254. 'colgroups' => [],
  255. 'empty' => t('There are no content panes for this page'),
  256. ];
  257. drupal_add_tabledrag('tripal-field-settings-table', 'order', 'sibling', 'tripal-field-settings-table-weights');
  258. $fset = [];
  259. $fset['fields'] = [
  260. '#type' => 'fieldset',
  261. '#title' => 'Field Selection',
  262. '#description' => t('Select the fields that you want displayed on the gene page for each transcript that belongs to the gene. If no fields are selected then a default display is provided.'),
  263. '#collapsible' => TRUE,
  264. '#collapsed' => TRUE,
  265. ];
  266. $fset['fields']['table'] = [
  267. '#type' => 'markup',
  268. '#markup' => theme_table($table),
  269. ];
  270. return drupal_render($fset['fields']);
  271. }
  272. }