Commit 3124ad82 authored by Thomas Junier's avatar Thomas Junier
Browse files

each_fasta() now has argument 'uppercase' (def: false); if true sequence is folded to uppercase

parent 5fa86f2f
......@@ -29,6 +29,31 @@ describe FastaReader do
it "has the right sequence for record 3" do
records[2][:seq].should eq "MERTHSNSGDKTSTVSLDVLSRHSHSSVNSTSDHTSASKRSVHDRFHSTCSCM"
end
uc_records = [] of FastaRecord
FastaReader.each_fasta("spec/test.fasta", uppercase: true) do |rec|
uc_records << rec
end
it "has the right number of records" do
uc_records.size.should eq 3
end
it "has the right header for record 1" do
uc_records[0][:hdr].should eq "ID_01"
end
it "has the right sequence for record 1" do
uc_records[0][:seq].should eq "ATGATGATGCTTCTTCTT"
end
it "has the right header for record 2" do
uc_records[1][:hdr].should eq "ID 02"
end
it "has the right sequence for record 2" do
uc_records[1][:seq].should eq "CGATCGACGATCGATGCACGATGAACCTACGAGCARTCGAGCATCAGAGCAGCATCGAGCAGCTACGAGATT"
end
it "has the right header for record 3" do
uc_records[2][:hdr].should eq "XPQ_LATCH|Latimeria chalumnae unknown protein"
end
it "has the right sequence for record 3" do
uc_records[2][:seq].should eq "MERTHSNSGDKTSTVSLDVLSRHSHSSVNSTSDHTSASKRSVHDRFHSTCSCM"
end
end
describe "#each_fasta_with_index" do
......
......@@ -16,10 +16,12 @@ module FastaReader
# Yields a FastaRecord for each record in the Fasta file whose name is passed
# as argument. The file is closed automatically after being read.
#
# If 'uppercase' is set to true, the residue symbols are folded to uppercase.
#
# Further versions should take a File or even an IO object, allowing users to
# read Fasta from strings or input streams.
#
def each_fasta(fasta_fname : String, &block : FastaRecord -> Nil)
def each_fasta(fasta_fname : String, uppercase : Bool = false, &block : FastaRecord -> Nil)
File.open(fasta_fname) do |fasta|
header = nil
sequence = ""
......@@ -29,6 +31,7 @@ module FastaReader
case first_char
when ">"
if header
sequence = sequence.upcase if uppercase
rec = {hdr: header[1..], seq: sequence}
yield rec
end
......@@ -43,6 +46,7 @@ module FastaReader
end
# yield the last entry
if header
sequence = sequence.upcase if uppercase
rec = {hdr: header[1..], seq: sequence}
yield rec
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment