local__gbrowse_image.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. class local__gbrowse_image extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'GBrowse View';
  12. // The default description for this field.
  13. public static $description = 'A GBrowse image of a feature alignment to another sequence.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'local',
  24. // The name of the term.
  25. 'term_name' => 'Gbrowse Image',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => 'gbrowse_image',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. // The URL for the GBrowse image
  33. 'gbrowse_url' => '',
  34. );
  35. // The default widget for this field.
  36. public static $default_widget = 'local__gbrowse_image_widget';
  37. // The default formatter for this field.
  38. public static $default_formatter = 'local__gbrowse_image_formatter';
  39. /**
  40. * @see TripalField::elementInfo()
  41. */
  42. public function elementInfo() {
  43. $field_term = $this->getFieldTermID();
  44. return array(
  45. $field_term => array(
  46. 'sortable' => FALSE,
  47. 'searchable' => FALSE,
  48. 'type' => 'xs:string',
  49. 'readonly' => TRUE,
  50. ),
  51. );
  52. }
  53. /**
  54. * @see TripalField::load()
  55. */
  56. public function load($entity) {
  57. $field_name = $this->field['field_name'];
  58. $feature = $entity->chado_record;
  59. $record = $entity->chado_record;
  60. $settings = $this->instance['settings'];
  61. // Set some defauls for the empty record
  62. $entity->{$field_name}['und'][0] = array(
  63. 'value' => '',
  64. );
  65. // If we don't have a record then just return
  66. if (!$record) {
  67. return;
  68. }
  69. // Replace any tokens in the string.
  70. $gbrowse_url = NULL;
  71. $g_urls = $settings['gbrowse_url'];
  72. $g_urls = explode("\n", $g_urls);
  73. foreach ($g_urls as $g_url) {
  74. $matches = [];
  75. if (preg_match('/^(.*)\s+(.*)\s+(http.*)$/', $g_url, $matches)) {
  76. if ($record->organism_id->genus == $matches[1] and
  77. $record->organism_id->species == $matches[2]) {
  78. $gbrowse_url = $matches[3];
  79. }
  80. }
  81. elseif (preg_match('/^http.*$/', $g_url)) {
  82. $gbrowse_url = $g_url;
  83. }
  84. }
  85. if (!$gbrowse_url) {
  86. return;
  87. }
  88. $bundle = tripal_load_bundle_entity(['name' => $this->instance['bundle']]);
  89. $gbrowse_url = tripal_replace_entity_tokens($gbrowse_url, $entity, $bundle);
  90. // Use the term associated to the db.url table of Chado.
  91. $url_term = chado_get_semweb_term('db', 'url');
  92. // Add the URL to the values
  93. $entity->{$field_name}['und'][0]['value'] = [
  94. $url_term => $gbrowse_url
  95. ];
  96. }
  97. /**
  98. * @see TripalField::globalSettingsForm()
  99. */
  100. public function instanceSettingsForm() {
  101. $element = parent::instanceSettingsForm();
  102. $settings = $this->instance['settings'];
  103. $element['gbrowse_url'] = array(
  104. '#type' => 'textarea',
  105. '#title' => 'GBrowse URL',
  106. '#description' => t('Please enter the URL for generating feature ' .
  107. 'images on the remote GBrowse site. You may use tokens to substitute ' .
  108. 'field values from this content type. Open the "Available Tokens" fieldset below. ' .
  109. 'For example, to show a view of a gene from the rice genome GBrowse the ' .
  110. 'following URL with tokens can be used: ' .
  111. 'http://rice.plantbiology.msu.edu/cgi-bin/gbrowse_img/rice/?name=[schema__name];type=Landmarks%3Aregion+Genes+Rice_Annotation' .
  112. '. If you need to specify different GBrowse URLs for different ' .
  113. 'species, put the genus, species and URL all on one line with each separated ' .
  114. 'by a space. For example: ' .
  115. '"Oryza sativa http://rice.plantbiology.msu.edu/cgi-bin/gbrowse_img/rice/?name=[schema__name];type=Landmarks%3Aregion+Genes+Rice_Annotation"'),
  116. '#default_value' => $settings['gbrowse_url'],
  117. );
  118. $element['tokens'] = array(
  119. '#type' => 'fieldset',
  120. '#collapsed' => TRUE,
  121. '#collapsible' => TRUE,
  122. '#title' => 'Available Tokens'
  123. );
  124. $headers = array('Token', 'Description');
  125. $rows = array();
  126. // Get the bundle tokens
  127. $bundle = tripal_load_bundle_entity(['name' => $this->instance['bundle']]);
  128. $tokens = tripal_get_entity_tokens($bundle);
  129. foreach ($tokens as $token) {
  130. $rows[] = array(
  131. $token['token'],
  132. $token['description'],
  133. );
  134. }
  135. $table_vars = array(
  136. 'header' => $headers,
  137. 'rows' => $rows,
  138. 'attributes' => array(),
  139. 'sticky' => FALSE,
  140. 'caption' => '',
  141. 'colgroups' => array(),
  142. 'empty' => 'There are no tokens',
  143. );
  144. $element['tokens']['list'] = array(
  145. '#type' => 'item',
  146. '#markup' => theme_table($table_vars),
  147. );
  148. return $element;
  149. }
  150. }