Class: Forage

Inherits:
Object
  • Object
show all
Defined in:
lib/forage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#handleObject

Returns the value of attribute handle.



2
3
4
# File 'lib/forage.rb', line 2

def handle
  @handle
end

#iupacObject

Returns the value of attribute iupac.



2
3
4
# File 'lib/forage.rb', line 2

def iupac
  @iupac
end

#regexObject

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

#eachObject

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

Yields:

  • ([ header, sequence ])


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