<commit_before>module Transrate

  class ReadMetrics

    require 'bettersam'
    require 'bio-samtools'

    attr_reader :total
    attr_reader :bad
    attr_reader :supported_bridges
    attr_reader :pr_good_mapping
    attr_reader :percent_mapping
    attr_reader :prop_expressed
    attr_reader :has_run

    def initialize assembly
      @assembly = assembly
      @mapper = Bowtie2.new
      self.initial_values
    end

    def run left, right, insertsize:200, insertsd:50, threads:8
      [left, right].each do |readfile|
        if readfile.nil?
          raise IOError.new "Read file is nil"
        end
        unless File.exist? readfile
          raise IOError.new "ReadMetrics read file does not exist: #{readfile}"
        end
      end
      @mapper.build_index @assembly.file
      @num_pairs = `wc -l #{left}`.strip.split(/\s+/)[0].to_i/4
      samfile = @mapper.map_reads(@assembly.file, left, right,
                                  insertsize: insertsize,
                                  insertsd: insertsd,
                                  threads: threads)
      # check_bridges
      analyse_read_mappings(samfile, insertsize, insertsd, true)
      analyse_coverage(samfile)
      @pr_good_mapping = @good.to_f / @num_pairs.to_f
      @percent_mapping = @total.to_f / @num_pairs.to_f * 100.0
      @pc_good_mapping = @pr_good_mapping * 100.0
      @has_run = true
    end

    def read_stats
      {
        :num_pairs => @num_pairs,
        :total_mappings => @total,
        :percent_mapping => @percent_mapping,
        :good_mappings => @good,
        :pc_good_mapping => @pc_good_mapping,
        :bad_mappings => @bad,
        :potential_bridges => @supported_bridges,
        :mean_coverage => @mean_coverage,
        :n_uncovered_bases => @n_uncovered_bases,
        :p_uncovered_bases => @p_uncovered_bases,
        :n_uncovered_base_contigs => @n_uncovered_base_contigs,
        :p_uncovered_base_contigs => @p_uncovered_base_contigs,
        :n_uncovered_contigs => @n_uncovered_contigs,
        :p_uncovered_contigs => @p_uncovered_contigs,
        :n_lowcovered_contigs => @n_lowcovered_contigs,
        :p_lowcovered_contigs => @p_lowcovered_contigs,
        :edit_distance_per_base => @edit_distance / @total_bases.to_f,
        :n_low_uniqueness_bases => @n_low_uniqueness_bases
      }
    end

    def analyse_read_mappings samfile, insertsize, insertsd, bridge=true
      @bridges = {} if bridge
      realistic_dist = self.realistic_distance(insertsize, insertsd)
      if File.exists?(samfile) && File.size(samfile) > 0
        ls = BetterSam.new
        rs = BetterSam.new
        sam = File.open(samfile)
        line = sam.readline
        while line and line=~/^@/
          line = sam.readline rescue nil
        end
        while line
          ls.parse_line(line)
          @assembly[ls.chrom].edit_distance += ls.edit_distance
          @assembly[ls.chrom].bases_mapped += ls.length
          @edit_distance += ls.edit_distance
          @total_bases += ls.length
          if ls.mate_unmapped?
            self.check_read_single(ls)
            line = sam.readline rescue nil
          else
            line2 = sam.readline rescue nil<commit_msg>add mean mapq and proportion of low uniqueness bases to reads stats output<commit_after>module Transrate

  class ReadMetrics

    require 'bettersam'
    require 'bio-samtools'

    attr_reader :total
    attr_reader :bad
    attr_reader :supported_bridges
    attr_reader :pr_good_mapping
    attr_reader :percent_mapping
    attr_reader :prop_expressed
    attr_reader :has_run

    def initialize assembly
      @assembly = assembly
      @mapper = Bowtie2.new
      self.initial_values
    end

    def run left, right, insertsize:200, insertsd:50, threads:8
      [left, right].each do |readfile|
        if readfile.nil?
          raise IOError.new "Read file is nil"
        end
        unless File.exist? readfile
          raise IOError.new "ReadMetrics read file does not exist: #{readfile}"
        end
      end
      @mapper.build_index @assembly.file
      @num_pairs = `wc -l #{left}`.strip.split(/\s+/)[0].to_i/4
      samfile = @mapper.map_reads(@assembly.file, left, right,
                                  insertsize: insertsize,
                                  insertsd: insertsd,
                                  threads: threads)
      # check_bridges
      analyse_read_mappings(samfile, insertsize, insertsd, true)
      analyse_coverage(samfile)
      @pr_good_mapping = @good.to_f / @num_pairs.to_f
      @percent_mapping = @total.to_f / @num_pairs.to_f * 100.0
      @pc_good_mapping = @pr_good_mapping * 100.0
      @has_run = true
    end

    def read_stats
      {
        :num_pairs => @num_pairs,
        :total_mappings => @total,
        :percent_mapping => @percent_mapping,
        :good_mappings => @good,
        :pc_good_mapping => @pc_good_mapping,
        :bad_mappings => @bad,
        :potential_bridges => @supported_bridges,
        :mean_coverage => @mean_coverage,
        :mean_mapq => @mean_mapq,
        :n_uncovered_bases => @n_uncovered_bases,
        :p_uncovered_bases => @p_uncovered_bases,
        :n_uncovered_base_contigs => @n_uncovered_base_contigs,
        :p_uncovered_base_contigs => @p_uncovered_base_contigs,
        :n_uncovered_contigs => @n_uncovered_contigs,
        :p_uncovered_contigs => @p_uncovered_contigs,
        :n_lowcovered_contigs => @n_lowcovered_contigs,
        :p_lowcovered_contigs => @p_lowcovered_contigs,
        :edit_distance_per_base => @edit_distance / @total_bases.to_f,
        :n_low_uniqueness_bases => @n_low_uniqueness_bases
        :p_low_uniqueness_bases => @p_low_uniqueness_bases
      }
    end

    def analyse_read_mappings samfile, insertsize, insertsd, bridge=true
      @bridges = {} if bridge
      realistic_dist = self.realistic_distance(insertsize, insertsd)
      if File.exists?(samfile) && File.size(samfile) > 0
        ls = BetterSam.new
        rs = BetterSam.new
        sam = File.open(samfile)
        line = sam.readline
        while line and line=~/^@/
          line = sam.readline rescue nil
        end
        while line
          ls.parse_line(line)
          @assembly[ls.chrom].edit_distance += ls.edit_distance
          @assembly[ls.chrom].bases_mapped += ls.length
          @edit_distance += ls.edit_distance
          @total_bases += ls.length
          if ls.mate_unmapped?
            self.check_read_single(ls)
            line = sam.readline rescue nil
          else
            line2 = sam.readline rescue nil
{'fraction_non_alphanumeric': 0.08945443462359037, 'fraction_numerical': 0.005790917403230723, 'mean_word_length': 2.353602452733776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1757109', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2088, 'n_words': 407}