<commit_before>require 'forwardable'

require 'genevalidator/exceptions'
require 'genevalidator/validation_report'
require 'genevalidator/validation_test'

module GeneValidator
  ##
  # Class that stores the validation output information
  class MakerQIValidationOutput < ValidationReport
    def initialize(short_header, header, description, splice_sites, exons,
                   expected = :yes)
      @short_header, @header, @description = short_header, header, description
      @splice_sites = splice_sites
      @exons        = exons
      @expected     = expected
      @result       = validation
      @approach     = 'We obtain the fraction of splice sites and exons' \
                      ' confirmed by EST/RNASeq alignment from the FASTA' \
                      ' defline for MAKER predicted gene models. RNASeq is' \
                      ' often best evidence to ascertain the quality of gene' \
                      ' models'
      @explanation  = explain
      @conclusion   = conclude
    end

    def explain
      if @splice_sites == -100
        "#{@exons}% of exons match an EST/mRNA-seq alignment." \
        " No splice sites were identified and as such cannot be confirmed by" \
        " an EST/mRNA-seq alignment."
      else
        "#{@exons}% of exons match an EST/mRNA-seq alignment and" \
        " #{@splice_sites}% of splice sites are confirmed by EST/mRNA-seq" \
        " alignment."
      end
    end

    def conclude
      if @result == :yes
        'More than 80% of this gene is confirmed by RNASeq evidence.' \
        'Thus, the MAKER Quality Index suggests that the query sequence is of' \
        ' a good quality.'
      else
        'Less than 80% of this gene is confirmed by RNASeq evidence.' \
        'Thus, the MAKER Quality Index suggests that there may be some issues'\
        ' with the query seqeunce.'
      end
    end

    def print
      "Exons:&nbsp;#{@exons}%;" \
      " Splice&nbsp;Sites:&nbsp;#{@splice_sites}%"
    end

    def validation
      (@splice_sites > 80 && @exons > 80) ? :yes : :no
    end
  end

  ##
  # This class contains the methods necessary for
  # reading frame validation based on BLAST output
  class MakerQIValidation < ValidationTest
    def initialize(type, prediction, hits = nil)
      super
      @short_header = 'QualityIndex'
      @header       = 'Quality Index'
      @description  = 'MAKER mRNA Quality Index'
      @cli_name     = 'maker_qi'
    end

    ##
    # Check reading frame inconsistency
    # Params:
    # +lst+: vector of +Sequence+ objects
    # Output:
    # +QIValidationOutput+ object
    def run
      fail unless prediction.is_a?(Query)

      start  = Time.now

      number = '-?\d*\.?\d*'
      match  = @prediction.definition.match(/QI:#{number}\|(#{number})\|
                                             (#{number})\|#{number}\|
                                             #{number}\|#{number}\|#{number}\|
                                             #{number}\|#{number}/x)

      fail NotEnoughEvidence if match.nil?

      # % of splice sites confirmed by EST/mRNA-seq alignment
      splice_sites = (match[1].to_f * 100).round
      # % of exons that match an EST/mRNA-seq alignment
      exons = (match[2].to_f * 100).round

      @validation_report = MakerQIValidationOutput.new(@short_header, @header,
                                                       @description,
                                                       splice_sites, exons)
      @validation_report.run_time = Time.now - start
      @validation_report

    rescue NotEnoughEvidence
      @validation_report =  ValidationReport.new('No MAKER Quality Index',
                                                 :warning, @short_header,
                                                 @header, @description)
    # rescue
    #   @validation_report = ValidationReport.new('Unexpected error', :error,
    #                                             @short_header, @header,
    #                                             @description)
    #   @validation_report.errors.push 'Unexpected Error'
    end
  end<commit_msg>uncomment rescue block in the MAKER QI validation<commit_after>require 'forwardable'

require 'genevalidator/exceptions'
require 'genevalidator/validation_report'
require 'genevalidator/validation_test'

module GeneValidator
  ##
  # Class that stores the validation output information
  class MakerQIValidationOutput < ValidationReport
    def initialize(short_header, header, description, splice_sites, exons,
                   expected = :yes)
      @short_header, @header, @description = short_header, header, description
      @splice_sites = splice_sites
      @exons        = exons
      @expected     = expected
      @result       = validation
      @approach     = 'We obtain the fraction of splice sites and exons' \
                      ' confirmed by EST/RNASeq alignment from the FASTA' \
                      ' defline for MAKER predicted gene models. RNASeq is' \
                      ' often best evidence to ascertain the quality of gene' \
                      ' models'
      @explanation  = explain
      @conclusion   = conclude
    end

    def explain
      if @splice_sites == -100
        "#{@exons}% of exons match an EST/mRNA-seq alignment." \
        " No splice sites were identified and as such cannot be confirmed by" \
        " an EST/mRNA-seq alignment."
      else
        "#{@exons}% of exons match an EST/mRNA-seq alignment and" \
        " #{@splice_sites}% of splice sites are confirmed by EST/mRNA-seq" \
        " alignment."
      end
    end

    def conclude
      if @result == :yes
        'More than 80% of this gene is confirmed by RNASeq evidence.' \
        'Thus, the MAKER Quality Index suggests that the query sequence is of' \
        ' a good quality.'
      else
        'Less than 80% of this gene is confirmed by RNASeq evidence.' \
        'Thus, the MAKER Quality Index suggests that there may be some issues'\
        ' with the query seqeunce.'
      end
    end

    def print
      "Exons:&nbsp;#{@exons}%;" \
      " Splice&nbsp;Sites:&nbsp;#{@splice_sites}%"
    end

    def validation
      (@splice_sites > 80 && @exons > 80) ? :yes : :no
    end
  end

  ##
  # This class contains the methods necessary for
  # reading frame validation based on BLAST output
  class MakerQIValidation < ValidationTest
    def initialize(type, prediction, hits = nil)
      super
      @short_header = 'QualityIndex'
      @header       = 'Quality Index'
      @description  = 'MAKER mRNA Quality Index'
      @cli_name     = 'maker_qi'
    end

    ##
    # Check reading frame inconsistency
    # Params:
    # +lst+: vector of +Sequence+ objects
    # Output:
    # +QIValidationOutput+ object
    def run
      fail unless prediction.is_a?(Query)

      start  = Time.now

      number = '-?\d*\.?\d*'
      match  = @prediction.definition.match(/QI:#{number}\|(#{number})\|
                                             (#{number})\|#{number}\|
                                             #{number}\|#{number}\|#{number}\|
                                             #{number}\|#{number}/x)

      fail NotEnoughEvidence if match.nil?

      # % of splice sites confirmed by EST/mRNA-seq alignment
      splice_sites = (match[1].to_f * 100).round
      # % of exons that match an EST/mRNA-seq alignment
      exons = (match[2].to_f * 100).round

      @validation_report = MakerQIValidationOutput.new(@short_header, @header,
                                                       @description,
                                                       splice_sites, exons)
      @validation_report.run_time = Time.now - start
      @validation_report

    rescue NotEnoughEvidence
      @validation_report =  ValidationReport.new('No MAKER Quality Index',
                                                 :warning, @short_header,
                                                 @header, @description)
    rescue
      @validation_report = ValidationReport.new('Unexpected error', :error,
                                                @short_header, @header,
                                                @description)
      @validation_report.errors.push 'Unexpected Error'
    end
  end
{'fraction_non_alphanumeric': 0.0899541173629558, 'fraction_numerical': 0.004588263704419222, 'mean_word_length': 1.7674574006014032, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1774855', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2091, 'n_words': 660}