tripal_jbrowse_mgmt_tracks.form.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * @file tripal_jbrowse_mgmt_tracks.form.inc
  4. */
  5. /**
  6. * Add a track to an instance form.
  7. *
  8. * @param array $form
  9. * The drupal form.
  10. * @param array $form_state
  11. * The form state array.
  12. * @param int $instance_id
  13. * The parent instance ID to add the new track to.
  14. *
  15. * @return array
  16. * The modified form array.
  17. */
  18. function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
  19. if (empty(tripal_jbrowse_mgmt_get_instance($instance_id))) {
  20. drupal_not_found();
  21. return [];
  22. }
  23. $form['label'] = [
  24. '#type' => 'textfield',
  25. '#title' => t('Track Label'),
  26. '#description' => t('This will appear on the sidebar.'),
  27. '#required' => TRUE,
  28. ];
  29. $form['instance_id'] = [
  30. '#type' => 'hidden',
  31. '#value' => $instance_id,
  32. ];
  33. $form['data'] = [
  34. '#type' => 'fieldset',
  35. '#title' => t('Track Files'),
  36. ];
  37. $form['data']['track_type'] = [
  38. '#type' => 'select',
  39. '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
  40. '#required' => TRUE,
  41. '#title' => t('Track Type'),
  42. '#options' => drupal_map_assoc(tripal_jbrowse_mgmt_get_track_types()),
  43. ];
  44. $form['data']['file_type'] = [
  45. '#type' => 'select',
  46. '#title' => t('File Type'),
  47. '#options' => drupal_map_assoc(['gff', 'bed', 'gbk', 'bam', 'cram', 'vcf', 'bw']),
  48. '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
  49. '#required' => TRUE,
  50. ];
  51. $form['data']['file'] = [
  52. '#type' => 'file',
  53. '#title' => t('File'),
  54. ];
  55. $form['data']['file2'] = [
  56. '#type' => 'file',
  57. '#title' => t('Index File'),
  58. '#states' => [
  59. 'visible' => [
  60. [':input[name="file_type"]' => ['value' => 'vcf'],],
  61. [':input[name="file_type"]' => ['value' => 'bam'],],
  62. [':input[name="file_type"]' => ['value' => 'cram'],],
  63. ],
  64. ],
  65. ];
  66. $form['data']['file_path'] = [
  67. '#type' => 'textfield',
  68. '#title' => t('- OR Path to File on Server -'),
  69. '#description' => t('This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'),
  70. '#maxlength' => 255,
  71. '#states' => [
  72. 'invisible' => [
  73. [':input[name="file_type"]' => ['value' => 'vcf'],],
  74. [':input[name="file_type"]' => ['value' => 'bam'],],
  75. [':input[name="file_type"]' => ['value' => 'cram'],],
  76. ],
  77. ],
  78. ];
  79. $form['data']['dir_path'] = [
  80. '#type' => 'textfield',
  81. '#title' => t('- OR Path to Directory on Server -'),
  82. '#description' => t('This path will be ignored if a file is provided above. ' . 'The directory must contain both the .tbi and .gz files.'),
  83. '#maxlength' => 255,
  84. '#states' => [
  85. 'visible' => [
  86. [':input[name="file_type"]' => ['value' => 'vcf'],],
  87. [':input[name="file_type"]' => ['value' => 'bam'],],
  88. [':input[name="file_type"]' => ['value' => 'cram'],],
  89. ],
  90. ],
  91. ];
  92. $form['data']['symbolic_link'] = [
  93. '#type' => 'checkbox',
  94. '#title' => t('Symbolic Link'),
  95. '#description' => t('Create a symbolic link rather than make a copy of the file. This only applies when a path on the server is supplied.<br>Please have Symbolic Link selected if the same file is used for new track.'),
  96. ];
  97. $form['submit'] = [
  98. '#type' => 'submit',
  99. '#value' => 'Add New Track',
  100. ];
  101. return $form;
  102. }
  103. /**
  104. * Validate the add track form.
  105. *
  106. * This function also takes care of the uploading of files.
  107. * TODO: If the file is invalid, delete it!
  108. *
  109. * @param array $form
  110. * The Drupal form.
  111. * @param array $form_state
  112. * The form state.
  113. */
  114. function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
  115. $values = $form_state['values'];
  116. $file = $_FILES['files']['tmp_name']['file'];
  117. $settings = tripal_jbrowse_mgmt_get_settings();
  118. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  119. $data = $settings['data_dir'];
  120. $file_type = $values['file_type'];
  121. $symbolic_link = $values['symbolic_link'];
  122. $path = NULL;
  123. $base_path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  124. if(isset($instance->analysis_id)){
  125. $base_path = $base_path . '_' . $instance->analysis_id;
  126. }
  127. $base_path = $base_path . '/data';
  128. if ($file_type === 'vcf') {
  129. $path = $base_path . '/vcf';
  130. }
  131. if (($file_type === 'bam') OR ($file_type === 'cram')){
  132. $path = $base_path . '/bam';
  133. }
  134. elseif ($file_type === 'bw') {
  135. $path = $base_path . '/wig';
  136. }
  137. else {
  138. $path = $base_path;
  139. }
  140. switch ($file_type) {
  141. case 'vcf':
  142. $index = $_FILES['files']['tmp_name']['file2'];
  143. $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
  144. break;
  145. case 'bam':
  146. $index = $_FILES['files']['tmp_name']['file2'];
  147. $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
  148. break;
  149. case 'cram':
  150. $index = $_FILES['files']['tmp_name']['file2'];
  151. $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
  152. break;
  153. default:
  154. $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
  155. if (empty($file) && empty($local_file)) {
  156. form_set_error('file',
  157. 'Please provide a local file path or upload a new file.');
  158. }
  159. elseif (empty($file) && !empty($local_file)) {
  160. if (!file_exists($local_file)) {
  161. form_set_error('file_path', 'The file path provided does not exist.');
  162. }
  163. else {
  164. try {
  165. if (!tripal_jbrowse_mgmt_copy_file($local_file, $path, $symbolic_link)) {
  166. form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path. '. If this track is expected to create by existed file, please have Symbolic Link selected.');
  167. }
  168. } catch (Exception $exception) {
  169. form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
  170. }
  171. }
  172. }
  173. else {
  174. $uploaded = tripal_jbrowse_mgmt_upload_file('file');
  175. if (!$uploaded) {
  176. form_set_error('file', 'Unable to upload file');
  177. }
  178. else {
  179. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
  180. if (!isset($uploaded)) {
  181. form_set_error('file', 'Failed to move file to ' . $path);
  182. }
  183. else {
  184. $form_state['values']['uploaded_file'] = $uploaded;
  185. }
  186. }
  187. }
  188. break;
  189. }
  190. }
  191. /**
  192. * Handle form submission for adding a track.
  193. *
  194. * @param array $form
  195. * @param array $form_state
  196. *
  197. * @throws \Exception
  198. */
  199. function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
  200. global $user;
  201. $values = $form_state['values'];
  202. $file = isset($values['file_path']) ? $values['file_path'] : NULL;
  203. if (!empty($values['dir_path'])) {
  204. $file = $values['dir_path'];
  205. }
  206. if (!empty($values['uploaded_file_index'])) {
  207. $file = $values['uploaded_file_index'];
  208. }
  209. if (!empty($values['uploaded_file_data'])) {
  210. $file = $values['uploaded_file_data'];
  211. }
  212. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  213. $track_id = tripal_jbrowse_mgmt_create_track($instance, [
  214. 'label' => $values['label'],
  215. 'track_type' => $values['track_type'],
  216. 'file_type' => $values['file_type'],
  217. 'file' => $file,
  218. 'created_at' => time(),
  219. ]);
  220. tripal_add_job('Add JBrowse track to ' . $instance->title, 'tripal_jbrowse_mgmt',
  221. 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id], $user->uid);
  222. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $instance->id);
  223. }
  224. /**
  225. * Delete a track form.
  226. *
  227. * @param array $form
  228. * @param array $form_state
  229. * @param int $track_id
  230. *
  231. * @return array
  232. */
  233. function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
  234. $track = tripal_jbrowse_mgmt_get_track($track_id);
  235. if (!$track->id) {
  236. $form['error'] = [
  237. '#type' => 'item',
  238. '#markup' => '<p style="color: red">Unable to find track.</p>',
  239. ];
  240. return $form;
  241. }
  242. $form['description'] = [
  243. '#type' => 'item',
  244. '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
  245. ];
  246. $form['track_id'] = [
  247. '#type' => 'hidden',
  248. '#value' => $track_id,
  249. ];
  250. $form['submit'] = [
  251. '#type' => 'submit',
  252. '#value' => 'Delete Track',
  253. ];
  254. $form['cancel'] = [
  255. '#type' => 'markup',
  256. '#markup' => l('Cancel',
  257. 'admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id),
  258. ];
  259. return $form;
  260. }
  261. /**
  262. * @param $form
  263. * @param $form_state
  264. *
  265. * @throws \Exception
  266. */
  267. function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
  268. global $user;
  269. $values = $form_state['values'];
  270. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  271. tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
  272. 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
  273. $user->uid);
  274. tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
  275. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id);
  276. }
  277. /**
  278. * Track json editor advance form.
  279. * allow user to make all configurations of a track
  280. *
  281. * @param array $form
  282. * @param array $form_state
  283. * @param int $track_id
  284. *
  285. * @return array
  286. * @throws \Exception
  287. */
  288. function tripal_jbrowse_mgmt_json_editor_advance_form($form, &$form_state, $track_id) {
  289. $track = tripal_jbrowse_mgmt_get_track($track_id);
  290. if (empty($track)) {
  291. drupal_not_found();
  292. return $form;
  293. }
  294. drupal_set_title('Edit Track Json: ' . $track->label);
  295. $breadcrumb = array();
  296. $breadcrumb[] = l('Home', '');
  297. $breadcrumb[] = l('Administration', 'admin');
  298. $breadcrumb[] = l('Tripal', 'admin/tripal');
  299. $breadcrumb[] = l('Extensions', 'admin/tripal/extension');
  300. $breadcrumb[] = l('Tripal JBrowse', 'admin/tripal/extension/tripal_jbrowse/management');
  301. $breadcrumb[] = l('Instance', 'admin/tripal/extension/tripal_jbrowse/management/instances/'.$track->instance_id);
  302. drupal_set_breadcrumb($breadcrumb);
  303. if (!$track->id) {
  304. $form['error'] = [
  305. '#type' => 'item',
  306. '#markup' => '<p style="color: red">Unable to find track.</p>',
  307. ];
  308. return $form;
  309. }
  310. $instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  311. $json = tripal_jbrowse_mgmt_get_json($instance);
  312. $form_state['track_json'] = $json;
  313. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  314. $track_json = NULL;
  315. $track_index = NULL;
  316. foreach ($json['tracks'] as $index => $jtrack) {
  317. if ($jtrack['label'] === $key) {
  318. $track_json = $jtrack;
  319. $track_index = $index;
  320. break;
  321. }
  322. }
  323. if (!$track_json) {
  324. $form['error'] = [
  325. '#type' => 'item',
  326. '#markup' => '<p style="color: red">Unable to find track in json!</p>',
  327. ];
  328. return $form;
  329. }
  330. $form['track_index'] = [
  331. '#type' => 'hidden',
  332. '#value' => $track_index,
  333. ];
  334. $form['track_id'] = [
  335. '#type' => 'hidden',
  336. '#value' => $track->id,
  337. ];
  338. $instr_detail = '<p><strong>Only use this functionality if the configuration option you want is not included in the "Track Manage" form.</strong>. For more information on the JBrowse track configuration see the '.l('JBrowse Documentation', 'http://jbrowse.org/docs/canvas_features.html').'.</p>
  339. <div class="messages warning">Please be extra cautious while editing track configuration, since it will make changes in file trackList.json directly. Also, do not change the "label" since it will disconnect this track from the JBrowse Management form.</div>';
  340. $form['Instruction']=[
  341. '#type' => 'markup',
  342. '#markup' => $instr_detail,
  343. ];
  344. $form['track_all_config'] = [
  345. '#type' => 'textarea',
  346. '#title' => t('Track configuration in json'),
  347. '#description' => t('The track info from trackList.json. Only '),
  348. '#default_value' => json_encode($track_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  349. '#rows' => '10',
  350. '#required' => TRUE,
  351. ];
  352. $form['submit'] = [
  353. '#type' => 'submit',
  354. '#value' => 'Save Track Configuration',
  355. ];
  356. return $form;
  357. }
  358. /**
  359. * Validate the form.
  360. *
  361. * @param $form
  362. * @param $form_state
  363. */
  364. function tripal_jbrowse_mgmt_json_editor_advance_form_validate($form, &$form_state) {
  365. $values = $form_state['values'];
  366. $track_all_config = $values['track_all_config'] ?? NULL;
  367. $track_key = $values['track_index'];
  368. if ($track_all_config && !empty($track_all_config)) {
  369. if (!json_decode($track_all_config)) {
  370. form_set_error(
  371. 'track_all_config',
  372. 'Invalid JSON. Please verify that the menu template contains only valid JSON.'
  373. );
  374. }
  375. }
  376. $json_before_edit = $form_state['track_json']['tracks'][$track_key];
  377. $json_after_edit = json_decode($form_state['values']['track_all_config'], TRUE);
  378. if (($json_before_edit['key'] != $json_after_edit['key']) OR ($json_before_edit['label'] != $json_after_edit['label'])){
  379. form_set_error('track_all_config', 'Key or Label changed. Please don\'t change Key or Label here. The functionality is provided in "Track Manage" form.');
  380. }
  381. }
  382. /**
  383. * @param array $form
  384. * @param array $form_state
  385. *
  386. * @throws \Exception
  387. */
  388. function tripal_jbrowse_mgmt_json_editor_advance_form_submit($form, &$form_state) {
  389. $values = $form_state['values'];
  390. $track_index = $values['track_index'];
  391. $track_all_config = $values['track_all_config'] ?? NULL;
  392. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  393. $json = $form_state['track_json'];
  394. $json['tracks'][$track_index] = json_decode($track_all_config, TRUE);
  395. tripal_jbrowse_mgmt_update_track($track, ['label' => $json['tracks'][$track_index]['key']]);
  396. if (tripal_jbrowse_mgmt_save_json($track->instance, $json) === FALSE) {
  397. drupal_set_message(
  398. 'Unable to save JSON file. Please make sure it\'s editable.'
  399. );
  400. return;
  401. }
  402. drupal_set_message('Track updated successfully');
  403. }