tripal_jbrowse_mgmt_tracks.form.inc 10 KB

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