Class: BioDSL::Fasta

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

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.



60
61
62
63
64
65
66
# File 'lib/BioDSL/fasta.rb', line 60

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.



58
59
60
# File 'lib/BioDSL/fasta.rb', line 58

def seq
  @seq
end

#seq_nameObject

Returns the value of attribute seq_name.



58
59
60
# File 'lib/BioDSL/fasta.rb', line 58

def seq_name
  @seq_name
end

Class Method Details

.open(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/BioDSL/fasta.rb', line 32

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

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

.read(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/BioDSL/fasta.rb', line 46

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

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

  entries
end

Instance Method Details

#eachObject



68
69
70
71
72
# File 'lib/BioDSL/fasta.rb', line 68

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.



80
81
82
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
# File 'lib/BioDSL/fasta.rb', line 80

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

    next if line.empty?

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

      @got_first = true

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

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

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

        raise FastaError, "Bad FASTA format -> truncated Fasta header: no content after '>'" if @seq_name.empty?
      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 not @got_last and not @seq.empty?
    raise FastaError, "Bad FASTA format -> content witout Fasta header: #{@seq}"
  end

  nil
end

#puts(*args) ⇒ Object



74
75
76
# File 'lib/BioDSL/fasta.rb', line 74

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