annotate bin/resize.pm @ 15:16c3c1b0362a draft

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