tripal_jbrowse_mgmt_commands.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Add a reference sequence to the instance.
  4. *
  5. * @param $instance
  6. *
  7. * @return string
  8. * @throws \Exception
  9. */
  10. function tripal_jbrowse_mgmt_cmd_prepare_refseq($instance) {
  11. $settings = tripal_jbrowse_mgmt_get_settings();
  12. $data = $settings['data_dir'];
  13. $bin = $settings['bin_path'];
  14. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  15. if(isset($instance->analysis_id)){
  16. $path = $path . '_' . $instance->analysis_id;
  17. }
  18. if (!file_exists($path)) {
  19. if (!mkdir($path)) {
  20. throw new Exception(
  21. 'Unable to make data directory! Please make sure the directory
  22. at ' . $data . ' exists and is writable by the current user.'
  23. );
  24. }
  25. }
  26. $out = $path . '/data';
  27. tripal_jbrowse_mgmt_safe_exec(
  28. 'perl',
  29. [
  30. $bin . '/prepare-refseqs.pl',
  31. '--fasta',
  32. $instance->file,
  33. '--out',
  34. $out,
  35. ],
  36. $ignore,
  37. $ret
  38. );
  39. return $ret;
  40. }
  41. /**
  42. * Add a track to an instance.
  43. *
  44. * @param $track
  45. *
  46. * @return string
  47. * @throws \Exception
  48. */
  49. function tripal_jbrowse_mgmt_cmd_add_track($track) {
  50. $settings = tripal_jbrowse_mgmt_get_settings();
  51. $data = $settings['data_dir'];
  52. $bin = $settings['bin_path'];
  53. $instance = $track->instance;
  54. $menu_template = $settings['menu_template'];
  55. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  56. if(isset($instance->analysis_id)){
  57. $path = $path . '_' . $instance->analysis_id;
  58. }
  59. $out = $path . '/data';
  60. if (!file_exists($out)) {
  61. throw new Exception('Data directory does not exist: ' . $out);
  62. }
  63. switch ($track->track_type)
  64. {
  65. case 'HTMLVariants':
  66. $json = tripal_jbrowse_mgmt_get_json($instance);
  67. $directory = 'vcf';
  68. $file_name = $track->file;
  69. if (is_dir($track->file)) {
  70. $file_name = glob($track->file . '/' . '*.vcf.gz')[0];
  71. $index_name = glob($track->file . '/' . '*.vcf.gz.[cti][sbd][ix]')[0];
  72. }
  73. $file_name = pathinfo($file_name)['basename'];
  74. $track_in_json = [
  75. 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
  76. 'key' => $track->label,
  77. 'urlTemplate' => $directory . '/' . $file_name,
  78. 'type' => $track->track_type,
  79. ];
  80. // check if index format and give specific (csi)urltemplate / storeClass
  81. if ($index_name){
  82. $index_name = pathinfo($index_name)['basename'];
  83. $extension = pathinfo($index_name)['extension'];
  84. switch ($extension)
  85. {
  86. case 'csi':
  87. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTabix';
  88. $track_in_json['csiUrlTemplate'] = $directory . '/' . $index_name;
  89. break;
  90. case 'tbi':
  91. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTabix';
  92. break;
  93. case 'idx':
  94. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTribble';
  95. break;
  96. }
  97. }
  98. $json['tracks'][] = $track_in_json;
  99. tripal_jbrowse_mgmt_save_json($instance, $json);
  100. break;
  101. case 'Alignment':
  102. $json = tripal_jbrowse_mgmt_get_json($instance);
  103. $directory = 'bam';
  104. if (is_dir($track->file)) {
  105. $file_name = glob($track->file . '/' . '*.bam')[0];
  106. $index_file_name = glob($track->file . '/' . '*.bam.*')[0];
  107. if (!$file_name) {
  108. $file_name = glob($track->file . '/' . '*.cram')[0];
  109. $index_file_name = glob($track->file . '/' . '*.cram.*')[0];
  110. }
  111. }
  112. $track_in_json = [
  113. 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
  114. 'key' => $track->label,
  115. 'urlTemplate' => $directory . '/' . pathinfo($file_name)['basename'],
  116. 'type' => 'JBrowse/View/Track/Alignments2',
  117. ];
  118. $index_path_info = pathinfo($index_file_name);
  119. switch($index_path_info['extension']){
  120. case "bai":
  121. $track_in_json['baiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
  122. break;
  123. case "csi":
  124. $track_in_json['csiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
  125. break;
  126. }
  127. $extension = pathinfo($file_name)['extension'];
  128. switch($extension){
  129. case 'bam':
  130. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/BAM';
  131. break;
  132. case 'cram':
  133. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/CRAM';
  134. break;
  135. }
  136. $json['tracks'][] = $track_in_json;
  137. tripal_jbrowse_mgmt_save_json($instance, $json);
  138. break;
  139. case 'XYPlot':
  140. $json = tripal_jbrowse_mgmt_get_json($instance);
  141. $basename = pathinfo($track->file)['basename'];
  142. $json['tracks'][] = [
  143. 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
  144. 'key' => $track->label,
  145. 'storeClass' => 'JBrowse/Store/SeqFeature/BigWig',
  146. 'urlTemplate' => 'wig/' . $basename,
  147. 'type' => 'JBrowse/View/Track/Wiggle/XYPlot',
  148. ];
  149. tripal_jbrowse_mgmt_save_json($instance, $json);
  150. break;
  151. default:
  152. tripal_jbrowse_mgmt_safe_exec(
  153. 'perl',
  154. [
  155. $bin . '/flatfile-to-json.pl',
  156. '--' . $track->file_type,
  157. $track->file,
  158. '--trackLabel',
  159. tripal_jbrowse_mgmt_make_slug($track->label),
  160. '--key',
  161. $track->label,
  162. '--out',
  163. $out,
  164. '--trackType',
  165. $track->track_type,
  166. ],
  167. $ignore,
  168. $ret
  169. );
  170. return $ret;
  171. }
  172. }
  173. /**
  174. * Generate names for a specific instance.
  175. *
  176. * @param $instance
  177. *
  178. * @return string
  179. * @throws \Exception
  180. */
  181. function tripal_jbrowse_mgmt_cmd_generate_names($instance) {
  182. $settings = tripal_jbrowse_mgmt_get_settings();
  183. $data = $settings['data_dir'];
  184. $bin = $settings['bin_path'];
  185. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  186. if(isset($instance->analysis_id)){
  187. $path = $path . '_' . $instance->analysis_id;
  188. }
  189. if (!file_exists($path)) {
  190. if (!mkdir($path)) {
  191. throw new Exception(
  192. 'Unable to make data directory! Please make sure the directory
  193. at ' . $data . ' exists and is writable by the current user.'
  194. );
  195. }
  196. }
  197. $out = $path . '/data';
  198. tripal_jbrowse_mgmt_safe_exec(
  199. 'perl',
  200. [
  201. $bin . '/generate-names.pl',
  202. '--out',
  203. $out,
  204. ],
  205. $ignore,
  206. $ret
  207. );
  208. return $ret;
  209. }
  210. /**
  211. * Delete a track to an instance.
  212. *
  213. * @param $track
  214. *
  215. * @return string
  216. * @throws \Exception
  217. */
  218. function tripal_jbrowse_mgmt_cmd_delete_track($track) {
  219. $settings = tripal_jbrowse_mgmt_get_settings();
  220. $data = $settings['data_dir'];
  221. $bin = $settings['bin_path'];
  222. $instance = $track->instance;
  223. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  224. if(isset($instance->analysis_id)){
  225. $path = $path . '_' . $instance->analysis_id;
  226. }
  227. $out = $path . '/data';
  228. if (!file_exists($out)) {
  229. throw new Exception('Data directory does not exist: ' . $out);
  230. }
  231. return tripal_jbrowse_mgmt_safe_exec(
  232. 'perl',
  233. [
  234. $bin . '/remove-track.pl',
  235. '--trackLabel',
  236. tripal_jbrowse_mgmt_make_slug($track->label),
  237. '--dir',
  238. $out,
  239. '--delete',
  240. ]
  241. );
  242. }
  243. /**
  244. * Safely execute a command.
  245. *
  246. * @param string $command The path to the command to execute.
  247. * @param array $args Arguments passed as flag => $argument or a list of
  248. * arguments as [arg1, arg2, arg3]
  249. * @param array $output If the output argument is present, then the
  250. * specified array will be filled with every line of output from the
  251. * command. Trailing whitespace, such as \n, is not
  252. * included in this array. Note that if the array already contains some
  253. * elements, exec will append to the end of the array.
  254. * If you do not want the function to append elements, call
  255. * unset on the array before passing it to
  256. * exec.
  257. * @param int $return_var If the return_var argument is present
  258. * along with the output argument, then the
  259. * return status of the executed command will be written to this
  260. *
  261. * @return string
  262. */
  263. function tripal_jbrowse_mgmt_safe_exec(
  264. $command,
  265. array $args = [],
  266. &$output = NULL,
  267. &$return = NULL
  268. ) {
  269. $cmd = escapeshellcmd($command) . ' ';
  270. foreach ($args as $flag => $arg) {
  271. if (is_string($flag)) {
  272. $cmd .= escapeshellarg($flag) . ' ';
  273. }
  274. $cmd .= escapeshellarg($arg) . ' ';
  275. }
  276. print "Running the following command:\n";
  277. print $cmd . "\n";
  278. return exec($cmd, $output, $return);
  279. }