<commit_before>require 'set'
require 'crb-blast'

module Transrate

  class ComparativeMetrics

    attr_reader :rbh_per_contig
    attr_reader :rbh_per_reference
    attr_reader :reciprocal_hits
    attr_reader :has_run
    attr_reader :reference_coverage
    attr_reader :comp_stats
    attr_reader :n_chimeras, :p_chimeras

    def initialize assembly, reference, threads
      @assembly = assembly
      @reference = reference
      @threads = threads
      @comp_stats = Hash.new
    end

    def run
      @crbblast = reciprocal_best_blast
      @ortholog_hit_ratio = ortholog_hit_ratio @crbblast
      @collapse_factor = collapse_factor @crbblast.target_results
      @reciprocal_hits = @crbblast.size
      @rbh_per_reference = @reciprocal_hits.to_f / @reference.size.to_f
      @reference_coverage = @ortholog_hit_ratio * @rbh_per_reference
      @p_contigs_with_recip = @crbblast.reciprocals.size / @assembly.size.to_f
      @n_contigs_with_recip = @crbblast.reciprocals.size
      @p_refs_with_recip = @n_refs_with_recip / @reference.size.to_f
      chimeras @crbblast
      self.run_comp_stats
      @has_run = true
    end

    def run_comp_stats
      @comp_stats[:reciprocal_hits] = @reciprocal_hits
      @comp_stats[:rbh_per_contig] = @rbh_per_contig
      @comp_stats[:p_contigs_with_recip] = @p_contigs_with_recip
      @comp_stats[:n_contigs_with_recip] = @n_contigs_with_recip
      @comp_stats[:p_refs_with_recip] = @p_refs_with_recip
      @comp_stats[:n_refs_with_recip] = @n_refs_with_recip
      @comp_stats[:rbh_per_reference] = @rbh_per_reference
      @comp_stats[:reference_coverage] = @reference_coverage
      @comp_stats[:ortholog_hit_ratio] = @ortholog_hit_ratio
      @comp_stats[:collapse_factor] = @collapse_factor
      @comp_stats[:n_chimeras] = @n_chimeras
      @comp_stats[:p_chimeras] = @p_chimeras
    end

    def reciprocal_best_blast
      crbblast = CRB_Blast.new @assembly.file, @reference.file
      crbblast.run 1e-5, @threads
      crbblast
    end

    # coverage of contigs that have reciprocal hits
    # divided by
    # number of reciprocal targets
    def ortholog_hit_ratio crbblast
      return @ortholog_hit_ratio unless @ortholog_hit_ratio.nil?

      targets = Hash.new
      crbblast.reciprocals.each_pair do |key, list|
        list.each do |hit|
          targets[hit.target] ||= [] # if key doesn't exist add it with a []
          targets[hit.target] << hit
        end
      end
      @n_refs_with_recip = targets.size
      total_coverage=0<commit_msg>use new CRB-Blast run API<commit_after>require 'set'
require 'crb-blast'

module Transrate

  class ComparativeMetrics

    attr_reader :rbh_per_contig
    attr_reader :rbh_per_reference
    attr_reader :reciprocal_hits
    attr_reader :has_run
    attr_reader :reference_coverage
    attr_reader :comp_stats
    attr_reader :n_chimeras, :p_chimeras

    def initialize assembly, reference, threads
      @assembly = assembly
      @reference = reference
      @threads = threads
      @comp_stats = Hash.new
    end

    def run
      @crbblast = reciprocal_best_blast
      @ortholog_hit_ratio = ortholog_hit_ratio @crbblast
      @collapse_factor = collapse_factor @crbblast.target_results
      @reciprocal_hits = @crbblast.size
      @rbh_per_reference = @reciprocal_hits.to_f / @reference.size.to_f
      @reference_coverage = @ortholog_hit_ratio * @rbh_per_reference
      @p_contigs_with_recip = @crbblast.reciprocals.size / @assembly.size.to_f
      @n_contigs_with_recip = @crbblast.reciprocals.size
      @p_refs_with_recip = @n_refs_with_recip / @reference.size.to_f
      chimeras @crbblast
      self.run_comp_stats
      @has_run = true
    end

    def run_comp_stats
      @comp_stats[:reciprocal_hits] = @reciprocal_hits
      @comp_stats[:rbh_per_contig] = @rbh_per_contig
      @comp_stats[:p_contigs_with_recip] = @p_contigs_with_recip
      @comp_stats[:n_contigs_with_recip] = @n_contigs_with_recip
      @comp_stats[:p_refs_with_recip] = @p_refs_with_recip
      @comp_stats[:n_refs_with_recip] = @n_refs_with_recip
      @comp_stats[:rbh_per_reference] = @rbh_per_reference
      @comp_stats[:reference_coverage] = @reference_coverage
      @comp_stats[:ortholog_hit_ratio] = @ortholog_hit_ratio
      @comp_stats[:collapse_factor] = @collapse_factor
      @comp_stats[:n_chimeras] = @n_chimeras
      @comp_stats[:p_chimeras] = @p_chimeras
    end

    def reciprocal_best_blast
      crbblast = CRB_Blast.new @assembly.file, @reference.file
      crbblast.run(1e-5, @threads, true)
      crbblast
    end

    # coverage of contigs that have reciprocal hits
    # divided by
    # number of reciprocal targets
    def ortholog_hit_ratio crbblast
      return @ortholog_hit_ratio unless @ortholog_hit_ratio.nil?

      targets = Hash.new
      crbblast.reciprocals.each_pair do |key, list|
        list.each do |hit|
          targets[hit.target] ||= [] # if key doesn't exist add it with a []
          targets[hit.target] << hit
        end
      end
      @n_refs_with_recip = targets.size
      total_coverage=0
{'fraction_non_alphanumeric': 0.07979458818882086, 'fraction_numerical': 0.0011850681414181316, 'mean_word_length': 3.4265734265734267, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4106744', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1728, 'n_words': 307}