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

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 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

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/parse_fasta/record.rb', line 52

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", "")

  if fastq? # is fastQ
    @seq = seq
  else # is fastA
    @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



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

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 ‘@’



36
37
38
# File 'lib/parse_fasta/record.rb', line 36

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



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

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



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

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

#seqString

Returns the sequence of the record.

Returns:

  • (String)

    the sequence of the record



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

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



76
77
78
79
# File 'lib/parse_fasta/record.rb', line 76

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



164
165
166
# File 'lib/parse_fasta/record.rb', line 164

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



117
118
119
# File 'lib/parse_fasta/record.rb', line 117

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:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/parse_fasta/record.rb', line 144

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



96
97
98
99
100
101
102
# File 'lib/parse_fasta/record.rb', line 96

def to_s
  if fastq?
    to_fastq
  else
    to_fasta
  end
end