chado_gene__transcripts.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 fieldSettingsForm()
  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 static function formatterSettingsSummary($field, $instance,
  116. $view_mode) {
  117. }
  118. /**
  119. * @see TripalField::formatter_settings_form()
  120. */
  121. public static function formatterSettingsForm($field, $instance,
  122. $view_mode, $form, &$form_state) {
  123. }
  124. /**
  125. * @see TripalField::formatterView()
  126. */
  127. public static function formatterView(&$element, $entity_type, $entity,
  128. $field, $instance, $langcode, $items, $display) {
  129. // Get the settings
  130. $settings = $display['settings'];
  131. $headers = array('Transcript Name', 'Identifier', 'Type', 'Location');
  132. $rows = array();
  133. foreach ($items as $delta => $item) {
  134. if (!$item['value']) {
  135. continue;
  136. }
  137. $transcript = $item['value'];
  138. // Get the field values
  139. $feature_name = $transcript['name'];
  140. $feature_uname = $transcript['identifier'];
  141. $loc = $transcript['location'];
  142. $type = $transcript['type'];
  143. // Add a link i there is an entity.
  144. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  145. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  146. $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  147. }
  148. $rows[] = array($feature_name, $feature_uname, $type, $loc);
  149. }
  150. $table = array(
  151. 'header' => $headers,
  152. 'rows' => $rows,
  153. 'attributes' => array(
  154. 'id' => 'tripal_feature-table-transcripts-object',
  155. 'class' => 'tripal-data-table'
  156. ),
  157. 'sticky' => FALSE,
  158. 'caption' => "",
  159. 'colgroups' => array(),
  160. 'empty' => 'This feature has no transcripts',
  161. );
  162. $content = theme_table($table);
  163. // once we have our table array structure defined, we call Drupal's theme_table()
  164. // function to generate the table.
  165. if (count($items) > 0) {
  166. $element[0] = array(
  167. '#type' => 'markup',
  168. '#markup' => $content,
  169. );
  170. }
  171. }
  172. /**
  173. * @see TripalField::load()
  174. */
  175. static function load($field, $entity, $details = array()) {
  176. $record = $details['record'];
  177. $field_name = $field['field_name'];
  178. // Set some defaults for the empty record.
  179. $entity->{$field_name}['und'][0] = array(
  180. 'value' => array(
  181. 'type' => '',
  182. 'name' => '',
  183. 'identifier' => '',
  184. 'location' => '',
  185. ),
  186. );
  187. // TODO: If the tripal_get_feature_relationships() slows this down then
  188. // we may need to write a custom function to get the data.
  189. $rels = tripal_get_feature_relationships($record);
  190. // TODO: what if other transcripts names from SO are used. In that
  191. // case we should support those too (using cvtermpath table to find them).
  192. // mRNA should not be hard-coded below.
  193. // Set the value to be a array of "table" rows.
  194. $transcripts = array();
  195. if (key_exists('part of', $rels['object']) &&
  196. key_exists('mRNA', $rels['object']['part of'])) {
  197. $transcripts = $rels['object']['part of']['mRNA'];
  198. }
  199. $headers = array('Name' ,'Identifier', 'Location');
  200. $rows = array();
  201. $i = 0;
  202. foreach ($transcripts as $transcript) {
  203. // link the feature to it's node
  204. $feature_name = $transcript->record->subject_id->name;
  205. $locations = $transcript->child_featurelocs;
  206. $loc = "";
  207. foreach ($locations AS $location) {
  208. $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
  209. }
  210. $type = $transcript->record->subject_id->type_id;
  211. $entity->{$field_name}['und'][$i]['value'] = array(
  212. 'type' => $type->name,
  213. 'name' => $feature_name,
  214. 'identifier' => $transcript->record->subject_id->uniquename,
  215. 'location' => $loc,
  216. );
  217. // Add in the semantic web information that describes each key in the
  218. // value array.
  219. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  220. 'type' => $type->dbxref_id->db_id->name . ":" . $type->dbxref_id->accession,
  221. 'name' => tripal_get_chado_semweb_term('cvterm', 'name'),
  222. 'identifier' => tripal_get_chado_semweb_term('feature', 'uniquename'),
  223. 'location' => '',
  224. );
  225. if (property_exists($transcript->record->subject_id, 'entity_id')) {
  226. $entity_id = $transcript->record->subject_id->entity_id;
  227. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $entity_id;
  228. }
  229. $i++;
  230. }
  231. }
  232. /**
  233. * @see TripalField::widgetForm()
  234. */
  235. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  236. $langcode, $items, $delta, $element) {
  237. }
  238. }