annotate perl/scripts/readPerContig.pl @ 1:3203097d0a70 draft

Uploaded
author stheil
date Thu, 15 Oct 2015 09:53:48 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3203097d0a70 Uploaded
stheil
parents:
diff changeset
1 #!/usr/bin/perl
3203097d0a70 Uploaded
stheil
parents:
diff changeset
2 use strict;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
3 use Data::Dumper;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
4 use Logger::Logger;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
5 use File::Basename;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
6 use Getopt::Long;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
7 use Cwd 'abs_path' ;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
8 use Tools::Fasta ;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
9 use Tools::Fastq ;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
10
3203097d0a70 Uploaded
stheil
parents:
diff changeset
11
3203097d0a70 Uploaded
stheil
parents:
diff changeset
12 my $ref='';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
13 my @p1='';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
14 my @p2='';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
15 my @read='';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
16 my $identity = 95;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
17 my $coverage = 60;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
18 my $verbosity=1;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
19 my $format;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
20 my $stat;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
21 my $blatOutput='';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
22 my $statOutput='';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
23
3203097d0a70 Uploaded
stheil
parents:
diff changeset
24 GetOptions(
3203097d0a70 Uploaded
stheil
parents:
diff changeset
25 "r=s" => \$ref,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
26 "id=i" => \$identity,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
27 "cov=i" => \$coverage,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
28 "b|blat" => \$blatOutput,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
29 "o|output=s" => \$statOutput,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
30 "f=s" => \$format,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
31 "s=s" => \$stat,
3203097d0a70 Uploaded
stheil
parents:
diff changeset
32 "v|verbosity=i" => \$verbosity
3203097d0a70 Uploaded
stheil
parents:
diff changeset
33 );
3203097d0a70 Uploaded
stheil
parents:
diff changeset
34
3203097d0a70 Uploaded
stheil
parents:
diff changeset
35 Logger::Logger->changeMode($verbosity);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
36
3203097d0a70 Uploaded
stheil
parents:
diff changeset
37 &main;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
38
3203097d0a70 Uploaded
stheil
parents:
diff changeset
39 sub main {
3203097d0a70 Uploaded
stheil
parents:
diff changeset
40 my $self = {};
3203097d0a70 Uploaded
stheil
parents:
diff changeset
41 bless $self;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
42 $self->_setOptions();
3203097d0a70 Uploaded
stheil
parents:
diff changeset
43
3203097d0a70 Uploaded
stheil
parents:
diff changeset
44 $self->_checkFormat($format);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
45
3203097d0a70 Uploaded
stheil
parents:
diff changeset
46 $self->{ref_tool} = Tools::Fasta->new(file => $self->{reference});
3203097d0a70 Uploaded
stheil
parents:
diff changeset
47
3203097d0a70 Uploaded
stheil
parents:
diff changeset
48 $self->_parseBlat();
3203097d0a70 Uploaded
stheil
parents:
diff changeset
49
3203097d0a70 Uploaded
stheil
parents:
diff changeset
50 $self->_print($self->{_statistics}, $self->{statOutput});
3203097d0a70 Uploaded
stheil
parents:
diff changeset
51 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
52
3203097d0a70 Uploaded
stheil
parents:
diff changeset
53
3203097d0a70 Uploaded
stheil
parents:
diff changeset
54 sub _parseBlat {
3203097d0a70 Uploaded
stheil
parents:
diff changeset
55 my ($self) = @_;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
56 open(PSL,$self->{blatOutput}) || $logger->logdie('Cant read psl file ' . $self->{blatOutput});
3203097d0a70 Uploaded
stheil
parents:
diff changeset
57 while(<PSL>){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
58 chomp;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
59 my @line = split(/\t/,$_);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
60 my $tSize = $line[14];
3203097d0a70 Uploaded
stheil
parents:
diff changeset
61 my $tStart = $line[15];
3203097d0a70 Uploaded
stheil
parents:
diff changeset
62 my $tEnd = $line[16];
3203097d0a70 Uploaded
stheil
parents:
diff changeset
63 my $coverage = (($tEnd-$tStart)/$tSize) * 100;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
64
3203097d0a70 Uploaded
stheil
parents:
diff changeset
65 if($coverage >= $self->{_covThreshold}){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
66 $self->{readNumber}->{$line[9]}++;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
67 $self->{totalReadNumber}++;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
68 $self->{size}->{$line[9]} = $line[10];
3203097d0a70 Uploaded
stheil
parents:
diff changeset
69 $self->{reads_size_sum}->{$line[9]} += $tSize;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
70 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
71 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
72 next;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
73 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
74 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
75 close PSL;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
76 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
77
3203097d0a70 Uploaded
stheil
parents:
diff changeset
78
3203097d0a70 Uploaded
stheil
parents:
diff changeset
79 sub _print {
3203097d0a70 Uploaded
stheil
parents:
diff changeset
80 my ($self, $mode, $statOutput) = @_;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
81 $logger->info('Printing results ...');
3203097d0a70 Uploaded
stheil
parents:
diff changeset
82
3203097d0a70 Uploaded
stheil
parents:
diff changeset
83 if(!defined $mode){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
84 $mode = 'nb_reads';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
85 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
86
3203097d0a70 Uploaded
stheil
parents:
diff changeset
87 if(!defined $statOutput){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
88 $statOutput = $self->{reference} . '.rn';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
89 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
90
3203097d0a70 Uploaded
stheil
parents:
diff changeset
91 open(OUT,">$statOutput");
3203097d0a70 Uploaded
stheil
parents:
diff changeset
92 foreach my $ref (sort(keys(%{$self->{ref_tool}->{index}}))){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
93 my $value = 0;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
94 if(defined($self->{readNumber}->{$ref})){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
95 if($mode eq 'nb_reads'){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
96 $value = $self->{readNumber}->{$ref};
3203097d0a70 Uploaded
stheil
parents:
diff changeset
97 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
98 elsif($mode eq 'rpkm'){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
99 $value = $self->{readNumber}->{$ref} / ($self->{size}->{$ref}/1000 * $self->{totalReadNumber}/1000000);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
100 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
101 elsif($mode eq 'mean_coverage'){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
102 $value = $self->{reads_size_sum}->{$ref} / $self->{size}->{$ref};
3203097d0a70 Uploaded
stheil
parents:
diff changeset
103 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
104 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
105 print OUT $ref . "\t" . $value . "\n";
3203097d0a70 Uploaded
stheil
parents:
diff changeset
106 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
107 close OUT;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
108 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
109
3203097d0a70 Uploaded
stheil
parents:
diff changeset
110
3203097d0a70 Uploaded
stheil
parents:
diff changeset
111 sub _setOptions {
3203097d0a70 Uploaded
stheil
parents:
diff changeset
112 my ($self) = @_;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
113 if(-e $ref){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
114 $self->{reference} = abs_path($ref);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
115 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
116 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
117 $logger->error('You must provide a reference.');
3203097d0a70 Uploaded
stheil
parents:
diff changeset
118 &help;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
119 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
120 if($identity >= 0 && $identity <= 100){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
121 $self->{_idThreshold} = $identity;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
122 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
123 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
124 $logger->logdie('ERROR: identity must be between 0 and 100.');
3203097d0a70 Uploaded
stheil
parents:
diff changeset
125 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
126 if($coverage >=0 && $coverage <= 100){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
127 $self->{_covThreshold} = $coverage;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
128 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
129 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
130 $logger->logdie('ERROR: coverage must be between 0 and 100.');
3203097d0a70 Uploaded
stheil
parents:
diff changeset
131 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
132 if($stat =~ /nb_reads|rpkm|mean_coverage/){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
133 $self->{_statistics} = $stat;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
134 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
135 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
136 $logdie('');
3203097d0a70 Uploaded
stheil
parents:
diff changeset
137 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
138 if($blatOutput eq ''){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
139 $self->{blatOutput} = 'blat.psl';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
140 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
141 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
142 $self->{blatOutput} = $blatOutput;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
143 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
144 if($statOutput eq ''){
3203097d0a70 Uploaded
stheil
parents:
diff changeset
145 my($filename, $dirs, $suffix) = fileparse($ref, qr/\.[^.]*/);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
146 $statOutput = $filename . '.psl';
3203097d0a70 Uploaded
stheil
parents:
diff changeset
147 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
148 else{
3203097d0a70 Uploaded
stheil
parents:
diff changeset
149 $self->{statOutput} = $statOutput;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
150 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
151 }
3203097d0a70 Uploaded
stheil
parents:
diff changeset
152
3203097d0a70 Uploaded
stheil
parents:
diff changeset
153
3203097d0a70 Uploaded
stheil
parents:
diff changeset
154 sub help {
3203097d0a70 Uploaded
stheil
parents:
diff changeset
155 my $prog = basename($0) ;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
156 print STDERR <<EOF ;
3203097d0a70 Uploaded
stheil
parents:
diff changeset
157 #### $prog ####
3203097d0a70 Uploaded
stheil
parents:
diff changeset
158 #
3203097d0a70 Uploaded
stheil
parents:
diff changeset
159 # AUTHOR: Sebastien THEIL
3203097d0a70 Uploaded
stheil
parents:
diff changeset
160 # LAST MODIF: 16/07/2015
3203097d0a70 Uploaded
stheil
parents:
diff changeset
161 # PURPOSE: This script is basically used to map reads (using blat) on contigs (or scaffolds) and count the
3203097d0a70 Uploaded
stheil
parents:
diff changeset
162 number of read per contig.
3203097d0a70 Uploaded
stheil
parents:
diff changeset
163 Input can be multiple fasta or fastq.
3203097d0a70 Uploaded
stheil
parents:
diff changeset
164 Output is a 2 column tabular file.
3203097d0a70 Uploaded
stheil
parents:
diff changeset
165
3203097d0a70 Uploaded
stheil
parents:
diff changeset
166 USAGE:
3203097d0a70 Uploaded
stheil
parents:
diff changeset
167 $prog -i singl.fastq -i singl.fasta -1 R1.fastq -2 R2.fastq ....
3203097d0a70 Uploaded
stheil
parents:
diff changeset
168
3203097d0a70 Uploaded
stheil
parents:
diff changeset
169 ### OPTIONS ###
3203097d0a70 Uploaded
stheil
parents:
diff changeset
170 -r <string> Rererence sequences file.
3203097d0a70 Uploaded
stheil
parents:
diff changeset
171 -id <int> Identity threshold. (Default: $identity)
3203097d0a70 Uploaded
stheil
parents:
diff changeset
172 -cov <int> Coverage threshold. (Default: $coverage)
3203097d0a70 Uploaded
stheil
parents:
diff changeset
173 -b <string> Blat output file path
3203097d0a70 Uploaded
stheil
parents:
diff changeset
174 -o <string> Output file path
3203097d0a70 Uploaded
stheil
parents:
diff changeset
175 -s <string> Statistics to print : nb_reads, rpkm or mean_coverage.
3203097d0a70 Uploaded
stheil
parents:
diff changeset
176 -v <int> Verbosity level. (0 -> 4).
3203097d0a70 Uploaded
stheil
parents:
diff changeset
177 EOF
3203097d0a70 Uploaded
stheil
parents:
diff changeset
178 exit(1);
3203097d0a70 Uploaded
stheil
parents:
diff changeset
179 }