chado_gene__transcripts.inc 8.8 KB

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