chado_gene__transcripts.inc 8.0 KB

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