Sha256: e8d8ed3cf64b5952ac121ec0fef5d4c045624d820673582216477e4e56048645

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'delegate'
require 'scaffolder'
require 'bio'

require 'scaffolder/extensions'

class Scaffolder::AnnotationLocator < DelegateClass(Array)

  def initialize(scaffold_file,sequence_file,gff_file)
    @scaffold_file = scaffold_file
    @sequence_file = sequence_file
    @gff_file      = gff_file

    updated_records = Array.new
    scaffold.inject(0) do |prior_length,entry|

      if entry.entry_type == :sequence
        records[entry.source].each do |record|

          # Don't include this record if it overlaps with an insert
          next if record.overlap?(entry.inserts.map{|i| (i.open..i.close)})

          # Skip this record it lies in the start or stop trimmed regions
          next if record.start < entry.start
          next if record.end   > entry.stop

          # Update record location by size differences of prior inserts
          entry.inserts.select {|i| i.close < record.start }.each do |insert|
            record.change_position_by insert.size_diff
          end

          # Decrease record position by distance contig is trimmed at start
          record.change_position_by(1 - entry.start)

          # Reverse complement record positions if contig is reversed
          record.reverse_complement_by entry.sequence.length if entry.reverse

          # Increase record position by length of prior contigs
          record.change_position_by prior_length

          record.seqname = "scaffold"

          updated_records << record
        end
      end

      prior_length + entry.sequence.length
    end

    super updated_records
  end

  def scaffold
    YAML::ENGINE.yamler = 'syck'
    Scaffolder.new(YAML.load(File.read(@scaffold_file)),@sequence_file)
  end

  def records
    gff3 = Bio::GFF::GFF3.new(File.read(@gff_file)).records
    gff3.inject(Hash.new{|h,k| h[k] = Array.new }) do |hash,record|
      hash[record.seqname] << record
      hash
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scaffolder-annotation-locator-0.1.2 lib/scaffolder/annotation_locator.rb