dna 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dna.rb +8 -74
- data/readme.md +6 -4
- data/spec/dna_spec.rb +3 -3
- metadata +38 -18
data/lib/dna.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'parsers', '*.rb')).each { |f| require f }
|
2
|
+
|
1
3
|
##
|
2
4
|
# Dna
|
3
5
|
#
|
4
|
-
class Dna
|
6
|
+
class Dna
|
5
7
|
include Enumerable
|
6
8
|
|
7
9
|
attr_reader :format
|
8
10
|
|
9
|
-
def initialize(handle
|
11
|
+
def initialize(handle)
|
10
12
|
@handle = handle
|
11
13
|
@format = detect_format
|
12
14
|
@iterator =
|
13
15
|
case @format
|
14
16
|
when :fasta
|
15
|
-
|
17
|
+
FastaParser.new @handle
|
16
18
|
when :fastq
|
17
|
-
|
19
|
+
FastqParser.new @handle
|
18
20
|
when :qseq
|
19
|
-
|
21
|
+
QSEQParser.new @handle
|
20
22
|
else
|
21
23
|
raise "#{@format} not supported."
|
22
24
|
end
|
@@ -39,75 +41,7 @@ class Dna # iterator
|
|
39
41
|
end
|
40
42
|
|
41
43
|
def each &block
|
42
|
-
@iterator.each
|
43
|
-
if block_given?
|
44
|
-
block.call r
|
45
|
-
else
|
46
|
-
yield r
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def fasta_parser
|
54
|
-
sequence, header = nil, nil
|
55
|
-
Enumerator.new do |enum|
|
56
|
-
@handle.each do |line|
|
57
|
-
if line[0].chr == '>'
|
58
|
-
enum.yield Fasta.new(:name => header, :sequence => sequence) if sequence
|
59
|
-
sequence = ''
|
60
|
-
header = line[1..-1].strip
|
61
|
-
else
|
62
|
-
sequence << line.strip.tr(' ','')
|
63
|
-
end
|
64
|
-
end
|
65
|
-
enum.yield Fasta.new(:name => header, :sequence => sequence)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def fastq_parser
|
70
|
-
c = (0..3).cycle
|
71
|
-
Enumerator.new do |enum|
|
72
|
-
params = { :name => nil, :sequence => nil, :quality => nil }
|
73
|
-
@handle.each do |line|
|
74
|
-
n = c.next
|
75
|
-
case n
|
76
|
-
when 0
|
77
|
-
params[:name] = line.strip[1..-1]
|
78
|
-
when 1
|
79
|
-
params[:sequence] = line.strip
|
80
|
-
when 2
|
81
|
-
nil
|
82
|
-
when 3
|
83
|
-
params[:quality] = line.strip
|
84
|
-
record = Fastq.new params
|
85
|
-
enum.yield record
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def qseq_parser
|
92
|
-
Enumerator.new do |enum|
|
93
|
-
@handle.each do |line|
|
94
|
-
line = line.strip.split("\t")
|
95
|
-
record = QSEQ.new(
|
96
|
-
:machine => line[0],
|
97
|
-
:run => line[1],
|
98
|
-
:lane => line[2],
|
99
|
-
:tile => line[3],
|
100
|
-
:x => line[4],
|
101
|
-
:y => line[5],
|
102
|
-
:index => line[6],
|
103
|
-
:read_no => line[7],
|
104
|
-
:sequence => line[8],
|
105
|
-
:quality => line[9],
|
106
|
-
:filtered => line[10]
|
107
|
-
)
|
108
|
-
enum.yield record
|
109
|
-
end
|
110
|
-
end
|
44
|
+
@iterator.each(&block)
|
111
45
|
end
|
112
46
|
end
|
113
47
|
|
data/readme.md
CHANGED
@@ -14,7 +14,7 @@ Supported Formats:
|
|
14
14
|
|
15
15
|
## Installation
|
16
16
|
|
17
|
-
With Ruby 1.
|
17
|
+
With Ruby 1.8.7 or better:
|
18
18
|
|
19
19
|
`gem install dna`
|
20
20
|
|
@@ -24,8 +24,10 @@ With Ruby 1.9.2 or better:
|
|
24
24
|
|
25
25
|
require 'dna'
|
26
26
|
|
27
|
+
# format detected automatically by inspecting the contents of the file.
|
28
|
+
|
27
29
|
File.open('sequences.fasta') do |handle|
|
28
|
-
records = Dna.new handle
|
30
|
+
records = Dna.new handle
|
29
31
|
|
30
32
|
records.each do |record|
|
31
33
|
puts record.length
|
@@ -33,7 +35,7 @@ File.open('sequences.fasta') do |handle|
|
|
33
35
|
end
|
34
36
|
|
35
37
|
File.open('sequences.fastq') do |handle|
|
36
|
-
records = Dna.new handle
|
38
|
+
records = Dna.new handle
|
37
39
|
|
38
40
|
records.each do |record|
|
39
41
|
puts record.quality
|
@@ -41,7 +43,7 @@ File.open('sequences.fastq') do |handle|
|
|
41
43
|
end
|
42
44
|
|
43
45
|
File.open('sequences.qseq') do |handle|
|
44
|
-
records = Dna.new handle
|
46
|
+
records = Dna.new handle
|
45
47
|
puts records.first.inspect
|
46
48
|
end
|
47
49
|
```
|
data/spec/dna_spec.rb
CHANGED
@@ -3,9 +3,9 @@ require 'spec_helper'
|
|
3
3
|
describe Dna do
|
4
4
|
include_context "parser stuff"
|
5
5
|
|
6
|
-
let(:fasta) { Dna.new
|
7
|
-
let
|
8
|
-
let
|
6
|
+
let(:fasta) { Dna.new(@fasta_file) }
|
7
|
+
let(:fastq) { Dna.new @fastq_file }
|
8
|
+
let(:qseq) { Dna.new @qseq_file }
|
9
9
|
|
10
10
|
it 'can auto-detect fasta format' do
|
11
11
|
fasta.format == 'fasta'
|
metadata
CHANGED
@@ -1,22 +1,32 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: dna
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Austin G. Davis-Richardson
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
17
|
+
|
18
|
+
date: 2012-04-05 00:00:00 Z
|
13
19
|
dependencies: []
|
20
|
+
|
14
21
|
description: for parsing various types of DNA sequence files
|
15
22
|
email: harekrishna@gmail.com
|
16
23
|
executables: []
|
24
|
+
|
17
25
|
extensions: []
|
26
|
+
|
18
27
|
extra_rdoc_files: []
|
19
|
-
|
28
|
+
|
29
|
+
files:
|
20
30
|
- lib/dna.rb
|
21
31
|
- spec/dna_spec.rb
|
22
32
|
- spec/record_spec.rb
|
@@ -24,26 +34,36 @@ files:
|
|
24
34
|
- readme.md
|
25
35
|
homepage: http://github.com/audy/dna
|
26
36
|
licenses: []
|
37
|
+
|
27
38
|
post_install_message:
|
28
39
|
rdoc_options: []
|
29
|
-
|
40
|
+
|
41
|
+
require_paths:
|
30
42
|
- lib
|
31
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
44
|
none: false
|
33
|
-
requirements:
|
34
|
-
- -
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
|
37
|
-
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 3
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
53
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
43
61
|
requirements: []
|
62
|
+
|
44
63
|
rubyforge_project:
|
45
|
-
rubygems_version: 1.8.
|
64
|
+
rubygems_version: 1.8.21
|
46
65
|
signing_key:
|
47
66
|
specification_version: 3
|
48
67
|
summary: DNA sequence parser
|
49
68
|
test_files: []
|
69
|
+
|