tripal_jbrowse_mgmt_tracks.form.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. ],
  62. ],
  63. ];
  64. $form['data']['file_path'] = [
  65. '#type' => 'textfield',
  66. '#title' => t('- OR Path to File on Server -'),
  67. '#description' => t('This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'),
  68. '#maxlength' => 255,
  69. '#states' => [
  70. 'invisible' => [
  71. ':input[name="file_type"]' => ['value' => 'vcf'],
  72. ],
  73. ],
  74. ];
  75. $form['data']['dir_path'] = [
  76. '#type' => 'textfield',
  77. '#title' => t('- OR Path to Directory on Server -'),
  78. '#description' => t('This path will be ignored if a file is provided above. ' . 'The directory must contain both the .tbi and .gz files.'),
  79. '#maxlength' => 255,
  80. '#states' => [
  81. 'visible' => [
  82. ':input[name="file_type"]' => ['value' => 'vcf'],
  83. ],
  84. ],
  85. ];
  86. $form['data']['symbolic_link'] = [
  87. '#type' => 'checkbox',
  88. '#title' => t('Symbolic Link'),
  89. '#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.'),
  90. ];
  91. $form['submit'] = [
  92. '#type' => 'submit',
  93. '#value' => 'Add New Track',
  94. ];
  95. return $form;
  96. }
  97. /**
  98. * Validate the add track form.
  99. *
  100. * This function also takes care of the uploading of files.
  101. * TODO: If the file is invalid, delete it!
  102. *
  103. * @param array $form
  104. * The Drupal form.
  105. * @param array $form_state
  106. * The form state.
  107. */
  108. function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
  109. $values = $form_state['values'];
  110. $file = $_FILES['files']['tmp_name']['file'];
  111. $settings = tripal_jbrowse_mgmt_get_settings();
  112. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  113. $data = $settings['data_dir'];
  114. $file_type = $values['file_type'];
  115. $symbolic_link = $values['symbolic_link'];
  116. $path = NULL;
  117. $base_path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  118. if(isset($instance->analysis_id)){
  119. $base_path = $base_path . '_' . $instance->analysis_id;
  120. }
  121. $base_path = $base_path . '/data';
  122. if ($file_type === 'vcf') {
  123. $path = $base_path . '/vcf';
  124. }
  125. if (($file_type === 'bam') OR ($file_type === 'cram')){
  126. $path = $base_path . '/bam';
  127. }
  128. elseif ($file_type === 'bw') {
  129. $path = $base_path . '/wig';
  130. }
  131. else {
  132. $path = $base_path;
  133. }
  134. switch ($file_type) {
  135. case 'vcf':
  136. $index = $_FILES['files']['tmp_name']['file2'];
  137. $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
  138. break;
  139. case 'bam':
  140. $index = $_FILES['files']['tmp_name']['file2'];
  141. $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
  142. break;
  143. case 'carm':
  144. $index = $_FILES['files']['tmp_name']['file2'];
  145. $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
  146. break;
  147. default:
  148. $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
  149. if (empty($file) && empty($local_file)) {
  150. form_set_error('file',
  151. 'Please provide a local file path or upload a new file.');
  152. }
  153. elseif (empty($file) && !empty($local_file)) {
  154. if (!file_exists($local_file)) {
  155. form_set_error('file_path', 'The file path provided does not exist.');
  156. }
  157. else {
  158. try {
  159. if (!tripal_jbrowse_mgmt_copy_file($local_file, $path, $symbolic_link)) {
  160. 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.');
  161. }
  162. } catch (Exception $exception) {
  163. 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.');
  164. }
  165. }
  166. }
  167. else {
  168. $uploaded = tripal_jbrowse_mgmt_upload_file('file');
  169. if (!$uploaded) {
  170. form_set_error('file', 'Unable to upload file');
  171. }
  172. else {
  173. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
  174. if (!isset($uploaded)) {
  175. form_set_error('file', 'Failed to move file to ' . $path);
  176. }
  177. else {
  178. $form_state['values']['uploaded_file'] = $uploaded;
  179. }
  180. }
  181. }
  182. break;
  183. }
  184. }
  185. /**
  186. * Handle form submission for adding a track.
  187. *
  188. * @param array $form
  189. * @param array $form_state
  190. *
  191. * @throws \Exception
  192. */
  193. function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
  194. global $user;
  195. $values = $form_state['values'];
  196. $file = isset($values['file_path']) ? $values['file_path'] : NULL;
  197. if (!empty($values['dir_path'])) {
  198. $file = $values['dir_path'];
  199. }
  200. if (!empty($values['uploaded_file_index'])) {
  201. $file = $values['uploaded_file_index'];
  202. }
  203. if (!empty($values['uploaded_file_data'])) {
  204. $file = $values['uploaded_file_data'];
  205. }
  206. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  207. $track_id = tripal_jbrowse_mgmt_create_track($instance, [
  208. 'label' => $values['label'],
  209. 'track_type' => $values['track_type'],
  210. 'file_type' => $values['file_type'],
  211. 'file' => $file,
  212. 'created_at' => time(),
  213. ]);
  214. tripal_add_job('Add JBrowse track to ' . $instance->title, 'tripal_jbrowse_mgmt',
  215. 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id], $user->uid);
  216. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $instance->id);
  217. }
  218. /**
  219. * Delete a track form.
  220. *
  221. * @param array $form
  222. * @param array $form_state
  223. * @param int $track_id
  224. *
  225. * @return array
  226. */
  227. function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
  228. $track = tripal_jbrowse_mgmt_get_track($track_id);
  229. if (!$track->id) {
  230. $form['error'] = [
  231. '#type' => 'item',
  232. '#markup' => '<p style="color: red">Unable to find track.</p>',
  233. ];
  234. return $form;
  235. }
  236. $form['description'] = [
  237. '#type' => 'item',
  238. '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
  239. ];
  240. $form['track_id'] = [
  241. '#type' => 'hidden',
  242. '#value' => $track_id,
  243. ];
  244. $form['submit'] = [
  245. '#type' => 'submit',
  246. '#value' => 'Delete Track',
  247. ];
  248. $form['cancel'] = [
  249. '#type' => 'markup',
  250. '#markup' => l('Cancel',
  251. 'admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id),
  252. ];
  253. return $form;
  254. }
  255. /**
  256. * @param $form
  257. * @param $form_state
  258. *
  259. * @throws \Exception
  260. */
  261. function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
  262. global $user;
  263. $values = $form_state['values'];
  264. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  265. tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
  266. 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
  267. $user->uid);
  268. tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
  269. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id);
  270. }
  271. /**
  272. * Track json editor advance form.
  273. * allow user to make all configurations of a track
  274. *
  275. * @param array $form
  276. * @param array $form_state
  277. * @param int $track_id
  278. *
  279. * @return array
  280. * @throws \Exception
  281. */
  282. function tripal_jbrowse_mgmt_json_editor_advance_form($form, &$form_state, $track_id) {
  283. $track = tripal_jbrowse_mgmt_get_track($track_id);
  284. if (empty($track)) {
  285. drupal_not_found();
  286. return $form;
  287. }
  288. drupal_set_title('Edit Track Json: ' . $track->label);
  289. $breadcrumb = array();
  290. $breadcrumb[] = l('Home', '');
  291. $breadcrumb[] = l('Administration', 'admin');
  292. $breadcrumb[] = l('Tripal', 'admin/tripal');
  293. $breadcrumb[] = l('Extensions', 'admin/tripal/extension');
  294. $breadcrumb[] = l('Tripal JBrowse', 'admin/tripal/extension/tripal_jbrowse/management');
  295. $breadcrumb[] = l('Instance', 'admin/tripal/extension/tripal_jbrowse/management/instances/'.$track->instance_id);
  296. drupal_set_breadcrumb($breadcrumb);
  297. if (!$track->id) {
  298. $form['error'] = [
  299. '#type' => 'item',
  300. '#markup' => '<p style="color: red">Unable to find track.</p>',
  301. ];
  302. return $form;
  303. }
  304. $instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  305. $json = tripal_jbrowse_mgmt_get_json($instance);
  306. $form_state['track_json'] = $json;
  307. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  308. $track_json = NULL;
  309. $track_index = NULL;
  310. foreach ($json['tracks'] as $index => $jtrack) {
  311. if ($jtrack['label'] === $key) {
  312. $track_json = $jtrack;
  313. $track_index = $index;
  314. break;
  315. }
  316. }
  317. if (!$track_json) {
  318. $form['error'] = [
  319. '#type' => 'item',
  320. '#markup' => '<p style="color: red">Unable to find track in json!</p>',
  321. ];
  322. return $form;
  323. }
  324. $form['track_index'] = [
  325. '#type' => 'hidden',
  326. '#value' => $track_index,
  327. ];
  328. $form['track_id'] = [
  329. '#type' => 'hidden',
  330. '#value' => $track->id,
  331. ];
  332. $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>
  333. <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>';
  334. $form['Instruction']=[
  335. '#type' => 'markup',
  336. '#markup' => $instr_detail,
  337. ];
  338. $form['track_all_config'] = [
  339. '#type' => 'textarea',
  340. '#title' => t('Track configuration in json'),
  341. '#description' => t('The track info from trackList.json. Only '),
  342. '#default_value' => json_encode($track_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  343. '#rows' => '10',
  344. '#required' => TRUE,
  345. ];
  346. $form['submit'] = [
  347. '#type' => 'submit',
  348. '#value' => 'Save Track Configuration',
  349. ];
  350. return $form;
  351. }
  352. /**
  353. * Validate the form.
  354. *
  355. * @param $form
  356. * @param $form_state
  357. */
  358. function tripal_jbrowse_mgmt_json_editor_advance_form_validate($form, &$form_state) {
  359. $values = $form_state['values'];
  360. $track_all_config = $values['track_all_config'] ?? NULL;
  361. $track_key = $values['track_index'];
  362. if ($track_all_config && !empty($track_all_config)) {
  363. if (!json_decode($track_all_config)) {
  364. form_set_error(
  365. 'track_all_config',
  366. 'Invalid JSON. Please verify that the menu template contains only valid JSON.'
  367. );
  368. }
  369. }
  370. $json_before_edit = $form_state['track_json']['tracks'][$track_key];
  371. $json_after_edit = json_decode($form_state['values']['track_all_config'], TRUE);
  372. if (($json_before_edit['key'] != $json_after_edit['key']) OR ($json_before_edit['label'] != $json_after_edit['label'])){
  373. 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.');
  374. }
  375. }
  376. /**
  377. * @param array $form
  378. * @param array $form_state
  379. *
  380. * @throws \Exception
  381. */
  382. function tripal_jbrowse_mgmt_json_editor_advance_form_submit($form, &$form_state) {
  383. $values = $form_state['values'];
  384. $track_index = $values['track_index'];
  385. $track_all_config = $values['track_all_config'] ?? NULL;
  386. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  387. $json = $form_state['track_json'];
  388. $json['tracks'][$track_index] = json_decode($track_all_config, TRUE);
  389. tripal_jbrowse_mgmt_update_track($track, ['label' => $json['tracks'][$track_index]['key']]);
  390. if (tripal_jbrowse_mgmt_save_json($track->instance, $json) === FALSE) {
  391. drupal_set_message(
  392. 'Unable to save JSON file. Please make sure it\'s editable.'
  393. );
  394. return;
  395. }
  396. drupal_set_message('Track updated successfully');
  397. }