chado_gene__transcripts.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. 'type' => '',
  54. 'ns' => '',
  55. 'nsurl' => '',
  56. ),
  57. ),
  58. );
  59. return $field_info;
  60. }
  61. /**
  62. * @see TripalField::widget_info()
  63. */
  64. public function widget_info() {
  65. return array(
  66. 'label' => t('Transcripts Settings'),
  67. 'field types' => array('chado_gene__transcripts')
  68. );
  69. }
  70. /**
  71. * @see TripalField::formatter_info()
  72. */
  73. public function formatter_info() {
  74. return array(
  75. 'label' => t('Transcripts'),
  76. 'field types' => array('chado_gene__transcripts'),
  77. 'settings' => array(
  78. ),
  79. );
  80. }
  81. /**
  82. * @see TripalField::formatter_settings_summary()
  83. */
  84. public function formatter_settings_summary($field, $instance,
  85. $view_mode) {
  86. }
  87. /**
  88. * @see TripalField::formatter_settings_form()
  89. */
  90. public function formatter_settings_form($field, $instance,
  91. $view_mode, $form, &$form_state) {
  92. }
  93. /**
  94. * @see TripalField::formatter_view()
  95. */
  96. public function formatter_view(&$element, $entity_type, $entity,
  97. $field, $instance, $langcode, $items, $display) {
  98. // Get the settings
  99. $settings = $display['settings'];
  100. $record = $entity->chado_record;
  101. $headers = array('Feature Name', 'Unique Name', 'Type', 'Location');
  102. $rows = array();
  103. foreach ($items as $delta => $item) {
  104. $transcript = $item['value'];
  105. $feature_name = $transcript['name'];
  106. $feature_uname = $transcript['unique name'];
  107. $entity_id = $transcript['entity_id'];
  108. if ($entity_id) {
  109. $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  110. }
  111. $loc = $transcript['location'];
  112. $type = $transcript['type'];
  113. $rows[] = array($feature_name, $feature_uname, $type, $loc);
  114. }
  115. $table = array(
  116. 'header' => $headers,
  117. 'rows' => $rows,
  118. 'attributes' => array(
  119. 'id' => 'tripal_feature-table-transcripts-object',
  120. 'class' => 'tripal-data-table'
  121. ),
  122. 'sticky' => FALSE,
  123. 'caption' => "",
  124. 'colgroups' => array(),
  125. 'empty' => '',
  126. );
  127. $content = theme_table($table);
  128. // once we have our table array structure defined, we call Drupal's theme_table()
  129. // function to generate the table.
  130. $element[$delta] = array(
  131. '#type' => 'markup',
  132. '#markup' => $content,
  133. );
  134. }
  135. /**
  136. * @see TripalField::load()
  137. */
  138. public function load($field, $entity, $details) {
  139. $record = $details['record'];
  140. $field_name = $field['field_name'];
  141. // TODO: If the tripal_get_feature_relationships() slows this down then
  142. // we may need to write a custom function to get the data.
  143. $rels = tripal_get_feature_relationships($record);
  144. // Set the value to be a array of "table" rows.
  145. $transcripts = array();
  146. if (key_exists('part of', $rels['object']) &&
  147. key_exists('mRNA', $rels['object']['part of'])) {
  148. $transcripts = $rels['object']['part of']['mRNA'];
  149. }
  150. $headers = array('Feature Name' ,'Unique Name', 'Location');
  151. $rows = array();
  152. $i = 0;
  153. foreach ($transcripts as $transcript) {
  154. // link the feature to it's node
  155. $feature_name = $transcript->record->subject_id->name;
  156. $entity_id = $transcript->record->subject_id->entity_id;
  157. $locations = $transcript->child_featurelocs;
  158. $loc = "";
  159. foreach ($locations AS $location) {
  160. $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
  161. }
  162. $entity->{$field_name}['und'][$i]['value'] = array(
  163. 'name' => $feature_name,
  164. 'type' => $transcript->record->subject_id->type_id->name,
  165. 'unique name' => $transcript->record->subject_id->uniquename,
  166. 'location' => $loc,
  167. 'entity_id' => $entity_id,
  168. 'entity_type' => 'TripalEntity',
  169. );
  170. $i++;
  171. }
  172. }
  173. /**
  174. * @see TripalField::widget_form()
  175. */
  176. public function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  177. }
  178. }