tripal_jbrowse_mgmt_commands.inc 7.8 KB

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