chado_gene__transcripts.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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::attach_info()
  22. */
  23. public function attach_info($entity_type, $bundle, $target) {
  24. $field_info = array();
  25. $table_name = $target['data_table'];
  26. $type_table = $target['type_table'];
  27. $type_field = $target['field'];
  28. $cv_id = $target['cv_id'];
  29. $cvterm_id = $target['cvterm_id'];
  30. // If the linker table does not exists or this is not a gene then we don't want to add attach.
  31. $rel_table = $table_name . '_relationship';
  32. if (!chado_table_exists($rel_table) || $bundle->label != 'gene') {
  33. return $field_info;
  34. }
  35. $schema = chado_get_schema($rel_table);
  36. $pkey = $schema['primary key'][0];
  37. // Initialize the field array.
  38. $field_info = array(
  39. 'field_name' => 'gene__transcripts',
  40. 'field_type' => 'chado_gene__transcripts',
  41. 'widget_type' => 'chado_gene__transcripts_widget',
  42. 'widget_settings' => array('display_label' => 1),
  43. 'description' => '',
  44. 'label' => 'Transcripts',
  45. 'is_required' => 0,
  46. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  47. 'storage' => 'field_chado_storage',
  48. 'field_settings' => array(
  49. 'chado_table' => $rel_table,
  50. 'chado_column' => $pkey,
  51. 'base_table' => $table_name,
  52. 'semantic_web' => array(
  53. 'name' => 'transcript',
  54. 'accession' => '0000673',
  55. 'ns' => 'SO',
  56. 'nsurl' => 'http://www.sequenceontology.org',
  57. ),
  58. ),
  59. );
  60. return $field_info;
  61. }
  62. /**
  63. * @see TripalField::widget_info()
  64. */
  65. public function widget_info() {
  66. return array(
  67. 'label' => t('Transcripts Settings'),
  68. 'field types' => array('chado_gene__transcripts')
  69. );
  70. }
  71. /**
  72. * @see TripalField::formatter_info()
  73. */
  74. public function formatter_info() {
  75. return array(
  76. 'label' => t('Transcripts'),
  77. 'field types' => array('chado_gene__transcripts'),
  78. 'settings' => array(
  79. ),
  80. );
  81. }
  82. /**
  83. * @see TripalField::formatter_settings_summary()
  84. */
  85. public function formatter_settings_summary($field, $instance,
  86. $view_mode) {
  87. }
  88. /**
  89. * @see TripalField::formatter_settings_form()
  90. */
  91. public function formatter_settings_form($field, $instance,
  92. $view_mode, $form, &$form_state) {
  93. }
  94. /**
  95. * @see TripalField::formatter_view()
  96. */
  97. public function formatter_view(&$element, $entity_type, $entity,
  98. $field, $instance, $langcode, $items, $display) {
  99. // Get the settings
  100. $settings = $display['settings'];
  101. $record = $entity->chado_record;
  102. $headers = array('Feature Name', 'Unique Name', 'Type', 'Location');
  103. $rows = array();
  104. foreach ($items as $delta => $item) {
  105. $transcript = $item['value'];
  106. // Get the field values
  107. $feature_name = $transcript['name'];
  108. $feature_uname = $transcript['unique name'];
  109. $loc = $transcript['location'];
  110. $type = $transcript['type'];
  111. // Add a link i there is an entity.
  112. if (array_key_exists('entity_id', $transcript) and $transcript['$entity_id']) {
  113. $entity_id = $transcript['entity_id'];
  114. $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  115. }
  116. $rows[] = array($feature_name, $feature_uname, $type, $loc);
  117. }
  118. $table = array(
  119. 'header' => $headers,
  120. 'rows' => $rows,
  121. 'attributes' => array(
  122. 'id' => 'tripal_feature-table-transcripts-object',
  123. 'class' => 'tripal-data-table'
  124. ),
  125. 'sticky' => FALSE,
  126. 'caption' => "",
  127. 'colgroups' => array(),
  128. 'empty' => '',
  129. );
  130. $content = theme_table($table);
  131. // once we have our table array structure defined, we call Drupal's theme_table()
  132. // function to generate the table.
  133. $element[$delta] = array(
  134. '#type' => 'markup',
  135. '#markup' => $content,
  136. );
  137. }
  138. /**
  139. * @see TripalField::load()
  140. */
  141. public function load($field, $entity, $details) {
  142. $record = $details['record'];
  143. $field_name = $field['field_name'];
  144. // TODO: If the tripal_get_feature_relationships() slows this down then
  145. // we may need to write a custom function to get the data.
  146. $rels = tripal_get_feature_relationships($record);
  147. // TODO: what if other transcripts names from SO are used. In that
  148. // case we should support those too (using cvtermpath table to find them).
  149. // mRNA should not be hard-coded below.
  150. // Set the value to be a array of "table" rows.
  151. $transcripts = array();
  152. if (key_exists('part of', $rels['object']) &&
  153. key_exists('mRNA', $rels['object']['part of'])) {
  154. $transcripts = $rels['object']['part of']['mRNA'];
  155. }
  156. $headers = array('Feature Name' ,'Unique Name', 'Location');
  157. $rows = array();
  158. $i = 0;
  159. foreach ($transcripts as $transcript) {
  160. // link the feature to it's node
  161. $feature_name = $transcript->record->subject_id->name;
  162. $locations = $transcript->child_featurelocs;
  163. $loc = "";
  164. foreach ($locations AS $location) {
  165. $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
  166. }
  167. $entity->{$field_name}['und'][$i]['value'] = array(
  168. 'name' => $feature_name,
  169. 'type' => $transcript->record->subject_id->type_id->name,
  170. 'unique name' => $transcript->record->subject_id->uniquename,
  171. 'location' => $loc,
  172. );
  173. if (property_exists($transcript->record->subject_id, 'entity_id')) {
  174. $entity_id = $transcript->record->subject_id->entity_id;
  175. $entity->{$field_name}['und'][$i]['entity_id'] = $entity_id;
  176. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  177. }
  178. $i++;
  179. }
  180. }
  181. /**
  182. * @see TripalField::settings_form()
  183. */
  184. public function settings_form($field, $instance, $view_mode) {
  185. }
  186. /**
  187. * @see TripalField::widget_form()
  188. */
  189. public function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  190. }
  191. }