crossconta-detection.pl 9.26 KB
Newer Older
khalid's avatar
khalid committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/perl -w

use strict;

##########################################################################################
##########################################################################################

my @files;
my $fasta;
my $bowtieout;
my $ebwt;
my $target;
my $parsebowtie;
my $single;
my $left;
my $right;
my $fileout;
my $files1;
my @files1;
my $mode;
my $file1;
my $file2;
my $fold = 10;
my $min_cov = 2;
my $trim5 = 0;
my $trim3 = 0;
my $processors = 1;

##########################################################################################
# command line requires base name of the transcriptome files e.g. \*.fasta
#
# NB Script requires unity between transcriptome and raw data file names
# FOR paired end:::: NAME.fasta,  NAME.L.fastq NAME.R.fastq  
# FOR unpaired  :::: NAME.fasta,  NAME.fastq
##########################################################################################

##########################################################################################
# get suffix of files and mode (paired or unpaired)
##########################################################################################

for (my $i=0; $i<=$#ARGV; ++$i) {

    if($ARGV[$i] eq "-in") { # -in = input files i.e. output of bwt
        if(($i+1)>($#ARGV)) { exit_error1(); }
        $files1 = ($ARGV[$i+1]);
        $i++;
    }
    if($ARGV[$i] eq "-mode") { # -mode = paired or unpaired data
        if(($i+1)>($#ARGV)) { exit_error1(); }
        $mode = ($ARGV[$i+1]);
        $i++;
    }
    if($ARGV[$i] eq "-fold") { # -fold is how many fold enrichment of real over others is required to keep
        if(($i+1)>($#ARGV)) { exit_error1(); }
        $fold = ($ARGV[$i+1]);
        $i++;
    }
    if($ARGV[$i] eq "-min_cov") { # -fold is how many fold enrichment of real
        over others is required to keep if(($i+1)>($#ARGV)) { exit_error1(); }
        $min_cov = ($ARGV[$i+1]); $i++;
    }
    if($ARGV[$i] eq "-trim5") { # -fold is how many fold enrichment of real over others is required to keep
        if(($i+1)>($#ARGV)) { exit_error1(); }
        $trim5 = ($ARGV[$i+1]);
        $i++;
    }
    if($ARGV[$i] eq "-trim3") { # -fold is how many fold enrichment of real over others is required to keep
        if(($i+1)>($#ARGV)) { exit_error1(); }
        $trim3 = ($ARGV[$i+1]);
        $i++;
    }
    if($ARGV[$i] eq "-proc") { # -fold is how many fold enrichment of real over others is required to keep
        if(($i+1)>($#ARGV)) { exit_error1(); }
        $processors = ($ARGV[$i+1]);
        $i++;
    }
}


@files1 = glob $files1;
unless (scalar @files1 > 0 ) {
    print "no data files present.  Check spelling!\n\n\n\n";
    &exit_error;
}


unless ($mode eq 'u' or $mode eq 'p') {
    print "-mode not set (specify -mode p (paired) or -mode u (unpaied)\n\n\n\n";
    &exit_error;
}

##########################################################################################
# base files into new array
##########################################################################################

print "mode = $mode\n";
foreach (@files1) {
    $_ =~ s/.fasta//;
    push @files, $_;
}

##########################################################################################
# Run bowtie build on transcript files if needed.
##########################################################################################

foreach $file1 (@files){
    $fasta = $file1 . ".fasta";
    $bowtieout = $file1 . ".bowtie";
    $ebwt = $bowtieout . ".1.ebwt";
    unless (-e $ebwt) {
        #system "/Users/maxtelford/othersbin/bowtie-1.1.1/bowtie-build --offrate 3 $fasta $bowtieout";
        system "bowtie-build --offrate 3 $fasta $bowtieout";
    }
    else {
        print "skipping build stage for $fasta\n";
    }
}

##########################################################################################
# Set up required filenames and for each transcriptome file, run each of the raw datasets 
# against it to count how many raw reads from all the spp hit each hit each seq
##########################################################################################

foreach $file1 (@files){
    print "\n\n\n";
    $fasta = $file1 . ".fasta";						print "fasta = $fasta\n";
    $target = $file1 . "vs" . $file1 . '.out';		print "target = $target\n";
    $parsebowtie = "\\\*vs$file1.out";				print "parsebowtie = $parsebowtie\n";
    $bowtieout = $file1 . ".bowtie";
    foreach $file2 (@files){
        $single = $file2 . ".fastq";
        print "single = $single\n";
        $left = $file2 . ".L.fastq";
        print "left = $left\n";
        $right = $file2 . ".R.fastq";
        print "right = $right\n";
        $fileout = $file2 . 'vs' . $file1 . '.out';
        print "fileout = $fileout\n";

##########################################################################################
# Run mode paired (p) or unpaired (u) run bowtie differently.
##########################################################################################
        unless (-e $fileout) {
            print "Running bowtie $file2 versus $file1\n";
            if ($mode eq 'u') {
                #system "/Users/maxtelford/othersbin/bowtie-1.1.1/bowtie -p $processors -a --trim5 $trim5 --trim3 $trim3 --suppress 1,2,4,5,6,7,8 $bowtieout $single > $fileout\n\n";
                system "bowtie -p $processors -a --trim5 $trim5 --trim3 $trim3 --suppress 1,2,4,5,6,7,8 $bowtieout $single > $fileout\n\n";
            }
            elsif ($mode eq 'p') {
                #system "/Users/maxtelford/othersbin/bowtie-1.1.1/bowtie  -p $processors -a --trim5 $trim5 --trim3 $trim3 --suppress 1,2,4,5,6,7,8 $bowtieout -1 $left -2 $right > $fileout\n\n";
                system "bowtie  -p $processors -a --trim5 $trim5 --trim3 $trim3 --suppress 1,2,4,5,6,7,8 $bowtieout -1 $left -2 $right > $fileout\n\n";
            }
            else {
                print "please indicate paired end \"p\" or unpaired \"u\" data\n";
                exit;
            }
##########################################################################################
# parse output file and summarise to reduce file size to "seq_name \t count"
##########################################################################################

            print "reducing output\n";
            my %seqshash=();
            my $seq;
            open (FILE, $fileout)  || die; #
            while (<FILE>) {
                chomp;
                if ($_ =~ m/\w/i) {
                    if (exists $seqshash{$_}) {
                        $seqshash{$_}++;
                    }
                    else {
                        $seqshash{$_} = 1;
                    }
                }
            }
            close FILE;

##########################################################################################
# overwrite original output file
##########################################################################################

            open (NEWFILE, ">$fileout");
            foreach $seq (keys %seqshash) {
                print NEWFILE $seq, "\t", $seqshash{$seq}, "\n";
            }
            close NEWFILE;
            %seqshash = ();
        }
    }
##########################################################################################
# Run 'parsebowtie.pl which selects those sppX assembled seqs for which sppX raw data hit
# more often than sppY or sppZ etc.
##########################################################################################
    system "parsebowtie.pl -in $parsebowtie -transcripts $fasta -target $target -fold $fold -min_cov $min_cov";
}


##########################################################################################
#Exit error
##########################################################################################

sub exit_error {
    die"\n
    ***************************************
    \tBT2.pl
    ***************************************


    Prog to run bowtie (http://bowtie-bio.sourceforge.net/index.shtml) on several assembled transcriptomes deriving from single illumina track
    with potential cross contamination.

    Runs bowtie-build for each set of assembled transcripts.

    Runs bowtie for each set of raw illumina data against each assembled transcriptome.

    Runs separate script 'parsebowtie.pl' to identify those assembled seqs with highest hit to correct raw data files (by factor of 100 
    more than nearest wrong raw data seq)

    Saves good hits to 'clean' file and rejects those that do not pass this threshold (these end up in 'bad' file).

    Command line requires base name of the transcriptome files e.g. \*.fasta and indication of paired or unpaired raw data


    Usage = bt2.pl -in <base_file> -mode <p or u for paired or unpaired> -fold <fold enrichment> -min_cov <minimum coverage to keep>

    e.g. bt2.pl -in \\*.fasta -mode p -fold 10 -min_cov 5 

    NOTE \\*.fasta   NOT \*.fasta!!


    NB Script requires unity between transcriptome and raw data file names:

    FOR paired end	:
    NAME.fasta		(assembled transcriptome seqs)
    NAME.L.fastq	(raw illumina data LEFT)
    NAME.R.fastq	(raw illumina data RIGHT)

    FOR unpaired	:
    NAME.fasta		(assembled transcriptome seqs)
    NAME.fastq		(raw illumina data)

    To trim 5' or 3' nucleotides from the fastq reads (e.g. low quality) use -trim5 and -trim3

    -trim5 20 (trims first 20)
    -trim3 50 (trims last 50)

    to use more than one processor use -proc
    e.g. -proc 10
    ";
}