chado_gene__transcripts.inc 7.9 KB

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