annotate bin/subgroups.pm @ 3:3918492da907 draft

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