annotate bin/resize.pm @ 11:2ac343502096 draft

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