-
Notifications
You must be signed in to change notification settings - Fork 18
/
fastq_utility.pm
100 lines (85 loc) · 2.45 KB
/
fastq_utility.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
#!/usr/bin/env perl
use strict;
use warnings;
package fastq_utility;
sub quality_encoding_coversion
{
# given quality acsii string, input offset and output offset
my $q_string=shift;
my $input_offset=shift;
my $out_offset=shift;
$q_string=~ s/(\S)/chr(ord($1)-$input_offset+$out_offset)/eg;
return($q_string);
}
sub low_complexity_filter
{
my ($seq,$cut_off_ratio) = @_;
my $seq_len = length ($seq);
my @low_complex_array=("A","T","C","G","AT","AC","AG","TA","TC","TG","CA","CT","CG","GA","GT","GC");
my $filter=0;
for my $item (@low_complex_array)
{
my $item_len = length ($item);
my $num_low_complex = $seq =~ s/$item/$item/g;
if (($num_low_complex*$item_len/$seq_len) >= $cut_off_ratio)
{
$filter=1;
last; #end for loop
}
}
return ($filter);
}
sub is_fastq {
#given a file it will return 1 for fastq, 0 for others.
my $file=shift;
my $fastq=0;
open (IN, "< $file") or die "$!\n";
my $header=<IN>;
close IN;
$fastq=1 if ($header =~ /^@/);
return $fastq;
}
sub checkQualityFormat {
# $offset_value=&checkQualityFormat($fastq_file)
# $offset_value = -1 means not proper fastq format.
my $fastq_file=shift;
# open the files
if ($fastq_file =~ /gz$/)
{
open FQ, "zcat $fastq_file | " or die $!;
}
else
{
open FQ, "<", $fastq_file or die $!;
}
# initiate
my @line;
my $l;
my $number;
my $offset;
# go thorugh the file
my $first_line=<FQ>;
if ($first_line !~ /^@/) {$offset=-1; return $offset;}
OUTER:while(<FQ>){
# if it is the line before the quality line
if($_ =~ /^\+/){
$l = <FQ>; # get the quality line
@line = split(//,$l); # divide in chars
for(my $i = 0; $i <= $#line; $i++){ # for each char
$number = ord($line[$i]); # get the number represented by the ascii char
# check if it is sanger or illumina/solexa, based on the ASCII image at http://en.wikipedia.org/wiki/FASTQ_format#Encoding
if($number > 74){ # if solexa/illumina
$offset=64;
#die "This file is solexa/illumina format\n"; # print result to terminal and die
last OUTER;
}elsif($number < 59){ # if sanger
$offset=33;
#die "This file is sanger format\n"; # print result to terminal and die
last OUTER;
}
}
}
}
return $offset;
}
1;