chado_gene__transcripts.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. class chado_gene__transcripts extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. public function field_info() {
  7. return array(
  8. 'label' => t('Transcripts'),
  9. 'description' => t('Transcripts of genes.'),
  10. 'default_widget' => 'chado_gene__transcripts_widget',
  11. 'default_formatter' => 'chado_gene__transcripts_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::can_attach()
  22. */
  23. protected function can_attach($entity_type, $bundle, $details) {
  24. $table_name = $details['chado_table'];
  25. $type_table = $details['chado_type_table'];
  26. $type_field = $details['chado_type_column'];
  27. $cv_id = $details['chado_cv_id'];
  28. $cvterm_id = $details['chado_cvterm_id'];
  29. // If the linker table does not exists or this is not a gene then we don't want to add attach.
  30. $rel_table = $table_name . '_relationship';
  31. if (chado_table_exists($rel_table) and $bundle->label == 'gene') {
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. /**
  37. * @see TripalField::create_info()
  38. */
  39. function create_info($entity_type, $bundle, $details) {
  40. if (!$this->can_attach($entity_type, $bundle, $details)) {
  41. return;
  42. }
  43. $table_name = $details['chado_table'];
  44. $type_table = $details['chado_type_table'];
  45. $type_field = $details['chado_type_column'];
  46. $cv_id = $details['chado_cv_id'];
  47. $cvterm_id = $details['chado_cvterm_id'];
  48. $rel_table = $table_name . '_relationship';
  49. $schema = chado_get_schema($rel_table);
  50. $pkey = $schema['primary key'][0];
  51. return array(
  52. 'field_name' => 'gene_transcripts',
  53. 'type' => 'chado_gene__transcripts',
  54. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  55. 'locked' => FALSE,
  56. 'storage' => array(
  57. 'type' => 'field_chado_storage',
  58. ),
  59. 'settings' => array(
  60. 'chado_table' => $rel_table,
  61. 'chado_column' => 'md5checksum',
  62. 'base_table' => $table_name,
  63. 'semantic_web' => 'SO:0000673',
  64. ),
  65. );
  66. }
  67. /**
  68. * @see TripalField::create_instance_info()
  69. */
  70. function create_instance_info($entity_type, $bundle, $details) {
  71. if (!$this->can_attach($entity_type, $bundle, $details)) {
  72. return;
  73. }
  74. $table_name = $details['chado_table'];
  75. $type_table = $details['chado_type_table'];
  76. $type_field = $details['chado_type_column'];
  77. $cv_id = $details['chado_cv_id'];
  78. $cvterm_id = $details['chado_cvterm_id'];
  79. return array(
  80. 'field_name' => 'gene_transcripts',
  81. 'entity_type' => $entity_type,
  82. 'bundle' => $bundle->name,
  83. 'label' => 'Transcripts',
  84. 'description' => 'These transcripts are associated with this gene.',
  85. 'required' => FALSE,
  86. 'settings' => array(),
  87. 'widget' => array(
  88. 'type' => 'chado_gene__transcripts_widget',
  89. 'settings' => array(
  90. 'display_label' => 1,
  91. ),
  92. ),
  93. 'display' => array(
  94. 'deafult' => array(
  95. 'label' => 'above',
  96. 'type' => 'chado_gene__transcripts_formatter',
  97. 'settings' => array(),
  98. ),
  99. ),
  100. );
  101. }
  102. /**
  103. * @see TripalField::widget_info()
  104. */
  105. public function widget_info() {
  106. return array(
  107. 'label' => t('Transcripts Settings'),
  108. 'field types' => array('chado_gene__transcripts')
  109. );
  110. }
  111. /**
  112. * @see TripalField::formatter_info()
  113. */
  114. public function formatter_info() {
  115. return array(
  116. 'label' => t('Transcripts'),
  117. 'field types' => array('chado_gene__transcripts'),
  118. 'settings' => array(
  119. ),
  120. );
  121. }
  122. /**
  123. * @see TripalField::formatter_settings_summary()
  124. */
  125. public function formatter_settings_summary($field, $instance,
  126. $view_mode) {
  127. }
  128. /**
  129. * @see TripalField::formatter_settings_form()
  130. */
  131. public function formatter_settings_form($field, $instance,
  132. $view_mode, $form, &$form_state) {
  133. }
  134. /**
  135. * @see TripalField::formatter_view()
  136. */
  137. public function formatter_view(&$element, $entity_type, $entity,
  138. $field, $instance, $langcode, $items, $display) {
  139. // Get the settings
  140. $settings = $display['settings'];
  141. $record = $entity->chado_record;
  142. $headers = array('Feature Name', 'Unique Name', 'Type', 'Location');
  143. $rows = array();
  144. foreach ($items as $delta => $item) {
  145. if (!$item['value']) {
  146. continue;
  147. }
  148. $transcript = $item['value'];
  149. // Get the field values
  150. $feature_name = $transcript['name'];
  151. $feature_uname = $transcript['unique name'];
  152. $loc = $transcript['location'];
  153. $type = $transcript['type'];
  154. // Add a link i there is an entity.
  155. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  156. $entity_id = $item['entity_id'];
  157. $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  158. }
  159. $rows[] = array($feature_name, $feature_uname, $type, $loc);
  160. }
  161. $table = array(
  162. 'header' => $headers,
  163. 'rows' => $rows,
  164. 'attributes' => array(
  165. 'id' => 'tripal_feature-table-transcripts-object',
  166. 'class' => 'tripal-data-table'
  167. ),
  168. 'sticky' => FALSE,
  169. 'caption' => "",
  170. 'colgroups' => array(),
  171. 'empty' => '',
  172. );
  173. $content = theme_table($table);
  174. // once we have our table array structure defined, we call Drupal's theme_table()
  175. // function to generate the table.
  176. $element[$delta] = array(
  177. '#type' => 'markup',
  178. '#markup' => $content,
  179. );
  180. }
  181. /**
  182. * @see TripalField::load()
  183. */
  184. public function load($field, $entity, $details) {
  185. $record = $details['record'];
  186. $field_name = $field['field_name'];
  187. // Set some defaults for the empty record.
  188. $entity->{$field_name}['und'][0] = array(
  189. 'value' => array(),
  190. );
  191. // TODO: If the tripal_get_feature_relationships() slows this down then
  192. // we may need to write a custom function to get the data.
  193. $rels = tripal_get_feature_relationships($record);
  194. // TODO: what if other transcripts names from SO are used. In that
  195. // case we should support those too (using cvtermpath table to find them).
  196. // mRNA should not be hard-coded below.
  197. // Set the value to be a array of "table" rows.
  198. $transcripts = array();
  199. if (key_exists('part of', $rels['object']) &&
  200. key_exists('mRNA', $rels['object']['part of'])) {
  201. $transcripts = $rels['object']['part of']['mRNA'];
  202. }
  203. $headers = array('Feature Name' ,'Unique Name', 'Location');
  204. $rows = array();
  205. $i = 0;
  206. foreach ($transcripts as $transcript) {
  207. // link the feature to it's node
  208. $feature_name = $transcript->record->subject_id->name;
  209. $locations = $transcript->child_featurelocs;
  210. $loc = "";
  211. foreach ($locations AS $location) {
  212. $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
  213. }
  214. $type = $transcript->record->subject_id->type_id;
  215. $entity->{$field_name}['und'][$i]['value'] = array(
  216. '@type' => $type->dbxref_id->db_id->name . ":" . $type->dbxref_id->accession,
  217. 'type' => $type->name,
  218. 'name' => $feature_name,
  219. 'unique name' => $transcript->record->subject_id->uniquename,
  220. 'location' => $loc,
  221. );
  222. if (property_exists($transcript->record->subject_id, 'entity_id')) {
  223. $entity_id = $transcript->record->subject_id->entity_id;
  224. $entity->{$field_name}['und'][$i]['entity_id'] = $entity_id;
  225. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  226. }
  227. $i++;
  228. }
  229. }
  230. /**
  231. * @see TripalField::settings_form()
  232. */
  233. public function settings_form($field, $instance, $view_mode) {
  234. }
  235. /**
  236. * @see TripalField::widget_form()
  237. */
  238. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  239. $langcode, $items, $delta, $element) {
  240. }
  241. }