Skip to content

Commit

Permalink
analysis: use constants in QC analysis, #TASK-6772, #TASK-6766
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Sep 10, 2024
1 parent 90436dd commit a6a8c86
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

public class AnalysisUtils {

public static final String ANALYSIS_FOLDER = "analysis/";
public static final String ANALYSIS_RESOURCES_FOLDER = ANALYSIS_FOLDER + "resources/";

public static boolean isSupportedCommand(String commands) {
Set<String> commandSet = new HashSet<>(Arrays.asList(commands.replace(" ", "").split(",")));
if (!commandSet.contains(commands)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.opencga.analysis.ResourceUtils;
import org.opencb.opencga.analysis.tools.OpenCgaToolScopeStudy;
import org.opencb.opencga.analysis.variant.inferredSex.InferredSexAnalysis;
import org.opencb.opencga.analysis.variant.relatedness.RelatednessAnalysis;
import org.opencb.opencga.catalog.db.api.IndividualDBAdaptor;
import org.opencb.opencga.catalog.exceptions.CatalogException;
Expand Down Expand Up @@ -51,14 +50,20 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.opencb.opencga.analysis.AnalysisUtils.ANALYSIS_FOLDER;
import static org.opencb.opencga.analysis.AnalysisUtils.ANALYSIS_RESOURCES_FOLDER;
import static org.opencb.opencga.core.models.common.InternalStatus.READY;
import static org.opencb.opencga.core.models.common.QualityControlStatus.COMPUTING;
import static org.opencb.opencga.core.models.study.StudyPermissions.Permissions.WRITE_SAMPLES;

public class VariantQcAnalysis extends OpenCgaToolScopeStudy {

// For relatedness analysis
// QC folders
public static final String QC_FOLDER = "qc/";
public static final String QC_DATA_FOLDER = QC_FOLDER + "data/";

// For relatedness analysis
public static final String RELATEDNESS_ANALYSIS_ID = "relatedness";
protected static final String RELATEDNESS_POP_FREQ_FILENAME = "autosomes_1000G_QC_prune_in.frq";
protected static final String RELATEDNESS_POP_FREQ_FILE_MSG = "Population frequency file";
protected static final String RELATEDNESS_POP_EXCLUDE_VAR_FILENAME = "autosomes_1000G_QC.prune.out";
Expand All @@ -67,7 +72,7 @@ public class VariantQcAnalysis extends OpenCgaToolScopeStudy {
protected static final String RELATEDNESS_THRESHOLDS_FILE_MSG = "Relatedness thresholds file";

// For inferred sex analysis

public static final String INFERRED_SEX_ANALYSIS_ID = "inferred-sex";
protected static final String INFERRED_SEX_THRESHOLDS_FILENAME = "karyotypic_sex_thresholds.json";
protected static final String INFERRED_SEX_THRESHOLDS_FILE_MSG = "Karyotypic sex thresholds file";

Expand Down Expand Up @@ -143,8 +148,13 @@ protected static void checkRelatednessParameters(QcRelatednessAnalysisParams rel
}

protected void updateRelatednessFilePaths(QcRelatednessAnalysisParams relatednessParams) throws ToolException {
// Sanity check
if (relatednessParams == null) {
throw new ToolException("Internal error input parameter is null");
}

// Get relatedness population frequency
if (relatednessParams != null && StringUtils.isNotEmpty(relatednessParams.getPopulationFrequencyFile())) {
if (StringUtils.isNotEmpty(relatednessParams.getPopulationFrequencyFile())) {
Path path = checkFileParameter(relatednessParams.getPopulationFrequencyFile(), RELATEDNESS_POP_FREQ_FILE_MSG, getStudy(),
catalogManager, getToken());
relatednessParams.setPopulationFrequencyFile(path.toAbsolutePath().toString());
Expand All @@ -154,7 +164,7 @@ protected void updateRelatednessFilePaths(QcRelatednessAnalysisParams relatednes
}

// Get relatedness population exclude variant
if (relatednessParams != null && StringUtils.isNotEmpty(relatednessParams.getPopulationExcludeVariantsFile())) {
if (StringUtils.isNotEmpty(relatednessParams.getPopulationExcludeVariantsFile())) {
Path path = checkFileParameter(relatednessParams.getPopulationExcludeVariantsFile(), RELATEDNESS_POP_EXCLUDE_VAR_FILE_MSG,
getStudy(), catalogManager, getToken());
relatednessParams.setPopulationExcludeVariantsFile(path.toAbsolutePath().toString());
Expand All @@ -164,24 +174,29 @@ protected void updateRelatednessFilePaths(QcRelatednessAnalysisParams relatednes
}

// Get relatedness thresholds
if (relatednessParams != null && StringUtils.isNotEmpty(relatednessParams.getThresholdsFile())) {
if (StringUtils.isNotEmpty(relatednessParams.getThresholdsFile())) {
Path path = checkFileParameter(relatednessParams.getThresholdsFile(), RELATEDNESS_THRESHOLDS_FILE_MSG, getStudy(),
catalogManager, getToken());
relatednessParams.setThresholdsFile(path.toAbsolutePath().toString());
} else {
Path path = getExternalFilePath(RelatednessAnalysis.ID, RELATEDNESS_THRESHOLDS_FILENAME);
Path path = getExternalFilePath(RELATEDNESS_ANALYSIS_ID, RELATEDNESS_THRESHOLDS_FILENAME);
relatednessParams.setThresholdsFile(path.toAbsolutePath().toString());
}
}

protected void updateInferredSexFilePaths(QcInferredSexAnalysisParams inferredSexParams) throws ToolException {
// Sanity check
if (inferredSexParams == null) {
throw new ToolException("Internal error input parameter is null");
}

// Get inferred sex thresholds
if (inferredSexParams != null && StringUtils.isNotEmpty(inferredSexParams.getThresholdsFile())) {
if (StringUtils.isNotEmpty(inferredSexParams.getThresholdsFile())) {
Path path = checkFileParameter(inferredSexParams.getThresholdsFile(), INFERRED_SEX_THRESHOLDS_FILE_MSG, getStudy(),
catalogManager, getToken());
inferredSexParams.setThresholdsFile(path.toAbsolutePath().toString());
} else {
Path path = getExternalFilePath(InferredSexAnalysis.ID, INFERRED_SEX_THRESHOLDS_FILENAME);
Path path = getExternalFilePath(INFERRED_SEX_ANALYSIS_ID, INFERRED_SEX_THRESHOLDS_FILENAME);
inferredSexParams.setThresholdsFile(path.toAbsolutePath().toString());
}
}
Expand Down Expand Up @@ -240,7 +255,7 @@ protected Path getExternalFilePath(String analysisId, String resourceName) throw
switch (resourceName) {
case RELATEDNESS_THRESHOLDS_FILENAME:
case INFERRED_SEX_THRESHOLDS_FILENAME:
return copyExternalFile(getOutDir().resolve("analysis/qc/data").resolve(resourceName));
return copyExternalFile(getOpencgaHome().resolve(ANALYSIS_FOLDER).resolve(QC_DATA_FOLDER).resolve(resourceName));
default:
return downloadExternalFile(analysisId, resourceName);
}
Expand All @@ -263,7 +278,7 @@ protected Path copyExternalFile(Path source) throws ToolException {
protected Path downloadExternalFile(String analysisId, String resourceName) throws ToolException {
URL url = null;
try {
url = new URL(ResourceUtils.URL + "analysis/" + analysisId + "/" + resourceName);
url = new URL(ResourceUtils.URL + ANALYSIS_FOLDER + analysisId + "/" + resourceName);
ResourceUtils.downloadThirdParty(url, getOutDir());
} catch (IOException e) {
throw new ToolException("Something wrong happened downloading the resource '" + resourceName + "' from '" + url + "'", e);
Expand All @@ -277,7 +292,7 @@ protected Path downloadExternalFile(String analysisId, String resourceName) thro

protected Path downloadExternalFileAtResources(String analysisId, String resourceName) throws ToolException {
// Check if the resource has been downloaded previously
Path resourcePath = getOpencgaHome().resolve("analysis/resources/" + analysisId);
Path resourcePath = getOpencgaHome().resolve(ANALYSIS_RESOURCES_FOLDER + analysisId);
if (!Files.exists(resourcePath)) {
// Create the resource path if it does not exist yet
try {
Expand All @@ -290,7 +305,7 @@ protected Path downloadExternalFileAtResources(String analysisId, String resourc
// Otherwise, download it from the resource repository
URL url = null;
try {
url = new URL(ResourceUtils.URL + "analysis/" + analysisId + "/" + resourceName);
url = new URL(ResourceUtils.URL + ANALYSIS_FOLDER + analysisId + "/" + resourceName);
ResourceUtils.downloadThirdParty(url, resourcePath);
} catch (IOException e) {
throw new ToolException("Something wrong happened downloading the resource '" + resourceName + "' from '" + url + "'", e);
Expand Down

0 comments on commit a6a8c86

Please sign in to comment.