tripal_jbrowse_mgmt_add.form.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
  3. $settings = tripal_jbrowse_mgmt_get_settings();
  4. // Ensure the module is configured.
  5. if (empty(array_filter($settings))) {
  6. $form['incomplete'] = [
  7. '#type' => 'item',
  8. '#prefix' => '<div class="messages error">',
  9. '#markup' => t(
  10. 'You have not configured the module yet. Please visit the
  11. <a href="@url">settings page</a> and submit the form before continuing.',
  12. ['@url' => url('admin/tripal/extension/tripal_jbrowse/management/configure')]
  13. ),
  14. '#suffix' => '</div>',
  15. ];
  16. return $form;
  17. }
  18. $organisms = tripal_jbrowse_mgmt_get_organisms_list();
  19. $mapped_organisms = [];
  20. foreach ($organisms as $organism) {
  21. $mapped_organisms[$organism->organism_id] = tripal_jbrowse_mgmt_construct_organism_name(
  22. $organism
  23. );
  24. }
  25. $form_description = 'Create a new JBrowse instance for a given organism. Submitting this form
  26. creates all the necessary files for a new JBrowse instance.';
  27. $form['description_of_form'] = [
  28. '#type' => 'item',
  29. '#markup' => t($form_description),
  30. '#weight' => -10,
  31. ];
  32. $form['organism'] = [
  33. '#title' => t('Organism'),
  34. '#description' => t('Select the organism'),
  35. '#type' => 'select',
  36. '#options' => $mapped_organisms,
  37. '#required' => TRUE,
  38. '#weight' => -8,
  39. ];
  40. $form['description'] = [
  41. '#title' => t('Description'),
  42. '#description' => t('Optional description for the instance.'),
  43. '#type' => 'textarea',
  44. '#weight' => -7,
  45. ];
  46. $form['data'] = [
  47. '#type' => 'fieldset',
  48. '#title' => t('Reference Sequence File'),
  49. '#collabsible' => FALSE,
  50. 'weight' => 0
  51. ];
  52. $form['data']['data_desc'] = [
  53. '#type' => 'item',
  54. '#markup' => t(
  55. 'You may either upload a file below or provide
  56. the path to the reference sequence fasta file.'
  57. ),
  58. ];
  59. $form['data']['ref_seq_file'] = [
  60. '#type' => 'file',
  61. '#title' => t('Reference Sequence FASTA File'),
  62. ];
  63. $form['data']['ref_seq_path'] = [
  64. '#type' => 'textfield',
  65. '#title' => t('OR Path to File on Server'),
  66. '#description' => t(
  67. 'This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'
  68. ),
  69. ];
  70. $form['page'] = [
  71. '#type' => 'fieldset',
  72. '#tree' => TRUE,
  73. '#title' => 'Instance Page Settings',
  74. '#description' => 'The following settings pertain to link directing users to this instance (either embedded or the original).',
  75. ];
  76. $form['page']['start-loc'] = [
  77. '#type' => 'textfield',
  78. '#title' => 'Start Location',
  79. '#description' => "<p>The initial genomic position which will be visible in
  80. the viewing field. Possible input strings are:</p>\r\n
  81. <strong>\"Chromosome\": \"start point\"..\"end point\"</strong>\r\n<p>A
  82. chromosome name/ID followed by “:”, starting position, “..” and end
  83. position of the genome to be viewed in the browser is used as an input.
  84. Chromosome ID can be either a string or a mix of string and numbers.
  85. “CHR” to indicate chromosome may or may not be used. Strings are not
  86. case-sensitive. If the chromosome ID is found in the database reference
  87. sequence (RefSeq), the chromosome will be shown from the starting
  88. position to the end position given in URL.</p>\r\n
  89. <pre> ctgA:100..200</pre>\r\n
  90. <p>Chromosome ctgA will be displayed from position 100 to 200.</p>\r\n
  91. OR <strong>start point\"..\"end point</strong>\r\n<p>A string of
  92. numerical value, “..” and another numerical value is given with the loc
  93. option. JBrowse navigates through the currently selected chromosome from
  94. the first numerical value, start point, to the second numerical value,
  95. end point.</p>\r\n<pre> 200..600</pre>\r\n<p>Displays position 200
  96. to 600 of the current chromosome.</p>\r\n
  97. OR <strong>center base</strong>\r\n<p>If only one numerical value is given
  98. as an input, JBrowse treats the input as the center position. Then an
  99. arbitrary region of the currently selected gene is displayed in the
  100. viewing field with the given input position as basepair position on
  101. which to center the view.</p>\r\n
  102. OR <strong>feature name/ID</strong>\r\n<p>If a string or a mix of string
  103. and numbers are entered as an input, JBrowse treats the input as a
  104. feature name/ID of a gene. If the ID exists in the database RefSeq,
  105. JBrowser displays an arbitrary region of the feature from the the
  106. position 0, starting position of the gene, to a certain end point.</p>\r\n
  107. <pre> ctgA</pre>\r\n<p>Displays an arbitrary region from the ctgA
  108. reference.</p>",
  109. ];
  110. $form['page']['start-tracks'] = [
  111. '#type' => 'textarea',
  112. '#rows' => 2,
  113. '#title' => 'Tracks to Display',
  114. '#description' => "<p>A comma-delimited strings containing track names,
  115. each of which should correspond to the \"label\" element of the track
  116. information dictionaries that are currently viewed in the viewing field.</p>\r\n
  117. <pre> DNA,knownGene,ccdsGene,snp131,pgWatson,simpleRepeat</pre>",
  118. ];
  119. $form['submit'] = [
  120. '#type' => 'submit',
  121. '#value' => 'Create New Instance',
  122. '#weight' => 10,
  123. ];
  124. return $form;
  125. }
  126. /**
  127. * Validate the form.
  128. *
  129. * @param $form
  130. * @param $form_state
  131. */
  132. function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
  133. $values = $form_state['values'];
  134. $organism = isset($values['organism']) ? $values['organism'] : NULL;
  135. // Check if this is an add or edit form.
  136. $edit_form = FALSE;
  137. if (isset($form_state['build_info']['args'][0]) AND is_numeric($form_state['build_info']['args'][0])) {
  138. $instance_id = $form_state['build_info']['args'][0];
  139. $edit_form = TRUE;
  140. }
  141. $file = $_FILES['files']['tmp_name']['ref_seq_file'];
  142. $local_file = isset($values['ref_seq_path']) ? $values['ref_seq_path'] : NULL;
  143. if (empty($file) && empty($local_file)) {
  144. form_set_error(
  145. 'ref_seq_file',
  146. 'Please provide a local file path or upload a new file.'
  147. );
  148. }
  149. elseif (empty($file) && !empty($local_file)) {
  150. if (!file_exists($local_file)) {
  151. form_set_error('ref_seq_path', 'The file path provided does not exist.');
  152. }
  153. }
  154. else {
  155. $uploaded = tripal_jbrowse_mgmt_upload_file('ref_seq_file');
  156. if (!$uploaded) {
  157. form_set_error('ref_seq_file', 'Unable to upload file');
  158. }
  159. else {
  160. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded);
  161. $form_state['values']['uploaded_file'] = $uploaded;
  162. }
  163. }
  164. $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
  165. if (!empty($instances)) {
  166. form_set_error(
  167. 'organism',
  168. 'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
  169. );
  170. }
  171. $organism = db_select('chado.organism', 'CO')
  172. ->fields('CO')
  173. ->condition('organism_id', $organism)
  174. ->execute()
  175. ->fetchObject();
  176. if (empty($organism)) {
  177. form_set_error('organism', 'Invalid organism selected ' . $organism);
  178. }
  179. }
  180. /**
  181. * @param $form
  182. * @param $form_state
  183. *
  184. * @throws \Exception
  185. */
  186. function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
  187. global $user;
  188. $values = $form_state['values'];
  189. $organism_id = $values['organism'];
  190. $description = isset($values['description']) ? $values['description'] : '';
  191. if (empty($values['uploaded_file'])) {
  192. $file = $values['ref_seq_path'];
  193. }
  194. else {
  195. $file = $values['uploaded_file'];
  196. }
  197. $organism = db_select('chado.organism', 'CO')
  198. ->fields('CO')
  199. ->condition('organism_id', $organism_id)
  200. ->execute()
  201. ->fetchObject();
  202. $instance_id = tripal_jbrowse_mgmt_create_instance(
  203. [
  204. 'organism_id' => $organism_id,
  205. 'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
  206. 'description' => $description,
  207. 'created_at' => time(),
  208. 'file' => $file,
  209. ]
  210. );
  211. if ($instance_id) {
  212. drupal_set_message('Instance created successfully!');
  213. $name = 'Create JBrowse instance for ';
  214. $name .= tripal_jbrowse_mgmt_construct_organism_name($organism);
  215. tripal_add_job(
  216. $name,
  217. 'tripal_jbrowse_mgmt',
  218. 'tripal_jbrowse_mgmt_create_instance_files',
  219. [$instance_id],
  220. $user->uid
  221. );
  222. $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
  223. }
  224. else {
  225. drupal_set_message('Failed to create instance!', 'error');
  226. return;
  227. }
  228. // Now save the instance properties.
  229. tripal_jbrowse_mgmt_save_instance_properties(
  230. $instance_id,
  231. $form_state['values']['page']
  232. );
  233. }