tripal_jbrowse_mgmt_tracks.form.inc 10 KB

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