tripal_jbrowse_mgmt_commands.inc 7.7 KB

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