forked from grubbybio/pRNASeqTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.pm
executable file
·171 lines (155 loc) · 4.05 KB
/
Function.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env perl
package Function;
use Modern::Perl;
use Function;
use File::Path qw(remove_tree make_path);
sub SRR { #download SRA files
my ($self, $srr, $thread) = @_;
if($srr =~ /([SE]RR\d+)$/ && !-e $srr){
$srr = $1;
print $main::tee "Downloading...\n";
system ("fasterq-dump -p --threads ".$thread." --split-3 ".$srr);
if(-e $srr."_1.fastq"){
unlink $srr.".fastq" if (-e $srr.".fastq");
return ($srr."_1.fastq", $srr."_2.fastq");
}else{
return ($srr.".fastq");
}
}eles{
return $srr;
}
}
sub sum {
my $self = shift @_;
my $total = 0;
foreach (@_) {
$total += $_;
}
return $total;
}
sub average {
my $self = shift @_;
my $total = Function->sum(@_);
my $average = $total / @_;
return $average;
}
sub stdev {
my $self = shift @_;
my $average = Function->average(@_);
my $sqtotal = 0;
foreach(@_) {
$sqtotal += ($average-$_) ** 2;
}
my $std = ($sqtotal / (@_-1)) ** 0.5;
return $std;
}
sub revcomp {
my ($self, $inseq) = @_;
my $revcomp = reverse($inseq);
$revcomp =~ tr/ACGTacgt/TGCAtgca/;
return $revcomp;
}
sub unzip { #seq file format check and unzip
my ($self, $file, $tag) = @_;
if(-e $file){
if($file =~ /bz2$/){
print $main::tee "Decompressing...\n";
system ("bzip2 -dc ".$file." > ".$tag.".fastq");
}elsif($file =~ /gz$/){
print $main::tee "Decompressing...\n";
system ("gzip -dc ".$file." > ".$tag.".fastq");
}elsif($file =~ /gtz$/){
print $main::tee "Decompressing...\n";
system ("gtz -dc ".$file." > ".$tag.".fastq");
}elsif($file =~ /fastq$/ or $file =~ /fq$/){
if ($file ne $tag.".fastq"){
print $main::tee "Renaming...\n";
system ("cp ".$file." ".$tag.".fastq");
unlink $file if($file =~ /^SRR/);
}else{
print $main::tee "Backing up...\n";
system ("cp ".$file." ".$tag.".fastq.bak");
}
}else{
die "Please provide the seq file in correct formats!"
}
print $main::tee "Completed!\n";
}else{
die "Please provide the correct seq file!";
}
}
sub rmvc { #remove 3' polyC and reverse compliment
my ($self, @tag) = @_;
my $trig = 1;
my ($clen, $seqname, $outseq, $outq, %rmvc, %rmvc2, $fiseq, $index);
print $main::tee "Start to remove the 3' PolyC...\n";
open TRIM, $tag[0]."_trimmed.fastq" or die $!;
while (my $row = <TRIM>){
chomp $row;
if($. % 4 == 0){
$rmvc{$seqname}{"outq"} = reverse substr($row, 0, -$clen) if(exists $rmvc{$seqname}{"outseq"});
}elsif($. % 4 == 2){
if($row =~ /^([A-Z]+[ATG])(C+)$/){
$rmvc{$seqname}{"outseq"} = Function->revcomp($1);
$clen = length ($2);
}
}elsif($. % 4 == 1){
($seqname, $index) = split / /, $row;
}
}
close TRIM;
if($tag[1]){
open TRIM, $tag[1]."_trimmed.fastq" or die $!;
while (my $row = <TRIM>){
chomp $row;
if($. % 4 == 0){
$rmvc2{$seqname}{"outq"} = reverse substr($row,$clen) if(exists $rmvc2{$seqname}{"outseq"});
}elsif($. % 4 == 2){
if($row =~ /^(G+)([ATC][A-Z]+)$/){
$clen = length ($1);
$rmvc2{$seqname}{"outseq"} = Function->revcomp($2);
}
}elsif($. % 4 == 1){
($seqname, $index) = split / /, $row;
}
}
close TRIM;
}
open COUT, ">".$tag[0].".fastq" or die $!;
open COUT2, ">".$tag[1].".fastq" or die $! if($tag[1]);
foreach $seqname (keys %rmvc){
if($tag[1]){
if(exists $rmvc{$seqname}{"outq"} and exists $rmvc2{$seqname}{"outq"}){
print COUT "$seqname\n$rmvc{$seqname}{outseq}\n+\n$rmvc{$seqname}{outq}\n";
print COUT2 "$seqname\n$rmvc2{$seqname}{outseq}\n+\n$rmvc2{$seqname}{outq}\n";
}
}else{
if(exists $rmvc{$seqname}{"outq"}){
print COUT "$seqname\n$rmvc{$seqname}{outseq}\n+\n$rmvc{$seqname}{outq}\n";
}
}
}
close COUT;
close COUT2 if($tag[1]);
print STDERR "The 3' PolyC is removed!\n";
}
sub fastq2fasta {
my ($self, $input, $output) = $_;
my $nr = 1;
open INPUT, "<$input" or die $!;
open OUTPUT, ">$output" or die $!;
while(my $row = <INPUT>){
chomp $row;
if($nr == 1 or $nr == 2){
$row = s/^@/>/;
print OUTPUT "$row\n";
}elsif($nr == 4){
$nr = 0;
}
$nr ++;
}
close INPUT;
close OUTPUT;
}
1;
__END__