annotate bin/subgroups.pm @ 14:8b7000eac6a0 draft

Deleted selected files
author romaingred
date Mon, 16 Oct 2017 03:46:15 -0400
parents 198009598544
children 509d95c26dab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
198009598544 Uploaded
romaingred
parents:
diff changeset
1 package subgroups;
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 Exporter;
198009598544 Uploaded
romaingred
parents:
diff changeset
6 our @ISA = qw( Exporter );
198009598544 Uploaded
romaingred
parents:
diff changeset
7 our @EXPORT_OK = qw( &subgroups );
198009598544 Uploaded
romaingred
parents:
diff changeset
8
198009598544 Uploaded
romaingred
parents:
diff changeset
9 use POSIX;
198009598544 Uploaded
romaingred
parents:
diff changeset
10 use FindBin;
198009598544 Uploaded
romaingred
parents:
diff changeset
11 use lib $FindBin::Bin;
198009598544 Uploaded
romaingred
parents:
diff changeset
12 use align qw ( get_hash_alignment );
198009598544 Uploaded
romaingred
parents:
diff changeset
13
198009598544 Uploaded
romaingred
parents:
diff changeset
14 sub subgroups
198009598544 Uploaded
romaingred
parents:
diff changeset
15 {
198009598544 Uploaded
romaingred
parents:
diff changeset
16 my ($fin, $dir, $mis, $mis_TE, $proc, $tRNAs, $rRNAs, $snRNAs, $miRNAs, $exons, $TE, $min_si, $max_si, $min_pi, $max_pi, $report ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
17 my (@files, $sum, $pie, $repar, %ismapped, %isjunk, %repartition, @junk_ref, @all_ref );
198009598544 Uploaded
romaingred
parents:
diff changeset
18
198009598544 Uploaded
romaingred
parents:
diff changeset
19 print $report "----------------------------\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
20 print $report "Create subgroups:\nfastq_in: $fin\ndirectory_out: $dir\nmismatches: $mis\nmismatches TE: $mis_TE\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
21
198009598544 Uploaded
romaingred
parents:
diff changeset
22 mkdir $dir;
198009598544 Uploaded
romaingred
parents:
diff changeset
23 $dir = $dir.'/' unless $dir =~ /(.*)\/$/;
198009598544 Uploaded
romaingred
parents:
diff changeset
24
198009598544 Uploaded
romaingred
parents:
diff changeset
25 my $accept_miRNas = $dir.'miRNAs.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
26 my $reject_miRNAs = $dir.'miRNAs_rejected.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
27 my $sam_miRNAs = $dir.'miRNAs.sam';
198009598544 Uploaded
romaingred
parents:
diff changeset
28 my @tmp = get_hash_alignment($miRNAs, $mis, 1, 1, $accept_miRNas, $reject_miRNAs, $fin, $proc, 'miRNAs',$sam_miRNAs, $report);
198009598544 Uploaded
romaingred
parents:
diff changeset
29 my $mi = $tmp[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
30 $repartition{'miRNAs'} = $mi;
198009598544 Uploaded
romaingred
parents:
diff changeset
31
198009598544 Uploaded
romaingred
parents:
diff changeset
32 my $sam = new String::Random;
198009598544 Uploaded
romaingred
parents:
diff changeset
33 $sam = $sam->randpattern("CCcccccc");
198009598544 Uploaded
romaingred
parents:
diff changeset
34 my $reject_rRNAs = $dir.'rRNAs_rejected.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
35 @tmp = get_hash_alignment($rRNAs, $mis, 0, 1, 'NA', $reject_rRNAs, $reject_miRNAs, $proc, 'rRNAs', $sam, $report);
198009598544 Uploaded
romaingred
parents:
diff changeset
36 $repartition{'rRNAs'} = $tmp[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
37 unlink $sam, $sam.'_aln.err', $sam.'_samse.err';
198009598544 Uploaded
romaingred
parents:
diff changeset
38
198009598544 Uploaded
romaingred
parents:
diff changeset
39 $sam = new String::Random;
198009598544 Uploaded
romaingred
parents:
diff changeset
40 $sam = $sam->randpattern("CCcccccc");
198009598544 Uploaded
romaingred
parents:
diff changeset
41 my $reject_tRNAs = $dir.'tRNAs_rejected.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
42 @tmp = get_hash_alignment($tRNAs, $mis, 0, 1, 'NA', $reject_tRNAs, $reject_rRNAs, $proc, 'tRNAs', $sam, $report);
198009598544 Uploaded
romaingred
parents:
diff changeset
43 $repartition{'tRNAs'} = $tmp[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
44 unlink $sam, $sam.'_aln.err', $sam.'_samse.err';
198009598544 Uploaded
romaingred
parents:
diff changeset
45
198009598544 Uploaded
romaingred
parents:
diff changeset
46 $sam = new String::Random;
198009598544 Uploaded
romaingred
parents:
diff changeset
47 $sam = $sam->randpattern("CCcccccc");
198009598544 Uploaded
romaingred
parents:
diff changeset
48 my $bonafide = $dir.'bonafide_reads.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
49 @tmp = get_hash_alignment($snRNAs, $mis, 0, 1, 'NA', $bonafide, $reject_tRNAs, $proc, 'snRNAs', $sam, $report);
198009598544 Uploaded
romaingred
parents:
diff changeset
50 $repartition{'snRNAs'} = $tmp[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
51 my $bo = $tmp[1];
198009598544 Uploaded
romaingred
parents:
diff changeset
52 unlink $sam, $sam.'_aln.err', $sam.'_samse.err';
198009598544 Uploaded
romaingred
parents:
diff changeset
53
198009598544 Uploaded
romaingred
parents:
diff changeset
54 my $sam_exons = $dir.'exons.sam';
198009598544 Uploaded
romaingred
parents:
diff changeset
55 my $reject_exons = $dir.'rejected_exons.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
56 @tmp = get_hash_alignment($exons, $mis, 0, 1, 'NA', $reject_exons, $bonafide, $proc, 'exons', $sam_exons, $report, $dir.'exons.fai');
198009598544 Uploaded
romaingred
parents:
diff changeset
57 $repartition{'exons'} = $tmp[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
58
198009598544 Uploaded
romaingred
parents:
diff changeset
59
198009598544 Uploaded
romaingred
parents:
diff changeset
60 my $sam_TEs = $dir.'TEs.sam';
198009598544 Uploaded
romaingred
parents:
diff changeset
61 my $reject_TEs = $dir.'rejected.fastq';
198009598544 Uploaded
romaingred
parents:
diff changeset
62 @tmp = get_hash_alignment($TE, $mis_TE, 0, 1, 'NA', $reject_TEs, $reject_exons, $proc, 'TEs', $sam_TEs, $report, $dir.'TEs.fai' );
198009598544 Uploaded
romaingred
parents:
diff changeset
63 $repartition{'TEs'} = $tmp[0] ; $repartition{'others'} = $tmp[1];
198009598544 Uploaded
romaingred
parents:
diff changeset
64 unlink $sam, $sam.'_aln.err', $sam.'_samse.err';
198009598544 Uploaded
romaingred
parents:
diff changeset
65 unlink $reject_exons;
198009598544 Uploaded
romaingred
parents:
diff changeset
66 unlink $reject_rRNAs;
198009598544 Uploaded
romaingred
parents:
diff changeset
67 unlink $reject_miRNAs;
198009598544 Uploaded
romaingred
parents:
diff changeset
68 unlink $reject_tRNAs;
198009598544 Uploaded
romaingred
parents:
diff changeset
69
198009598544 Uploaded
romaingred
parents:
diff changeset
70 #create repartition
198009598544 Uploaded
romaingred
parents:
diff changeset
71 my $pi = fastqSubgroups($bonafide, $dir, $min_si, $max_si, $min_pi, $max_pi );
198009598544 Uploaded
romaingred
parents:
diff changeset
72
198009598544 Uploaded
romaingred
parents:
diff changeset
73 open (my $re, '>'.$dir.'repartition.txt') || die "cannot open $dir repartition.txt $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
74 print $re "type\tnumber\tpercentage\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
75 $sum += $_ foreach values %repartition;
198009598544 Uploaded
romaingred
parents:
diff changeset
76 foreach my $k ( sort keys %repartition )
198009598544 Uploaded
romaingred
parents:
diff changeset
77 {
198009598544 Uploaded
romaingred
parents:
diff changeset
78 my $prct = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
79 $prct = $repartition{$k} / $sum * 100 if $sum != 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
80 print $re "$k\t$repartition{$k}\t"; printf $re "%.2f\n",$prct;
198009598544 Uploaded
romaingred
parents:
diff changeset
81 }
198009598544 Uploaded
romaingred
parents:
diff changeset
82 return ( $bo, $mi, $pi);
198009598544 Uploaded
romaingred
parents:
diff changeset
83 }
198009598544 Uploaded
romaingred
parents:
diff changeset
84
198009598544 Uploaded
romaingred
parents:
diff changeset
85 sub fastqSubgroups
198009598544 Uploaded
romaingred
parents:
diff changeset
86 {
198009598544 Uploaded
romaingred
parents:
diff changeset
87 my ( $fastq, $output_directory, $min_si, $max_si, $min_pi, $max_pi ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
88 my $fastq_siRNA = $output_directory."siRNAs.fastq";
198009598544 Uploaded
romaingred
parents:
diff changeset
89 my $fastq_piRNA = $output_directory."piRNAs.fastq";
198009598544 Uploaded
romaingred
parents:
diff changeset
90
198009598544 Uploaded
romaingred
parents:
diff changeset
91 open my $fic, '<', $fastq || die "cannot open input file $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
92 open my $si, '>', $fastq_siRNA || die "cannot open siRNA.fastq $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
93 open my $pi, '>', $fastq_piRNA || die "cannot open piRNA.fastq $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
94
198009598544 Uploaded
romaingred
parents:
diff changeset
95 my ($length, $cmp, $type, $siRNA_number, $miRNA_h_number, $piRNA_number, $not_pi_number) = (0,0,0,0,0,0,0);
198009598544 Uploaded
romaingred
parents:
diff changeset
96 my (@fastq) =(); my $seq_name;
198009598544 Uploaded
romaingred
parents:
diff changeset
97 my $out;
198009598544 Uploaded
romaingred
parents:
diff changeset
98 while(<$fic>)
198009598544 Uploaded
romaingred
parents:
diff changeset
99 {
198009598544 Uploaded
romaingred
parents:
diff changeset
100 chomp $_;
198009598544 Uploaded
romaingred
parents:
diff changeset
101 $cmp++;
198009598544 Uploaded
romaingred
parents:
diff changeset
102 if ($cmp == 1)
198009598544 Uploaded
romaingred
parents:
diff changeset
103 {
198009598544 Uploaded
romaingred
parents:
diff changeset
104 die "file do not contain a @ at line $cmp\n" unless ($_ =~ /^\@/ );
198009598544 Uploaded
romaingred
parents:
diff changeset
105 $type = 0; @fastq = ();
198009598544 Uploaded
romaingred
parents:
diff changeset
106 if ($_ =~ /^\@(.*)\s.*/) { $seq_name = $1;}
198009598544 Uploaded
romaingred
parents:
diff changeset
107 elsif ($_ =~ /^\@(.*)/) {$seq_name = $1;}
198009598544 Uploaded
romaingred
parents:
diff changeset
108 push(@fastq,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
109 }
198009598544 Uploaded
romaingred
parents:
diff changeset
110 elsif ($cmp == 2)
198009598544 Uploaded
romaingred
parents:
diff changeset
111 {
198009598544 Uploaded
romaingred
parents:
diff changeset
112 #die "unrecognized symbol at line $cmp\n" unless $_ =~ /[atcgATCGnN]+/;
198009598544 Uploaded
romaingred
parents:
diff changeset
113 push(@fastq,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
114 $length = length($_);
198009598544 Uploaded
romaingred
parents:
diff changeset
115 $type = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
116 if ( $length >= $min_si && $length <= $max_si )
198009598544 Uploaded
romaingred
parents:
diff changeset
117 {
198009598544 Uploaded
romaingred
parents:
diff changeset
118 $type = 2;
198009598544 Uploaded
romaingred
parents:
diff changeset
119 $siRNA_number++;
198009598544 Uploaded
romaingred
parents:
diff changeset
120 }
198009598544 Uploaded
romaingred
parents:
diff changeset
121 if ($length >= $min_pi && $length <= $max_pi )
198009598544 Uploaded
romaingred
parents:
diff changeset
122 {
198009598544 Uploaded
romaingred
parents:
diff changeset
123 $type += 4;
198009598544 Uploaded
romaingred
parents:
diff changeset
124 $piRNA_number++;
198009598544 Uploaded
romaingred
parents:
diff changeset
125 }
198009598544 Uploaded
romaingred
parents:
diff changeset
126 }
198009598544 Uploaded
romaingred
parents:
diff changeset
127 elsif ($cmp == 3 )
198009598544 Uploaded
romaingred
parents:
diff changeset
128 {
198009598544 Uploaded
romaingred
parents:
diff changeset
129 die "file do not contain a + at line $cmp\n" unless $_ =~ /^\+/;
198009598544 Uploaded
romaingred
parents:
diff changeset
130 push(@fastq,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
131 }
198009598544 Uploaded
romaingred
parents:
diff changeset
132 elsif ($cmp == 4 )
198009598544 Uploaded
romaingred
parents:
diff changeset
133 {
198009598544 Uploaded
romaingred
parents:
diff changeset
134 push(@fastq,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
135 $cmp = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
136 if ($type != 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
137 {
198009598544 Uploaded
romaingred
parents:
diff changeset
138 if ($type & 4 ) { foreach my $t (@fastq) { print $pi $t."\n";} }
198009598544 Uploaded
romaingred
parents:
diff changeset
139 if ($type & 2 ) { foreach my $t (@fastq) { print $si $t."\n";} }
198009598544 Uploaded
romaingred
parents:
diff changeset
140 }
198009598544 Uploaded
romaingred
parents:
diff changeset
141 }
198009598544 Uploaded
romaingred
parents:
diff changeset
142 }
198009598544 Uploaded
romaingred
parents:
diff changeset
143 close $fic;
198009598544 Uploaded
romaingred
parents:
diff changeset
144 close $si; close $pi;
198009598544 Uploaded
romaingred
parents:
diff changeset
145 return ($piRNA_number);
198009598544 Uploaded
romaingred
parents:
diff changeset
146 }
198009598544 Uploaded
romaingred
parents:
diff changeset
147
198009598544 Uploaded
romaingred
parents:
diff changeset
148 1;