class SampleReader

Class containing methods for reading and checking sample information.

Constants

Sample

Internal: Struct for holding sample information.

id - Sample id. index1 - Index1 sequence. index2 - Index2 sequence.

Examples

Sample.new("test1", "atcg", "gcta")
  # => <Sample>

Returns Sample object.

Public Class Methods

new(revcomp1, revcomp2) click to toggle source

Internal: Constructor method for SampleReader object. The given revcomp1 and revcomp2 flags are stored as instance variables.

revcomp1 - Flag indicating that index1 should be reverse-complemented. revcomp2 - Flag indicating that index2 should be reverse-complemented.

Examples

SampleReader.new(false, false)
# => <SampleReader>

Returns SampleReader object.

# File lib/sample_reader.rb, line 68
def initialize(revcomp1, revcomp2)
  @revcomp1 = revcomp1
  @revcomp2 = revcomp2
end
read(file, revcomp1, revcomp2) click to toggle source

Internal: Class method that reads sample information from a samples file, which consists of ASCII text in three tab separated columns: The first column is the sample_id, the second column is index1 and the third column is index2.

If revcomp1 or revcomp2 is set then index1 and index2 are reverse-complemented accordingly.

file - String with path to sample file. revcomp1 - Flag indicating that index1 should be reverse-complemented. revcomp2 - Flag indicating that index2 should be reverse-complemented.

Examples

SampleReader.read("samples.txt", false, false)
# => [<Sample>, <Sample>, <Sample> ...]

Returns an Array of Sample objects.

# File lib/sample_reader.rb, line 51
def self.read(file, revcomp1, revcomp2)
  sample_reader = new(revcomp1, revcomp2)
  sample_reader.samples_parse(file)
end

Public Instance Methods

samples_parse(file) click to toggle source

Internal: Method that reads sample information from a samples file, which consists of ASCII text in three tab separated columns: The first column is the sample_id, the second column is index1 and the third column is index2.

file - String with path to sample file.

Examples

samples_parse("samples.txt")
# => [<Sample>, <Sample>, <Sample> ...]

Returns an Array of Sample objects.

# File lib/sample_reader.rb, line 85
def samples_parse(file)
  samples = samples_read(file)
  samples_reverse_complement(samples)
  errors = []
  errors.push(*samples_check_index_combo(samples))
  errors.push(*samples_check_uniq_id(samples))

  unless errors.empty?
    warn errors
    fail SampleReaderError, 'errors found in sample file.'
  end

  samples
end

Private Instance Methods

index_reverse_complement(index) click to toggle source

Method that reverse-complements a given index sequence.

index - Index String.

Returns reverse-complemented index String.

# File lib/sample_reader.rb, line 152
def index_reverse_complement(index)
  BioPieces::Seq.new(seq: index, type: :dna).reverse.complement.seq
end
samples_check_index_combo(samples) click to toggle source

Internal: Method that iterates over the a given Array of sample Objects, and if the combination of index1 and index2 is non-unique an error is pushed on an error Array.

samples - Array of Sample objects.

Returns an Array of found errors.

# File lib/sample_reader.rb, line 163
def samples_check_index_combo(samples)
  errors = []
  lookup = {}

  samples.each do |sample|
    if (id2 = lookup["#{sample.index1}#{sample.index2}"])
      errors << ['Samples with same index combo', sample.id, id2].join("\t")
    else
      lookup["#{sample.index1}#{sample.index2}"] = sample.id
    end
  end

  errors
end
samples_check_uniq_id(samples) click to toggle source

Internal: Method that iterates over the a given Array of sample Objects, and if a sample id is non-unique an error is pushed on an error Array.

samples - Array of Sample objects.

Returns an Array of found errors.

# File lib/sample_reader.rb, line 184
def samples_check_uniq_id(samples)
  errors = []
  lookup = Set.new

  samples.each do |sample|
    if lookup.include? sample.id
      errors << ['Non-unique sample id', sample.id].join("\t")
    end

    lookup << sample.id
  end

  errors
end
samples_read(file) click to toggle source

Internal: Method that reads sample information form a samples file, which consists of ASCII text in three tab separated columns: The first column is the sample_id, the second column is index1 and the third column is index2.

If @options or @options is set then index1 and index2 are reverse-complemented accordingly.

file - String with path to sample file.

Examples

samples_read("samples.txt")
# => [<Sample>, <Sample>, <Sample> ...]

Returns an Array of Sample objects.

# File lib/sample_reader.rb, line 117
def samples_read(file)
  samples = []

  CSV.read(file, col_sep: ' ').each do |id, index1, index2|
    next if id[0] == '#'

    fail SampleReaderError, "Id not found in file: #{file}" if id.nil?
    fail SampleReaderError, "Index1 not found in file: #{file}" if index1.nil?
    fail SampleReaderError, "Index2 not found in file: #{file}" if index2.nil?

    samples << Sample.new(id, index1.upcase, index2.upcase)
  end

  samples
end
samples_reverse_complement(samples) click to toggle source

Internal: Method that iterates over the a given Array of sample Objects, and if @options or @options is set then index1 and index2 are reverse-complemented accordingly.

samples - Array of Sample objects.

Returns nothing.

# File lib/sample_reader.rb, line 140
def samples_reverse_complement(samples)
  samples.each do |sample|
    sample.index1 = index_reverse_complement(sample.index1) if @revcomp1
    sample.index2 = index_reverse_complement(sample.index2) if @revcomp2
  end
end