Class: Ensembl::Variation::Variation

Inherits:
ModelBase show all
Defined in:
lib/ensembl/variation/activerecord.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PrimaryKeyOverrides

#primary_key

Methods included from TableNameOverrides

#table_name

Methods included from ConnectionPool

connection

Class Method Details

.find_all_by_name(name) ⇒ Object



596
597
598
599
600
601
602
603
# File 'lib/ensembl/variation/activerecord.rb', line 596

def self.find_all_by_name(name)
  v_ids = where(name: name).pluck(:variation_id)
  v_ids = variation_synonyms.where(name: name).pluck(:variation_id) if v_ids.nil?

  return nil if v_ids.nil?

  where(variation_id: v_ids).order(:name)
end

.find_by_name(name) ⇒ Object

Find Variation by also using VariationSynonyms @name: name of the variation @return: [Variation]



588
589
590
591
592
593
594
# File 'lib/ensembl/variation/activerecord.rb', line 588

def self.find_by_name(name)
  v  = self.find_by(name: name)
  return v unless v.nil?
  vs = VariationSynonym.eager_load(:variation).find_by(name: name)
  return vs.variation unless vs.nil?
  nil
end

Instance Method Details

#all_phenotype_featuresObject



498
499
500
501
502
# File 'lib/ensembl/variation/activerecord.rb', line 498

def all_phenotype_features
  object_ids = synonym_names
  object_ids<<name
  PhenotypeFeature.eager_load(:phenotype).where(object_id: object_ids, type: 'Variation')
end

#genesObject



605
606
607
# File 'lib/ensembl/variation/activerecord.rb', line 605

def genes
  variation_genenames.pluck(:gene_name)
end

#genotype_code_idsObject



575
576
577
# File 'lib/ensembl/variation/activerecord.rb', line 575

def genotype_code_ids
  @genotype_code_ids||=individual_genotypes.values.uniq
end

#genotype_codesObject

Unique genotype codes from individual_genotypes



581
582
583
# File 'lib/ensembl/variation/activerecord.rb', line 581

def genotype_codes
  @genotype_codes||=GenotypeCode.genotypes_hash_for(genotype_code_ids)
end

#genotype_countsObject

Genotype counts for each population “CSHL-HAPMAP:HapMap-YRI”=>“C|T”=>1}



549
550
551
552
553
554
555
# File 'lib/ensembl/variation/activerecord.rb', line 549

def genotype_counts
  counts = Hash.new{ |hsh,k| hsh[k] = Hash.new 0 }

  individual_populations.pluck('population.name',:individual_id).map{|ip| [ip[0],genotype_codes[individual_genotypes[ip[1]]]] }.each{|r| counts[r[0]][r[1]]+=1}

  return counts
end

#individual_genotype_idsObject



565
566
567
# File 'lib/ensembl/variation/activerecord.rb', line 565

def individual_genotype_ids
  individual_genotypes.keys
end

#individual_genotypesObject

Individual and genotype_code id’s related to variation Example:

[1,2],,[<individual_id>,<genotype_code_id>]


561
562
563
# File 'lib/ensembl/variation/activerecord.rb', line 561

def individual_genotypes
  @individual_genotypes||=compressed_genotype_vars.map{|cgv| cgv.unpacked_genotypes }.flatten(1).to_h
end

#individual_populationsObject

IndividualPopulations from individual_genotypes



571
572
573
# File 'lib/ensembl/variation/activerecord.rb', line 571

def individual_populations
  IndividualPopulation.where(individual_id: individual_genotype_ids)
end

#phenotype_featuresObject



494
495
496
# File 'lib/ensembl/variation/activerecord.rb', line 494

def phenotype_features
  PhenotypeFeature.eager_load(:phenotype).where(object_id_column: name, type: 'Variation')
end

#phenotype_features_hashObject

Made because of the need to cut down database queries { phenotype_feature_id =>

 { :phenotype=> "Phenotype description" ,
   :phenotype_id => _ ,
   :p_value => _ ,
   :odds_ratio => _,
   :risk_allele => _ },
phenotype_feature_id =>
 { :phenotype=> "Phenotype description" ,
   :phenotype_id => _ ,
   :p_value => _ ,
   :odds_ratio => _,
   :risk_allele => _ }}

Returns:



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/ensembl/variation/activerecord.rb', line 518

def phenotype_features_hash

  # Do enable two level inserts hsh[:first][:second]
  hash=Hash.new{ |hsh,key| hsh[key] = Hash.new {} }

  all_phenotype_features
  .joins(:phenotype)
  .pluck(
      :phenotype_feature_id,
      'phenotype.description',
      :phenotype_id)
  .each{ |r| hash[r[0]][:phenotype]=r[1]; hash[r[0]][:phenotype_id]=r[2]}

  PhenotypeFeatureAttrib
  .where(phenotype_feature_id: hash.keys)
  .pluck(
      'phenotype_feature_attrib.phenotype_feature_id',
      'phenotype_feature_attrib.value',
      'phenotype_feature_attrib.attrib_type_id')
  .each{ |v| hash[v[0]][AttribType.symbol(v[2])]=v[1] }

  hash
end

#positionsObject



609
610
611
# File 'lib/ensembl/variation/activerecord.rb', line 609

def positions
  variation_features.includes(:seq_region).pluck('seq_region.name',:seq_region_start,:seq_region_end,:seq_region_strand).map{|r| Ensembl::Helpers::VariationPosition.new(r)}
end

#synonym_namesObject



542
543
544
# File 'lib/ensembl/variation/activerecord.rb', line 542

def synonym_names
  variation_synonyms.map{|vs| vs.name}
end