<commit_before>class Assertion < ActiveRecord::Base
  include Flaggable
  include Subscribable
  include WithAudits
  include WithTimepointCounts
  include Commentable
  include SoftDeletable
  include Moderated
  include WithCountableEnum
  include WithSingleValueAssociations

  belongs_to :gene
  belongs_to :variant
  belongs_to :disease
  has_and_belongs_to_many :acmg_codes
  has_and_belongs_to_many :evidence_items
  has_and_belongs_to_many :drugs

  has_one :submission_event,
    ->() { where(action: 'assertion submitted').includes(:originating_user) },
    as: :subject,
    class_name: Event
  has_one :submitter, through: :submission_event, source: :originating_user
  has_one :acceptance_event,
    ->() { where(action: 'assertion accepted').includes(:originating_user) },
    as: :subject,
    class_name: Event
  has_one :acceptor, through: :acceptance_event, source: :originating_user
  has_one :rejection_event,
    ->() { where(action: 'assertion rejected').includes(:originating_user) },
    as: :subject,
    class_name: Event
  has_one :rejector, through: :rejection_event, source: :originating_user
  has_one :current_status_event,
    ->(a) { where(action: "assertion #{a.status}").includes(:originating_user).order('created_at DESC') }, 
    as: :subject,
    class_name: Event

  associate_by_attribute :disease, :name
  associate_by_attribute :gene, :name
  associate_by_attribute :variant, :name

  enum evidence_type: Constants::EVIDENCE_TYPES
  enum nccn_guideline: Constants::NCCN_GUIDELINES
  enum amp_level: Constants::AMP_LEVELS
  enum clinical_significance: Constants::CLINICAL_SIGNIFICANCES
  enum drug_interaction_type: Constants::DRUG_INTERACTION_TYPES
  enum evidence_direction: Constants::EVIDENCE_DIRECTIONS

  def self.index_scope
    eager_load(:evidence_items)
  end

  def self.view_scope
    eager_load(:gene, :variant, :disease, :drugs, :acmg_codes, evidence_items: [:disease, :source, :drugs, :variant])
  end

  def self.datatable_scope
    joins('LEFT OUTER JOIN variants ON variants.id = assertions.variant_id')
      .joins('LEFT OUTER JOIN genes ON genes.id = assertions.gene_id')
      .joins('LEFT OUTER JOIN diseases ON diseases.id = assertions.disease_id')
      .joins('LEFT OUTER JOIN assertions_evidence_items ON assertions_evidence_items.assertion_id = assertions.id')
      .joins('LEFT OUTER JOIN assertions_drugs ON assertions_drugs.assertion_id = assertions.id')
      .joins('LEFT OUTER JOIN drugs ON drugs.id = assertions_drugs.drug_id')
  end

  def name
    "#{tag}#{id}"
  end

  def tag
    "AID"
  end

  def pending_evidence
    self.evidence_items.select{|ei| ei.status == 'submitted'}
  end

  def parent_subscribables
    [variant]
  end

  def lifecycle_events
    {
      submitted: :submission_event,
      last_modified: :last_applied_change,
      last_reviewed: :last_review_event,
      last_commented_on: :last_comment_event
    }.tap do |events_hash|
      events_hash[self.status.to_sym] = :current_status_event
    end
  end

  def self.timepoint_query
    ->(x) {
      self.where("assertions.status != 'rejected'")
        .where('assertions.created_at >= ?', x)
        .distinct
    }
  end

  def additional_changes_info
    @@additional_variant_changes ||= {
      'evidence_items' => {
        output_field_name: 'evidence_item_ids',
        creation_query: ->(x) { EvidenceItem.find(x) },
        application_query: ->(x) { EvidenceItem.find(x) },
        id_field: 'id',
      },
      'acmg_codes' => {
        output_field_name: 'acmg_code_ids',
        creation_query: ->(x) { AcmgCode.where(code: x) },
        application_query: ->(x) { AcmgCode.where(code: x) },
        id_field: 'id'
      },
      'drugs' => {
        output_field_name: 'drug_ids',
        creation_query: ->(x) { Drug.get_drugs_from_list(x) },
        application_query: ->(x) { Drug.find(x) },
        id_field: 'id'
      }
    }
  end

  def self.propose(direct_attributes, relational_attributes, proposing_user)
    cmd = Actions::ProposeAssertion.new(direct_attributes, relational_attributes, proposing_user)<commit_msg>Fix acmg code application query<commit_after>class Assertion < ActiveRecord::Base
  include Flaggable
  include Subscribable
  include WithAudits
  include WithTimepointCounts
  include Commentable
  include SoftDeletable
  include Moderated
  include WithCountableEnum
  include WithSingleValueAssociations

  belongs_to :gene
  belongs_to :variant
  belongs_to :disease
  has_and_belongs_to_many :acmg_codes
  has_and_belongs_to_many :evidence_items
  has_and_belongs_to_many :drugs

  has_one :submission_event,
    ->() { where(action: 'assertion submitted').includes(:originating_user) },
    as: :subject,
    class_name: Event
  has_one :submitter, through: :submission_event, source: :originating_user
  has_one :acceptance_event,
    ->() { where(action: 'assertion accepted').includes(:originating_user) },
    as: :subject,
    class_name: Event
  has_one :acceptor, through: :acceptance_event, source: :originating_user
  has_one :rejection_event,
    ->() { where(action: 'assertion rejected').includes(:originating_user) },
    as: :subject,
    class_name: Event
  has_one :rejector, through: :rejection_event, source: :originating_user
  has_one :current_status_event,
    ->(a) { where(action: "assertion #{a.status}").includes(:originating_user).order('created_at DESC') }, 
    as: :subject,
    class_name: Event

  associate_by_attribute :disease, :name
  associate_by_attribute :gene, :name
  associate_by_attribute :variant, :name

  enum evidence_type: Constants::EVIDENCE_TYPES
  enum nccn_guideline: Constants::NCCN_GUIDELINES
  enum amp_level: Constants::AMP_LEVELS
  enum clinical_significance: Constants::CLINICAL_SIGNIFICANCES
  enum drug_interaction_type: Constants::DRUG_INTERACTION_TYPES
  enum evidence_direction: Constants::EVIDENCE_DIRECTIONS

  def self.index_scope
    eager_load(:evidence_items)
  end

  def self.view_scope
    eager_load(:gene, :variant, :disease, :drugs, :acmg_codes, evidence_items: [:disease, :source, :drugs, :variant])
  end

  def self.datatable_scope
    joins('LEFT OUTER JOIN variants ON variants.id = assertions.variant_id')
      .joins('LEFT OUTER JOIN genes ON genes.id = assertions.gene_id')
      .joins('LEFT OUTER JOIN diseases ON diseases.id = assertions.disease_id')
      .joins('LEFT OUTER JOIN assertions_evidence_items ON assertions_evidence_items.assertion_id = assertions.id')
      .joins('LEFT OUTER JOIN assertions_drugs ON assertions_drugs.assertion_id = assertions.id')
      .joins('LEFT OUTER JOIN drugs ON drugs.id = assertions_drugs.drug_id')
  end

  def name
    "#{tag}#{id}"
  end

  def tag
    "AID"
  end

  def pending_evidence
    self.evidence_items.select{|ei| ei.status == 'submitted'}
  end

  def parent_subscribables
    [variant]
  end

  def lifecycle_events
    {
      submitted: :submission_event,
      last_modified: :last_applied_change,
      last_reviewed: :last_review_event,
      last_commented_on: :last_comment_event
    }.tap do |events_hash|
      events_hash[self.status.to_sym] = :current_status_event
    end
  end

  def self.timepoint_query
    ->(x) {
      self.where("assertions.status != 'rejected'")
        .where('assertions.created_at >= ?', x)
        .distinct
    }
  end

  def additional_changes_info
    @@additional_variant_changes ||= {
      'evidence_items' => {
        output_field_name: 'evidence_item_ids',
        creation_query: ->(x) { EvidenceItem.find(x) },
        application_query: ->(x) { EvidenceItem.find(x) },
        id_field: 'id',
      },
      'acmg_codes' => {
        output_field_name: 'acmg_code_ids',
        creation_query: ->(x) { AcmgCode.where(code: x) },
        application_query: ->(x) { AcmgCode.find(x) },
        id_field: 'id'
      },
      'drugs' => {
        output_field_name: 'drug_ids',
        creation_query: ->(x) { Drug.get_drugs_from_list(x) },
        application_query: ->(x) { Drug.find(x) },
        id_field: 'id'
      }
    }
  end

  def self.propose(direct_attributes, relational_attributes, proposing_user)
    cmd = Actions::ProposeAssertion.new(direct_attributes, relational_attributes, proposing_user)
{'fraction_non_alphanumeric': 0.10585942258496772, 'fraction_numerical': 0.0, 'mean_word_length': 4.0060975609756095, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '5735106', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2706, 'n_words': 540}