tripal_jbrowse_mgmt_add.form.inc 12 KB

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