annotate bin/align.pm @ 22:b9cf69322742 draft

Uploaded
author romaingred
date Fri, 20 Oct 2017 08:35:06 -0400
parents 39b039d82743
children 08bbde9d0c9d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
198009598544 Uploaded
romaingred
parents:
diff changeset
1 package align;
198009598544 Uploaded
romaingred
parents:
diff changeset
2
198009598544 Uploaded
romaingred
parents:
diff changeset
3 use strict;
198009598544 Uploaded
romaingred
parents:
diff changeset
4 use warnings;
198009598544 Uploaded
romaingred
parents:
diff changeset
5 use File::Basename;
198009598544 Uploaded
romaingred
parents:
diff changeset
6 use String::Random;
198009598544 Uploaded
romaingred
parents:
diff changeset
7
198009598544 Uploaded
romaingred
parents:
diff changeset
8 use FindBin;
198009598544 Uploaded
romaingred
parents:
diff changeset
9 use lib $FindBin::Bin;
198009598544 Uploaded
romaingred
parents:
diff changeset
10 use Rcall qw ( histogram );
198009598544 Uploaded
romaingred
parents:
diff changeset
11
198009598544 Uploaded
romaingred
parents:
diff changeset
12 use Exporter;
198009598544 Uploaded
romaingred
parents:
diff changeset
13 our @ISA = qw( Exporter );
198009598544 Uploaded
romaingred
parents:
diff changeset
14 our @EXPORT = qw( &BWA_call &to_build &get_unique &sam_sorted_bam &get_hash_alignment &sam_to_bam_bg &sam_count &sam_count_mis &rpms_rpkm &get_fastq_seq &extract_sam );
198009598544 Uploaded
romaingred
parents:
diff changeset
15
198009598544 Uploaded
romaingred
parents:
diff changeset
16 sub to_build
198009598544 Uploaded
romaingred
parents:
diff changeset
17 {
198009598544 Uploaded
romaingred
parents:
diff changeset
18 my $toBuildHashP = shift; my $log = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
19
198009598544 Uploaded
romaingred
parents:
diff changeset
20 while ( my ( $k, $v ) = each %{ $toBuildHashP } )
198009598544 Uploaded
romaingred
parents:
diff changeset
21 {
198009598544 Uploaded
romaingred
parents:
diff changeset
22 build_index ( $k, $log ) if $v == 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
23 }
198009598544 Uploaded
romaingred
parents:
diff changeset
24 }
198009598544 Uploaded
romaingred
parents:
diff changeset
25
198009598544 Uploaded
romaingred
parents:
diff changeset
26 sub build_index
198009598544 Uploaded
romaingred
parents:
diff changeset
27 {
198009598544 Uploaded
romaingred
parents:
diff changeset
28 my $to_index = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
29 my $log = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
30 my $index_log = $to_index.'_index.err';
198009598544 Uploaded
romaingred
parents:
diff changeset
31 `bwa index $to_index 2> $index_log`;
198009598544 Uploaded
romaingred
parents:
diff changeset
32 print $log "Creating index for $to_index\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
33 }
198009598544 Uploaded
romaingred
parents:
diff changeset
34
198009598544 Uploaded
romaingred
parents:
diff changeset
35 sub get_unique
198009598544 Uploaded
romaingred
parents:
diff changeset
36 {
198009598544 Uploaded
romaingred
parents:
diff changeset
37 my ( $sam, $s_uni, $prefix, $details, $report ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
38
198009598544 Uploaded
romaingred
parents:
diff changeset
39 my $fout = $prefix.'all.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
40 my $funi = $prefix.'unique.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
41 my $frej = $prefix.'rejected.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
42
198009598544 Uploaded
romaingred
parents:
diff changeset
43 my $repartition = $prefix.'distribution.txt';
198009598544 Uploaded
romaingred
parents:
diff changeset
44 my $png_rep = $prefix.'distribution.png';
198009598544 Uploaded
romaingred
parents:
diff changeset
45 my ( %duplicates, %genome_hits) ;
198009598544 Uploaded
romaingred
parents:
diff changeset
46
198009598544 Uploaded
romaingred
parents:
diff changeset
47 #alignement to the first reference
198009598544 Uploaded
romaingred
parents:
diff changeset
48 my @return = sam_parse( $sam, $fout, $funi, $frej, $s_uni, \%duplicates, \%genome_hits, $report );
198009598544 Uploaded
romaingred
parents:
diff changeset
49 my $ref_fai = $return[4];
198009598544 Uploaded
romaingred
parents:
diff changeset
50 my $mappers = $return[5];
198009598544 Uploaded
romaingred
parents:
diff changeset
51 my $mappers_uni = $return[6];
198009598544 Uploaded
romaingred
parents:
diff changeset
52 my $size_mappedHashR = $return[7];
198009598544 Uploaded
romaingred
parents:
diff changeset
53
198009598544 Uploaded
romaingred
parents:
diff changeset
54 if ( $details == 1 )
198009598544 Uploaded
romaingred
parents:
diff changeset
55 {
198009598544 Uploaded
romaingred
parents:
diff changeset
56 #print number of duplicates and hits number
198009598544 Uploaded
romaingred
parents:
diff changeset
57 my ($pourcentage, $total) =(0,0);
198009598544 Uploaded
romaingred
parents:
diff changeset
58
198009598544 Uploaded
romaingred
parents:
diff changeset
59 $total += $_ foreach values %{$size_mappedHashR};
198009598544 Uploaded
romaingred
parents:
diff changeset
60 open (my $rep, '>'.$repartition) || die "cannot create $repartition $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
61 print $rep "size\tnumber\tpercentage\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
62 foreach my $k (sort{$a cmp $b} keys (%{$size_mappedHashR}))
198009598544 Uploaded
romaingred
parents:
diff changeset
63 {
198009598544 Uploaded
romaingred
parents:
diff changeset
64 $pourcentage = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
65 $pourcentage = $size_mappedHashR->{$k} / $total * 100 unless $total ==0;
198009598544 Uploaded
romaingred
parents:
diff changeset
66
198009598544 Uploaded
romaingred
parents:
diff changeset
67 print $rep "$k\t$size_mappedHashR->{$k}\t";
198009598544 Uploaded
romaingred
parents:
diff changeset
68 printf $rep "%.2f\n",$pourcentage;
198009598544 Uploaded
romaingred
parents:
diff changeset
69 }
198009598544 Uploaded
romaingred
parents:
diff changeset
70
198009598544 Uploaded
romaingred
parents:
diff changeset
71 histogram($size_mappedHashR, $png_rep, $total);
198009598544 Uploaded
romaingred
parents:
diff changeset
72
198009598544 Uploaded
romaingred
parents:
diff changeset
73
198009598544 Uploaded
romaingred
parents:
diff changeset
74 my $dup = $prefix.'dup_mapnum.txt';
198009598544 Uploaded
romaingred
parents:
diff changeset
75 my $dup_u = $prefix .'dup_unique.txt';
198009598544 Uploaded
romaingred
parents:
diff changeset
76 my $dup_r = $prefix .'dup_nonmapp.txt';
198009598544 Uploaded
romaingred
parents:
diff changeset
77 open(my $tab,">".$dup) || die "cannot open output txt file\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
78 open(my $tab_r,">".$dup_r) || die "cannot open output txt file\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
79 open(my $tab_u,">".$dup_u) || die "cannot open output txt file\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
80 print $tab "sequence\tcount\tmapnum\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
81 print $tab_u "sequence\tcount\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
82 print $tab_r "sequence\tcount\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
83 foreach my $k (sort {$duplicates{$b} <=> $duplicates{$a}}keys %duplicates)
198009598544 Uploaded
romaingred
parents:
diff changeset
84 {
198009598544 Uploaded
romaingred
parents:
diff changeset
85 $duplicates{$k} = 0 unless exists($duplicates{$k});
198009598544 Uploaded
romaingred
parents:
diff changeset
86 $genome_hits{$k} = 0 unless exists($genome_hits{$k});
198009598544 Uploaded
romaingred
parents:
diff changeset
87 if ($genome_hits{$k} != 0) { print $tab $k."\t".$duplicates{$k}."\t".$genome_hits{$k}."\n"; }
198009598544 Uploaded
romaingred
parents:
diff changeset
88 else {print $tab_r $k."\t".$duplicates{$k}."\n";}
198009598544 Uploaded
romaingred
parents:
diff changeset
89 if ($genome_hits{$k} == 1) { print $tab_u $k."\t".$duplicates{$k}."\n"; }
198009598544 Uploaded
romaingred
parents:
diff changeset
90 }
198009598544 Uploaded
romaingred
parents:
diff changeset
91 close $dup; close $dup_r; close $dup_u;
198009598544 Uploaded
romaingred
parents:
diff changeset
92 }
198009598544 Uploaded
romaingred
parents:
diff changeset
93 return ( $ref_fai, $mappers, $mappers_uni );
198009598544 Uploaded
romaingred
parents:
diff changeset
94 }
198009598544 Uploaded
romaingred
parents:
diff changeset
95
198009598544 Uploaded
romaingred
parents:
diff changeset
96 sub sam_parse
198009598544 Uploaded
romaingred
parents:
diff changeset
97 {
198009598544 Uploaded
romaingred
parents:
diff changeset
98 my ( $sam, $fastq_accepted, $fastq_accepted_unique, $fastq_rejected, $sam_unique, $duplicate_hashR, $best_hit_number_hashR, $report ) = @_ ;
198009598544 Uploaded
romaingred
parents:
diff changeset
99 my ($reads, $mappers, $mappersUnique, @garbage, %size_num, %size_num_spe, %number, %numberSens, %numberReverse, %unique_number, %numberNM, %numberM, %size);
198009598544 Uploaded
romaingred
parents:
diff changeset
100 $mappers = $mappersUnique = $reads = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
101
198009598544 Uploaded
romaingred
parents:
diff changeset
102 open my $fic, '<', $sam || die "cannot open $sam $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
103 open my $accepted, '>', $fastq_accepted || die "cannot create $fastq_accepted $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
104 open my $unique, '>', $fastq_accepted_unique || die "cannot create $fastq_accepted_unique $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
105 open my $rejected, '>', $fastq_rejected || die "cannot create $fastq_rejected $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
106 open my $sam_uni, '>', $sam_unique || die "cannot create $sam_unique $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
107 my $sequence = '';
198009598544 Uploaded
romaingred
parents:
diff changeset
108 while(<$fic>)
198009598544 Uploaded
romaingred
parents:
diff changeset
109 {
198009598544 Uploaded
romaingred
parents:
diff changeset
110 chomp $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
111 if ($_ =~ /^\@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ || $_ =~ /^\@CO\t.*/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
112 {
198009598544 Uploaded
romaingred
parents:
diff changeset
113 if ($_ =~ /\@SQ\tSN:(.*)\tLN:(\d*)/)
198009598544 Uploaded
romaingred
parents:
diff changeset
114 {
198009598544 Uploaded
romaingred
parents:
diff changeset
115 $size{$1} = $2;
198009598544 Uploaded
romaingred
parents:
diff changeset
116 $unique_number{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
117 $number{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
118 $numberNM{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
119 $numberM{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
120 }
198009598544 Uploaded
romaingred
parents:
diff changeset
121 print $sam_uni $_."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
122 next;
198009598544 Uploaded
romaingred
parents:
diff changeset
123 }
198009598544 Uploaded
romaingred
parents:
diff changeset
124 $reads++;
198009598544 Uploaded
romaingred
parents:
diff changeset
125 my @line = split (/\t/,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
126 $sequence = $line[9];
198009598544 Uploaded
romaingred
parents:
diff changeset
127 if ($line[1] & 16)
198009598544 Uploaded
romaingred
parents:
diff changeset
128 {
198009598544 Uploaded
romaingred
parents:
diff changeset
129 $sequence =reverse($sequence);
198009598544 Uploaded
romaingred
parents:
diff changeset
130 $sequence =~ tr/atgcuATGCU/tacgaTACGA/;
198009598544 Uploaded
romaingred
parents:
diff changeset
131 }
198009598544 Uploaded
romaingred
parents:
diff changeset
132 if ($line[1] == 16 || $line[1] == 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
133 {
198009598544 Uploaded
romaingred
parents:
diff changeset
134 my $len = length($sequence);
198009598544 Uploaded
romaingred
parents:
diff changeset
135 $size_num{$len} ++;
198009598544 Uploaded
romaingred
parents:
diff changeset
136 $size_num_spe{$line[2]}{$len}++;
198009598544 Uploaded
romaingred
parents:
diff changeset
137 $mappers ++;
198009598544 Uploaded
romaingred
parents:
diff changeset
138
198009598544 Uploaded
romaingred
parents:
diff changeset
139 ${$best_hit_number_hashR}{$sequence} = $1 if ($line[13] =~ /X0:i:(\d*)/ || $line[14] =~/X0:i:(\d*)/ );
198009598544 Uploaded
romaingred
parents:
diff changeset
140 ${$duplicate_hashR}{$sequence}++;
198009598544 Uploaded
romaingred
parents:
diff changeset
141 $number{$line[2]}++;
198009598544 Uploaded
romaingred
parents:
diff changeset
142 $numberSens{$line[2]}++ if $line[1] == 0 ;
198009598544 Uploaded
romaingred
parents:
diff changeset
143 $numberReverse{$line[2]}++ if $line[1] == 16 ;
198009598544 Uploaded
romaingred
parents:
diff changeset
144 print $accepted "\@".$line[0]."\n".$sequence."\n+\n".$line[10]."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
145
198009598544 Uploaded
romaingred
parents:
diff changeset
146 if ($line[11] eq "XT:A:U")
198009598544 Uploaded
romaingred
parents:
diff changeset
147 {
198009598544 Uploaded
romaingred
parents:
diff changeset
148 $unique_number{$line[2]}++;
198009598544 Uploaded
romaingred
parents:
diff changeset
149 $mappersUnique++;
198009598544 Uploaded
romaingred
parents:
diff changeset
150 print $unique "\@".$line[0]."\n".$sequence."\n+\n".$line[10]."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
151 print $sam_uni $_."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
152 }
198009598544 Uploaded
romaingred
parents:
diff changeset
153 if ($_ =~ /.*XM:i:(\d+).*/)
198009598544 Uploaded
romaingred
parents:
diff changeset
154 {
198009598544 Uploaded
romaingred
parents:
diff changeset
155 if ($1 == 0){$numberNM{$line[2]}++;}else{$numberM{$line[2]}++;}
198009598544 Uploaded
romaingred
parents:
diff changeset
156 }
198009598544 Uploaded
romaingred
parents:
diff changeset
157 }
198009598544 Uploaded
romaingred
parents:
diff changeset
158 else
198009598544 Uploaded
romaingred
parents:
diff changeset
159 {
198009598544 Uploaded
romaingred
parents:
diff changeset
160 ${$best_hit_number_hashR}{$sequence} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
161 ${$duplicate_hashR}{$sequence}++;
198009598544 Uploaded
romaingred
parents:
diff changeset
162 print $rejected "\@".$line[0]."\n".$sequence."\n+\n".$line[10]."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
163 }
198009598544 Uploaded
romaingred
parents:
diff changeset
164 }
198009598544 Uploaded
romaingred
parents:
diff changeset
165 close $fic; close $accepted; close $unique; close $rejected; close $sam_uni;
198009598544 Uploaded
romaingred
parents:
diff changeset
166
198009598544 Uploaded
romaingred
parents:
diff changeset
167 print $report "Parsing $sam file\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
168 print $report "\treads: $reads\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
169 print $report "\tmappers: $mappers\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
170 print $report "\tunique mappers: $mappersUnique\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
171 print $report "-----------------------------\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
172 return (\%number, \%unique_number, \%numberSens, \%numberReverse, \%size, $mappers, $mappersUnique, \%size_num, \%size_num_spe, \%numberNM, \%numberM );
198009598544 Uploaded
romaingred
parents:
diff changeset
173 }
198009598544 Uploaded
romaingred
parents:
diff changeset
174
198009598544 Uploaded
romaingred
parents:
diff changeset
175 sub get_hash_alignment
198009598544 Uploaded
romaingred
parents:
diff changeset
176 {
198009598544 Uploaded
romaingred
parents:
diff changeset
177 my ($index, $mismatches, $accept, $reject, $outA, $outR, $fastq, $number_of_cpus, $name, $sam, $report, $fai_f) = @_ ;
198009598544 Uploaded
romaingred
parents:
diff changeset
178 my ($reads, $mappers, $unmapped) = (0,0,0);
198009598544 Uploaded
romaingred
parents:
diff changeset
179 my $accep_unique;
198009598544 Uploaded
romaingred
parents:
diff changeset
180 BWA_call ( $index, $fastq, $sam, $mismatches, $number_of_cpus, $report );
198009598544 Uploaded
romaingred
parents:
diff changeset
181
198009598544 Uploaded
romaingred
parents:
diff changeset
182 open my $fic, '<', $sam || die "cannot open $sam $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
183 open my $accepted, '>', $outA || die "cannot open $outA\n" if $accept == 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
184 open my $rejected, '>', $outR || die "cannot open $outR\n" if $reject == 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
185 open my $fai, '>', $fai_f || die "cannot open $fai_f\n" if $fai_f;
198009598544 Uploaded
romaingred
parents:
diff changeset
186 #if ($name eq "snRNAs") {
198009598544 Uploaded
romaingred
parents:
diff changeset
187 # open ( $accep_unique, ">".$1."-unique.fastq") if $outR =~ /(.*)\.fastq/;
198009598544 Uploaded
romaingred
parents:
diff changeset
188 #}
198009598544 Uploaded
romaingred
parents:
diff changeset
189 my $sequence = '';
198009598544 Uploaded
romaingred
parents:
diff changeset
190 while(<$fic>)
198009598544 Uploaded
romaingred
parents:
diff changeset
191 {
198009598544 Uploaded
romaingred
parents:
diff changeset
192 chomp $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
193 if( $_ =~ /^\@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ || $_ =~ /^\@CO\t.*/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
194 {
198009598544 Uploaded
romaingred
parents:
diff changeset
195 if ($fai_f && $_ =~ /\@SQ\tSN:(.*)\tLN:(\d*)/)
198009598544 Uploaded
romaingred
parents:
diff changeset
196 {
198009598544 Uploaded
romaingred
parents:
diff changeset
197 print $fai $1."\t".$2."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
198 }
198009598544 Uploaded
romaingred
parents:
diff changeset
199 next;
198009598544 Uploaded
romaingred
parents:
diff changeset
200 }
198009598544 Uploaded
romaingred
parents:
diff changeset
201 $reads++;
198009598544 Uploaded
romaingred
parents:
diff changeset
202 my @line = split (/\t/,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
203 $sequence = $line[9];
198009598544 Uploaded
romaingred
parents:
diff changeset
204 if ($line[1] & 16)
198009598544 Uploaded
romaingred
parents:
diff changeset
205 {
198009598544 Uploaded
romaingred
parents:
diff changeset
206 $sequence =reverse($sequence);
198009598544 Uploaded
romaingred
parents:
diff changeset
207 $sequence =~ tr/atgcuATGCU/tacgaTACGA/;
198009598544 Uploaded
romaingred
parents:
diff changeset
208 }
198009598544 Uploaded
romaingred
parents:
diff changeset
209 if ($line[1] & 16 || $line[1] == 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
210 {
198009598544 Uploaded
romaingred
parents:
diff changeset
211 $mappers ++;
198009598544 Uploaded
romaingred
parents:
diff changeset
212 if ($accept == 1 )
198009598544 Uploaded
romaingred
parents:
diff changeset
213 {
198009598544 Uploaded
romaingred
parents:
diff changeset
214 print $accepted "\@".$line[0]."\n".$sequence."\n+\n".$line[10]."\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
215 # print $accep_unique "\@".$line[0]."\n".$sequence."\n+\n".$line[10]."\n" if ($name eq "snRNAs" && $line[11] eq "XT:A:U");
198009598544 Uploaded
romaingred
parents:
diff changeset
216 }
198009598544 Uploaded
romaingred
parents:
diff changeset
217 }
198009598544 Uploaded
romaingred
parents:
diff changeset
218 else
198009598544 Uploaded
romaingred
parents:
diff changeset
219 {
198009598544 Uploaded
romaingred
parents:
diff changeset
220 print $rejected "\@".$line[0]."\n".$sequence."\n+\n".$line[10]."\n" if $reject == 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
221 $unmapped++;
198009598544 Uploaded
romaingred
parents:
diff changeset
222 }
198009598544 Uploaded
romaingred
parents:
diff changeset
223 }
198009598544 Uploaded
romaingred
parents:
diff changeset
224 # close $accep_unique if ($name eq "bonafide_reads");
198009598544 Uploaded
romaingred
parents:
diff changeset
225 close $fic;
198009598544 Uploaded
romaingred
parents:
diff changeset
226 close $accepted if $accept == 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
227 close $rejected if $reject ==1;
198009598544 Uploaded
romaingred
parents:
diff changeset
228 close $fai if $fai_f;
198009598544 Uploaded
romaingred
parents:
diff changeset
229 print $report "\treads: $reads\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
230 print $report "\tmappers: $mappers\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
231 print $report "\tunmapped: $unmapped\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
232 print $report "-----------------------------\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
233 return ($mappers, $unmapped);
198009598544 Uploaded
romaingred
parents:
diff changeset
234 }
198009598544 Uploaded
romaingred
parents:
diff changeset
235
198009598544 Uploaded
romaingred
parents:
diff changeset
236 sub sam_count
198009598544 Uploaded
romaingred
parents:
diff changeset
237 {
198009598544 Uploaded
romaingred
parents:
diff changeset
238 my $sam = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
239 my ( %number, %size );
198009598544 Uploaded
romaingred
parents:
diff changeset
240
198009598544 Uploaded
romaingred
parents:
diff changeset
241 open my $fic, '<', $sam || die "cannot open $sam file $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
242 while(<$fic>)
198009598544 Uploaded
romaingred
parents:
diff changeset
243 {
198009598544 Uploaded
romaingred
parents:
diff changeset
244 chomp $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
245 if ($_ =~ /^\@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ || $_ =~ /^\@CO\t.*/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
246 {
198009598544 Uploaded
romaingred
parents:
diff changeset
247 if ($_ =~ /\@SQ\tSN:(.*)\tLN:(\d*)/)
198009598544 Uploaded
romaingred
parents:
diff changeset
248 {
198009598544 Uploaded
romaingred
parents:
diff changeset
249 $size{$1} = $2;
198009598544 Uploaded
romaingred
parents:
diff changeset
250 $number{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
251 }
198009598544 Uploaded
romaingred
parents:
diff changeset
252 }
198009598544 Uploaded
romaingred
parents:
diff changeset
253 else
198009598544 Uploaded
romaingred
parents:
diff changeset
254 {
198009598544 Uploaded
romaingred
parents:
diff changeset
255 my @line = split (/\t/,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
256 if ( $line[1] & 16 || $line[1] == 0 )
198009598544 Uploaded
romaingred
parents:
diff changeset
257 {
198009598544 Uploaded
romaingred
parents:
diff changeset
258 $number{$line[2]}++;
198009598544 Uploaded
romaingred
parents:
diff changeset
259 }
198009598544 Uploaded
romaingred
parents:
diff changeset
260 }
198009598544 Uploaded
romaingred
parents:
diff changeset
261 }
198009598544 Uploaded
romaingred
parents:
diff changeset
262 close $fic;
198009598544 Uploaded
romaingred
parents:
diff changeset
263 return ( \%number, \%size );
198009598544 Uploaded
romaingred
parents:
diff changeset
264 }
198009598544 Uploaded
romaingred
parents:
diff changeset
265
198009598544 Uploaded
romaingred
parents:
diff changeset
266 sub sam_count_mis
198009598544 Uploaded
romaingred
parents:
diff changeset
267 {
198009598544 Uploaded
romaingred
parents:
diff changeset
268
198009598544 Uploaded
romaingred
parents:
diff changeset
269 my $sam = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
270 my ( %number, %numberNM, %numberM, %size);
198009598544 Uploaded
romaingred
parents:
diff changeset
271
198009598544 Uploaded
romaingred
parents:
diff changeset
272 open my $fic, '<', $sam || die "cannot open $sam file $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
273 while(<$fic>)
198009598544 Uploaded
romaingred
parents:
diff changeset
274 {
198009598544 Uploaded
romaingred
parents:
diff changeset
275 chomp $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
276 if ($_ =~ /^\@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ || $_ =~ /^\@CO\t.*/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
277 {
198009598544 Uploaded
romaingred
parents:
diff changeset
278 if ($_ =~ /\@SQ\tSN:(.*)\tLN:(\d*)/)
198009598544 Uploaded
romaingred
parents:
diff changeset
279 {
198009598544 Uploaded
romaingred
parents:
diff changeset
280 $size{$1} = $2;
198009598544 Uploaded
romaingred
parents:
diff changeset
281 $number{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
282 $numberNM{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
283 $numberM{$1} = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
284 }
198009598544 Uploaded
romaingred
parents:
diff changeset
285 }
198009598544 Uploaded
romaingred
parents:
diff changeset
286 else
198009598544 Uploaded
romaingred
parents:
diff changeset
287 {
198009598544 Uploaded
romaingred
parents:
diff changeset
288 my @line = split (/\t/,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
289 if ( $line[1] & 16 || $line[1] == 0 )
198009598544 Uploaded
romaingred
parents:
diff changeset
290 {
198009598544 Uploaded
romaingred
parents:
diff changeset
291 $number{ $line[2] }++;
198009598544 Uploaded
romaingred
parents:
diff changeset
292 if ($_ =~ /.*XM:i:(\d+).*/)
198009598544 Uploaded
romaingred
parents:
diff changeset
293 {
198009598544 Uploaded
romaingred
parents:
diff changeset
294 if ( $1 == 0 ){ $numberNM{$line[2]}++; } else { $numberM{$line[2]}++; }
198009598544 Uploaded
romaingred
parents:
diff changeset
295 }
198009598544 Uploaded
romaingred
parents:
diff changeset
296 }
198009598544 Uploaded
romaingred
parents:
diff changeset
297 }
198009598544 Uploaded
romaingred
parents:
diff changeset
298 }
198009598544 Uploaded
romaingred
parents:
diff changeset
299 return (\%number, \%size, \%numberNM, \%numberM );
198009598544 Uploaded
romaingred
parents:
diff changeset
300 }
198009598544 Uploaded
romaingred
parents:
diff changeset
301
198009598544 Uploaded
romaingred
parents:
diff changeset
302 sub sam_to_bam_bg
198009598544 Uploaded
romaingred
parents:
diff changeset
303 {
198009598544 Uploaded
romaingred
parents:
diff changeset
304 my ( $sam, $scale, $number_of_cpus ) = @_;
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
305 my ( $bam_sorted, $bedgraphM, $bedgraphP, $view_err, $sort_err ) = ( '', '', '', '', '' );
0
198009598544 Uploaded
romaingred
parents:
diff changeset
306 if ( $sam =~ /(.*?).sam$/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
307 {
198009598544 Uploaded
romaingred
parents:
diff changeset
308 $bam_sorted = $1.'_sorted.bam';
198009598544 Uploaded
romaingred
parents:
diff changeset
309 $bedgraphP= $1.'_plus.bedgraph';
198009598544 Uploaded
romaingred
parents:
diff changeset
310 $bedgraphM = $1.'_minus.bedgraph';
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
311 $view_err = $1.'_view.err';
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
312 $sort_err = $1.'_sort.err';
0
198009598544 Uploaded
romaingred
parents:
diff changeset
313 }
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
314 `samtools view -Shb --threads $number_of_cpus $sam 2> $view_err | samtools sort -O BAM --threads $number_of_cpus /dev/stdin 2> $sort_err > $bam_sorted`;
10
42bc59c7db3a Uploaded
romaingred
parents: 0
diff changeset
315 `bedtools genomecov -scale $scale -strand + -bga -ibam $bam_sorted > $bedgraphP`;
42bc59c7db3a Uploaded
romaingred
parents: 0
diff changeset
316 `bedtools genomecov -scale $scale -strand - -bga -ibam $bam_sorted > $bedgraphM`;
0
198009598544 Uploaded
romaingred
parents:
diff changeset
317 }
198009598544 Uploaded
romaingred
parents:
diff changeset
318
198009598544 Uploaded
romaingred
parents:
diff changeset
319 sub sam_sorted_bam
198009598544 Uploaded
romaingred
parents:
diff changeset
320 {
198009598544 Uploaded
romaingred
parents:
diff changeset
321 my ( $sam, $number_of_cpus ) = @_;
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
322 my ( $bam_sorted, $view_err, $sort_err ) = ( '', '', '' );
0
198009598544 Uploaded
romaingred
parents:
diff changeset
323 if ( $sam =~ /(.*?).sam$/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
324 {
198009598544 Uploaded
romaingred
parents:
diff changeset
325 $bam_sorted = $1.'_sorted.bam';
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
326 $view_err = $1.'_view.err';
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
327 $sort_err = $1.'_sort.err';
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
328
0
198009598544 Uploaded
romaingred
parents:
diff changeset
329 }
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
330 `samtools view -Shb --threads $number_of_cpus $sam 2> $view_err | samtools sort -O BAM --threads $number_of_cpus /dev/stdin 2> $sort_err > $bam_sorted`;
0
198009598544 Uploaded
romaingred
parents:
diff changeset
331 }
198009598544 Uploaded
romaingred
parents:
diff changeset
332
198009598544 Uploaded
romaingred
parents:
diff changeset
333 sub BWA_call
198009598544 Uploaded
romaingred
parents:
diff changeset
334 {
198009598544 Uploaded
romaingred
parents:
diff changeset
335 my ( $index, $fastq, $sam, $mismatches, $number_of_cpus, $report ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
336 my ( $aln_err, $samse_err, $seq_num ) = ( $sam.'_aln.err', $sam.'_samse.err', 0 );
198009598544 Uploaded
romaingred
parents:
diff changeset
337 print $report "-----------------------------\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
338 print $report "bwa aln -t $number_of_cpus -n $mismatches $index $fastq 2> $aln_err | bwa samse $index /dev/stdin $fastq 2> $samse_err > $sam\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
339 `bwa aln -t $number_of_cpus -n $mismatches $index $fastq 2> $aln_err | bwa samse $index /dev/stdin $fastq 2> $samse_err > $sam `;
198009598544 Uploaded
romaingred
parents:
diff changeset
340 }
198009598544 Uploaded
romaingred
parents:
diff changeset
341
198009598544 Uploaded
romaingred
parents:
diff changeset
342 sub rpms_rpkm
198009598544 Uploaded
romaingred
parents:
diff changeset
343 {
198009598544 Uploaded
romaingred
parents:
diff changeset
344 my ( $counthashR, $sizehashR, $mapped, $out_file, $piRNA_number, $miRNA_number, $bonafide_number ) =@_;
198009598544 Uploaded
romaingred
parents:
diff changeset
345 open(my $out, ">".$out_file) || die "cannot open normalized file $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
346 print $out "ID\treads counts\tRPKM";
198009598544 Uploaded
romaingred
parents:
diff changeset
347 print $out "\tper million of piRNAs" if ($piRNA_number != 0);
198009598544 Uploaded
romaingred
parents:
diff changeset
348 print $out "\tper million of miRNAs" if ($miRNA_number != 0);
198009598544 Uploaded
romaingred
parents:
diff changeset
349 print $out "\tper million of bonafide reads" if ($bonafide_number != 0);
198009598544 Uploaded
romaingred
parents:
diff changeset
350 print $out "\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
351 foreach my $k ( sort keys %{$counthashR} )
198009598544 Uploaded
romaingred
parents:
diff changeset
352 {
198009598544 Uploaded
romaingred
parents:
diff changeset
353 my ($rpkm, $pirna, $mirna, $bonafide) = (0,0,0,0);
198009598544 Uploaded
romaingred
parents:
diff changeset
354
198009598544 Uploaded
romaingred
parents:
diff changeset
355 $rpkm = ( $counthashR->{$k} * 1000000000) / ( $sizehashR->{$k} * $mapped) if ( $sizehashR->{$k} * $mapped) != 0 ;
198009598544 Uploaded
romaingred
parents:
diff changeset
356 print $out $k."\t".$counthashR->{$k}."\t"; printf $out "%.2f",$rpkm;
198009598544 Uploaded
romaingred
parents:
diff changeset
357
198009598544 Uploaded
romaingred
parents:
diff changeset
358 if ($piRNA_number != 0 )
198009598544 Uploaded
romaingred
parents:
diff changeset
359 {
198009598544 Uploaded
romaingred
parents:
diff changeset
360 $pirna = ( $counthashR->{$k} * 1000000) / $piRNA_number;
198009598544 Uploaded
romaingred
parents:
diff changeset
361 printf $out "\t%.2f",$pirna;
198009598544 Uploaded
romaingred
parents:
diff changeset
362 }
198009598544 Uploaded
romaingred
parents:
diff changeset
363 if ($miRNA_number != 0 )
198009598544 Uploaded
romaingred
parents:
diff changeset
364 {
198009598544 Uploaded
romaingred
parents:
diff changeset
365 $mirna = ( $counthashR->{$k} * 1000000) / $miRNA_number;
198009598544 Uploaded
romaingred
parents:
diff changeset
366 printf $out "\t%.2f",$mirna;
198009598544 Uploaded
romaingred
parents:
diff changeset
367 }
198009598544 Uploaded
romaingred
parents:
diff changeset
368 if ($bonafide_number != 0 )
198009598544 Uploaded
romaingred
parents:
diff changeset
369 {
198009598544 Uploaded
romaingred
parents:
diff changeset
370 $bonafide = ( $counthashR->{$k} * 1000000) / $bonafide_number;
198009598544 Uploaded
romaingred
parents:
diff changeset
371 printf $out "\t%.2f",$bonafide;
198009598544 Uploaded
romaingred
parents:
diff changeset
372 }
198009598544 Uploaded
romaingred
parents:
diff changeset
373 print $out "\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
374 }
198009598544 Uploaded
romaingred
parents:
diff changeset
375 close $out;
198009598544 Uploaded
romaingred
parents:
diff changeset
376 }
198009598544 Uploaded
romaingred
parents:
diff changeset
377
198009598544 Uploaded
romaingred
parents:
diff changeset
378 sub extract_sam
198009598544 Uploaded
romaingred
parents:
diff changeset
379 {
198009598544 Uploaded
romaingred
parents:
diff changeset
380 my ( $hashRef, $sam_in, $sam_out, $sam_uni_out, $fastq_out, $fastq_uni_out ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
381
198009598544 Uploaded
romaingred
parents:
diff changeset
382 open my $s_in, '<', $sam_in || die "cannot open $sam_in file $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
383
198009598544 Uploaded
romaingred
parents:
diff changeset
384 open my $f_out, '>', $fastq_out || die "cannot create $fastq_out $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
385 open my $f_uni_out, '>', $fastq_uni_out || die "cannot create $fastq_uni_out $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
386
198009598544 Uploaded
romaingred
parents:
diff changeset
387 open my $s_out, '>', $sam_out || die "cannot create $sam_out file $!\n" if defined ($hashRef);
198009598544 Uploaded
romaingred
parents:
diff changeset
388 open my $s_uni_out, '>', $sam_uni_out || die "cannot create $sam_uni_out file $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
389
198009598544 Uploaded
romaingred
parents:
diff changeset
390 my $sequence = '';
198009598544 Uploaded
romaingred
parents:
diff changeset
391 while(<$s_in>)
198009598544 Uploaded
romaingred
parents:
diff changeset
392 {
198009598544 Uploaded
romaingred
parents:
diff changeset
393 if ($_ =~ /^\@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ || $_ =~ /^\@CO\t.*/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
394 {
198009598544 Uploaded
romaingred
parents:
diff changeset
395 print $s_out $_ if defined ($hashRef);
198009598544 Uploaded
romaingred
parents:
diff changeset
396 print $s_uni_out $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
397 next;
198009598544 Uploaded
romaingred
parents:
diff changeset
398 }
198009598544 Uploaded
romaingred
parents:
diff changeset
399 my @line = split (/\t/,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
400 $sequence = $line[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
401 if ( (! defined ($hashRef) )|| ( exists $hashRef->{$sequence} && $hashRef->{$sequence} == 1 ) )
198009598544 Uploaded
romaingred
parents:
diff changeset
402 {
198009598544 Uploaded
romaingred
parents:
diff changeset
403 my $arn = $line[9];
198009598544 Uploaded
romaingred
parents:
diff changeset
404 if ($line[1] & 16)
198009598544 Uploaded
romaingred
parents:
diff changeset
405 {
198009598544 Uploaded
romaingred
parents:
diff changeset
406 $arn =reverse($arn);
198009598544 Uploaded
romaingred
parents:
diff changeset
407 $arn =~ tr/atgcuATGCU/tacgaTACGA/;
198009598544 Uploaded
romaingred
parents:
diff changeset
408 }
19
39b039d82743 Uploaded
romaingred
parents: 10
diff changeset
409
0
198009598544 Uploaded
romaingred
parents:
diff changeset
410 if ( ( $line[1] == 16 || $line[1] == 0 ) )
198009598544 Uploaded
romaingred
parents:
diff changeset
411 {
198009598544 Uploaded
romaingred
parents:
diff changeset
412 print $f_out "\@".$line[0]."\n".$arn."\n+\n".$line[10]."\n" ;
198009598544 Uploaded
romaingred
parents:
diff changeset
413 print $s_out $_ if defined ($hashRef);
198009598544 Uploaded
romaingred
parents:
diff changeset
414 if ( $line[11] eq "XT:A:U" )
198009598544 Uploaded
romaingred
parents:
diff changeset
415 {
198009598544 Uploaded
romaingred
parents:
diff changeset
416 print $f_uni_out "\@".$line[0]."\n".$arn."\n+\n".$line[10]."\n" ;
198009598544 Uploaded
romaingred
parents:
diff changeset
417 print $s_uni_out $_ ;
198009598544 Uploaded
romaingred
parents:
diff changeset
418 }
198009598544 Uploaded
romaingred
parents:
diff changeset
419 }
198009598544 Uploaded
romaingred
parents:
diff changeset
420 }
198009598544 Uploaded
romaingred
parents:
diff changeset
421 }
198009598544 Uploaded
romaingred
parents:
diff changeset
422 close $s_in; close $s_out if defined ($hashRef);
198009598544 Uploaded
romaingred
parents:
diff changeset
423 close $s_uni_out; close $f_out; close $f_uni_out;
198009598544 Uploaded
romaingred
parents:
diff changeset
424 }
198009598544 Uploaded
romaingred
parents:
diff changeset
425
198009598544 Uploaded
romaingred
parents:
diff changeset
426 sub get_fastq_seq{
198009598544 Uploaded
romaingred
parents:
diff changeset
427 my $fastq = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
428 my %hash; my $cmp = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
429
198009598544 Uploaded
romaingred
parents:
diff changeset
430 open my $fic, '<', $fastq || die "cannot open input file $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
431 while(<$fic>)
198009598544 Uploaded
romaingred
parents:
diff changeset
432 {
198009598544 Uploaded
romaingred
parents:
diff changeset
433 chomp $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
434 $cmp++;
198009598544 Uploaded
romaingred
parents:
diff changeset
435 if ($cmp % 4 == 1)
198009598544 Uploaded
romaingred
parents:
diff changeset
436 {
198009598544 Uploaded
romaingred
parents:
diff changeset
437 die "file do not contain a @ at line $cmp\n" unless ($_ =~ /^\@/ );
198009598544 Uploaded
romaingred
parents:
diff changeset
438 if ($_ =~ /^\@(.*)\s.*/) { $hash{$1} = 1;}
198009598544 Uploaded
romaingred
parents:
diff changeset
439 elsif ($_ =~ /^\@(.*)/) { $hash{$1} = 1;}
198009598544 Uploaded
romaingred
parents:
diff changeset
440 }
198009598544 Uploaded
romaingred
parents:
diff changeset
441 elsif ($cmp % 4 == 3 )
198009598544 Uploaded
romaingred
parents:
diff changeset
442 {
198009598544 Uploaded
romaingred
parents:
diff changeset
443 die "file do not contain a + at line $cmp\n" unless $_ =~ /^\+/;
198009598544 Uploaded
romaingred
parents:
diff changeset
444 }
198009598544 Uploaded
romaingred
parents:
diff changeset
445 }
198009598544 Uploaded
romaingred
parents:
diff changeset
446 close $fic;
198009598544 Uploaded
romaingred
parents:
diff changeset
447 return \%hash;
198009598544 Uploaded
romaingred
parents:
diff changeset
448 }
198009598544 Uploaded
romaingred
parents:
diff changeset
449
198009598544 Uploaded
romaingred
parents:
diff changeset
450 1;