Class: Ms::Fasta::Entry

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

Overview

Entry represents a FASTA formatted entry.

entry = Entry.parse %q{
>gi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]
LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV
EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG
LLILILLLLLLALLSPDMLGDPDNHMPADPLNTPLHIKPEWYFLFAYAILRSVPNKLGGVLALFLSIVIL
GLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSIILAFLPIAGX
IENY
}.strip

entry.header[0,30]       # => 'gi|5524211|gb|AAD44166.1| cyto'
entry.sequence[0,30]     # => 'LCLYTHIGRNIYYGSYLYSETWNTGIMLLL'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header = "", sequence = "") ⇒ Entry

Returns a new instance of Entry.



39
40
41
42
# File 'lib/ms/fasta/entry.rb', line 39

def initialize(header="", sequence="")
  @header = header
  @sequence = sequence
end

Instance Attribute Details

#headerObject

The header for self



34
35
36
# File 'lib/ms/fasta/entry.rb', line 34

def header
  @header
end

#sequenceObject

The sequence of self



37
38
39
# File 'lib/ms/fasta/entry.rb', line 37

def sequence
  @sequence
end

Class Method Details

.parse(str) ⇒ Object

Parses the entry string into a Fasta::Entry. The entry string must be well-formatted, ie begin with ‘>’.



23
24
25
26
27
28
29
30
# File 'lib/ms/fasta/entry.rb', line 23

def parse(str)
  unless str[0] == ?>
    raise "input should begin with '>'"
  end

  seq_begin = str.index(/\r?\n/)
  Entry.new(str[1, seq_begin-1], str[seq_begin, str.length - seq_begin].gsub(/\r?\n/, ""))
end

Instance Method Details

#dump(target = "", options = {}) ⇒ Object

Formats and dumps self to the target. Use the options to modify the output:

line_length

the length of each output line (default 70)



62
63
64
65
66
67
# File 'lib/ms/fasta/entry.rb', line 62

def dump(target="", options={})
  line_length = options.has_key?(:line_length) ? options[:line_length ] : 70
  target << self.lines(line_length).join("\n")
  target << "\n"
  target
end

#lines(line_length = 70) ⇒ Object

Returns the header and sequence formated into lines of line_length or less. The ‘>’ delimiter is added to the header line.

Raises:

  • (ArgumentError)


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

def lines(line_length=70)
  raise ArgumentError.new("line length must be greater than 0") unless line_length > 0

  lines = [">#{header}"]
  current = 0
  while current < sequence.length
    lines << sequence[current, line_length]
    current += line_length
  end

  lines
end

#to_sObject

Returns self formatted as a string



70
71
72
# File 'lib/ms/fasta/entry.rb', line 70

def to_s
  dump
end