Search in sources :

Example 1 with CatalogException

use of org.opencb.opencga.catalog.exceptions.CatalogException in project opencga by opencb.

the class IndividualQcUtils method getSamples.

public static List<String> getSamples(String study, String familyId, CatalogManager catalogManager, String token) throws ToolException {
    Set<String> sampleSet = new HashSet<>();
    // Sanity check
    if (StringUtils.isEmpty(familyId)) {
        throw new ToolException("Missing family ID");
    }
    try {
        OpenCGAResult<Family> familyResult = catalogManager.getFamilyManager().get(study, familyId, QueryOptions.empty(), token);
        // Check family result
        if (familyResult.getResults().size() == 0) {
            throw new ToolException("Family not found for family ID '" + familyId + "'");
        }
        if (familyResult.getResults().size() > 1) {
            throw new ToolException("More than one family result for family ID '" + familyId + "'");
        }
        // Get list of individual IDs
        List<String> individualIds = familyResult.first().getMembers().stream().map(m -> m.getId()).collect(Collectors.toList());
        // Populate individual (and sample IDs) from individual IDs
        Query query = new Query(IndividualDBAdaptor.QueryParams.ID.key(), individualIds);
        QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, "samples.id");
        OpenCGAResult<Individual> individualResult = catalogManager.getIndividualManager().search(study, query, queryOptions, token);
        for (Individual individual : individualResult.getResults()) {
            sampleSet.addAll(individual.getSamples().stream().map(s -> s.getId()).collect(Collectors.toList()));
        }
    } catch (CatalogException e) {
        throw new ToolException(e);
    }
    return sampleSet.stream().collect(Collectors.toList());
}
Also used : OpenCGAResult(org.opencb.opencga.core.response.OpenCGAResult) java.util(java.util) VariantType(org.opencb.biodata.models.variant.avro.VariantType) IndividualDBAdaptor(org.opencb.opencga.catalog.db.api.IndividualDBAdaptor) VariantStorageManager(org.opencb.opencga.analysis.variant.manager.VariantStorageManager) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) QueryOptions(org.opencb.commons.datastore.core.QueryOptions) ToolException(org.opencb.opencga.core.exceptions.ToolException) ProjectDBAdaptor(org.opencb.opencga.catalog.db.api.ProjectDBAdaptor) TPED(org.opencb.opencga.storage.core.variant.io.VariantWriterFactory.VariantOutputFormat.TPED) Path(java.nio.file.Path) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) Project(org.opencb.opencga.core.models.project.Project) VariantQueryParam(org.opencb.opencga.storage.core.variant.adaptors.VariantQueryParam) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Command(org.opencb.commons.exec.Command) File(java.io.File) DockerUtils(org.opencb.commons.utils.DockerUtils) StorageEngineException(org.opencb.opencga.storage.core.exceptions.StorageEngineException) Individual(org.opencb.opencga.core.models.individual.Individual) Sample(org.opencb.opencga.core.models.sample.Sample) Family(org.opencb.opencga.core.models.family.Family) PlinkWrapperAnalysisExecutor(org.opencb.opencga.analysis.wrappers.plink.PlinkWrapperAnalysisExecutor) Query(org.opencb.commons.datastore.core.Query) CatalogManager(org.opencb.opencga.catalog.managers.CatalogManager) InternalStatus(org.opencb.opencga.core.models.common.InternalStatus) Query(org.opencb.commons.datastore.core.Query) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) QueryOptions(org.opencb.commons.datastore.core.QueryOptions) Individual(org.opencb.opencga.core.models.individual.Individual) Family(org.opencb.opencga.core.models.family.Family) ToolException(org.opencb.opencga.core.exceptions.ToolException)

Example 2 with CatalogException

use of org.opencb.opencga.catalog.exceptions.CatalogException in project opencga by opencb.

the class IndividualQcUtils method getFamilyByIndividualId.

public static Family getFamilyByIndividualId(String studyId, String individualId, CatalogManager catalogManager, String token) throws ToolException {
    Query query = new Query();
    query.put("members", individualId);
    OpenCGAResult<Family> familyResult;
    try {
        familyResult = catalogManager.getFamilyManager().search(studyId, query, QueryOptions.empty(), token);
    } catch (CatalogException e) {
        throw new ToolException(e);
    }
    if (familyResult.getNumResults() == 0) {
        return null;
    }
    // Return the first family
    return familyResult.first();
}
Also used : Query(org.opencb.commons.datastore.core.Query) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) Family(org.opencb.opencga.core.models.family.Family) ToolException(org.opencb.opencga.core.exceptions.ToolException)

Example 3 with CatalogException

use of org.opencb.opencga.catalog.exceptions.CatalogException in project opencga by opencb.

the class InferredSexComputation method computeRatios.

@Deprecated
public static double[] computeRatios(String study, String sample, String assembly, FileManager fileManager, AlignmentStorageManager alignmentStorageManager, String token) throws ToolException {
    // Look for the bam file for each sample
    OpenCGAResult<File> fileQueryResult;
    Query query = new Query(FileDBAdaptor.QueryParams.FORMAT.key(), File.Format.BAM);
    QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, FileDBAdaptor.QueryParams.UUID.key());
    query.put(FileDBAdaptor.QueryParams.SAMPLE_IDS.key(), sample);
    try {
        fileQueryResult = fileManager.search(study, query, queryOptions, token);
    } catch (CatalogException e) {
        throw new ToolException(e);
    }
    // Sanity check
    if (fileQueryResult.getNumResults() == 0) {
        throw new ToolException("BAM file not found for sample " + sample);
    }
    if (fileQueryResult.getNumResults() > 1) {
        throw new ToolException("Found more than one BAM files (" + fileQueryResult.getNumResults() + ") for sample " + sample);
    }
    // Compute coverage for each chromosome for each BAM file
    // TODO get chromosomes from cellbase
    Map<String, Integer> chromosomes;
    if (assembly.toLowerCase().equals("grch37")) {
        chromosomes = GRCH37_CHROMOSOMES;
    } else {
        chromosomes = GRCH38_CHROMOSOMES;
    }
    double[] means = new double[] { 0d, 0d, 0d };
    for (String chrom : chromosomes.keySet()) {
        int chromSize = chromosomes.get(chrom);
        Region region = new Region(chrom, 1, chromSize - 1);
        try {
            List<RegionCoverage> regionCoverages = alignmentStorageManager.coverageQuery(study, fileQueryResult.first().getUuid(), region, 0, 100, chromSize, token).getResults();
            double meanCoverage = 0d;
            for (RegionCoverage regionCoverage : regionCoverages) {
                meanCoverage += regionCoverage.meanCoverage();
            }
            meanCoverage /= regionCoverages.size();
            String name = chrom.toUpperCase();
            switch(name) {
                case "Y":
                    {
                        means[2] = meanCoverage;
                        break;
                    }
                case "X":
                    {
                        means[1] = meanCoverage;
                        break;
                    }
                default:
                    {
                        means[0] += meanCoverage;
                        break;
                    }
            }
        } catch (Exception e) {
            throw new ToolException(e);
        }
    }
    means[0] /= (chromosomes.size() - 2);
    // Create sex report for that sample
    return new double[] { 1.0d * means[1] / means[0], 1.0d * means[2] / means[0] };
}
Also used : Query(org.opencb.commons.datastore.core.Query) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) QueryOptions(org.opencb.commons.datastore.core.QueryOptions) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) IOException(java.io.IOException) ToolException(org.opencb.opencga.core.exceptions.ToolException) Region(org.opencb.biodata.models.core.Region) RegionCoverage(org.opencb.biodata.models.alignment.RegionCoverage) ToolException(org.opencb.opencga.core.exceptions.ToolException) File(org.opencb.opencga.core.models.file.File)

Example 4 with CatalogException

use of org.opencb.opencga.catalog.exceptions.CatalogException in project opencga by opencb.

the class JobIndexTask method run.

@Override
protected void run() throws Exception {
    // Get all the studies
    Query query = new Query();
    QueryOptions options = new QueryOptions().append(QueryOptions.INCLUDE, Arrays.asList(StudyDBAdaptor.QueryParams.UID.key(), StudyDBAdaptor.QueryParams.ID.key(), StudyDBAdaptor.QueryParams.FQN.key(), StudyDBAdaptor.QueryParams.VARIABLE_SET.key())).append(DBAdaptor.INCLUDE_ACLS, true);
    OpenCGAResult<Study> studyDataResult = catalogManager.getStudyManager().search(query, options, token);
    if (studyDataResult.getNumResults() == 0) {
        throw new CatalogException("Could not index catalog into solr. No studies found");
    }
    // Create solr collections if they don't exist
    catalogSolrManager.createSolrCollections(CatalogSolrManager.JOB_SOLR_COLLECTION);
    for (Study study : studyDataResult.getResults()) {
        Map<String, Set<String>> studyAcls = SolrConverterUtil.parseInternalOpenCGAAcls((List<Map<String, Object>>) study.getAttributes().get("OPENCGA_ACL"));
        // We replace the current studyAcls for the parsed one
        study.getAttributes().put("OPENCGA_ACL", studyAcls);
        indexJob(catalogSolrManager, study);
    }
}
Also used : Study(org.opencb.opencga.core.models.study.Study) Set(java.util.Set) Query(org.opencb.commons.datastore.core.Query) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) QueryOptions(org.opencb.commons.datastore.core.QueryOptions) Map(java.util.Map)

Example 5 with CatalogException

use of org.opencb.opencga.catalog.exceptions.CatalogException in project opencga by opencb.

the class TieringInterpretationAnalysisExecutor method query.

private Boolean query(Pedigree pedigree, Disorder disorder, Map<String, String> sampleMap, ClinicalProperty.ModeOfInheritance moi, Map<ClinicalProperty.ModeOfInheritance, List<Variant>> resultMap) {
    Query query;
    Map<String, List<String>> genotypes;
    switch(moi) {
        case AUTOSOMAL_DOMINANT:
            query = new Query(dominantQuery);
            genotypes = ModeOfInheritance.dominant(pedigree, disorder, penetrance);
            break;
        case Y_LINKED:
            query = new Query(dominantQuery).append(VariantQueryParam.REGION.key(), "Y");
            genotypes = ModeOfInheritance.yLinked(pedigree, disorder, penetrance);
            break;
        case X_LINKED_DOMINANT:
            query = new Query(dominantQuery).append(VariantQueryParam.REGION.key(), "X");
            genotypes = ModeOfInheritance.xLinked(pedigree, disorder, true, penetrance);
            break;
        case AUTOSOMAL_RECESSIVE:
            query = new Query(recessiveQuery);
            genotypes = ModeOfInheritance.recessive(pedigree, disorder, penetrance);
            break;
        case X_LINKED_RECESSIVE:
            query = new Query(recessiveQuery).append(VariantQueryParam.REGION.key(), "X");
            genotypes = ModeOfInheritance.xLinked(pedigree, disorder, false, penetrance);
            break;
        case MITOCHONDRIAL:
            query = new Query(mitochondrialQuery);
            genotypes = ModeOfInheritance.mitochondrial(pedigree, disorder, penetrance);
            filterOutHealthyGenotypes(genotypes);
            break;
        default:
            return false;
    }
    query.append(VariantQueryParam.INCLUDE_GENOTYPE.key(), true).append(VariantQueryParam.STUDY.key(), studyId).append(VariantQueryParam.FILTER.key(), VCFConstants.PASSES_FILTERS_v4).append(VariantQueryParam.UNKNOWN_GENOTYPE.key(), "./.");
    if (ModeOfInheritance.isEmptyMapOfGenotypes(genotypes)) {
        return false;
    }
    addGenotypeFilter(genotypes, sampleMap, query);
    try {
        resultMap.put(moi, clinicalInterpretationManager.getVariantStorageManager().get(query, QueryOptions.empty(), sessionId).getResults());
    } catch (CatalogException | StorageEngineException | IOException e) {
        return false;
    }
    return true;
}
Also used : Query(org.opencb.commons.datastore.core.Query) StorageEngineException(org.opencb.opencga.storage.core.exceptions.StorageEngineException) CatalogException(org.opencb.opencga.catalog.exceptions.CatalogException) IOException(java.io.IOException)

Aggregations

CatalogException (org.opencb.opencga.catalog.exceptions.CatalogException)375 Study (org.opencb.opencga.core.models.study.Study)157 AuditRecord (org.opencb.opencga.core.models.audit.AuditRecord)131 Query (org.opencb.commons.datastore.core.Query)117 Status (org.opencb.biodata.models.common.Status)111 QueryOptions (org.opencb.commons.datastore.core.QueryOptions)108 ObjectMap (org.opencb.commons.datastore.core.ObjectMap)96 OpenCGAResult (org.opencb.opencga.core.response.OpenCGAResult)96 IOException (java.io.IOException)69 ToolException (org.opencb.opencga.core.exceptions.ToolException)65 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)48 Sample (org.opencb.opencga.core.models.sample.Sample)48 File (org.opencb.opencga.core.models.file.File)47 StorageEngineException (org.opencb.opencga.storage.core.exceptions.StorageEngineException)44 Individual (org.opencb.opencga.core.models.individual.Individual)37 Collectors (java.util.stream.Collectors)34 java.util (java.util)30 CatalogManager (org.opencb.opencga.catalog.managers.CatalogManager)28 FileStatus (org.opencb.opencga.core.models.file.FileStatus)27 Path (java.nio.file.Path)26