chado_gene__transcripts.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. 'auto_attach' => FALSE,
  88. ),
  89. 'widget' => array(
  90. 'type' => 'chado_gene__transcripts_widget',
  91. 'settings' => array(
  92. 'display_label' => 1,
  93. ),
  94. ),
  95. 'display' => array(
  96. 'deafult' => array(
  97. 'label' => 'above',
  98. 'type' => 'chado_gene__transcripts_formatter',
  99. 'settings' => array(),
  100. ),
  101. ),
  102. );
  103. }
  104. /**
  105. * @see TripalField::widget_info()
  106. */
  107. public function widget_info() {
  108. return array(
  109. 'label' => t('Transcripts Settings'),
  110. 'field types' => array('chado_gene__transcripts')
  111. );
  112. }
  113. /**
  114. * @see TripalField::formatter_info()
  115. */
  116. public function formatter_info() {
  117. return array(
  118. 'label' => t('Transcripts'),
  119. 'field types' => array('chado_gene__transcripts'),
  120. 'settings' => array(
  121. ),
  122. );
  123. }
  124. /**
  125. * @see TripalField::formatter_settings_summary()
  126. */
  127. public function formatter_settings_summary($field, $instance,
  128. $view_mode) {
  129. }
  130. /**
  131. * @see TripalField::formatter_settings_form()
  132. */
  133. public function formatter_settings_form($field, $instance,
  134. $view_mode, $form, &$form_state) {
  135. }
  136. /**
  137. * @see TripalField::formatter_view()
  138. */
  139. public function formatter_view(&$element, $entity_type, $entity,
  140. $field, $instance, $langcode, $items, $display) {
  141. // Get the settings
  142. $settings = $display['settings'];
  143. $record = $entity->chado_record;
  144. $headers = array('Feature Name', 'Unique Name', 'Type', 'Location');
  145. $rows = array();
  146. foreach ($items as $delta => $item) {
  147. if (!$item['value']) {
  148. continue;
  149. }
  150. $transcript = $item['value'];
  151. // Get the field values
  152. $feature_name = $transcript['name'];
  153. $feature_uname = $transcript['unique name'];
  154. $loc = $transcript['location'];
  155. $type = $transcript['type'];
  156. // Add a link i there is an entity.
  157. if (array_key_exists('entity_id', $item) and $item['entity_id']) {
  158. $entity_id = $item['entity_id'];
  159. $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  160. }
  161. $rows[] = array($feature_name, $feature_uname, $type, $loc);
  162. }
  163. $table = array(
  164. 'header' => $headers,
  165. 'rows' => $rows,
  166. 'attributes' => array(
  167. 'id' => 'tripal_feature-table-transcripts-object',
  168. 'class' => 'tripal-data-table'
  169. ),
  170. 'sticky' => FALSE,
  171. 'caption' => "",
  172. 'colgroups' => array(),
  173. 'empty' => '',
  174. );
  175. $content = theme_table($table);
  176. // once we have our table array structure defined, we call Drupal's theme_table()
  177. // function to generate the table.
  178. if (count($items) > 0) {
  179. $element[0] = array(
  180. '#type' => 'markup',
  181. '#markup' => $content,
  182. );
  183. }
  184. }
  185. /**
  186. * @see TripalField::load()
  187. */
  188. public function load($field, $entity, $details) {
  189. $record = $details['record'];
  190. $field_name = $field['field_name'];
  191. // Set some defaults for the empty record.
  192. $entity->{$field_name}['und'][0] = array(
  193. 'value' => array(),
  194. );
  195. // TODO: If the tripal_get_feature_relationships() slows this down then
  196. // we may need to write a custom function to get the data.
  197. $rels = tripal_get_feature_relationships($record);
  198. // TODO: what if other transcripts names from SO are used. In that
  199. // case we should support those too (using cvtermpath table to find them).
  200. // mRNA should not be hard-coded below.
  201. // Set the value to be a array of "table" rows.
  202. $transcripts = array();
  203. if (key_exists('part of', $rels['object']) &&
  204. key_exists('mRNA', $rels['object']['part of'])) {
  205. $transcripts = $rels['object']['part of']['mRNA'];
  206. }
  207. $headers = array('Feature Name' ,'Unique Name', 'Location');
  208. $rows = array();
  209. $i = 0;
  210. foreach ($transcripts as $transcript) {
  211. // link the feature to it's node
  212. $feature_name = $transcript->record->subject_id->name;
  213. $locations = $transcript->child_featurelocs;
  214. $loc = "";
  215. foreach ($locations AS $location) {
  216. $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
  217. }
  218. $type = $transcript->record->subject_id->type_id;
  219. $entity->{$field_name}['und'][$i]['value'] = array(
  220. '@type' => $type->dbxref_id->db_id->name . ":" . $type->dbxref_id->accession,
  221. 'type' => $type->name,
  222. 'name' => $feature_name,
  223. 'unique name' => $transcript->record->subject_id->uniquename,
  224. 'location' => $loc,
  225. );
  226. if (property_exists($transcript->record->subject_id, 'entity_id')) {
  227. $entity_id = $transcript->record->subject_id->entity_id;
  228. $entity->{$field_name}['und'][$i]['entity_id'] = $entity_id;
  229. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  230. }
  231. $i++;
  232. }
  233. }
  234. /**
  235. * @see TripalField::settings_form()
  236. */
  237. public function settings_form($field, $instance, $view_mode) {
  238. }
  239. /**
  240. * @see TripalField::widget_form()
  241. */
  242. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  243. $langcode, $items, $delta, $element) {
  244. }
  245. }