tripal_jbrowse_mgmt_tracks.form.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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', '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.'),
  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) . '/data';
  118. if ($file_type === 'vcf') {
  119. $path = $base_path . '/vcf';
  120. }
  121. elseif ($file === 'bw') {
  122. $path = $base_path . '/wig';
  123. }
  124. else {
  125. $path = $base_path;
  126. }
  127. switch ($file_type) {
  128. case 'vcf':
  129. $index = $_FILES['files']['tmp_name']['file2'];
  130. $local_dir = isset($values['dir_path']) ? $values['dir_path'] : NULL;
  131. if (empty($file) && empty($index) && empty($local_dir)) {
  132. form_set_error('file',
  133. 'Please provide a local directory path or upload files.');
  134. }
  135. elseif (empty($file) && empty($index) && !empty($local_dir)) {
  136. if (!file_exists($local_dir)) {
  137. form_set_error('file_path', 'The directory provided does not exist.');
  138. }
  139. else {
  140. if (!is_dir($local_dir)) {
  141. form_set_error('file_path',
  142. 'The file provided is not a directory.');
  143. }
  144. else {
  145. $file_gz = glob($local_dir . '/*.vcf.gz');
  146. $file_index = glob($local_dir . '/*.vcf.gz.[cti][sbd][ix]');
  147. if (count($file_gz) != 1 || count($file_index) != 1) {
  148. form_set_error('file_path',
  149. 'Please provide a directory with exactly one gz and one index file.');
  150. }
  151. else {
  152. try {
  153. if (!tripal_jbrowse_mgmt_copy_file($file_gz[0], $path, $symbolic_link)) {
  154. form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path);
  155. }
  156. else {
  157. if (!tripal_jbrowse_mgmt_copy_file($file_index[0], $path, $symbolic_link)) {
  158. form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path);
  159. }
  160. }
  161. } catch (Exception $exception) {
  162. form_set_error($exception->getMessage());
  163. }
  164. }
  165. }
  166. }
  167. }
  168. elseif (empty($file) && !empty($index)) {
  169. form_set_error('file', 'Please upload both a gz and an index file.');
  170. }
  171. elseif (!empty($file) && empty($index)) {
  172. form_set_error('file2', 'Please upload both a gz and an index file.');
  173. }
  174. else {
  175. $gz_uploaded = tripal_jbrowse_mgmt_upload_file('file');
  176. if (!$gz_uploaded) {
  177. form_set_error('file', 'Unable to upload file');
  178. }
  179. else {
  180. $gz_uploaded = tripal_jbrowse_mgmt_move_file($gz_uploaded, $path);
  181. if (!isset($gz_uploaded)) {
  182. form_set_error('file', 'Failed to move gz file to ' . $path . '.');
  183. }
  184. else {
  185. $form_state['values']['uploaded_gz'] = $gz_uploaded;
  186. }
  187. }
  188. $index_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
  189. if (!$index_uploaded) {
  190. form_set_error('file2', 'Unable to upload file');
  191. }
  192. else {
  193. $index_uploaded = tripal_jbrowse_mgmt_move_file($index_uploaded, $path);
  194. if (!isset($index_uploaded)) {
  195. form_set_error('file2', 'Failed to move index file to ' . $path . '.');
  196. }
  197. else {
  198. $form_state['values']['uploaded_tbi'] = $index_uploaded;
  199. }
  200. }
  201. }
  202. break;
  203. default:
  204. $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
  205. if (empty($file) && empty($local_file)) {
  206. form_set_error('file',
  207. 'Please provide a local file path or upload a new file.');
  208. }
  209. elseif (empty($file) && !empty($local_file)) {
  210. if (!file_exists($local_file)) {
  211. form_set_error('file_path', 'The file path provided does not exist.');
  212. }
  213. else {
  214. try {
  215. if (!tripal_jbrowse_mgmt_copy_file($local_file, $path, $symbolic_link)) {
  216. form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path);
  217. }
  218. } catch (Exception $exception) {
  219. form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path);
  220. }
  221. }
  222. }
  223. else {
  224. $uploaded = tripal_jbrowse_mgmt_upload_file('file');
  225. if (!$uploaded) {
  226. form_set_error('file', 'Unable to upload file');
  227. }
  228. else {
  229. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
  230. if (!isset($uploaded)) {
  231. form_set_error('file', 'Failed to move file to ' . $path);
  232. }
  233. else {
  234. $form_state['values']['uploaded_file'] = $uploaded;
  235. }
  236. }
  237. }
  238. break;
  239. }
  240. }
  241. /**
  242. * Handle form submission for adding a track.
  243. *
  244. * @param array $form
  245. * @param array $form_state
  246. *
  247. * @throws \Exception
  248. */
  249. function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
  250. global $user;
  251. $values = $form_state['values'];
  252. $file = isset($values['file_path']) ? $values['file_path'] : NULL;
  253. if (!empty($values['dir_path'])) {
  254. $file = $values['dir_path'];
  255. }
  256. if (!empty($values['uploaded_gz'])) {
  257. $file = $values['uploaded_gz'];
  258. }
  259. if (!empty($values['uploaded_file'])) {
  260. $file = $values['uploaded_file'];
  261. }
  262. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  263. $track_id = tripal_jbrowse_mgmt_create_track($instance, [
  264. 'label' => $values['label'],
  265. 'track_type' => $values['track_type'],
  266. 'file_type' => $values['file_type'],
  267. 'file' => $file,
  268. 'created_at' => time(),
  269. ]);
  270. tripal_add_job('Add JBrowse track to ' . $instance->title, 'tripal_jbrowse_mgmt',
  271. 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id], $user->uid);
  272. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $instance->id);
  273. }
  274. /**
  275. * Delete a track form.
  276. *
  277. * @param array $form
  278. * @param array $form_state
  279. * @param int $track_id
  280. *
  281. * @return array
  282. */
  283. function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
  284. $track = tripal_jbrowse_mgmt_get_track($track_id);
  285. if (!$track->id) {
  286. $form['error'] = [
  287. '#type' => 'item',
  288. '#markup' => '<p style="color: red">Unable to find track.</p>',
  289. ];
  290. return $form;
  291. }
  292. $form['description'] = [
  293. '#type' => 'item',
  294. '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
  295. ];
  296. $form['track_id'] = [
  297. '#type' => 'hidden',
  298. '#value' => $track_id,
  299. ];
  300. $form['submit'] = [
  301. '#type' => 'submit',
  302. '#value' => 'Delete Track',
  303. ];
  304. $form['cancel'] = [
  305. '#type' => 'markup',
  306. '#markup' => l('Cancel',
  307. 'admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id),
  308. ];
  309. return $form;
  310. }
  311. /**
  312. * @param $form
  313. * @param $form_state
  314. *
  315. * @throws \Exception
  316. */
  317. function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
  318. global $user;
  319. $values = $form_state['values'];
  320. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  321. tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
  322. 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
  323. $user->uid);
  324. tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
  325. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id);
  326. }
  327. /**
  328. * Track json editor advance form.
  329. * allow user to make all configurations of a track
  330. *
  331. * @param array $form
  332. * @param array $form_state
  333. * @param int $track_id
  334. *
  335. * @return array
  336. * @throws \Exception
  337. */
  338. function tripal_jbrowse_mgmt_json_editor_advance_form($form, &$form_state, $track_id) {
  339. $track = tripal_jbrowse_mgmt_get_track($track_id);
  340. if (!$track->id) {
  341. $form['error'] = [
  342. '#type' => 'item',
  343. '#markup' => '<p style="color: red">Unable to find track.</p>',
  344. ];
  345. return $form;
  346. }
  347. $instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  348. $json = tripal_jbrowse_mgmt_get_json($instance);
  349. $form_state['track_json'] = $json;
  350. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  351. $track_json = NULL;
  352. $track_index = NULL;
  353. foreach ($json['tracks'] as $index => $jtrack) {
  354. if ($jtrack['label'] === $key) {
  355. $track_json = $jtrack;
  356. $track_index = $index;
  357. break;
  358. }
  359. }
  360. if (!$track_json) {
  361. $form['error'] = [
  362. '#type' => 'item',
  363. '#markup' => '<p style="color: red">Unable to find track in json!</p>',
  364. ];
  365. return $form;
  366. }
  367. $form['track_index'] = [
  368. '#type' => 'hidden',
  369. '#value' => $track_index,
  370. ];
  371. $form['track_id'] = [
  372. '#type' => 'hidden',
  373. '#value' => $track->id,
  374. ];
  375. $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>
  376. <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>';
  377. $form['Instruction']=[
  378. '#type' => 'markup',
  379. '#markup' => $instr_detail,
  380. ];
  381. $form['track_all_config'] = [
  382. '#type' => 'textarea',
  383. '#title' => t('Track configuration in json'),
  384. '#description' => t('The track info from trackList.json. Only '),
  385. '#default_value' => json_encode($track_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
  386. '#rows' => '10',
  387. '#required' => TRUE,
  388. ];
  389. $form['submit'] = [
  390. '#type' => 'submit',
  391. '#value' => 'Save Track Configuration',
  392. ];
  393. return $form;
  394. }
  395. /**
  396. * Validate the form.
  397. *
  398. * @param $form
  399. * @param $form_state
  400. */
  401. function tripal_jbrowse_mgmt_json_editor_advance_form_validate($form, &$form_state) {
  402. $values = $form_state['values'];
  403. $track_all_config = $values['track_all_config'] ?? NULL;
  404. $track_key = $values['track_index'];
  405. if ($track_all_config && !empty($track_all_config)) {
  406. if (!json_decode($track_all_config)) {
  407. form_set_error(
  408. 'track_all_config',
  409. 'Invalid JSON. Please verify that the menu template contains only valid JSON.'
  410. );
  411. }
  412. }
  413. $json_before_edit = $form_state['track_json']['tracks'][$track_key];
  414. $json_after_edit = json_decode($form_state['values']['track_all_config'], TRUE);
  415. if (($json_before_edit['key'] != $json_after_edit['key']) OR ($json_before_edit['label'] != $json_after_edit['label'])){
  416. 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.');
  417. }
  418. }
  419. /**
  420. * @param array $form
  421. * @param array $form_state
  422. *
  423. * @throws \Exception
  424. */
  425. function tripal_jbrowse_mgmt_json_editor_advance_form_submit($form, &$form_state) {
  426. $values = $form_state['values'];
  427. $track_index = $values['track_index'];
  428. $track_all_config = $values['track_all_config'] ?? NULL;
  429. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  430. $json = $form_state['track_json'];
  431. $json['tracks'][$track_index] = json_decode($track_all_config, TRUE);
  432. tripal_jbrowse_mgmt_update_track($track, ['label' => $json['tracks'][$track_index]['key']]);
  433. if (tripal_jbrowse_mgmt_save_json($track->instance, $json) === FALSE) {
  434. drupal_set_message(
  435. 'Unable to save JSON file. Please make sure it\'s editable.'
  436. );
  437. return;
  438. }
  439. drupal_set_message('Track updated successfully');
  440. }