comparison chewBBACA.pl @ 0:a3b5bee8ae1b draft

Uploaded
author iss
date Fri, 03 May 2019 10:06:09 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a3b5bee8ae1b
1 #!/usr/bin/env perl
2 ## A wrapper script to call chewBBACA.py and collect its output
3 use strict;
4 use warnings;
5 use Cwd;
6 use English;
7 use File::Copy;
8 use File::Basename;
9
10 # Parse arguments
11 my ($myFunction,
12 $input1,
13 $input1_names,
14 $output,
15 $python) = @ARGV;
16
17 # Run program
18 my $abs_path = Cwd::abs_path($PROGRAM_NAME);
19 my $scriptdir = dirname($abs_path);
20
21 prepareEnvironment($input1,$input1_names,"input_dir");
22 runChewBBACA();
23 collectOutput();
24 exit(0);
25
26 # Run chewBBACA
27 sub runChewBBACA {
28 my $abs_path = Cwd::abs_path($PROGRAM_NAME);
29 my $scriptdir = dirname($abs_path);
30 my $createschemadir = "$scriptdir/createschema";
31 my $allelecalldir = "$scriptdir/allelecall";
32 my $SchemaEvaluatordir = "$scriptdir/SchemaEvaluator";
33 my $utilsdir = "$scriptdir/utils";
34 my $newpath = "PATH=$ENV{PATH}:$createschemadir:$allelecalldir:$SchemaEvaluatordir:$utilsdir";
35 my $result = system("$newpath; $python");
36 return 0;
37 }
38
39 # Run prepareEnvironment, create the directory $indir with symlinks to the files listed in $inlist
40 sub prepareEnvironment {
41 my ($inlist, $inlist_names, $indir) = @_;
42 if ($inlist ne "NULL") {
43 mkdir($indir);
44 my @inputs = split(',', $inlist);
45 if ($inlist_names ne "NULL") {
46 my @inputs_names = split(',', $inlist_names);
47 for my $i ( 0 .. $#inputs ){
48 my $name = $inputs_names[$i];
49 $name =~ s/ /_/g;
50 $name =~ s/\//_/g;
51 $name =~ s/\(/_/g;
52 $name =~ s/\)/_/g;
53 $name =~ s/\[/_/g;
54 $name =~ s/\]/_/g;
55 $name =~ s/\{/_/g;
56 $name =~ s/\}/_/g;
57 symlink($inputs[$i], $indir . "/" . $name);
58 }
59 }
60 else {
61 foreach my $i (@inputs){
62 symlink($i, $indir . "/" . basename($i));
63 }
64 }
65 }
66 return 0;
67 }
68
69 # Collect output
70 sub collectOutput{
71 # CreateSchema
72 if ($myFunction eq "CreateSchema") {
73 my($dataname, $datadir, $datasuffix) = fileparse($output,qr/\.[^.]*/);
74 my @fasta_files = glob "output_dir/*.fasta";
75 my $dest_dir = $datadir . $dataname . "/";
76 mkdir($dest_dir);
77 open(my $fh1, '>', $output) or die "Could not open file '$output' $!";
78 my $new_fasta_file = "";
79 foreach my $fasta_file (@fasta_files) {
80 $new_fasta_file = $dest_dir . basename($fasta_file);
81 print $fh1 "$new_fasta_file\n";
82 move($fasta_file, $new_fasta_file) or die "Could not move $fasta_file: $!\n";
83 }
84 close $fh1;
85 my @short_fasta_files = glob "output_dir/short/*.fasta";
86 my $short_dest_dir = $datadir . $dataname . "/short/";
87 mkdir($short_dest_dir);
88 foreach my $short_fasta_file (@short_fasta_files) {
89 move($short_fasta_file, $short_dest_dir) or die "Could not move $short_fasta_file: $!\n";
90 }
91 }
92 # AlleleCall
93 if ($myFunction eq "AlleleCall") {
94 my @outputs = split(',', $output);
95 my @statistics = glob "output_dir/results_*/results_statistics.tsv";
96 if (@statistics == 1) {
97 move($statistics[0], $outputs[0])
98 }
99 my @contigsinfo = glob "output_dir/results_*/results_contigsInfo.tsv";
100 if (@contigsinfo == 1) {
101 move($contigsinfo[0], $outputs[1])
102 }
103 my @alleles = glob "output_dir/results_*/results_alleles.tsv";
104 if (@alleles == 1) {
105 move($alleles[0], $outputs[2])
106 }
107 my @logginginfo = glob "output_dir/results_*/logging_info.txt";
108 if (@logginginfo == 1) {
109 move($logginginfo[0], $outputs[3])
110 }
111 my @repeatedloci = glob "output_dir/results_*/RepeatedLoci.txt";
112 if (@repeatedloci == 1) {
113 move($repeatedloci[0], $outputs[4])
114 }
115 }
116 # SchemaEvaluator
117 if ($myFunction eq "SchemaEvaluator") {
118 my($dataname, $datadir, $datasuffix) = fileparse($output,qr/\.[^.]*/);
119 my @output_files = glob "output_rms/*.*";
120 my $dest_dir = $dataname . "/";
121 mkdir($dest_dir);
122 my $new_output_file = "";
123 foreach my $output_file (@output_files) {
124 $new_output_file = $dest_dir . basename($output_file);
125 move($output_file, $new_output_file) or die "Could not move $output_file: $!\n";
126 }
127 open(my $fh2, '<', $dest_dir . "SchemaEvaluator.html") || die "File not found";
128 my @lines = <$fh2>;
129 close($fh2);
130 my @newlines;
131 foreach(@lines) {
132 $_ =~ s/\.\.\/\.\.\/\.\.\/.*?\/database\/files\/...\/dataset/dataset/g;
133 push(@newlines,$_);
134 }
135 # write the html to the Galaxy output file
136 #open($fh2, '>', $output) || die "File not found";
137 #print $fh2 @newlines;
138 #close($fh2);
139 my @html_output_files = glob "output_rms/genes_html/*.*";
140 my $html_dest_dir = $dest_dir . "genes_html/";
141 mkdir($html_dest_dir);
142 foreach my $html_output_file (@html_output_files) {
143 move($html_output_file, $html_dest_dir) or die "Could not move $html_output_file: $!\n";
144 }
145 my $cmd = `tar -cpf $output $dest_dir`;
146 }
147 return 0;
148 }
149