tripal_jbrowse_mgmt_add.form.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. '#maxlength' => 255,
  67. '#description' => t(
  68. 'This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'
  69. ),
  70. ];
  71. $form['page'] = [
  72. '#type' => 'fieldset',
  73. '#tree' => TRUE,
  74. '#title' => 'Instance Page Settings',
  75. '#description' => 'The following settings pertain to link directing users to this instance (either embedded or the original).',
  76. ];
  77. $form['page']['start-loc'] = [
  78. '#type' => 'textfield',
  79. '#title' => 'Start Location',
  80. '#description' => "<p>The initial genomic position which will be visible in
  81. the viewing field. Possible input strings are:</p>\r\n
  82. <strong>\"Chromosome\": \"start point\"..\"end point\"</strong>\r\n<p>A
  83. chromosome name/ID followed by “:”, starting position, “..” and end
  84. position of the genome to be viewed in the browser is used as an input.
  85. Chromosome ID can be either a string or a mix of string and numbers.
  86. “CHR” to indicate chromosome may or may not be used. Strings are not
  87. case-sensitive. If the chromosome ID is found in the database reference
  88. sequence (RefSeq), the chromosome will be shown from the starting
  89. position to the end position given in URL.</p>\r\n
  90. <pre> ctgA:100..200</pre>\r\n
  91. <p>Chromosome ctgA will be displayed from position 100 to 200.</p>\r\n
  92. OR <strong>start point\"..\"end point</strong>\r\n<p>A string of
  93. numerical value, “..” and another numerical value is given with the loc
  94. option. JBrowse navigates through the currently selected chromosome from
  95. the first numerical value, start point, to the second numerical value,
  96. end point.</p>\r\n<pre> 200..600</pre>\r\n<p>Displays position 200
  97. to 600 of the current chromosome.</p>\r\n
  98. OR <strong>center base</strong>\r\n<p>If only one numerical value is given
  99. as an input, JBrowse treats the input as the center position. Then an
  100. arbitrary region of the currently selected gene is displayed in the
  101. viewing field with the given input position as basepair position on
  102. which to center the view.</p>\r\n
  103. OR <strong>feature name/ID</strong>\r\n<p>If a string or a mix of string
  104. and numbers are entered as an input, JBrowse treats the input as a
  105. feature name/ID of a gene. If the ID exists in the database RefSeq,
  106. JBrowser displays an arbitrary region of the feature from the the
  107. position 0, starting position of the gene, to a certain end point.</p>\r\n
  108. <pre> ctgA</pre>\r\n<p>Displays an arbitrary region from the ctgA
  109. reference.</p>",
  110. ];
  111. $form['page']['start-tracks'] = [
  112. '#type' => 'textarea',
  113. '#rows' => 2,
  114. '#title' => 'Tracks to Display',
  115. '#description' => "<p>A comma-delimited strings containing track names,
  116. each of which should correspond to the \"label\" element of the track
  117. information dictionaries that are currently viewed in the viewing field.</p>\r\n
  118. <pre> DNA,knownGene,ccdsGene,snp131,pgWatson,simpleRepeat</pre>",
  119. ];
  120. $form['submit'] = [
  121. '#type' => 'submit',
  122. '#value' => 'Create New Instance',
  123. '#weight' => 10,
  124. ];
  125. return $form;
  126. }
  127. /**
  128. * Validate the form.
  129. *
  130. * @param $form
  131. * @param $form_state
  132. */
  133. function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
  134. $values = $form_state['values'];
  135. $organism = isset($values['organism']) ? $values['organism'] : NULL;
  136. // Check if this is an add or edit form.
  137. $edit_form = FALSE;
  138. if (isset($form_state['build_info']['args'][0]) AND is_numeric($form_state['build_info']['args'][0])) {
  139. $instance_id = $form_state['build_info']['args'][0];
  140. $edit_form = TRUE;
  141. }
  142. $file = $_FILES['files']['tmp_name']['ref_seq_file'];
  143. $local_file = isset($values['ref_seq_path']) ? $values['ref_seq_path'] : NULL;
  144. if (empty($file) && empty($local_file)) {
  145. form_set_error(
  146. 'ref_seq_file',
  147. 'Please provide a local file path or upload a new file.'
  148. );
  149. }
  150. elseif (empty($file) && !empty($local_file)) {
  151. if (!file_exists($local_file)) {
  152. form_set_error('ref_seq_path', 'The file path provided does not exist.');
  153. }
  154. }
  155. else {
  156. $uploaded = tripal_jbrowse_mgmt_upload_file('ref_seq_file');
  157. if (!$uploaded) {
  158. form_set_error('ref_seq_file', 'Unable to upload file');
  159. }
  160. else {
  161. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded);
  162. $form_state['values']['uploaded_file'] = $uploaded;
  163. }
  164. }
  165. $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
  166. if (!empty($instances)) {
  167. form_set_error(
  168. 'organism',
  169. 'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
  170. );
  171. }
  172. $organism = db_select('chado.organism', 'CO')
  173. ->fields('CO')
  174. ->condition('organism_id', $organism)
  175. ->execute()
  176. ->fetchObject();
  177. if (empty($organism)) {
  178. form_set_error('organism', 'Invalid organism selected ' . $organism);
  179. }
  180. }
  181. /**
  182. * @param $form
  183. * @param $form_state
  184. *
  185. * @throws \Exception
  186. */
  187. function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
  188. global $user;
  189. $values = $form_state['values'];
  190. $organism_id = $values['organism'];
  191. $description = isset($values['description']) ? $values['description'] : '';
  192. if (empty($values['uploaded_file'])) {
  193. $file = $values['ref_seq_path'];
  194. }
  195. else {
  196. $file = $values['uploaded_file'];
  197. }
  198. $organism = db_select('chado.organism', 'CO')
  199. ->fields('CO')
  200. ->condition('organism_id', $organism_id)
  201. ->execute()
  202. ->fetchObject();
  203. $instance_id = tripal_jbrowse_mgmt_create_instance(
  204. [
  205. 'organism_id' => $organism_id,
  206. 'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
  207. 'description' => $description,
  208. 'created_at' => time(),
  209. 'file' => $file,
  210. ]
  211. );
  212. if ($instance_id) {
  213. drupal_set_message('Instance created successfully!');
  214. $name = 'Create JBrowse instance for ';
  215. $name .= tripal_jbrowse_mgmt_construct_organism_name($organism);
  216. tripal_add_job(
  217. $name,
  218. 'tripal_jbrowse_mgmt',
  219. 'tripal_jbrowse_mgmt_create_instance_files',
  220. [$instance_id],
  221. $user->uid
  222. );
  223. $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
  224. }
  225. else {
  226. drupal_set_message('Failed to create instance!', 'error');
  227. return;
  228. }
  229. // Now save the instance properties.
  230. tripal_jbrowse_mgmt_save_instance_properties(
  231. $instance_id,
  232. $form_state['values']['page']
  233. );
  234. }