so__transcript.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. $defaults = [];
  188. if (array_key_exists($field_name, $settings['transcript_fields'])) {
  189. $defaults = $settings['transcript_fields'][$field_name];
  190. }
  191. $weight = array_key_exists('weight', $defaults) ? $defaults['weight'] : $details['display']['default']['weight'];
  192. $checked = array_key_exists('show', $defaults) ? $defaults['show'] : 0;
  193. $element['themeable']['field-'. $weight] = [
  194. '#type' => 'value',
  195. '#value' => $field_name,
  196. '#weight' => $weight,
  197. ];
  198. $element['themeable']['field-'. $weight . '-check'] = [
  199. '#type' => 'checkbox',
  200. '#weight' => $weight,
  201. '#default_value' => $checked,
  202. ];
  203. $element['themeable']['field-'. $weight . '-label'] = [
  204. '#type' => 'markup',
  205. '#markup' => t($details['label']),
  206. '#weight' => $weight,
  207. ];
  208. $element['themeable']['field-'. $weight . '-description'] = [
  209. '#type' => 'markup',
  210. '#markup' => t($details['description']),
  211. '#weight' => $weight,
  212. ];
  213. $element['themeable']['field-'. $weight . '-weight'] = [
  214. '#type' => 'textfield',
  215. '#default_value' => $weight,
  216. '#weight' => $weight,
  217. '#size' => 3,
  218. '#attributes' => [
  219. 'class' => ['tripal-field-settings-table-weights'],
  220. ],
  221. ];
  222. }
  223. return $element;
  224. }
  225. /**
  226. * @see TripalField::instanceSettingsFormValidate()
  227. */
  228. public function instanceSettingsFormValidate($form, &$form_state) {
  229. $settings = $form_state['values']['instance']['settings']['themeable'];
  230. // Iterate through the themable elements to get the weights and
  231. // checks for each field. Store that info in a temp array.
  232. $temp = [];
  233. foreach ($settings as $element => $value) {
  234. $matches = [];
  235. if (preg_match('/^field-(\d+)-check$/', $element, $matches)) {
  236. $orig_weight = $matches[1];
  237. $new_weight = $settings['field-' . $orig_weight . '-weight'];
  238. $field_name = $settings['field-' . $orig_weight];
  239. $temp[$new_weight]['name'] = $field_name;
  240. $temp[$new_weight]['show'] = $value;
  241. }
  242. }
  243. // Sort and resstart the weights from zero.
  244. ksort($temp);
  245. $temp = array_values($temp);
  246. // Now build the transcirpt_fields setting value.
  247. $transcript_fields = [];
  248. foreach ($temp as $weight => $field) {
  249. $field_name = $field['name'];
  250. $transcript_fields[$field_name] = [
  251. 'weight' => $weight,
  252. 'show' => $field['show'],
  253. ];
  254. }
  255. // Now store the values for the setting properly and remove the unwanted.
  256. $form_state['values']['instance']['settings']['transcript_fields'] = $transcript_fields;
  257. unset($form_state['values']['instance']['settings']['themeable']);
  258. }
  259. /**
  260. * @see TripalField::settingsTheme()
  261. */
  262. public function settingsTheme($element) {
  263. $headers = ['', 'Field', 'Description', ''];
  264. $rows = [];
  265. foreach (element_children($element) as $child) {
  266. $matches = [];
  267. if (preg_match('/^field-(\d+)-(.+)$/', $child, $matches)) {
  268. $weight = $matches[1];
  269. switch ($matches[2]) {
  270. case 'check':
  271. $rows[$weight]['data'][0] = drupal_render($element[$child]);
  272. break;
  273. case 'label':
  274. $rows[$weight]['data'][1] = drupal_render($element[$child]);
  275. break;
  276. case 'description':
  277. $rows[$weight]['data'][2] = drupal_render($element[$child]);
  278. break;
  279. case 'weight':
  280. $rows[$weight]['data'][3] = drupal_render($element[$child]);
  281. break;
  282. }
  283. ksort($rows[$weight]['data']);
  284. $rows[$weight]['class'] = ['draggable'];
  285. }
  286. }
  287. ksort($rows);
  288. $table = [
  289. 'header' => $headers,
  290. 'rows' => $rows,
  291. 'attributes' => ["id" => 'tripal-field-settings-table'],
  292. 'sticky' => TRUE,
  293. 'caption' => t('Content Panes Available in the TOC'),
  294. 'colgroups' => [],
  295. 'empty' => t('There are no content panes for this page'),
  296. ];
  297. drupal_add_tabledrag('tripal-field-settings-table', 'order', 'sibling', 'tripal-field-settings-table-weights');
  298. $fset = [];
  299. $fset['fields'] = [
  300. '#type' => 'fieldset',
  301. '#title' => 'Transcript (mRNA) Field Selection',
  302. '#description' => t('Select the fields that you want displayed on the gene page for each transcript. If no fields are selected then a default display is provided.'),
  303. '#collapsible' => TRUE,
  304. '#collapsed' => TRUE,
  305. '#attributes' => ['class' => ['collapsible', 'collapsed']],
  306. ];
  307. drupal_add_library('system', 'drupal.collapse');
  308. $fset['fields']['table'] = [
  309. '#type' => 'markup',
  310. '#markup' => theme_table($table),
  311. ];
  312. return drupal_render($fset['fields']);
  313. }
  314. }