Skip to content

Commit

Permalink
rudimentary genotype-phenotype search
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Coffman committed Sep 15, 2016
1 parent b63bb45 commit 86baa91
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 16 deletions.
15 changes: 11 additions & 4 deletions app/models/ga4gh/converters/evidence_item.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
require 'ga4gh/genotype_phenotype_pb'

module Gaa4gh; module Converters
module Ga4gh; module Converters
class EvidenceItem
def from_ga4gh(json)
end
attr_reader :evidence_item

def initialize(evidence_item)
@evidence_item = evidence_item
end

def to_ga4gh(evidence_item)
def to_ga4gh
Ga4gh::Evidence.new(
evidence_type: nil,
description: evidence_item.description,
info: {}
)
end
end
end; end
Expand Down
64 changes: 64 additions & 0 deletions app/models/ga4gh/converters/external_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'ga4gh/common_pb'

add_message "ga4gh.ExternalIdentifier" do
optional :database, :string, 1
optional :identifier, :string, 2
optional :version, :string, 3
end
module Ga4gh; module Converters
class ExternalIdentifier
attr_reader :ext_id

def initialize(ext_id)
@ext_id = ext_id
end

def is_supported?
self.class.databases(ext_id)
end

def error
if is_supported?
nil
else
"Database #{ext_id.database} version #{ext_id.version} is not currently supported"
end
end

private
def self.databases(ext_id)
@databases ||= {
'entrez' => :handle_entrez
'disease ontology' => :handle_doid
'sequence ontology' => :handle_soid
'pubmed' => :handle_pubmed
}
end
def handle_entrez(query)
query.where(evidence_items: { variants: { genes: { entrez_id: ext_id.identifier }}}
end
def handle_doid(query)
term = if ext_id.identifier =~ /doid/i
ext_id.identifier.downcase.gsub('doid:', '')
else
ext_id.identifier
end
query.where(doid: term)
end
def handle_soid(query)
term = if ext_id.identifier =~ /soid/i
ext_id.identifier.downcase.gsub('soid:', '')
else
ext_id.identifier
end
query.where(evidence_items: { variants: { variant_types: { soid: term }}})
end

def handle_pubmed(query)
query.where(evidence_items: { sources: { pubmed_id: ext_id.identifier }})
end
end
end
41 changes: 38 additions & 3 deletions app/models/ga4gh/converters/feature_phenotype_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,50 @@

module Ga4gh; module Converters
class FeaturePhenotypeAssociation
attr_reader :diseases

def initialize(diseases)
@diseases = diseases
end

def to_ga4gh
{
environmental_contexts: environmental_contexts
}
diseases.map do |disease|
Ga4gh::FeaturePhenotypeAssociation.new(
id: '',
phenotype_association_set_id: '',
feature_ids: feature_ids(disease),
evidence: evidence(disease),
phenotype: Ga4gh::Converters::PhenotypeDisease.new(disease).to_ga4gh,
description: '',
environmental_contexts: environmental_contexts,
info: {}
)
end
end

private
def feature_ids(disease)
disease.evidence_items.map(&:variant).map(&:id).map(&:to_s).uniq
end

def evidence(disease)
disease.evidence_items.map do |ei|
Ga4gh::Converters::EvidenceItem.new(ei).to_ga4gh
end
end

def environmental_contexts
#drug?
[
Ga4gh::EnvironmentalContext.new(
id: '',
description: 'cancer',
environment_type: Ga4gh::OntologyTerm.new(
id: 'http://purl.obolibrary.org/obo/OBI_1110053',
term: 'cancer',
source_name: 'Ontology for Biomedical Investigation',
source_version: ''
)
)
]
end
Expand Down
7 changes: 5 additions & 2 deletions app/models/ga4gh/converters/phenotype_disease.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

module Ga4gh; module Converters
class PhenotypeDisease
def from_ga4gh(json)
attr_reader :disease

def initialize(disease)
@disease = disease
end

def to_ga4gh(disease)
def to_ga4gh
Ga4gh::PhenotypeInstance.new(
id: disease.id.to_s,
type: Ga4gh::Converters::Disease.new.to_ga4gh(disease),
Expand Down
46 changes: 43 additions & 3 deletions app/models/ga4gh/queries/feature_phenotype_associations_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,56 @@ def initialize(request_body)
end

def perform
query = generate_query
evidence_query = request.evidence_query
variant_ids = request.feature_ids
disease_ids = request.phenotype_ids

if evidence_query || variant_ids || disease_ids
initial_diseases = Disease.eager_load(evidence_items: [:source, variant: [:gene, :variant_types]])
if disease_ids && disease_ids.any?
initial_diseases = initial_diseases.where(id: disease_ids)
end

if variant_ids && variant_ids.any?
initial_diseases = initial_diseases.where(evidence_items: { variants: {id: variant_ids }})
end

if evidence_query
initial_diseases = process_evidence_query(evidence_query, initial_diseases)
end

response.associations = Ga4gh::Converters::FeaturePhenotypeAssociation.new(diseases).to_ga4gh
response.next_page_token = next_page_token(initial_diseases)
else
@errors << 'Please specify at least one of the following: features, phenotypes, evidence'
end
end

def success?
errors.none?
end

private
def generate_query
::EvidenceItems.view_scope
def process_evidence_query(evidence_query, db_query)
if evidence_query.evidenceType.present?
@errors << 'Evidence Type query is not currently supported'
end

if evidence_query.description.present?
db_query = db_query.where('evidence_items.description ILIKE ?', "%#{evidence_query.description}%")
end

if evidence_query.external_identifiers.present?
evidence_query.external_identifiers.each do |ext_id|
converter = Ga4gh::Converters::ExternalIdentifier.new(ext_id)
if converter.is_supported?
db_query = converter.filter(db_query)
else
@errors << converter.error
end
end
end
db_query
end
end
end; end
12 changes: 8 additions & 4 deletions app/models/ga4gh/queries/with_pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

module Ga4gh; module Queries;
module WithPagination
def next_page_token
page_size = current_page_size
page_number = current_page
Base64.encode64({ 's' => page_size, 'n' => page_number + 1 }.to_json).strip
def next_page_token(query)
if query.count > current_page_size * current_page
page_size = current_page_size
page_number = current_page
Base64.encode64({ 's' => page_size, 'n' => page_number + 1 }.to_json).strip
else
nil
end
end

def paginate_query(query)
Expand Down

0 comments on commit 86baa91

Please sign in to comment.