tripal_jbrowse_mgmt_tracks.form.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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) . '/data';
  116. if ($file_type === 'vcf') {
  117. $path = $base_path . '/vcf';
  118. }
  119. elseif ($file === 'bw') {
  120. $path = $base_path . '/wig';
  121. }
  122. else {
  123. $path = $base_path;
  124. }
  125. switch ($file_type) {
  126. case 'vcf':
  127. $index = $_FILES['files']['tmp_name']['file2'];
  128. $local_dir = isset($values['dir_path']) ? $values['dir_path'] : NULL;
  129. if (empty($file) && empty($index) && empty($local_dir)) {
  130. form_set_error('file',
  131. 'Please provide a local directory path or upload files.');
  132. }
  133. elseif (empty($file) && empty($index) && !empty($local_dir)) {
  134. if (!file_exists($local_dir)) {
  135. form_set_error('file_path', 'The directory provided does not exist.');
  136. }
  137. else {
  138. if (!is_dir($local_dir)) {
  139. form_set_error('file_path',
  140. 'The file provided is not a directory.');
  141. }
  142. else {
  143. $file_gz = glob($local_dir . '/*.vcf.gz');
  144. $file_index = glob($local_dir . '/*.vcf.gz.[cti][sbd][ix]');
  145. if (count($file_gz) != 1 || count($file_index) != 1) {
  146. form_set_error('file_path',
  147. 'Please provide a directory with exactly one gz and one index file.');
  148. }
  149. else {
  150. try {
  151. if (!tripal_jbrowse_mgmt_copy_file($file_gz[0], $path, $symbolic_link)) {
  152. form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path);
  153. }
  154. else {
  155. if (!tripal_jbrowse_mgmt_copy_file($file_index[0], $path, $symbolic_link)) {
  156. form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path);
  157. }
  158. }
  159. } catch (Exception $exception) {
  160. form_set_error($exception->getMessage());
  161. }
  162. }
  163. }
  164. }
  165. }
  166. elseif (empty($file) && !empty($index)) {
  167. form_set_error('file', 'Please upload both a gz and an index file.');
  168. }
  169. elseif (!empty($file) && empty($index)) {
  170. form_set_error('file2', 'Please upload both a gz and an index file.');
  171. }
  172. else {
  173. $gz_uploaded = tripal_jbrowse_mgmt_upload_file('file');
  174. if (!$gz_uploaded) {
  175. form_set_error('file', 'Unable to upload file');
  176. }
  177. else {
  178. $gz_uploaded = tripal_jbrowse_mgmt_move_file($gz_uploaded, $path);
  179. if (!isset($gz_uploaded)) {
  180. form_set_error('file', 'Failed to move gz file to ' . $path . '.');
  181. }
  182. else {
  183. $form_state['values']['uploaded_gz'] = $gz_uploaded;
  184. }
  185. }
  186. $index_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
  187. if (!$index_uploaded) {
  188. form_set_error('file2', 'Unable to upload file');
  189. }
  190. else {
  191. $index_uploaded = tripal_jbrowse_mgmt_move_file($index_uploaded, $path);
  192. if (!isset($index_uploaded)) {
  193. form_set_error('file2', 'Failed to move index file to ' . $path . '.');
  194. }
  195. else {
  196. $form_state['values']['uploaded_tbi'] = $index_uploaded;
  197. }
  198. }
  199. }
  200. break;
  201. default:
  202. $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
  203. if (empty($file) && empty($local_file)) {
  204. form_set_error('file',
  205. 'Please provide a local file path or upload a new file.');
  206. }
  207. elseif (empty($file) && !empty($local_file)) {
  208. if (!file_exists($local_file)) {
  209. form_set_error('file_path', 'The file path provided does not exist.');
  210. }
  211. else {
  212. try {
  213. if (!tripal_jbrowse_mgmt_copy_file($local_file, $path, $symbolic_link)) {
  214. form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path);
  215. }
  216. } catch (Exception $exception) {
  217. form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path);
  218. }
  219. }
  220. }
  221. else {
  222. $uploaded = tripal_jbrowse_mgmt_upload_file('file');
  223. if (!$uploaded) {
  224. form_set_error('file', 'Unable to upload file');
  225. }
  226. else {
  227. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
  228. if (!isset($uploaded)) {
  229. form_set_error('file', 'Failed to move file to ' . $path);
  230. }
  231. else {
  232. $form_state['values']['uploaded_file'] = $uploaded;
  233. }
  234. }
  235. }
  236. break;
  237. }
  238. }
  239. /**
  240. * Handle form submission for adding a track.
  241. *
  242. * @param array $form
  243. * @param array $form_state
  244. *
  245. * @throws \Exception
  246. */
  247. function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
  248. global $user;
  249. $values = $form_state['values'];
  250. $file = isset($values['file_path']) ? $values['file_path'] : NULL;
  251. if (!empty($values['dir_path'])) {
  252. $file = $values['dir_path'];
  253. }
  254. if (!empty($values['uploaded_gz'])) {
  255. $file = $values['uploaded_gz'];
  256. }
  257. if (!empty($values['uploaded_file'])) {
  258. $file = $values['uploaded_file'];
  259. }
  260. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  261. $track_id = tripal_jbrowse_mgmt_create_track($instance, [
  262. 'label' => $values['label'],
  263. 'track_type' => $values['track_type'],
  264. 'file_type' => $values['file_type'],
  265. 'file' => $file,
  266. 'created_at' => time(),
  267. ]);
  268. tripal_add_job('Add JBrowse track to ' . $instance->title, 'tripal_jbrowse_mgmt',
  269. 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id], $user->uid);
  270. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $instance->id);
  271. }
  272. /**
  273. * Delete a track form.
  274. *
  275. * @param array $form
  276. * @param array $form_state
  277. * @param int $track_id
  278. *
  279. * @return array
  280. */
  281. function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
  282. $track = tripal_jbrowse_mgmt_get_track($track_id);
  283. if (!$track->id) {
  284. $form['error'] = [
  285. '#type' => 'item',
  286. '#markup' => '<p style="color: red">Unable to find track.</p>',
  287. ];
  288. return $form;
  289. }
  290. $form['description'] = [
  291. '#type' => 'item',
  292. '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
  293. ];
  294. $form['track_id'] = [
  295. '#type' => 'hidden',
  296. '#value' => $track_id,
  297. ];
  298. $form['submit'] = [
  299. '#type' => 'submit',
  300. '#value' => 'Delete Track',
  301. ];
  302. $form['cancel'] = [
  303. '#type' => 'markup',
  304. '#markup' => l('Cancel',
  305. 'admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id),
  306. ];
  307. return $form;
  308. }
  309. /**
  310. * @param $form
  311. * @param $form_state
  312. *
  313. * @throws \Exception
  314. */
  315. function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
  316. global $user;
  317. $values = $form_state['values'];
  318. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  319. tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
  320. 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
  321. $user->uid);
  322. tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
  323. drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id);
  324. }