tripal_jbrowse_mgmt_add.form.inc 11 KB

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