Class: ParseFasta::Record

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Record

TODO:

This is destructive with respect to the input seq arg. Does it need to be?

The constructor takes keyword args.

Examples:

Init a new Record object for a fastA record

Record.new header: "apple", seq: "actg"

Init a new Record object for a fastA record without checking for ‘>’ in the sequence.

Record.new header: "apple", seq: "pie>good", check_fasta_seq: false

Init a new Record object for a fastQ record

Record.new header: "apple", seq: "actd", desc: "", qual: "IIII"

Parameters:

  • header (String)

    the header of the record

  • seq (String)

    the sequence of the record

  • desc (String)

    the description line of a fastQ record

  • qual (String)

    the quality string of a fastQ record

  • check_fasta_seq (Bool)

    Pass false if you don’t want to check for ‘>’ characters in the sequence. Defaults to true, which checks for ‘>’ in the sequence and raises an error.

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/parse_fasta/record.rb', line 43

def initialize args = {}
  @header = args.fetch :header
  @id = @header.split(" ")[0]

  @desc = args.fetch :desc, nil
  @qual = args.fetch :qual, nil

  @qual.tr!(" \t\n\r", "") if @qual

  seq = args.fetch(:seq)
  seq.tr!(" \t\n\r", "")

  do_check_fasta_seq = args.fetch :check_fasta_seq, true

  if fastq? || (!fastq? && !do_check_fasta_seq)
    @seq = seq
  else
    @seq = check_fasta_seq(seq)
  end
end

Instance Attribute Details

#descString or Nil

Returns if the record is from a fastA file, it is nil; else, the description line of the fastQ record.

Returns:

  • (String or Nil)

    if the record is from a fastA file, it is nil; else, the description line of the fastQ record



18
# File 'lib/parse_fasta/record.rb', line 18

attr_accessor :header, :id, :seq, :desc, :qual

#headerString

Returns the full header of the record without the ‘>’ or ‘@’.

Returns:

  • (String)

    the full header of the record without the ‘>’ or ‘@’



18
19
20
# File 'lib/parse_fasta/record.rb', line 18

def header
  @header
end

#idString

Returns the “id” i.e., the first token when split by whitespace.

Returns:

  • (String)

    the “id” i.e., the first token when split by whitespace



18
# File 'lib/parse_fasta/record.rb', line 18

attr_accessor :header, :id, :seq, :desc, :qual

#qualString or Nil

Returns if the record is from a fastA file, it is nil; else, the quality string of the fastQ record.

Returns:

  • (String or Nil)

    if the record is from a fastA file, it is nil; else, the quality string of the fastQ record



18
# File 'lib/parse_fasta/record.rb', line 18

attr_accessor :header, :id, :seq, :desc, :qual

#seqString

Returns the sequence of the record.

Returns:

  • (String)

    the sequence of the record



18
# File 'lib/parse_fasta/record.rb', line 18

attr_accessor :header, :id, :seq, :desc, :qual

Instance Method Details

#==(rec) ⇒ Bool

Compare attrs of this rec with another

Parameters:

  • rec (Record)

    a Record object to compare with

Returns:

  • (Bool)

    true or false



69
70
71
72
# File 'lib/parse_fasta/record.rb', line 69

def == rec
  self.header == rec.header && self.seq == rec.seq &&
      self.desc == rec.desc && self.qual == rec.qual
end

#fastq?Bool

Returns true if record is a fastQ record.

This method returns true if the fastq instance method is set.

Returns:

  • (Bool)

    true if record is fastQ, false if it is fastA



157
158
159
# File 'lib/parse_fasta/record.rb', line 157

def fastq?
  true if @qual
end

#to_fastaString

Returns a fastA record ready to print.

If the record is fastQ like, the desc and qual are dropped.

Examples:

When the record is fastA like

rec = Record.new header: "Apple", seq: "ACTG"
rec.to_fasta #=> ">Apple\nACTG"

When the record is fastQ like

rec = Record.new header: "Apple", seq: "ACTG", desc: "Hi", qual: "IIII"
rec.to_fasta #=> ">Apple\nACTG"

Returns:

  • (String)

    a printable fastA sequence record



110
111
112
# File 'lib/parse_fasta/record.rb', line 110

def to_fasta
  ">#{header}\n#{seq}"
end

#to_fastq(opts = {}) ⇒ String

Returns a fastA record ready to print.

If the record is fastA like, the desc and qual can be specified.

Examples:

When the record is fastA like, no args

rec = Record.new header: "Apple", seq: "ACTG"
rec.to_fastq #=> "@Apple\nACTG\n+\nIIII"

When the record is fastA like, desc and qual specified

rec = Record.new header: "Apple", seq: "ACTG"
rec.to_fastq decs: "Hi", qual: "A" #=> "@Apple\nACTG\n+Hi\nAAAA"

When the record is fastA like, can specify fancy qual strings

rec = Record.new header: "Apple", seq: "ACTGACTG"
rec.to_fastq decs: "Hi", qual: "!a2" #=> "@Apple\nACTG\n+Hi\n!a2!a2!a"

When the record is fastQ like

rec = Record.new header: "Apple", seq: "ACTG", desc: "Hi", qual: "IIII"
rec.to_fastq #=> ">Apple\nACTG"

Returns:

  • (String)

    a printable fastQ sequence record

Raises:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/parse_fasta/record.rb', line 137

def to_fastq opts = {}
  if fastq?
    "@#{@header}\n#{@seq}\n+#{@desc}\n#{qual}"
  else
    qual = opts.fetch :qual, "I"
    check_qual qual

    desc  = opts.fetch :desc, ""

    qual_str = make_qual_str qual

    "@#{@header}\n#{@seq}\n+#{desc}\n#{qual_str}"
  end
end

#to_sString

Return a fastA or fastQ record ready to print.

If the Record is fastQ like then it returns a fastQ record string. If the record is fastA like, then it returns a fastA record string.

Examples:

When the record is fastA like

rec = Record.new header: "Apple", seq: "ACTG"
rec.to_s #=> ">Apple\nACTG"

When the record is fastQ like

rec = Record.new header: "Apple", seq: "ACTG", desc: "Hi", qual: "IIII"
rec.to_s #=> "@Apple\nACTG\n+Hi\nIIII"

Returns:

  • (String)

    a printable sequence record



89
90
91
92
93
94
95
# File 'lib/parse_fasta/record.rb', line 89

def to_s
  if fastq?
    to_fastq
  else
    to_fasta
  end
end