Skip to content

Commit

Permalink
Restricting getTopVariants to top k, adding variantstore support
Browse files Browse the repository at this point in the history
  • Loading branch information
meatcar authored and buske committed Nov 22, 2016
1 parent 38639c9 commit 7b04dcc
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public interface Exome
Double getGeneScore(String gene);

/**
* Get {@link Variant}s for a gene.
*
* Get {@link Variant}s for a gene, limit to k variants.
* @param gene the gene to get {@link Variant}s for.
* @param k the number of variants to return.
* @return an unmodifiable (potentially-empty) list of top {@link Variant}s for the gene, by decreasing score
*/
List<Variant> getTopVariants(String gene);
List<Variant> getTopVariants(String gene, int k);

/**
* Get the n highest genes, in descending order of score. If there are fewer than n genes, all will be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import org.json.JSONObject;

/**
* Base class for implementing exome-related classes, specifically those that include scored genes with variants.
* Base class for implementing exome-related classes, specifically those that
* include scored genes with variants.
*
* @version $Id$
* @since 1.0M6
Expand All @@ -58,13 +59,18 @@ public Double getGeneScore(String gene)
}

@Override
public List<Variant> getTopVariants(String gene)
public List<Variant> getTopVariants(String gene, int k)
{
List<Variant> result = this.variants.get(gene);
if (result == null) {
result = Collections.emptyList();
return Collections.emptyList();
}

if (k < result.size()) {
return result.subList(0, k);
} else {
return result;
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ public Double getGeneScore(String gene)
}

@Override
public List<Variant> getTopVariants(String gene)
public List<Variant> getTopVariants(String gene, int k)
{
if (this.matchGenotype == null) {
List<Variant> topVariants = Collections.emptyList();
return topVariants;
return Collections.emptyList();
} else {
return this.matchGenotype.getTopVariants(gene);
return this.matchGenotype.getTopVariants(gene, k);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ public Set<String> getGenes()
}

@Override
public List<Variant> getTopVariants(String gene)
public List<Variant> getTopVariants(String gene, int k)
{
//TODO add manually entered variants
List<Variant> variants;
if (this.exome == null) {
variants = new ArrayList<Variant>();
} else {
variants = this.exome.getTopVariants(gene);
variants = this.exome.getTopVariants(gene, k);
}
return variants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public class DefaultPatientGenotypeSimilarityView extends AbstractPatientGenotyp
*/
private static final int MAX_GENES_REPORTED_IN_JSON = 20;

/**
* Maximum number of variants to report per gene in JSON (0 for all). TODO: pull out into configuration.
*/
private static final int MAX_VARIANTS_PER_GENE_REPORTED_IN_JSON = 10;


/**
* Simple constructor passing the {@link #match matched patient}, the {@link #reference reference patient}, and the
* {@link #access patient access type}.
Expand Down Expand Up @@ -211,12 +217,12 @@ protected JSONObject getGeneJSON(String gene)
{
JSONObject variantsJSON = new JSONObject();
if (this.refGenotype != null) {
List<Variant> variants = this.refGenotype.getTopVariants(gene);
List<Variant> variants = this.refGenotype.getTopVariants(gene, MAX_VARIANTS_PER_GENE_REPORTED_IN_JSON);
variantsJSON.put("reference", getVariantsJSON(variants));
}
if (this.matchGenotype != null) {
// Use potentially access-controlled method to try to get variants
List<Variant> variants = this.getTopVariants(gene);
List<Variant> variants = this.getTopVariants(gene, MAX_VARIANTS_PER_GENE_REPORTED_IN_JSON);
variantsJSON.put("match", getVariantsJSON(variants));
}
return variantsJSON;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,6 @@ public Double getGeneScore(String gene)
return this.geneScores.get(gene);
}

@Override
public List<Variant> getTopVariants(String gene)
{
List<Variant> vs = this.variants.get(gene);
if (vs == null) {
vs = Collections.emptyList();
}
return vs;
}

@Override
public JSONArray toJSON()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*/
package org.phenotips.data.similarity.internal;

import org.phenotips.variantstore.shared.GACallInfoFields;
import org.phenotips.variantstore.shared.GAVariantInfoFields;
import org.phenotips.variantstore.shared.VariantUtils;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ga4gh.GACall;
import org.ga4gh.GAVariant;

/**
* A variant from the variant store. Annotated by Exomiser.
*
* @version $Id$
*/
public class GAVariantVariant extends AbstractVariant
{
/**
* Create a {@link Variant} from a {@link GAVariant} returned by a {@link
* org.phenotips.variantstore.VariantStoreInterface}.
*
* @param gaVariant a {@link GAVariant}
*/
public GAVariantVariant(GAVariant gaVariant) {
setChrom(gaVariant.getReferenceName());
setPosition((int) (gaVariant.getStart() + 1));

GACall call = gaVariant.getCalls().get(0);
List<Integer> genotype = call.getGenotype();

setGenotype(gaVariant.getReferenceBases(),
StringUtils.join(gaVariant.getAlternateBases(), ','),
StringUtils.join(genotype, '/'));

setEffect(VariantUtils.getInfo(gaVariant, GAVariantInfoFields.GENE_EFFECT));

Object value = VariantUtils.getInfo(call, GACallInfoFields.EXOMISER_VARIANT_SCORE);
if (value == null || value.equals("null")) {
setScore(0D);
} else {
setScore(Double.valueOf((String) value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public RestrictedPatientGenotypeSimilarityView(Patient match, Patient reference,
}

@Override
public List<Variant> getTopVariants(String gene)
public List<Variant> getTopVariants(String gene, int k)
{
if (this.access.isOpenAccess()) {
return super.getTopVariants(gene);
return super.getTopVariants(gene, k);
} else if (this.access.isLimitedAccess()) {
List<Variant> restrictedVariants = new ArrayList<Variant>();
for (Variant v : super.getTopVariants(gene)) {
for (Variant v : super.getTopVariants(gene, k)) {
restrictedVariants.add(new RestrictedVariant(v));
}
return restrictedVariants;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*/
package org.phenotips.data.similarity.internal;

import org.phenotips.data.Patient;
import org.phenotips.data.similarity.Exome;
import org.phenotips.data.similarity.Variant;
import org.phenotips.variantStoreIntegration.VariantStoreService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.ga4gh.GAVariant;
import org.json.JSONArray;

/**
* An exome implementation that's backed by {{@link VariantStore}}.
*
* @version $Id$
*/
public class VariantstoreExome implements Exome
{
private final Patient patient;

private VariantStoreService vs;

/**
* Initialize the exome with the variant store and the patient.
*
* @param vs the Variant Store instance
* @param p the patient
*/
public VariantstoreExome(VariantStoreService vs, Patient p) {
this.vs = vs;
this.patient = p;
}

/**
* Get the names of all genes with variants in the patient.
*
* @return an unmodifiable set of gene names with variants in the patient
*/
@Override
public Set<String> getGenes() {
return vs.getAllGenesForIndividual(patient.getId());
}

/**
* Return the score for a gene.
*
* @param gene the gene in question
*
* @return the score of the gene, between 0 and 1, where 1 is better (or
* {@code null} if no variants for gene)
*/
@Override
public Double getGeneScore(String gene) {
return vs.getGeneScore(patient.getId(), gene);
}

/**
* Get {@link Variant}s for a gene.
*
* @param gene the gene to get {@link Variant}s for.
* @param k
*
* @return an unmodifiable (potentially-empty) list of top {@link Variant}s
* for the gene, by decreasing score
*/
@Override
public List<Variant> getTopVariants(String gene, int k) {
List<GAVariant> gaVariants = vs.getTopHarmfullVariantsForGene(patient.getId(), gene, k);
List<Variant> variants = new ArrayList<>();
for (GAVariant gaVariant : gaVariants) {
variants.add(new GAVariantVariant(gaVariant));
}
return variants;
}

/**
* Get the n highest genes, in descending order of score. If there are fewer
* than n genes, all will be returned.
*
* @param n the number of genes to return (specify 0 for all)
*
* @return an unmodifiable (potentially-empty) list of gene names
*/
@Override
public List<String> getTopGenes(int n) {
return vs.getTopGenesForIndividual(patient.getId(), n);
}

/**
* Retrieve all variant information in a JSON format. For example:
* <p/>
* <pre>
* [
* {
* "gene": "SRCAP",
* "score": 0.7, // phenotype score
* "variants": [ // variants sorted by decreasing score
* {
* "score": 0.8, // genotype score
* "chrom": "1",
* "position": 2014819,
* "type": "SPLICING",
* "ref": "A",
* "alt": "T",
* },
* {...},
* ]
* }
* ]
* </pre>
*
* @return the data about this value, using the org.json classes
*/
@Override
public JSONArray toJSON() {
return null;
}
}
Loading

0 comments on commit 7b04dcc

Please sign in to comment.