<commit_before>module Transrate

  class ReciprocalAnnotation

    attr_reader :l2r_hits
    attr_reader :r2l_hits
    attr_reader :results

    def initialize assembly, reference
      @assembly = assembly
      @reference = reference
      @usearch = Usearch.new
    end

    def run
      self.make_assembly_db
      self.make_reference_db
      left2right, right2left = self.reciprocal_align
      self.parse_results left2right, right2left
      @results
    end

    def make_assembly_db
      unless @assembly.orfs_ublast_db
        assembly_base = File.basename(@assembly.file, ".*")
        assembly_orfs = assembly_base + ".orfs"
        @usearch.findorfs @assembly.file, assembly_orfs
        assembly_db = assembly_base + ".udb"
        @usearch.makeudb_ublast assembly_orfs, assembly_db
        @assembly.orfs_ublast_db = assembly_db
      end
    end

    def make_reference_db
      unless @reference.ublast_db
        reference_base = File.basename(@reference.file, ".*")
        reference_db = reference_base + ".udb"
        @usearch.makeudb_ublast @reference.file, reference_db
        @reference.ublast_db = reference_db
      end
    end

    def reciprocal_align
      left2right = @usearch.ublast @assembly.file, @reference.ublast_db
      right2left = @usearch.ublast @reference.file, @assembly.orfs_ublast_db
      [left2right, right2left]
    end

    def parse_results left2right, right2left
      l2r_results = self.load_results_file left2right
      r2l_results = self.load_results_file right2left
      @l2r_hits = self.results_to_hits l2r_results
      @r2l_hits = self.results_to_hits r2l_results
      @results = {}
      @l2r_hits.each_pair do |query, best|
        next if best.nil?
        tbest = @r2l_hits[best.target]
        next if tbest.nil?
        @results[query] = best if query == tbest.target
      end
    end

    def results_to_hits results
      hits = {}
      results.each do |hit|
        if hits.has_key? hit.query<commit_msg>use full path to reference<commit_after>module Transrate

  class ReciprocalAnnotation

    attr_reader :l2r_hits
    attr_reader :r2l_hits
    attr_reader :results

    def initialize assembly, reference
      @assembly = assembly
      @reference = reference
      @usearch = Usearch.new
    end

    def run
      self.make_assembly_db
      self.make_reference_db
      left2right, right2left = self.reciprocal_align
      self.parse_results left2right, right2left
      @results
    end

    def make_assembly_db
      unless @assembly.orfs_ublast_db
        assembly_dir = File.dirname(@assembly.file)
        assembly_base = File.basename(@assembly.file, ".*")
        assembly_orfs = assembly_base + ".orfs"
        @usearch.findorfs @assembly.file, assembly_orfs
        assembly_db = File.join(assembly_dir, assembly_base + ".udb")
        @usearch.makeudb_ublast assembly_orfs, assembly_db
        @assembly.orfs_ublast_db = assembly_db
      end
    end

    def make_reference_db
      unless @reference.ublast_db
        reference_dir = File.dirname(@reference.file)
        reference_base = File.basename(@reference.file, ".*")
        reference_db = File.join(reference_dir, reference_base + ".udb")
        @usearch.makeudb_ublast @reference.file, reference_db
        @reference.ublast_db = reference_db
      end
    end

    def reciprocal_align
      left2right = @usearch.ublast @assembly.file, @reference.ublast_db
      right2left = @usearch.ublast @reference.file, @assembly.orfs_ublast_db
      [left2right, right2left]
    end

    def parse_results left2right, right2left
      l2r_results = self.load_results_file left2right
      r2l_results = self.load_results_file right2left
      @l2r_hits = self.results_to_hits l2r_results
      @r2l_hits = self.results_to_hits r2l_results
      @results = {}
      @l2r_hits.each_pair do |query, best|
        next if best.nil?
        tbest = @r2l_hits[best.target]
        next if tbest.nil?
        @results[query] = best if query == tbest.target
      end
    end

    def results_to_hits results
      hits = {}
      results.each do |hit|
        if hits.has_key? hit.query
{'fraction_non_alphanumeric': 0.07554479418886198, 'fraction_numerical': 0.010653753026634382, 'mean_word_length': 2.7182718271827184, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4539463', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1330, 'n_words': 269}