tripal_jbrowse_mgmt_add.form.inc 11 KB

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