tripal_jbrowse_mgmt_commands.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.[tci][bsd][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][cram]')[0];
  106. $index_file_name = glob($track->file . '/' . '*.[bai][csi][crai]')[0];
  107. }
  108. $track_in_json = [
  109. 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
  110. 'key' => $track->label,
  111. 'urlTemplate' => $directory . '/' . pathinfo($file_name)['basename'],
  112. 'type' => 'JBrowse/View/Track/Alignments2',
  113. ];
  114. $index_path_info = pathinfo($index_file_name);
  115. switch($index_path_info['extension']){
  116. case "bai":
  117. $track_in_json['baiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
  118. break;
  119. case "csi":
  120. $track_in_json['csiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
  121. break;
  122. }
  123. $extension = pathinfo($file_name)['extension'];
  124. switch($extension){
  125. case 'bam':
  126. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/BAM';
  127. break;
  128. case 'cram':
  129. $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/CRAM';
  130. break;
  131. }
  132. $json['tracks'][] = $track_in_json;
  133. tripal_jbrowse_mgmt_save_json($instance, $json);
  134. break;
  135. case 'XYPlot':
  136. $json = tripal_jbrowse_mgmt_get_json($instance);
  137. $basename = pathinfo($track->file)['basename'];
  138. $json['tracks'][] = [
  139. 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
  140. 'key' => $track->label,
  141. 'storeClass' => 'JBrowse/Store/SeqFeature/BigWig',
  142. 'urlTemplate' => 'wig/' . $basename,
  143. 'type' => 'JBrowse/View/Track/Wiggle/XYPlot',
  144. ];
  145. tripal_jbrowse_mgmt_save_json($instance, $json);
  146. break;
  147. default:
  148. tripal_jbrowse_mgmt_safe_exec(
  149. 'perl',
  150. [
  151. $bin . '/flatfile-to-json.pl',
  152. '--' . $track->file_type,
  153. $track->file,
  154. '--trackLabel',
  155. tripal_jbrowse_mgmt_make_slug($track->label),
  156. '--key',
  157. $track->label,
  158. '--out',
  159. $out,
  160. '--trackType',
  161. $track->track_type,
  162. ],
  163. $ignore,
  164. $ret
  165. );
  166. return $ret;
  167. }
  168. }
  169. /**
  170. * Generate names for a specific instance.
  171. *
  172. * @param $instance
  173. *
  174. * @return string
  175. * @throws \Exception
  176. */
  177. function tripal_jbrowse_mgmt_cmd_generate_names($instance) {
  178. $settings = tripal_jbrowse_mgmt_get_settings();
  179. $data = $settings['data_dir'];
  180. $bin = $settings['bin_path'];
  181. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  182. if(isset($instance->analysis_id)){
  183. $path = $path . '_' . $instance->analysis_id;
  184. }
  185. if (!file_exists($path)) {
  186. if (!mkdir($path)) {
  187. throw new Exception(
  188. 'Unable to make data directory! Please make sure the directory
  189. at ' . $data . ' exists and is writable by the current user.'
  190. );
  191. }
  192. }
  193. $out = $path . '/data';
  194. tripal_jbrowse_mgmt_safe_exec(
  195. 'perl',
  196. [
  197. $bin . '/generate-names.pl',
  198. '--out',
  199. $out,
  200. ],
  201. $ignore,
  202. $ret
  203. );
  204. return $ret;
  205. }
  206. /**
  207. * Delete a track to an instance.
  208. *
  209. * @param $track
  210. *
  211. * @return string
  212. * @throws \Exception
  213. */
  214. function tripal_jbrowse_mgmt_cmd_delete_track($track) {
  215. $settings = tripal_jbrowse_mgmt_get_settings();
  216. $data = $settings['data_dir'];
  217. $bin = $settings['bin_path'];
  218. $instance = $track->instance;
  219. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  220. if(isset($instance->analysis_id)){
  221. $path = $path . '_' . $instance->analysis_id;
  222. }
  223. $out = $path . '/data';
  224. if (!file_exists($out)) {
  225. throw new Exception('Data directory does not exist: ' . $out);
  226. }
  227. return tripal_jbrowse_mgmt_safe_exec(
  228. 'perl',
  229. [
  230. $bin . '/remove-track.pl',
  231. '--trackLabel',
  232. tripal_jbrowse_mgmt_make_slug($track->label),
  233. '--dir',
  234. $out,
  235. '--delete',
  236. ]
  237. );
  238. }
  239. /**
  240. * Safely execute a command.
  241. *
  242. * @param string $command The path to the command to execute.
  243. * @param array $args Arguments passed as flag => $argument or a list of
  244. * arguments as [arg1, arg2, arg3]
  245. * @param array $output If the output argument is present, then the
  246. * specified array will be filled with every line of output from the
  247. * command. Trailing whitespace, such as \n, is not
  248. * included in this array. Note that if the array already contains some
  249. * elements, exec will append to the end of the array.
  250. * If you do not want the function to append elements, call
  251. * unset on the array before passing it to
  252. * exec.
  253. * @param int $return_var If the return_var argument is present
  254. * along with the output argument, then the
  255. * return status of the executed command will be written to this
  256. *
  257. * @return string
  258. */
  259. function tripal_jbrowse_mgmt_safe_exec(
  260. $command,
  261. array $args = [],
  262. &$output = NULL,
  263. &$return = NULL
  264. ) {
  265. $cmd = escapeshellcmd($command) . ' ';
  266. foreach ($args as $flag => $arg) {
  267. if (is_string($flag)) {
  268. $cmd .= escapeshellarg($flag) . ' ';
  269. }
  270. $cmd .= escapeshellarg($arg) . ' ';
  271. }
  272. print "Running the following command:\n";
  273. print $cmd . "\n";
  274. return exec($cmd, $output, $return);
  275. }