tripal_jbrowse_mgmt_add.form.inc 11 KB

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