Class: Forage
- Inherits:
-
Object
- Object
- Forage
- Defined in:
- lib/forage.rb
Instance Attribute Summary collapse
-
#handle ⇒ Object
Returns the value of attribute handle.
-
#iupac ⇒ Object
Returns the value of attribute iupac.
-
#regex ⇒ Object
Returns the value of attribute regex.
Instance Method Summary collapse
-
#convert(s) ⇒ Object
convert string to regex.
-
#each ⇒ Object
return [header, [matches]].
-
#fasta {|[ header, sequence ]| ... } ⇒ Object
parse fasta-formatted data.
-
#initialize(handle, query) ⇒ Forage
constructor
A new instance of Forage.
Constructor Details
#initialize(handle, query) ⇒ Forage
Returns a new instance of Forage.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/forage.rb', line 4 def initialize(handle, query) @handle = handle @iupac = Hash.new{ |h, k| h[k] = k }.merge({ 'R' => '[AG]', 'Y' => '[CTUY]', 'S' => '[GCS]', 'W' => '[ATUW]', 'K' => '[GTUK]', 'M' => '[ACM]', 'B' => '[CGTUB]', 'D' => '[AGTUD]', 'H' => '[ACTUH]', 'V' => '[ACGV]', 'N' => '.', '-' => '.'} ) @regex = self.convert query end |
Instance Attribute Details
#handle ⇒ Object
Returns the value of attribute handle.
2 3 4 |
# File 'lib/forage.rb', line 2 def handle @handle end |
#iupac ⇒ Object
Returns the value of attribute iupac.
2 3 4 |
# File 'lib/forage.rb', line 2 def iupac @iupac end |
#regex ⇒ Object
Returns the value of attribute regex.
2 3 4 |
# File 'lib/forage.rb', line 2 def regex @regex end |
Instance Method Details
#convert(s) ⇒ Object
convert string to regex
37 38 39 40 |
# File 'lib/forage.rb', line 37 def convert(s) @regex = s.each_char.collect { |n| "#{@iupac[n]}" }.join Regexp.new @regex end |
#each ⇒ Object
return [header, [matches]]
26 27 28 29 30 31 32 33 34 |
# File 'lib/forage.rb', line 26 def each self.fasta do |record| next if record[0] == nil header = record[0] sequence = record[1] matches = sequence.scan(@regex) yield [header, matches] if matches != [] end end |
#fasta {|[ header, sequence ]| ... } ⇒ Object
parse fasta-formatted data
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/forage.rb', line 43 def fasta sequence, header = nil, nil @handle.each do |line| if line[0].chr == '>' yield [ header, sequence ] sequence = '' header = line[1..-1].strip else sequence << line.strip.tr(' ','').upcase end end yield [ header, sequence ] end |