so__transcript.inc 12 KB

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