Class: BioDSL::Fasta

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

Overview

Class for reading and writing FASTA files.

Defined Under Namespace

Classes: IO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Fasta

Returns a new instance of Fasta.


63
64
65
66
67
68
69
# File 'lib/BioDSL/fasta.rb', line 63

def initialize(io)
  @io        = io
  @seq_name  = nil
  @seq       = ''
  @got_first = nil
  @got_last  = nil
end

Instance Attribute Details

#seqObject

Returns the value of attribute seq


61
62
63
# File 'lib/BioDSL/fasta.rb', line 61

def seq
  @seq
end

#seq_nameObject

Returns the value of attribute seq_name


61
62
63
# File 'lib/BioDSL/fasta.rb', line 61

def seq_name
  @seq_name
end

Class Method Details

.open(*args) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/BioDSL/fasta.rb', line 35

def self.open(*args)
  ios = IO.open(*args)

  if block_given?
    begin
      yield new(ios)
    ensure
      ios.close
    end
  else
    return new(ios)
  end
end

.read(*args) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/BioDSL/fasta.rb', line 49

def self.read(*args)
  entries = []

  Fasta.open(*args) do |ios|
    ios.each do |entry|
      entries << entry
    end
  end

  entries
end

Instance Method Details

#eachObject


71
72
73
74
75
# File 'lib/BioDSL/fasta.rb', line 71

def each
  while (entry = next_entry)
    yield entry
  end
end

#next_entryObject

Method to get the next FASTA entry form an ios and return this as a Seq object. If no entry is found or eof then nil is returned.


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/BioDSL/fasta.rb', line 83

def next_entry
  @io.each do |line|
    line.chomp!

    next if line.empty?

    if line[0] == '>'
      if !@got_first && !@seq.empty?
        unless @seq.empty?
          fail FastaError, 'Bad FASTA format -> content before Fasta ' \
                           "header: #{@seq}"
        end
      end

      @got_first = true

      if @seq_name
        entry     = Seq.new(seq_name: @seq_name, seq: @seq)
        @seq_name = line[1..-1]
        @seq      = ''

        if @seq_name.empty?
          fail FastaError, 'Bad FASTA format -> truncated Fasta header: ' \
                           'no content after \'>\''
        end

        return entry
      else
        @seq_name = line[1..-1]

        if @seq_name.empty?
          fail FastaError, 'Bad FASTA format -> truncated Fasta header: ' \
                           ' no content after \'>\''
        end
      end
    else
      @seq << line
    end
  end

  if @seq_name
    @got_last = true
    entry     = Seq.new(seq_name: @seq_name, seq: @seq)
    @seq_name = nil
    return entry
  end

  if !@got_last && !@seq.empty?
    fail FastaError, 'Bad FASTA format -> content witout Fasta header: ' +
      @seq
  end

  nil
end

#puts(*args) ⇒ Object


77
78
79
# File 'lib/BioDSL/fasta.rb', line 77

def puts(*args)
  @io.puts(*args)
end