tripal_jbrowse_mgmt_tracks.form.inc 9.8 KB

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