tripal_jbrowse_mgmt_commands.inc 6.4 KB

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