tripal_jbrowse_mgmt_tracks.form.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * Add a track to an instance form.
  4. *
  5. * @param $form
  6. * @param $form_state
  7. * @param $instance_id
  8. *
  9. * @return array
  10. */
  11. function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
  12. if (empty(tripal_jbrowse_mgmt_get_instance($instance_id))) {
  13. drupal_not_found();
  14. return [];
  15. }
  16. $form['label'] = [
  17. '#type' => 'textfield',
  18. '#title' => t('Track Label'),
  19. '#description' => t('This will appear on the sidebar.'),
  20. '#required' => TRUE,
  21. ];
  22. $form['instance_id'] = [
  23. '#type' => 'hidden',
  24. '#value' => $instance_id,
  25. ];
  26. $form['data'] = [
  27. '#type' => 'fieldset',
  28. '#title' => t('Track Files'),
  29. ];
  30. $form['data']['track_type'] = [
  31. '#type' => 'select',
  32. '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
  33. '#required' => TRUE,
  34. '#title' => t('Track Type'),
  35. '#options' => drupal_map_assoc(tripal_jbrowse_mgmt_get_track_types()),
  36. ];
  37. $form['data']['file_type'] = [
  38. '#type' => 'select',
  39. '#title' => t('File Type'),
  40. '#options' => drupal_map_assoc(['gff', 'bed', 'gbk', 'vcf', 'bw']),
  41. '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
  42. '#required' => TRUE,
  43. ];
  44. $form['data']['file'] = [
  45. '#type' => 'file',
  46. '#title' => t('File'),
  47. ];
  48. $form['data']['file2'] = [
  49. '#type' => 'file',
  50. '#title' => t('TBI File'),
  51. '#states' => [
  52. 'visible' => [
  53. ':input[name="file_type"]' => ['value' => 'vcf'],
  54. ],
  55. ],
  56. ];
  57. $form['data']['file_path'] = [
  58. '#type' => 'textfield',
  59. '#title' => t('- OR Path to File on Server -'),
  60. '#description' => t('This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'),
  61. '#states' => [
  62. 'invisible' => [
  63. ':input[name="file_type"]' => ['value' => 'vcf'],
  64. ],
  65. ],
  66. ];
  67. $form['data']['dir_path'] = [
  68. '#type' => 'textfield',
  69. '#title' => t('- OR Path to Directory on Server -'),
  70. '#description' => t('This path will be ignored if a file is provided above. ' . 'The directory must contain both the .tbi and .gz files.'),
  71. '#states' => [
  72. 'visible' => [
  73. ':input[name="file_type"]' => ['value' => 'vcf'],
  74. ],
  75. ],
  76. ];
  77. $form['submit'] = [
  78. '#type' => 'submit',
  79. '#value' => 'Add New Track',
  80. ];
  81. return $form;
  82. }
  83. /**
  84. * Validate the add track form.
  85. *
  86. * @param array $form
  87. * @param array $form_state
  88. */
  89. function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
  90. $values = $form_state['values'];
  91. $file = $_FILES['files']['tmp_name']['file'];
  92. $settings = tripal_jbrowse_mgmt_get_settings();
  93. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  94. $data = $settings['data_dir'];
  95. $file_type = $values['file_type'];
  96. $path = NULL;
  97. switch ($file_type) {
  98. case 'vcf':
  99. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title) . '/data/vcf';
  100. break;
  101. case 'bw':
  102. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title) . '/data/wig';
  103. break;
  104. }
  105. switch ($file_type) {
  106. case 'vcf':
  107. $tbi = $_FILES['files']['tmp_name']['file2'];
  108. $local_dir = isset($values['dir_path']) ? $values['dir_path'] : NULL;
  109. if (empty($file) && empty($tbi) && empty($local_dir)) {
  110. form_set_error('file',
  111. 'Please provide a local directory path or upload files.');
  112. }
  113. elseif (empty($file) && empty($tbi) && !empty($local_dir)) {
  114. if (!file_exists($local_dir)) {
  115. form_set_error('file_path', 'The directory provided does not exist.');
  116. }
  117. else {
  118. if (!is_dir($local_dir)) {
  119. form_set_error('file_path',
  120. 'The file provided is not a directory.');
  121. }
  122. else {
  123. $file_gz = glob($local_dir . '/*.vcf.gz');
  124. $file_tbi = glob($local_dir . '/*.vcf.gz.tbi');
  125. if (count($file_gz) != 1 || count($file_tbi) != 1) {
  126. form_set_error('file_path',
  127. 'Please provide a directory with exactly one gz and one tbi file.');
  128. }
  129. else {
  130. if (!tripal_jbrowse_mgmt_copy_file($file_gz[0], $path)) {
  131. form_set_error('Failed to copy file' . $file_gz[0] . ' to ' . $path);
  132. }
  133. else {
  134. if (!tripal_jbrowse_mgmt_copy_file($file_tbi[0], $path)) {
  135. form_set_error('Failed to copy file' . $file_gz[0] . ' to ' . $path);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. elseif (empty($file) && !empty($tbi)) {
  143. form_set_error('file', 'Please upload both a gz and a tbi file.');
  144. }
  145. elseif (!empty($file) && empty($tbi)) {
  146. form_set_error('file2', 'Please upload both a gz and a tbi file.');
  147. }
  148. else {
  149. $gz_uploaded = tripal_jbrowse_mgmt_upload_file('file');
  150. if (!$gz_uploaded) {
  151. form_set_error('file', 'Unable to upload file');
  152. }
  153. else {
  154. $gz_uploaded = tripal_jbrowse_mgmt_move_file($gz_uploaded, $path);
  155. if (!isset($gz_uploaded)) {
  156. form_set_error('file', 'Failed to move gz file to ' . $path . '.');
  157. }
  158. else {
  159. $form_state['values']['uploaded_gz'] = $gz_uploaded;
  160. }
  161. }
  162. $tbi_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
  163. if (!$tbi_uploaded) {
  164. form_set_error('file2', 'Unable to upload file');
  165. }
  166. else {
  167. $tbi_uploaded = tripal_jbrowse_mgmt_move_file($tbi_uploaded, $path);
  168. if (!isset($tbi_uploaded)) {
  169. form_set_error('file2',
  170. 'Failed to move tbi file to ' . $path . '.');
  171. }
  172. else {
  173. $form_state['values']['uploaded_tbi'] = $tbi_uploaded;
  174. }
  175. }
  176. }
  177. break;
  178. default:
  179. $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
  180. if (empty($file) && empty($local_file)) {
  181. form_set_error('file',
  182. 'Please provide a local file path or upload a new file.');
  183. }
  184. elseif (empty($file) && !empty($local_file)) {
  185. if (!file_exists($local_file)) {
  186. form_set_error('file_path', 'The file path provided does not exist.');
  187. }
  188. else {
  189. if (!tripal_jbrowse_mgmt_copy_file($local_file, $path)) {
  190. form_set_error('Failed to copy file ' . $local_file . ' to ' . $path);
  191. }
  192. }
  193. }
  194. else {
  195. $uploaded = tripal_jbrowse_mgmt_upload_file('file');
  196. if (!$uploaded) {
  197. form_set_error('file', 'Unable to upload file');
  198. }
  199. else {
  200. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
  201. if (!isset($uploaded)) {
  202. form_set_error('file', 'Failed to move file to ' . $path);
  203. }
  204. else {
  205. $form_state['values']['uploaded_file'] = $uploaded;
  206. }
  207. }
  208. }
  209. break;
  210. }
  211. }
  212. /**
  213. * Handle form submission for adding a track.
  214. *
  215. * @param array $form
  216. * @param array $form_state
  217. *
  218. * @throws \Exception
  219. */
  220. function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
  221. global $user;
  222. $values = $form_state['values'];
  223. $file = isset($values['file_path']) ? $values['file_path'] : NULL;
  224. if (!empty($values['dir_path'])) {
  225. $file = $values['dir_path'];
  226. }
  227. if (!empty($values['uploaded_gz'])) {
  228. $file = $values['uploaded_gz'];
  229. }
  230. if (!empty($values['uploaded_file'])) {
  231. $file = $values['uploaded_file'];
  232. }
  233. $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
  234. $track_id = tripal_jbrowse_mgmt_create_track($instance, [
  235. 'label' => $values['label'],
  236. 'track_type' => $values['track_type'],
  237. 'file_type' => $values['file_type'],
  238. 'file' => $file,
  239. 'created_at' => time(),
  240. ]);
  241. tripal_add_job('Add JBrowse track to ' . $instance->title,
  242. 'tripal_jbrowse_mgmt', 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id],
  243. $user->uid);
  244. drupal_goto('admin/tripal_jbrowse_mgmt/instances/' . $instance->id);
  245. }
  246. /**
  247. * Delete a track form.
  248. *
  249. * @param array $form
  250. * @param array $form_state
  251. * @param int $track_id
  252. *
  253. * @return array
  254. */
  255. function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
  256. $track = tripal_jbrowse_mgmt_get_track($track_id);
  257. if (!$track->id) {
  258. $form['error'] = [
  259. '#type' => 'item',
  260. '#markup' => '<p style="color: red">Unable to find track.</p>',
  261. ];
  262. return $form;
  263. }
  264. $form['description'] = [
  265. '#type' => 'item',
  266. '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
  267. ];
  268. $form['track_id'] = [
  269. '#type' => 'hidden',
  270. '#value' => $track_id,
  271. ];
  272. $form['submit'] = [
  273. '#type' => 'submit',
  274. '#value' => 'Delete Track',
  275. ];
  276. $form['cancel'] = [
  277. '#type' => 'markup',
  278. '#markup' => l('Cancel',
  279. 'admin/tripal_jbrowse_mgmt/instances/' . $track->instance_id),
  280. ];
  281. return $form;
  282. }
  283. /**
  284. * @param $form
  285. * @param $form_state
  286. *
  287. * @throws \Exception
  288. */
  289. function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
  290. global $user;
  291. $values = $form_state['values'];
  292. $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
  293. tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
  294. 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
  295. $user->uid);
  296. tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
  297. drupal_goto('admin/tripal_jbrowse_mgmt/instances/' . $track->instance_id);
  298. }