annotate bin/resize.pm @ 40:4bc00caa60b4 draft

Uploaded
author pierre.pouchin
date Thu, 12 Jul 2018 08:27:08 -0400
parents 1df6aaac800e
children 9185ca0a7b43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
1 package resize;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
2
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
3 use strict;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
4 use warnings;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
5
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
6 use FindBin;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
7 use lib $FindBin::Bin;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
8 use Rcall qw ( histogram );
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
9
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
10 use Exporter;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
11 our @ISA = qw( Exporter );
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
12 our @EXPORT_OK = qw( &size_distribution );
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
13
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
14 sub size_distribution
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
15 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
16 my ( $fastq, $fastq_out, $dir, $min, $max ) = @_;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
17
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
18 my ( %fragments_size, %duplicates ) ;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
19 my $num = size($min, $max, $fastq, $fastq_out, \%fragments_size, \%duplicates);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
20
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
21 my $png = $dir.'histogram.png';
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
22 histogram(\%fragments_size, $png, $num);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
23
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
24 my $size = $dir.'reads_size.txt';
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
25
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
26
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
27 my $pourcentage;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
28 open my $o, '>', $size || die "cannot open $size $!\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
29 print $o "size\tnumber\tpercentage\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
30 foreach my $k (sort { $a <=> $b } keys %fragments_size )
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
31 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
32 $pourcentage = $fragments_size{$k} / $num * 100;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
33
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
34 print $o "$k\t$fragments_size{$k}\t";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
35 printf $o "%.2f\n",$pourcentage;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
36 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
37 close $o;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
38
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
39 my $dup = $dir.'duplicates.txt' ;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
40 open $o, '>', $dup || die "cannot open $size $!\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
41 print $o "size\tnumber\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
42 foreach my $k (sort { $duplicates{$b} <=> $duplicates{$a} } keys %duplicates )
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
43 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
44 print $o "$k\t$duplicates{$k}\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
45 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
46 close $o;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
47 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
48
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
49 sub size
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
50 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
51 my ($min, $max, $in_file, $out_file, $sizeHashR, $duplicateHashR) = @_;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
52 my ($numreads, $size, $cmp, $ok, $line) = (0, 0, 0, 0);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
53 my @fastq;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
54 open (my $in, $in_file) || die "cannot open $in_file $!\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
55 open (my $out, ">".$out_file) || die "cannot create $out_file $!\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
56 while(<$in>)
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
57 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
58 chomp $_;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
59 $cmp++; $line++;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
60 if ($cmp == 1)
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
61 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
62 die "file do not contain a @ at line $line\n" unless ($_ =~ /^\@/ );
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
63 $ok = 0; @fastq = ();
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
64 push(@fastq,$_);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
65 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
66 elsif ($cmp == 2)
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
67 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
68 #die "unrecognized symbol at line $line\n" unless ($_ =~ /[atcgATCGnN]+/ || $_ =~ /^$/ );
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
69 push(@fastq,$_);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
70 $size = length($_);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
71 if ($size >= $min && $size <= $max)
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
72 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
73 $numreads++;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
74 ${$sizeHashR}{$size}+=1;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
75 ${$duplicateHashR}{$_}+=1 if (defined($duplicateHashR));
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
76 $ok = 1;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
77 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
78 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
79 elsif ($cmp == 3 )
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
80 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
81 die "file do not contain a + at line $line\n" unless $_ =~ /^\+/;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
82 push(@fastq,$_);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
83 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
84 elsif ($cmp == 4 )
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
85 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
86 push(@fastq,$_);
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
87 $cmp = 0;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
88 if ($ok == 1)
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
89 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
90 foreach my $t (@fastq)
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
91 {
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
92 print $out $t."\n";
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
93 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
94 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
95 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
96 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
97 close $in; close $out;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
98 return $numreads;
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
99 }
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
100
4bc00caa60b4 Uploaded
pierre.pouchin
parents: 1
diff changeset
101 1;