<commit_before>require 'spec_helper'

describe Genome::Groupers::GeneGrouper do

  def create_entrez_gene_aliases
    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    gene_claims = (1..3).map { Fabricate(:gene_claim, source: entrez_source) }
    gene_claims.each do |gene_claim|
      Fabricate(:gene_claim_alias, gene_claim: gene_claim, nomenclature: 'Gene Symbol')
    end
    gene_claims
  end

  it 'should create a gene entity for each Entrez gene symbol' do
    create_entrez_gene_aliases

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::Gene.all.count).to eq DataModel::GeneClaimAlias.all.count
    gene_names = DataModel::Gene.pluck(:name).sort
    gene_claim_aliases = DataModel::GeneClaimAlias.pluck(:alias).sort
    expect(gene_names).to eq gene_claim_aliases
  end

  it 'should not create gene entities for non-Entrez gene symbols' do
    create_entrez_gene_aliases
    non_entrez_source = Fabricate(:source, source_db_name: 'NotEntrez')
    non_entrez_gene_claim = Fabricate(:gene_claim, source: non_entrez_source)
    gene_claim_alias = Fabricate(:gene_claim_alias, gene_claim: non_entrez_gene_claim)

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::Gene.all.count).to eq DataModel::GeneClaimAlias.all.count - 1
    expect(DataModel::Gene.where(name: gene_claim_alias.alias).count).to eq 0
  end

  it 'should add the gene claims used to create the gene entities to their genes' do
    gene_claims = create_entrez_gene_aliases
    gene_claims.each { |gc| expect(gc.gene).to be_nil }
    Genome::Groupers::GeneGrouper.run
    gene_claims.each { |gc| gc.reload; expect(gc.gene).not_to be_nil }
  end

  it 'should add gene claims to the gene if there was only one direct match' do
    (grouped_gene_claim,_) = create_entrez_gene_aliases
    gene_claim_to_group = Fabricate(:gene_claim,
                                    name: grouped_gene_claim.gene_claim_aliases.first.alias)
    Genome::Groupers::GeneGrouper.run

    expect(grouped_gene_claim.gene).to eq gene_claim_to_group.gene
  end

  it 'should add gene claims if there were no direct matches and one indirect match' do
    grouped_gene_claims = create_entrez_gene_aliases
    test_name = '<NAME>'
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: grouped_gene_claims.first)

    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    ungrouped_gene_claim = Fabricate(:gene_claim, source: entrez_source)
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: ungrouped_gene_claim)

    Genome::Groupers::GeneGrouper.run

    entrez_gene = grouped_gene_claims.first.gene
    grouped_gene = ungrouped_gene_claim.gene

    expect(entrez_gene).to eq grouped_gene
  end

  it 'should add the gene claim to the gene if there is 1 direct match even if there are indirect matches' do
    grouped_gene_claims = create_entrez_gene_aliases
    test_name = '<NAME>'
    Fabricate(:gene_claim_alias, alias: test_name,
              gene_claim: grouped_gene_claims.first)

    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    ungrouped_gene_claim = Fabricate(:gene_claim,
              name: grouped_gene_claims.last.gene_claim_aliases.first.alias,
              source: entrez_source)

    Fabricate(:gene_claim_alias,
              alias: test_name, gene_claim: ungrouped_gene_claim)

    Genome::Groupers::GeneGrouper.run
    ungrouped_gene_claim.reload
    expect(ungrouped_gene_claim.gene).not_to be_nil
  end

  describe 'it should not add gene claims if there was any ambiguity' do
    it 'has > 1 direct match' do
      (grouped_gene_claim, other_grouped_gene_claim) = create_entrez_gene_aliases
      gene_claim_to_group = Fabricate(:gene_claim,
                                      name: grouped_gene_claim.gene_claim_aliases.first.alias)
      Fabricate(:gene_claim_alias, gene_claim: gene_claim_to_group,
                alias: other_grouped_gene_claim.gene_claim_aliases.first.alias)

      Genome::Groupers::GeneGrouper.run

      expect(gene_claim_to_group.gene).to be_nil
    end

    it 'has > 1 indirect' do
      grouped_gene_claims = create_entrez_gene_aliases
      test_name = 'test gene name'
      Fabricate(:gene_claim_alias, alias: test_name, gene_claim: grouped_gene_claims.first)
      test_name2 = 'test gene name 2'
      Fabricate(:gene_claim_alias, alias: test_name2, gene_claim: grouped_gene_claims.last)

      ungrouped_gene_claim = Fabricate(:gene_claim)
      Fabricate(:gene_claim_alias, alias: test_name, gene_claim: ungrouped_gene_claim)
      Fabricate(:gene_claim_alias, alias: test_name2, gene_claim: ungrouped_gene_claim)

      Genome::Groupers::GeneGrouper.run

      expect(ungrouped_gene_claim.gene).to be_nil
    end

  end

  it 'it should add gene aliase' do
    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    other_source = Fabricate(:source, source_db_name: 'Other')
    entrez_gene_claim = Fabricate(:gene_claim, source: entrez_source)
    other_gene_claim = Fabricate(:gene_claim, source: other_source)
    test_name = 'test gene name'
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: entrez_gene_claim, nomenclature: 'Gene Symbol')
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: other_gene_claim, nomenclature: 'Gene Symbol')

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::GeneAlias.count).to eq 1
    expect(DataModel::GeneAlias.first.sources.count).to eq 2

    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: other_gene_claim, nomenclature: 'test nomenclature')

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::GeneAlias.count).to eq 2
  end

  it 'it should add gene attributes' do
    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    other_source = Fabricate(:source, source_db_name: 'Other')
    entrez_gene_claim = Fabricate(:gene_claim, source: entrez_source)
    other_gene_claim = Fabricate(:gene_claim, source: other_source)
    test_name = 'test gene name'
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: entrez_gene_claim, nomenclature: 'Gene Symbol')
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: other_gene_claim, nomenclature: 'Gene Symbol')

    Fabricate(:gene_claim_attribute, gene_claim: entrez_gene_claim, name: 'test attribute', value: 'test value')
    Fabricate(:gene_claim_attribute, gene_claim: other_gene_claim, name: 'test attribute', value: 'test value')

    Genome::Groupers::GeneGrouper.run<commit_msg>Update tests<commit_after>require 'spec_helper'

describe Genome::Groupers::GeneGrouper do

  def create_entrez_gene_aliases
    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    gene_claims = (1..3).map { Fabricate(:gene_claim, source: entrez_source) }
    gene_claims.each do |gene_claim|
      Fabricate(:gene_claim_alias, gene_claim: gene_claim, nomenclature: 'Gene Symbol')
    end
    gene_claims
  end

  it 'should create a gene entity for each Entrez gene symbol' do
    create_entrez_gene_aliases

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::Gene.all.count).to eq DataModel::GeneClaimAlias.all.count
    gene_names = DataModel::Gene.pluck(:name).sort
    gene_claim_aliases = DataModel::GeneClaimAlias.pluck(:alias).sort
    expect(gene_names).to eq gene_claim_aliases
  end

  it 'should not create gene entities for non-Entrez gene symbols' do
    create_entrez_gene_aliases
    non_entrez_source = Fabricate(:source, source_db_name: 'NotEntrez')
    non_entrez_gene_claim = Fabricate(:gene_claim, source: non_entrez_source)
    gene_claim_alias = Fabricate(:gene_claim_alias, gene_claim: non_entrez_gene_claim)

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::Gene.all.count).to eq DataModel::GeneClaimAlias.all.count - 1
    expect(DataModel::Gene.where(name: gene_claim_alias.alias).count).to eq 0
  end

  it 'should add the gene claims used to create the gene entities to their genes' do
    gene_claims = create_entrez_gene_aliases
    gene_claims.each { |gc| expect(gc.gene).to be_nil }
    Genome::Groupers::GeneGrouper.run
    gene_claims.each { |gc| gc.reload; expect(gc.gene).not_to be_nil }
  end

  it 'should add gene claims to the gene if there was only one direct match' do
    (grouped_gene_claim,_) = create_entrez_gene_aliases
    gene_claim_to_group = Fabricate(:gene_claim,
                                    name: grouped_gene_claim.gene_claim_aliases.first.alias)
    Genome::Groupers::GeneGrouper.run

    expect(grouped_gene_claim.gene).to eq gene_claim_to_group.gene
  end

  it 'should add gene claims if there were no direct matches and one indirect match' do
    grouped_gene_claims = create_entrez_gene_aliases
    test_name = 'test gene name'
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: grouped_gene_claims.first)

    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    ungrouped_gene_claim = Fabricate(:gene_claim, source: entrez_source)
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: ungrouped_gene_claim)

    Genome::Groupers::GeneGrouper.run

    entrez_gene = grouped_gene_claims.first.gene
    grouped_gene = ungrouped_gene_claim.gene

    expect(entrez_gene).to eq grouped_gene
  end

  it 'should add the gene claim to the gene if there is 1 direct match even if there are indirect matches' do
    grouped_gene_claims = create_entrez_gene_aliases
    test_name = 'test gene name'
    Fabricate(:gene_claim_alias, alias: test_name,
              gene_claim: grouped_gene_claims.first)

    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    ungrouped_gene_claim = Fabricate(:gene_claim,
              name: grouped_gene_claims.last.gene_claim_aliases.first.alias,
              source: entrez_source)

    Fabricate(:gene_claim_alias,
              alias: test_name, gene_claim: ungrouped_gene_claim)

    Genome::Groupers::GeneGrouper.run
    ungrouped_gene_claim.reload
    expect(ungrouped_gene_claim.gene).not_to be_nil
  end

  describe 'it should not add gene claims if there was any ambiguity' do
    it 'has > 1 direct match' do
      (grouped_gene_claim, other_grouped_gene_claim) = create_entrez_gene_aliases
      gene_claim_to_group = Fabricate(:gene_claim,
                                      name: grouped_gene_claim.gene_claim_aliases.first.alias)
      Fabricate(:gene_claim_alias, gene_claim: gene_claim_to_group,
                alias: other_grouped_gene_claim.gene_claim_aliases.first.alias)

      Genome::Groupers::GeneGrouper.run

      expect(gene_claim_to_group.gene).to be_nil
    end

    it 'has > 1 indirect' do
      grouped_gene_claims = create_entrez_gene_aliases
      test_name = 'test gene name'
      Fabricate(:gene_claim_alias, alias: test_name, gene_claim: grouped_gene_claims.first)
      test_name2 = 'test gene name 2'
      Fabricate(:gene_claim_alias, alias: test_name2, gene_claim: grouped_gene_claims.last)

      ungrouped_gene_claim = Fabricate(:gene_claim)
      Fabricate(:gene_claim_alias, alias: test_name, gene_claim: ungrouped_gene_claim)
      Fabricate(:gene_claim_alias, alias: test_name2, gene_claim: ungrouped_gene_claim)

      Genome::Groupers::GeneGrouper.run

      expect(ungrouped_gene_claim.gene).to be_nil
    end

  end

  it 'it should add gene aliase' do
    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    other_source = Fabricate(:source, source_db_name: 'Other')
    entrez_gene_claim = Fabricate(:gene_claim, source: entrez_source)
    other_gene_claim = Fabricate(:gene_claim, source: other_source)
    test_name = '<NAME>'
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: entrez_gene_claim, nomenclature: 'Gene Symbol')
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: other_gene_claim, nomenclature: 'Gene Symbol')

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::GeneAlias.count).to eq 1
    expect(DataModel::GeneAlias.first.sources.count).to eq 2

    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: other_gene_claim, nomenclature: 'test nomenclature')

    Genome::Groupers::GeneGrouper.run

    expect(DataModel::GeneAlias.count).to eq 1
  end

  it 'it should add gene attributes' do
    entrez_source = Fabricate(:source, source_db_name: 'Entrez')
    other_source = Fabricate(:source, source_db_name: 'Other')
    entrez_gene_claim = Fabricate(:gene_claim, source: entrez_source)
    other_gene_claim = Fabricate(:gene_claim, source: other_source)
    test_name = 'test gene name'
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: entrez_gene_claim, nomenclature: 'Gene Symbol')
    Fabricate(:gene_claim_alias, alias: test_name, gene_claim: other_gene_claim, nomenclature: 'Gene Symbol')

    Fabricate(:gene_claim_attribute, gene_claim: entrez_gene_claim, name: 'test attribute', value: 'test value')
    Fabricate(:gene_claim_attribute, gene_claim: other_gene_claim, name: 'test attribute', value: 'test value')

    Genome::Groupers::GeneGrouper.run
{'fraction_non_alphanumeric': 0.08598996526437669, 'fraction_numerical': 0.002161327672713238, 'mean_word_length': 4.503823279524214, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5435736', 'n_tokens_mistral': 4648, 'n_tokens_neox': 4219, 'n_words': 890}