File: Read.pm

package info (click to toggle)
hisat2 2.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,448 kB
  • sloc: cpp: 97,109; python: 11,075; perl: 7,279; sh: 2,328; ansic: 1,458; makefile: 532; javascript: 273; java: 116
file content (178 lines) | stat: -rw-r--r-- 5,003 bytes parent folder | download | duplicates (3)
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
172
173
174
175
176
177
178
#!/usr/bin/perl -w

#
# Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
#
# This file is part of Bowtie 2.
#
# Bowtie 2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bowtie 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bowtie 2.  If not, see <http://www.gnu.org/licenses/>.
#

package Read;
use strict;
use Carp;
use FindBin qw($Bin); 
use lib $Bin;
use DNA;
use Test;

sub new {
	my ($class, $name, $seq, $qual, $color, $fw, $orig) = @_;
	$name = "noname" unless defined($name);
	return bless {
		_name   => $name,
		_seq    => $seq   || croak("No sequence"),
		_qual   => $qual  || croak("No qualities"),
		_color  => $color || 0,
		_fw     => $fw    || croak("No orientation"),
		_orig   => $orig  || croak("No original read string")
	}, $class;
}
sub name  { return $_[0]->{_name}  }
sub seq   { return $_[0]->{_seq}   }
sub qual  { return $_[0]->{_qual}  }
sub color { return $_[0]->{_color} }
sub fw    { return $_[0]->{_fw}    }
sub orig  { return $_[0]->{_orig}  }
sub len   { return length($_[0]->seq()) }

##
# Obtain a character from the read.
#
sub at {
	my ($self, $off, $ori) = @_;
	my ($c, $q) = "";
	if($ori eq "RtL") {
		$c = uc substr($self->seq(), -$off-1, 1);
		$q = substr($self->qual(), -$off-1, 1);
	} else {
		$c = uc substr($self->seq(), $off, 1);
		$q = substr($self->qual(), $off, 1);
	}
	length($c) == 1 || die;
	return ($c, $q);
}

##
# Load a set of FASTQ reads into the given reads array.
#
sub fromFastq {
	my ($fh, $color, $reads) = @_;
	$reads = [] unless defined($reads);
	while(<$fh>) {
		my $l1 = $_;
		my $l2 = <$fh>; defined($l2) || croak("Name line followed by EOF");
		my $l3 = <$fh>; defined($l3) || croak("Sequence line followed by EOF");
		my $l4 = <$fh>; defined($l4) || croak("Name2 line followed by EOF");
		my $orig = "$l1$l2$l3$l4";
		chomp($l1); chomp($l2); chomp($l3); chomp($l4);
		push @{$reads}, Read->new(substr($l1, 1), $l2, $l4, $color, "FW", $orig);
	}
	return $reads;
}

##
# Load a set of FASTQ reads into the given reads array.
#
sub fromFastqs {
	my ($fqs, $color, $reads) = @_;
	$reads = [] unless defined($reads);
	for my $f (@$fqs) {
		my $fqfh;
		open($fqfh, $f =~ /\.gz$/ ? "gzip -dc $f |" : "$f") || croak("Could not open $f for reading");
		fromFastq($fqfh, $color, $reads);
		close($fqfh);
	}
	return $reads;
}

##
# Load a set of FASTA reads into the given reads array.
#
sub fromFasta {
	my ($fh, $color, $reads) = @_;
	$reads = [] unless defined($reads);
	while(<$fh>) {
		my $l1 = $_;
		my $l2 = <$fh>; defined($l2) || croak("Name line followed by EOF");
		my $orig = "$l1$l2";
		chomp($l1); chomp($l2);
		my $qual = "I" x length($l2);
		push @{$reads}, Read->new(substr($l1, 1), $l2, $qual, $color, "FW", $orig);
	}
	return $reads;
}

##
# Load a set of FASTA reads into the given reads array.
#
sub fromFastas {
	my ($fas, $color, $reads) = @_;
	$reads = [] unless defined($reads);
	for my $f (@$fas) {
		my $fafh;
		open($fafh, $f =~ /\.gz$/ ? "gzip -dc $f |" : "$f") || croak("Could not open $f for reading");
		fromFasta($fafh, $color, $reads);
		close($fafh);
	}
	return $reads;
}

##
# Load a set of FASTA reads into the given reads array.
#
sub fromStrings {
	my ($strs, $color, $reads) = @_;
	$reads = [] unless defined($reads);
	my $idx = 0;
	for my $str (@$strs) {
		my $qual = "I" x length($str);
		push @{$reads}, new Read($idx, $str, $qual, $color, "FW", $str);
		$idx++;
	}
	return $reads;
}

sub test1 {
	my $r = new Read("blah", "TTACGAACCACAACGTATCG", "I"x20, 0, "FW", "?");
	my ($c, $q) = $r->at(0, "LtR");
	($c eq "T" && $q eq "I") || croak("Expected (T, I), got ($c, $q)\n");
	($c, $q) = $r->at(0, "RtL");
	($c eq "G" && $q eq "I") || croak("Expected (G, I), got ($c, $q)\n");
	($c, $q) = $r->at(1, "LtR");
	($c eq "T" && $q eq "I") || croak("Expected (T, I), got ($c, $q)\n");
	($c, $q) = $r->at(1, "RtL");
	($c eq "C" && $q eq "I") || croak("Expected (C, I), got ($c, $q)\n");
	return 1;
}

sub test2 {
	my $rs = fromStrings(["ACGATGCTACG", "TGACGATGCTAG"], 0);
	$rs->[0]->seq()  eq "ACGATGCTACG"  || croak($rs->[0]->seq());
	$rs->[0]->qual() eq "IIIIIIIIIII"  || croak($rs->[0]->qual());
	$rs->[0]->name() eq "0"            || croak($rs->[0]->name());
	$rs->[1]->seq()  eq "TGACGATGCTAG" || croak($rs->[1]->seq());
	$rs->[1]->qual() eq "IIIIIIIIIIII" || croak($rs->[1]->qual());
	$rs->[1]->name() eq "1"            || croak($rs->[1]->name());
	return 1;
}

if($0 =~ /Read\.pm$/) {
	print "Running unit tests\n";
	# Run unit tests
	print "Test \"test1\"..."; test1(); print "PASSED\n";
	print "Test \"test2\"..."; test2(); print "PASSED\n";
}

1;