<commit_before># == Schema Information
#
# Table name: sequences
#
#  id       :integer          unsigned, not null, primary key
#  sequence :string(50)       not null
#  lca      :integer          unsigned
#  lca_il   :integer          unsigned
#  fa       :binary(16777215)
#  fa_il    :binary(16777215)
#

require 'ostruct'

class Sequence < ApplicationRecord
  include ReadOnlyModel
  self.primary_key = 'id'

  has_many :peptides
  has_many :original_peptides, foreign_key: 'original_sequence_id', primary_key: 'id', class_name: 'Peptide'

  belongs_to :lca_t, foreign_key: 'lca', primary_key: 'id', class_name: 'Taxon'
  belongs_to :lca_il_t, foreign_key: 'lca_il', primary_key: 'id', class_name: 'Taxon'

  alias generated_peptides peptides
  def peptides(equate_il = true)
    if equate_il
      generated_peptides
    else
      original_peptides
    end
  end

  def lineages(equate_il = true, eager = false)
    raise(ArgumentError, ':equate_il must be a boolean') unless Sequence.boolean?(equate_il)
    raise(ArgumentError, ':eager must be a boolean') unless Sequence.boolean?(eager)

    l = Lineage.select('lineages.*, count(*) as hits').joins(uniprot_entries: :peptides).group('lineages.taxon_id')
    l = if equate_il
          l.where('peptides.sequence_id = ?', id)
        else
          l.where('peptides.original_sequence_id = ?', id)
        end
    l = l.includes(:name, Lineage::ORDER_T) if eager
    l
  end

  # Calculates the lowest common ancestor for this sequence
  def calculate_lca(equate_il = true, return_taxon = false)
    if equate_il
      return lca_il_t if return_taxon

      lca_il
    else
      return lca_t if return_taxon

      lca
    end
  end

  def fa_il
    unmarshall_fa(:fa_il)
  end

  def fa
    unmarshall_fa(:fa)
  end

  # Calculates thefor this sequence
  def calculate_fa(equate_il = true)
    equate_il ? fa_il : fa
  end

  def self.missed_cleavage(sequence, equate_il)
    # Commentaar toevoegen eerste optimalisatie
    sequences = sequence
                .gsub(/([KR])([^P])/, "\\1\n\\2")
                .gsub(/([KR])([^P])/, "\\1\n\\2")
                .lines.map(&:strip).reject { |i| i.length.zero? }.to_a

    # If only one sequence is present in this array, no missed cleavages were detected for this sequence and we don't
    # need to continue computing these (since it will not make any difference anyway).
    return nil if sequences.length <= 1

    # search the most unique sequence. For each sequence (or thus tryptic peptide here), we can look up all the
    # original proteins in which it occurs. Then we need to check if the original peptide (with missed cleavages)
    # fully occurs in these original proteins (the proteins in which not the complete original peptide is present
    # need to be discarded). It thus makes sense to only look at the proteins from the peptide that's associated
    # with the least amount of proteins.
    p_counts = Sequence.where(sequence: sequences)
                       .joins(equate_il ? :peptides : :original_peptides)
                       .group('sequences.id')
                       .select('sequences.id, count(*) as num_of_peptides')

    return nil if p_counts.empty?

    entries = []

    Sequence
      .find(p_counts.min_by(&:num_of_peptides).id)<commit_msg>update documentation for one optimisation<commit_after># == Schema Information
#
# Table name: sequences
#
#  id       :integer          unsigned, not null, primary key
#  sequence :string(50)       not null
#  lca      :integer          unsigned
#  lca_il   :integer          unsigned
#  fa       :binary(16777215)
#  fa_il    :binary(16777215)
#

require 'ostruct'

class Sequence < ApplicationRecord
  include ReadOnlyModel
  self.primary_key = 'id'

  has_many :peptides
  has_many :original_peptides, foreign_key: 'original_sequence_id', primary_key: 'id', class_name: 'Peptide'

  belongs_to :lca_t, foreign_key: 'lca', primary_key: 'id', class_name: 'Taxon'
  belongs_to :lca_il_t, foreign_key: 'lca_il', primary_key: 'id', class_name: 'Taxon'

  alias generated_peptides peptides
  def peptides(equate_il = true)
    if equate_il
      generated_peptides
    else
      original_peptides
    end
  end

  def lineages(equate_il = true, eager = false)
    raise(ArgumentError, ':equate_il must be a boolean') unless Sequence.boolean?(equate_il)
    raise(ArgumentError, ':eager must be a boolean') unless Sequence.boolean?(eager)

    l = Lineage.select('lineages.*, count(*) as hits').joins(uniprot_entries: :peptides).group('lineages.taxon_id')
    l = if equate_il
          l.where('peptides.sequence_id = ?', id)
        else
          l.where('peptides.original_sequence_id = ?', id)
        end
    l = l.includes(:name, Lineage::ORDER_T) if eager
    l
  end

  # Calculates the lowest common ancestor for this sequence
  def calculate_lca(equate_il = true, return_taxon = false)
    if equate_il
      return lca_il_t if return_taxon

      lca_il
    else
      return lca_t if return_taxon

      lca
    end
  end

  def fa_il
    unmarshall_fa(:fa_il)
  end

  def fa
    unmarshall_fa(:fa)
  end

  # Calculates thefor this sequence
  def calculate_fa(equate_il = true)
    equate_il ? fa_il : fa
  end

  def self.missed_cleavage(sequence, equate_il)
    # If no missed cleavages are detected in the given sequence, an empty array will be returned.
    sequences = sequence
                .gsub(/([KR])([^P])/, "\\1\n\\2")
                .gsub(/([KR])([^P])/, "\\1\n\\2")
                .lines.map(&:strip).reject { |i| i.length.zero? }.to_a

    # If only one sequence is present in this array, no missed cleavages were detected for this sequence and we don't
    # need to continue computing these (since it will not make any difference anyway).
    return nil if sequences.length <= 1

    # search the most unique sequence. For each sequence (or thus tryptic peptide here), we can look up all the
    # original proteins in which it occurs. Then we need to check if the original peptide (with missed cleavages)
    # fully occurs in these original proteins (the proteins in which not the complete original peptide is present
    # need to be discarded). It thus makes sense to only look at the proteins from the peptide that's associated
    # with the least amount of proteins.
    p_counts = Sequence.where(sequence: sequences)
                       .joins(equate_il ? :peptides : :original_peptides)
                       .group('sequences.id')
                       .select('sequences.id, count(*) as num_of_peptides')

    return nil if p_counts.empty?

    entries = []

    Sequence
      .find(p_counts.min_by(&:num_of_peptides).id)
{'fraction_non_alphanumeric': 0.0927092709270927, 'fraction_numerical': 0.006900690069006901, 'mean_word_length': 2.8851981351981353, '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': '6680816', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2069, 'n_words': 649}