123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- /**
- * Add a reference sequence to the instance.
- *
- * @param $instance
- *
- * @return string
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_cmd_prepare_refseq($instance) {
- $settings = tripal_jbrowse_mgmt_get_settings();
- $data = $settings['data_dir'];
- $bin = $settings['bin_path'];
- $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
- if(isset($instance->analysis_id)){
- $path = $path . '_' . $instance->analysis_id;
- }
- if (!file_exists($path)) {
- if (!mkdir($path)) {
- throw new Exception(
- 'Unable to make data directory! Please make sure the directory
- at ' . $data . ' exists and is writable by the current user.'
- );
- }
- }
- $out = $path . '/data';
- tripal_jbrowse_mgmt_safe_exec(
- 'perl',
- [
- $bin . '/prepare-refseqs.pl',
- '--fasta',
- $instance->file,
- '--out',
- $out,
- ],
- $ignore,
- $ret
- );
- return $ret;
- }
- /**
- * Add a track to an instance.
- *
- * @param $track
- *
- * @return string
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_cmd_add_track($track) {
- $settings = tripal_jbrowse_mgmt_get_settings();
- $data = $settings['data_dir'];
- $bin = $settings['bin_path'];
- $instance = $track->instance;
- $menu_template = $settings['menu_template'];
- $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
- if(isset($instance->analysis_id)){
- $path = $path . '_' . $instance->analysis_id;
- }
- $out = $path . '/data';
- if (!file_exists($out)) {
- throw new Exception('Data directory does not exist: ' . $out);
- }
- switch ($track->track_type)
- {
- case 'HTMLVariants':
- $json = tripal_jbrowse_mgmt_get_json($instance);
- $directory = 'vcf';
- $file_name = $track->file;
- if (is_dir($track->file)) {
- $file_name = glob($track->file . '/' . '*.vcf.gz')[0];
- $index_name = glob($track->file . '/' . '*.vcf.gz.[cti][sbd][ix]')[0];
- }
- $file_name = pathinfo($file_name)['basename'];
- $track_in_json = [
- 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
- 'key' => $track->label,
- 'urlTemplate' => $directory . '/' . $file_name,
- 'type' => $track->track_type,
- ];
- // check if index format and give specific (csi)urltemplate / storeClass
- if ($index_name){
- $index_name = pathinfo($index_name)['basename'];
- $extension = pathinfo($index_name)['extension'];
- switch ($extension)
- {
- case 'csi':
- $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTabix';
- $track_in_json['csiUrlTemplate'] = $directory . '/' . $index_name;
- break;
- case 'tbi':
- $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTabix';
- break;
- case 'idx':
- $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTribble';
- break;
- }
- }
- $json['tracks'][] = $track_in_json;
- tripal_jbrowse_mgmt_save_json($instance, $json);
- break;
- case 'Alignment':
- $json = tripal_jbrowse_mgmt_get_json($instance);
- $directory = 'bam';
- if (is_dir($track->file)) {
- $file_name = glob($track->file . '/' . '*.bam')[0];
- $index_file_name = glob($track->file . '/' . '*.bam.*')[0];
- if (!$file_name) {
- $file_name = glob($track->file . '/' . '*.cram')[0];
- $index_file_name = glob($track->file . '/' . '*.cram.*')[0];
- }
- }
- $track_in_json = [
- 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
- 'key' => $track->label,
- 'urlTemplate' => $directory . '/' . pathinfo($file_name)['basename'],
- 'type' => 'JBrowse/View/Track/Alignments2',
- ];
- $index_path_info = pathinfo($index_file_name);
- switch($index_path_info['extension']){
- case "bai":
- $track_in_json['baiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
- break;
- case "csi":
- $track_in_json['csiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
- break;
- }
- $extension = pathinfo($file_name)['extension'];
- switch($extension){
- case 'bam':
- $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/BAM';
- break;
- case 'cram':
- $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/CRAM';
- break;
- }
- $json['tracks'][] = $track_in_json;
- tripal_jbrowse_mgmt_save_json($instance, $json);
- break;
- case 'XYPlot':
- $json = tripal_jbrowse_mgmt_get_json($instance);
- $basename = pathinfo($track->file)['basename'];
- $json['tracks'][] = [
- 'label' => tripal_jbrowse_mgmt_make_slug($track->label),
- 'key' => $track->label,
- 'storeClass' => 'JBrowse/Store/SeqFeature/BigWig',
- 'urlTemplate' => 'wig/' . $basename,
- 'type' => 'JBrowse/View/Track/Wiggle/XYPlot',
- ];
- tripal_jbrowse_mgmt_save_json($instance, $json);
- break;
- default:
- tripal_jbrowse_mgmt_safe_exec(
- 'perl',
- [
- $bin . '/flatfile-to-json.pl',
- '--' . $track->file_type,
- $track->file,
- '--trackLabel',
- tripal_jbrowse_mgmt_make_slug($track->label),
- '--key',
- $track->label,
- '--out',
- $out,
- '--trackType',
- $track->track_type,
- ],
- $ignore,
- $ret
- );
- return $ret;
- }
- }
- /**
- * Generate names for a specific instance.
- *
- * @param $instance
- *
- * @return string
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_cmd_generate_names($instance) {
- $settings = tripal_jbrowse_mgmt_get_settings();
- $data = $settings['data_dir'];
- $bin = $settings['bin_path'];
- $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
- if(isset($instance->analysis_id)){
- $path = $path . '_' . $instance->analysis_id;
- }
- if (!file_exists($path)) {
- if (!mkdir($path)) {
- throw new Exception(
- 'Unable to make data directory! Please make sure the directory
- at ' . $data . ' exists and is writable by the current user.'
- );
- }
- }
- $out = $path . '/data';
- tripal_jbrowse_mgmt_safe_exec(
- 'perl',
- [
- $bin . '/generate-names.pl',
- '--out',
- $out,
- ],
- $ignore,
- $ret
- );
- return $ret;
- }
- /**
- * Delete a track to an instance.
- *
- * @param $track
- *
- * @return string
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_cmd_delete_track($track) {
- $settings = tripal_jbrowse_mgmt_get_settings();
- $data = $settings['data_dir'];
- $bin = $settings['bin_path'];
- $instance = $track->instance;
- $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
- if(isset($instance->analysis_id)){
- $path = $path . '_' . $instance->analysis_id;
- }
- $out = $path . '/data';
- if (!file_exists($out)) {
- throw new Exception('Data directory does not exist: ' . $out);
- }
- return tripal_jbrowse_mgmt_safe_exec(
- 'perl',
- [
- $bin . '/remove-track.pl',
- '--trackLabel',
- tripal_jbrowse_mgmt_make_slug($track->label),
- '--dir',
- $out,
- '--delete',
- ]
- );
- }
- /**
- * Safely execute a command.
- *
- * @param string $command The path to the command to execute.
- * @param array $args Arguments passed as flag => $argument or a list of
- * arguments as [arg1, arg2, arg3]
- * @param array $output If the output argument is present, then the
- * specified array will be filled with every line of output from the
- * command. Trailing whitespace, such as \n, is not
- * included in this array. Note that if the array already contains some
- * elements, exec will append to the end of the array.
- * If you do not want the function to append elements, call
- * unset on the array before passing it to
- * exec.
- * @param int $return_var If the return_var argument is present
- * along with the output argument, then the
- * return status of the executed command will be written to this
- *
- * @return string
- */
- function tripal_jbrowse_mgmt_safe_exec(
- $command,
- array $args = [],
- &$output = NULL,
- &$return = NULL
- ) {
- $cmd = escapeshellcmd($command) . ' ';
- foreach ($args as $flag => $arg) {
- if (is_string($flag)) {
- $cmd .= escapeshellarg($flag) . ' ';
- }
- $cmd .= escapeshellarg($arg) . ' ';
- }
- print "Running the following command:\n";
- print $cmd . "\n";
- return exec($cmd, $output, $return);
- }
|