tripal_jbrowse_mgmt_add.form.inc 11 KB

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