tripal_jbrowse_mgmt_tracks.form.inc 15 KB

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