tripal_jbrowse_mgmt_commands.inc 6.8 KB

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