tripal_jbrowse_mgmt_commands.inc 6.8 KB

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