Skip to content
Snippets Groups Projects
Commit f724bffc authored by Thomas Junier's avatar Thomas Junier
Browse files

Tests of FastaReader ok

parents
Branches
No related tags found
No related merge requests found
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
/docs/
/lib/
/bin/
/.shards/
*.dwarf
# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
language: crystal
# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
LICENSE 0 → 100644
The MIT License (MIT)
Copyright (c) 2020 your-name-here
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# fasta_reader
TODO: Write a description here
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
fasta_reader:
github: your-github-user/fasta_reader
```
2. Run `shards install`
## Usage
```crystal
require "fasta_reader"
```
TODO: Write usage instructions here
## Development
TODO: Write development instructions here
## Contributing
1. Fork it (<https://github.com/your-github-user/fasta_reader/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [your-name-here](https://github.com/your-github-user) - creator and maintainer
name: fasta_reader
version: 0.1.0
authors:
- your-name-here <your-email-here>
crystal: 0.35.1
license: MIT
require "./spec_helper"
include FastaReader
describe FastaReader do
# TODO: Write tests
records = [] of FastaRecord
FastaReader.each_fasta("spec/test.fasta") do |rec|
records << rec
end
it "has the right number of records" do
records.size.should eq 3
end
it "has the right header for record 1" do
records[0][:hdr].should eq "ID_01"
end
it "has the right sequence for record 1" do
records[0][:seq].should eq "atgatgatgcttcttctt"
end
it "has the right header for record 2" do
records[1][:hdr].should eq "ID 02"
end
it "has the right sequence for record 2" do
records[1][:seq].should eq "cgatcgacgatcgatgcacgatgaacctacgagcartcgagcatcagagcagcatcgagcagctacgagatt"
end
it "has the right header for record 3" do
records[2][:hdr].should eq "XPQ_LATCH|Latimeria chalumnae unknown protein"
end
it "has the right sequence for record 3" do
records[2][:seq].should eq "MERTHSNSGDKTSTVSLDVLSRHSHSSVNSTSDHTSASKRSVHDRFHSTCSCM"
end
end
require "spec"
require "../src/fasta_reader"
>ID_01
atgatgatg
cttcttctt
>ID 02
cgatcgacgatcgatgcacgatga
acctacgagcartcgagcatcaga
gcagcatcgagcagctacgagatt
>XPQ_LATCH|Latimeria chalumnae unknown protein
MERTHSNSGDKTSTVSLDV
LSRHSHSSVNSTSDHTS
ASKRSVHDRFHSTCSCM
# TODO: Write documentation for `FastaReader`
module FastaReader
VERSION = "0.1.0"
extend self
alias FastaRecord = NamedTuple(hdr: String, seq: String)
def each_fasta(fasta_fname : String, &block : FastaRecord -> Nil)
File.open(fasta_fname) do |fasta|
header = nil
sequence = ""
fasta.each_line do |line|
first_char = line[0, 1]
case first_char
when ">"
if header
rec = {hdr: header[1..], seq: sequence}
yield rec
end
header = line
sequence = ""
when /[a-zA-Z-]/
sequence = sequence.not_nil! + line
else
raise "Unexpected residue character \"#{first_char}\""
end
end
# yield the last entry
if header
rec = {hdr: header[1..], seq: sequence}
yield rec
end
end
end
end
module FastaReader
extend self
alias FastaRecord = NamedTuple{hdr: String, seq: String}
def each_fasta(fasta_fname : String, &block : Entry -> Nil)
File.open(fasta_fname) do |fasta|
header = nil
sequence = ""
fasta.each_line do |line|
first_char = line[0, 1]
case first_char
when ">"
if header
rec = {hdr: header, seq: sequence}
yield rec
end
header = line
sequence = ""
when /[a-zA-Z-]/
sequence = sequence.not_nil! + line
else
raise "Unexpected residue character \"#{first_char}\""
end
end
# yield the last entry
if header
rec = {hdr: header, seq: sequence}
yield rec
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment