Search in sources :

Example 1 with ProcessingParameters

use of com.compomics.util.parameters.tools.ProcessingParameters in project peptide-shaker by compomics.

the class ProteinProcessor method processProteins.

/**
 * Scores the PTMs of all protein matches contained in an identification
 * object, estimates spectrum counting and summary statistics.
 *
 * @param modificationLocalizationScorer The modification localization
 * scorer to use.
 * @param metrics If provided, metrics on proteins will be saved while
 * iterating the matches.
 * @param modificationProvider The modification provider to use.
 * @param waitingHandler The handler displaying feedback to the user.
 * @param exceptionHandler The exception handler to use.
 * @param processingParameters The processing parameters.
 *
 * @throws java.lang.InterruptedException exception thrown if a thread gets
 * interrupted
 * @throws java.util.concurrent.TimeoutException exception thrown if the
 * operation times out
 */
public void processProteins(ModificationLocalizationScorer modificationLocalizationScorer, Metrics metrics, ModificationProvider modificationProvider, WaitingHandler waitingHandler, ExceptionHandler exceptionHandler, ProcessingParameters processingParameters) throws InterruptedException, TimeoutException {
    waitingHandler.setWaitingText("Scoring Protein Modification Localization. Please Wait...");
    int max = identification.getProteinIdentification().size();
    waitingHandler.setSecondaryProgressCounterIndeterminate(false);
    waitingHandler.setMaxSecondaryProgressCounter(max);
    // validate the proteins
    ExecutorService pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
    ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(waitingHandler);
    ArrayList<ProteinRunnable> runnables = new ArrayList<>(processingParameters.getnThreads());
    for (int i = 1; i <= processingParameters.getnThreads(); i++) {
        ProteinRunnable runnable = new ProteinRunnable(proteinMatchesIterator, modificationLocalizationScorer, modificationProvider, waitingHandler, exceptionHandler);
        pool.submit(runnable);
        runnables.add(runnable);
    }
    if (waitingHandler.isRunCanceled()) {
        pool.shutdownNow();
    }
    pool.shutdown();
    if (!pool.awaitTermination(identification.getProteinIdentification().size(), TimeUnit.MINUTES)) {
        throw new InterruptedException("Protein matches validation timed out. Please contact the developers.");
    }
    waitingHandler.setSecondaryProgressCounterIndeterminate(true);
    if (metrics != null) {
        metrics.setMaxSpectrumCounting(runnables.stream().mapToDouble(ProteinRunnable::getMaxSpectrumCounting).sum());
        metrics.setnValidatedProteins(runnables.stream().mapToInt(ProteinRunnable::getnValidatedProteins).sum());
        metrics.setnConfidentProteins(runnables.stream().mapToInt(ProteinRunnable::getnConfidentProteins).sum());
        metrics.setMaxNPeptides(runnables.stream().mapToInt(ProteinRunnable::getMaxPeptides).max().orElse(0));
        metrics.setMaxNPsms(runnables.stream().mapToInt(ProteinRunnable::getMaxPsms).max().orElse(0));
        metrics.setMaxMW(runnables.stream().mapToDouble(ProteinRunnable::getMaxMW).max().orElse(0.0));
        metrics.setMaxProteinAccessionLength(runnables.stream().mapToInt(ProteinRunnable::getMaxProteinAccessionLength).max().orElse(0));
        TreeMap<Double, TreeMap<Integer, TreeMap<Integer, TreeSet<Long>>>> orderMap1 = new TreeMap<>();
        for (int i = 0; i < runnables.size(); i++) {
            HashMap<Double, HashMap<Integer, HashMap<Integer, HashSet<Long>>>> threadMap1 = runnables.get(i).getOrderMap();
            for (Entry<Double, HashMap<Integer, HashMap<Integer, HashSet<Long>>>> entry1 : threadMap1.entrySet()) {
                double key1 = entry1.getKey();
                HashMap<Integer, HashMap<Integer, HashSet<Long>>> threadMap2 = entry1.getValue();
                TreeMap<Integer, TreeMap<Integer, TreeSet<Long>>> orderMap2 = orderMap1.get(key1);
                if (orderMap2 == null) {
                    orderMap2 = new TreeMap<>();
                    orderMap1.put(key1, orderMap2);
                }
                for (Entry<Integer, HashMap<Integer, HashSet<Long>>> entry2 : threadMap2.entrySet()) {
                    int key2 = entry2.getKey();
                    HashMap<Integer, HashSet<Long>> threadMap3 = entry2.getValue();
                    TreeMap<Integer, TreeSet<Long>> orderMap3 = orderMap2.get(key2);
                    if (orderMap3 == null) {
                        orderMap3 = new TreeMap<>();
                        orderMap2.put(key2, orderMap3);
                    }
                    for (Entry<Integer, HashSet<Long>> entry3 : threadMap3.entrySet()) {
                        int key3 = entry3.getKey();
                        HashSet<Long> threadSet = entry3.getValue();
                        TreeSet<Long> orderedSet = orderMap3.get(key3);
                        if (orderedSet == null) {
                            orderedSet = new TreeSet<>();
                            orderMap3.put(key3, orderedSet);
                        }
                        orderedSet.addAll(threadSet);
                    }
                }
            }
        }
        long[] proteinKeys = orderMap1.values().stream().flatMap(map -> map.values().stream()).flatMap(map -> map.values().stream()).flatMap(set -> set.stream()).mapToLong(a -> a).toArray();
        metrics.setProteinKeys(proteinKeys);
    }
}
Also used : Arrays(java.util.Arrays) IdentificationFeaturesGenerator(com.compomics.util.experiment.identification.features.IdentificationFeaturesGenerator) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WaitingHandler(com.compomics.util.waiting.WaitingHandler) ProteinMatch(com.compomics.util.experiment.identification.matches.ProteinMatch) ProteinMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator) ProcessingParameters(com.compomics.util.parameters.tools.ProcessingParameters) ModificationProvider(com.compomics.util.experiment.biology.modifications.ModificationProvider) ExecutorService(java.util.concurrent.ExecutorService) MatchValidationLevel(com.compomics.util.experiment.identification.validation.MatchValidationLevel) SequenceProvider(com.compomics.util.experiment.io.biology.protein.SequenceProvider) Identification(com.compomics.util.experiment.identification.Identification) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) IdentificationParameters(com.compomics.util.parameters.identification.IdentificationParameters) ModificationLocalizationScorer(eu.isas.peptideshaker.ptm.ModificationLocalizationScorer) PSParameter(com.compomics.util.experiment.identification.peptide_shaker.PSParameter) TreeMap(java.util.TreeMap) Entry(java.util.Map.Entry) ProteinUtils(com.compomics.util.experiment.identification.utils.ProteinUtils) ExceptionHandler(com.compomics.util.exceptions.ExceptionHandler) Metrics(com.compomics.util.experiment.identification.peptide_shaker.Metrics) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) ProteinMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator) HashSet(java.util.HashSet) TreeMap(java.util.TreeMap) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with ProcessingParameters

use of com.compomics.util.parameters.tools.ProcessingParameters in project peptide-shaker by compomics.

the class PeptideShakerCLI method createProject.

/**
 * Creates the PeptideShaker project based on the identification files
 * provided in the command line input
 *
 * @throws java.io.IOException exception thrown if an error occurs while
 * reading or writing a file
 * @throws java.lang.InterruptedException exception thrown if a thread is
 * interrupted
 * @throws java.util.concurrent.TimeoutException exception thrown if a
 * process times out
 */
public void createProject() throws IOException, InterruptedException, TimeoutException {
    // define new project reference
    projectParameters = new ProjectParameters(cliInputBean.getExperimentID());
    // set the project details
    projectDetails = new ProjectDetails();
    projectDetails.setCreationDate(new Date());
    projectDetails.setPeptideShakerVersion(new Properties().getVersion());
    // set up spectrum provider
    msFileHandler = new MsFileHandler();
    // get the input files
    ArrayList<File> identificationFilesInput = cliInputBean.getIdFiles();
    ArrayList<File> dataFolders = new ArrayList<>();
    ArrayList<File> spectrumFiles = cliInputBean.getSpectrumFiles();
    File fastaFile = null;
    // Extract data from zip files, try to find the search parameter and spectrum files
    ArrayList<File> identificationFiles = new ArrayList<>();
    IdentificationParameters tempIdentificationParameters = null;
    for (File inputFile : identificationFilesInput) {
        File parentFile = inputFile.getParentFile();
        if (!dataFolders.contains(parentFile)) {
            dataFolders.add(parentFile);
        }
        File dataFolder = new File(parentFile, "mgf");
        if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
            dataFolders.add(dataFolder);
        }
        dataFolder = new File(parentFile, "mzml");
        if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
            dataFolders.add(dataFolder);
        }
        dataFolder = new File(parentFile, "cms");
        if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
            dataFolders.add(dataFolder);
        }
        dataFolder = new File(parentFile, "fasta");
        if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
            dataFolders.add(dataFolder);
        }
        dataFolder = new File(parentFile, PeptideShaker.DATA_DIRECTORY);
        if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
            dataFolders.add(dataFolder);
        }
        String fileName = inputFile.getName();
        if (fileName.toLowerCase().endsWith(".zip")) {
            waitingHandler.appendReport("Unzipping " + fileName + ".", true, true);
            String newName = PsZipUtils.getTempFolderName(fileName);
            String parentFolder = PsZipUtils.getUnzipParentFolder();
            if (parentFolder == null) {
                parentFolder = parentFile.getAbsolutePath();
            }
            File parentFolderFile = new File(parentFolder, PsZipUtils.getUnzipSubFolder());
            File destinationFolder = new File(parentFolderFile, newName);
            destinationFolder.mkdir();
            TempFilesManager.registerTempFolder(parentFolderFile);
            ZipUtils.unzip(inputFile, destinationFolder, waitingHandler);
            if (waitingHandler instanceof WaitingHandlerCLIImpl) {
                waitingHandler.appendReportEndLine();
            }
            dataFolder = new File(destinationFolder, PeptideShaker.DATA_DIRECTORY);
            if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
                dataFolders.add(dataFolder);
            }
            dataFolder = new File(destinationFolder, ".mgf");
            if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
                dataFolders.add(dataFolder);
            }
            dataFolder = new File(parentFile, "mzml");
            if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
                dataFolders.add(dataFolder);
            }
            dataFolder = new File(destinationFolder, ".cms");
            if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
                dataFolders.add(dataFolder);
            }
            dataFolder = new File(destinationFolder, ".fasta");
            if (dataFolder.exists() && !dataFolders.contains(dataFolder)) {
                dataFolders.add(dataFolder);
            }
            for (File unzippedFile : destinationFolder.listFiles()) {
                String nameLowerCase = unzippedFile.getName().toLowerCase();
                if (nameLowerCase.endsWith(".omx") || nameLowerCase.endsWith(".t.xml") || nameLowerCase.endsWith(".pep.xml") || nameLowerCase.endsWith(".dat") || nameLowerCase.endsWith(".mzid") || nameLowerCase.endsWith(".ms-amanda.csv") || nameLowerCase.endsWith(".res") || nameLowerCase.endsWith(".tide-search.target.txt") || nameLowerCase.endsWith(".tags") || nameLowerCase.endsWith(".pnovo.txt") || nameLowerCase.endsWith(".novor.csv") || nameLowerCase.endsWith(".psm") || nameLowerCase.endsWith(".omx.gz") || nameLowerCase.endsWith(".t.xml.gz") || nameLowerCase.endsWith(".pep.xml.gz") || nameLowerCase.endsWith(".mzid.gz") || nameLowerCase.endsWith(".ms-amanda.csv.gz") || nameLowerCase.endsWith(".res.gz") || nameLowerCase.endsWith(".tide-search.target.txt.gz") || nameLowerCase.endsWith(".tags.gz") || nameLowerCase.endsWith(".pnovo.txt.gz") || nameLowerCase.endsWith(".novor.csv.gz") || nameLowerCase.endsWith(".psm.gz")) {
                    identificationFiles.add(unzippedFile);
                } else if (nameLowerCase.endsWith(".par")) {
                    try {
                        tempIdentificationParameters = IdentificationParameters.getIdentificationParameters(unzippedFile);
                        ValidationQcParameters validationQCParameters = tempIdentificationParameters.getIdValidationParameters().getValidationQCParameters();
                        if (validationQCParameters == null || validationQCParameters.getPsmFilters() == null || validationQCParameters.getPeptideFilters() == null || validationQCParameters.getProteinFilters() == null || validationQCParameters.getPsmFilters().isEmpty() && validationQCParameters.getPeptideFilters().isEmpty() && validationQCParameters.getProteinFilters().isEmpty()) {
                            MatchesValidator.setDefaultMatchesQCFilters(validationQCParameters);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        waitingHandler.appendReport("An error occurred while parsing the parameters file " + unzippedFile.getName() + ". " + getLogFileMessage(), true, true);
                        waitingHandler.setRunCanceled();
                    }
                }
            }
        } else {
            identificationFiles.add(inputFile);
        }
    }
    // list the spectrum files found
    HashSet<String> dataFileNamesRequired = new HashSet<>();
    for (File spectrumFile : spectrumFiles) {
        dataFileNamesRequired.add(IoUtil.getFileName(spectrumFile));
    }
    for (File dataFolder : dataFolders) {
        for (File file : dataFolder.listFiles()) {
            String name = file.getName();
            if (name.endsWith(".mgf") || name.endsWith(".mgf.gz") || name.endsWith(".mzml") || name.endsWith(".mzml.gz") || name.endsWith(".cms")) {
                if (!dataFileNamesRequired.contains(name)) {
                    spectrumFiles.add(file);
                    dataFileNamesRequired.add(name);
                }
            } else if (name.endsWith(".fasta")) {
                if (!dataFileNamesRequired.contains(name)) {
                    fastaFile = file;
                    dataFileNamesRequired.add(name);
                }
            }
        }
    }
    // Load the spectrum files
    for (File spectrumFile : spectrumFiles) {
        File folder = CmsFolder.getParentFolder() == null ? spectrumFile.getParentFile() : new File(CmsFolder.getParentFolder());
        msFileHandler.register(spectrumFile, folder, waitingHandler);
    }
    // If there is a specific fasta file chosen, it is used insted of the one included in the searchgui zip
    if (cliInputBean.getFastaFile() != null) {
        fastaFile = cliInputBean.getFastaFile();
    }
    // get the identification parameters
    IdentificationParametersInputBean identificationParametersInputBean = cliInputBean.getIdentificationParametersInputBean();
    if (tempIdentificationParameters != null && identificationParametersInputBean.getInputFile() == null) {
        identificationParametersInputBean.setIdentificationParameters(tempIdentificationParameters);
        identificationParametersInputBean.updateIdentificationParameters();
    }
    identificationParameters = identificationParametersInputBean.getIdentificationParameters();
    ValidationQcParameters validationQCParameters = identificationParameters.getIdValidationParameters().getValidationQCParameters();
    if (validationQCParameters == null || validationQCParameters.getPsmFilters() == null || validationQCParameters.getPeptideFilters() == null || validationQCParameters.getProteinFilters() == null || validationQCParameters.getPsmFilters().isEmpty() && validationQCParameters.getPeptideFilters().isEmpty() && validationQCParameters.getProteinFilters().isEmpty()) {
        MatchesValidator.setDefaultMatchesQCFilters(validationQCParameters);
    }
    if (identificationParameters == null) {
        waitingHandler.appendReport("Identification parameters not found!", true, true);
        waitingHandler.setRunCanceled();
    }
    SearchParameters searchParameters = identificationParameters.getSearchParameters();
    String error = PeptideShaker.loadModifications(searchParameters);
    if (error != null) {
        System.out.println(error);
    }
    // try to locate the fasta file
    if (fastaFile == null) {
        waitingHandler.appendReport("FASTA file not set (or not in zip file)!", true, true);
        waitingHandler.setRunCanceled();
    } else if (!fastaFile.exists()) {
        boolean found = false;
        // look in the database folder
        try {
            File tempDbFolder = utilitiesUserParameters.getDbFolder();
            File newFile = new File(tempDbFolder, fastaFile.getName());
            if (newFile.exists()) {
                fastaFile = newFile;
                projectDetails.setFastaFile(fastaFile);
                found = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!found) {
            // look in the data folders
            for (File dataFolder : dataFolders) {
                File newFile = new File(dataFolder, fastaFile.getName());
                if (newFile.exists()) {
                    fastaFile = newFile;
                    projectDetails.setFastaFile(fastaFile);
                    found = true;
                    break;
                }
            }
            if (!found) {
                waitingHandler.appendReport("FASTA file \'" + fastaFile.getName() + "\' not found.", true, true);
                waitingHandler.setRunCanceled();
            }
        }
    } else {
        projectDetails.setFastaFile(fastaFile);
    }
    // get the summary information for the FASTA file
    try {
        // get the FASTA summary
        FastaSummary fastaSummary = loadFastaFile(waitingHandler);
        // set the background species
        identificationParameters.getGeneParameters().setBackgroundSpeciesFromFastaSummary(fastaSummary);
    } catch (IOException e) {
        e.printStackTrace();
        waitingHandler.appendReport("An error occurred while parsing the FASTA file.", true, true);
        waitingHandler.setRunCanceled();
    }
    // set the processing settings
    ProcessingParameters processingParameters = new ProcessingParameters();
    Integer nThreads = cliInputBean.getnThreads();
    if (nThreads != null) {
        processingParameters.setnThreads(nThreads);
    }
    // set the spectrum counting prefrences
    spectrumCountingParameters = new SpectrumCountingParameters();
    // set the project type
    projectType = cliInputBean.getProjectType();
    // check the project reference
    for (String forbiddenChar : Util.FORBIDDEN_CHARACTERS) {
        if (cliInputBean.getExperimentID().contains(forbiddenChar)) {
            waitingHandler.appendReport("The project name cannot not contain " + forbiddenChar + ".", true, true);
            waitingHandler.setRunCanceled();
        }
    }
    // incrementing the counter for a new PeptideShaker start run via CLI
    if (utilitiesUserParameters.isAutoUpdate()) {
        Util.sendGAUpdate("UA-36198780-1", "startrun-cl", "peptide-shaker-" + PeptideShaker.getVersion());
    }
    // create a shaker which will perform the analysis
    PeptideShaker peptideShaker = new PeptideShaker(projectParameters);
    // import the files
    int outcome = peptideShaker.importFiles(waitingHandler, identificationFiles, msFileHandler, identificationParameters, projectDetails, processingParameters, exceptionHandler);
    if (outcome == 0) {
        peptideShaker.createProject(identificationParameters, processingParameters, spectrumCountingParameters, msFileHandler, projectDetails, projectType, waitingHandler, false, exceptionHandler);
    }
    if (!waitingHandler.isRunCanceled()) {
        // identification as created by PeptideShaker
        identification = peptideShaker.getIdentification();
        // metrics saved while processing the data
        metrics = peptideShaker.getMetrics();
        // fene maps
        geneMaps = peptideShaker.getGeneMaps();
        // the identification feature generator
        identificationFeaturesGenerator = peptideShaker.getIdentificationFeaturesGenerator();
        // the sequence provider
        sequenceProvider = peptideShaker.getSequenceProvider();
        // the protein details provider
        proteinDetailsProvider = peptideShaker.getProteinDetailsProvider();
        if (waitingHandler instanceof WaitingDialog) {
            projectDetails.setReport(((WaitingDialog) waitingHandler).getReport(null));
            ((WaitingDialog) waitingHandler).setRunNotFinished();
            ((WaitingDialog) waitingHandler).setCloseDialogWhenImportCompletes(true, false);
        }
    } else {
        if (waitingHandler instanceof WaitingDialog) {
            saveReport();
        }
        TempFilesManager.deleteTempFolders();
        waitingHandler.setWaitingText("PeptideShaker Processing Canceled.");
        System.out.println("<CompomicsError>PeptideShaker processing canceled. " + getLogFileMessage() + "</CompomicsError>");
    }
}
Also used : ArrayList(java.util.ArrayList) WaitingDialog(com.compomics.util.gui.waiting.waitinghandlers.WaitingDialog) Properties(eu.isas.peptideshaker.utils.Properties) FastaSummary(com.compomics.util.experiment.io.biology.protein.FastaSummary) SearchParameters(com.compomics.util.parameters.identification.search.SearchParameters) IdentificationParameters(com.compomics.util.parameters.identification.IdentificationParameters) ValidationQcParameters(com.compomics.util.parameters.identification.advanced.ValidationQcParameters) ProcessingParameters(com.compomics.util.parameters.tools.ProcessingParameters) MsFileHandler(com.compomics.util.experiment.io.mass_spectrometry.MsFileHandler) HashSet(java.util.HashSet) ProjectDetails(eu.isas.peptideshaker.preferences.ProjectDetails) IdentificationParametersInputBean(com.compomics.cli.identification_parameters.IdentificationParametersInputBean) SpectrumCountingParameters(com.compomics.util.parameters.quantification.spectrum_counting.SpectrumCountingParameters) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) SQLException(java.sql.SQLException) Point(java.awt.Point) ProjectParameters(com.compomics.util.experiment.ProjectParameters) WaitingHandlerCLIImpl(com.compomics.util.gui.waiting.waitinghandlers.WaitingHandlerCLIImpl) PeptideShaker(eu.isas.peptideshaker.PeptideShaker)

Example 3 with ProcessingParameters

use of com.compomics.util.parameters.tools.ProcessingParameters in project peptide-shaker by compomics.

the class MatchesValidator method validateIdentifications.

/**
 * This method validates the identification matches of an identification
 * object. Target Decoy thresholds must be set.
 *
 * @param identification The identification class containing the matches to
 * validate.
 * @param metrics If provided, metrics on fractions will be saved while
 * iterating the matches.
 * @param geneMaps The gene maps.
 * @param inputMap The target decoy map of all search engine scores.
 * @param waitingHandler The waiting handler displaying progress to the user
 * and allowing canceling the process.
 * @param exceptionHandler The handler for exceptions.
 * @param identificationFeaturesGenerator The identification features
 * generator computing information about the identification matches.
 * @param sequenceProvider The protein sequence provider.
 * @param proteinDetailsProvider The protein details provider.
 * @param spectrumProvider The spectrum provider.
 * @param identificationParameters The identification parameters.
 * @param projectType The project type.
 * @param processingParameters The processing parameters.
 *
 * @throws java.lang.InterruptedException exception thrown if a thread gets
 * interrupted
 * @throws java.util.concurrent.TimeoutException exception thrown if the
 * operation times out
 */
public void validateIdentifications(Identification identification, Metrics metrics, InputMap inputMap, WaitingHandler waitingHandler, ExceptionHandler exceptionHandler, IdentificationFeaturesGenerator identificationFeaturesGenerator, SequenceProvider sequenceProvider, ProteinDetailsProvider proteinDetailsProvider, SpectrumProvider spectrumProvider, GeneMaps geneMaps, IdentificationParameters identificationParameters, ProjectType projectType, ProcessingParameters processingParameters) throws InterruptedException, TimeoutException {
    IdMatchValidationParameters validationParameters = identificationParameters.getIdValidationParameters();
    waitingHandler.setWaitingText("Finding FDR Thresholds. Please Wait...");
    for (int algorithm : inputMap.getInputAlgorithms()) {
        TargetDecoyMap targetDecoyMap = inputMap.getTargetDecoyMap(algorithm);
        TargetDecoyResults currentResults = targetDecoyMap.getTargetDecoyResults();
        currentResults.setInputType(1);
        currentResults.setUserInput(validationParameters.getDefaultPsmFDR());
        currentResults.setFdrLimit(validationParameters.getDefaultPsmFDR());
        targetDecoyMap.getTargetDecoySeries().getFDRResults(currentResults);
    }
    TargetDecoyResults currentResults = psmMap.getTargetDecoyResults();
    currentResults.setInputType(1);
    currentResults.setUserInput(validationParameters.getDefaultPsmFDR());
    currentResults.setFdrLimit(validationParameters.getDefaultPsmFDR());
    psmMap.getTargetDecoySeries().getFDRResults(currentResults);
    waitingHandler.setSecondaryProgressCounterIndeterminate(false);
    currentResults = peptideMap.getTargetDecoyResults();
    currentResults.setInputType(1);
    currentResults.setUserInput(validationParameters.getDefaultPeptideFDR());
    currentResults.setFdrLimit(validationParameters.getDefaultPeptideFDR());
    peptideMap.getTargetDecoySeries().getFDRResults(currentResults);
    currentResults = proteinMap.getTargetDecoyResults();
    currentResults.setInputType(1);
    currentResults.setUserInput(validationParameters.getDefaultProteinFDR());
    currentResults.setFdrLimit(validationParameters.getDefaultProteinFDR());
    proteinMap.getTargetDecoySeries().getFDRResults(currentResults);
    ValidationQcParameters validationQCParameters = validationParameters.getValidationQCParameters();
    waitingHandler.setWaitingText("Match Validation and Quality Control. Please Wait...");
    waitingHandler.setSecondaryProgressCounterIndeterminate(false);
    waitingHandler.resetSecondaryProgressCounter();
    waitingHandler.setMaxSecondaryProgressCounter(identification.getProteinIdentification().size() + identification.getPeptideIdentification().size() + 2 * identification.getSpectrumIdentificationSize());
    // validate the spectrum matches
    if (inputMap != null) {
        inputMap.resetAdvocateContributions();
    }
    AnnotationParameters annotationParameters = identificationParameters.getAnnotationParameters();
    double intensityLimit = annotationParameters.getAnnotationIntensityLimit();
    annotationParameters.setIntensityLimit(0);
    ExecutorService pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
    SpectrumMatchesIterator psmIterator = identification.getSpectrumMatchesIterator(waitingHandler);
    ArrayList<PsmValidatorRunnable> psmRunnables = new ArrayList<>(processingParameters.getnThreads());
    for (int i = 1; i <= processingParameters.getnThreads(); i++) {
        PsmValidatorRunnable runnable = new PsmValidatorRunnable(psmIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, waitingHandler, exceptionHandler, inputMap, false, true);
        pool.submit(runnable);
        psmRunnables.add(runnable);
    }
    if (waitingHandler.isRunCanceled()) {
        pool.shutdownNow();
        return;
    }
    pool.shutdown();
    if (!pool.awaitTermination(identification.getSpectrumIdentificationSize(), TimeUnit.MINUTES)) {
        throw new TimeoutException("Spectrum matches validation timed out. Please contact the developers.");
    }
    // combine the precursor mz deviations from the different threads into one map
    HashMap<String, ArrayList<Double>> precursorMzDeviations = new HashMap<>(identification.getSpectrumIdentification().size());
    for (PsmValidatorRunnable runnable : psmRunnables) {
        for (String spectrumFileName : runnable.getThreadPrecursorMzDeviations().keySet()) {
            ArrayList<Double> threadPrecursorMzDeviations = runnable.getThreadPrecursorMzDeviations().get(spectrumFileName);
            ArrayList<Double> filePrecursorMzDeviations = precursorMzDeviations.get(spectrumFileName);
            if (filePrecursorMzDeviations != null) {
                filePrecursorMzDeviations.addAll(threadPrecursorMzDeviations);
            } else {
                precursorMzDeviations.put(spectrumFileName, threadPrecursorMzDeviations);
            }
        }
    }
    for (String spectrumFileName : precursorMzDeviations.keySet()) {
        double[] precursorMzDeviationsFile = precursorMzDeviations.get(spectrumFileName).stream().mapToDouble(a -> a).toArray();
        if (precursorMzDeviationsFile.length >= 100) {
            Arrays.sort(precursorMzDeviationsFile);
            identificationFeaturesGenerator.setMassErrorDistribution(spectrumFileName, precursorMzDeviationsFile);
        } else {
            // There are not enough precursors, disable probabilistic precursor filter
            if (validationQCParameters.getPsmFilters() != null) {
                for (Filter filter : validationQCParameters.getPsmFilters()) {
                    PsmFilter psmFilter = (PsmFilter) filter;
                    if (psmFilter.getItemsNames().contains(AssumptionFilterItem.precrusorMzErrorStat.name)) {
                        psmFilter.removeFilterItem(AssumptionFilterItem.precrusorMzErrorStat.name);
                        SearchParameters searchParameters = identificationParameters.getSearchParameters();
                        if (searchParameters.isPrecursorAccuracyTypePpm()) {
                            psmFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorPpm.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
                        } else {
                            psmFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorDa.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
                        }
                    }
                    AssumptionFilter assumptionFilter = psmFilter.getAssumptionFilter();
                    if (assumptionFilter.getItemsNames().contains(AssumptionFilterItem.precrusorMzErrorStat.name)) {
                        assumptionFilter.removeFilterItem(AssumptionFilterItem.precrusorMzErrorStat.name);
                        SearchParameters searchParameters = identificationParameters.getSearchParameters();
                        if (searchParameters.isPrecursorAccuracyTypePpm()) {
                            assumptionFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorPpm.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
                        } else {
                            assumptionFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorDa.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
                        }
                    }
                }
            }
        }
    }
    pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
    psmIterator = identification.getSpectrumMatchesIterator(waitingHandler);
    for (int i = 1; i <= processingParameters.getnThreads(); i++) {
        PsmValidatorRunnable runnable = new PsmValidatorRunnable(psmIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, waitingHandler, exceptionHandler, inputMap, true, false);
        pool.submit(runnable);
    }
    if (waitingHandler.isRunCanceled()) {
        pool.shutdownNow();
        return;
    }
    pool.shutdown();
    if (!pool.awaitTermination(identification.getSpectrumIdentificationSize(), TimeUnit.MINUTES)) {
        throw new TimeoutException("Spectrum matches validation timed out. Please contact the developers.");
    }
    annotationParameters.setIntensityLimit(intensityLimit);
    if (projectType == ProjectType.peptide || projectType == ProjectType.protein) {
        // validate the peptides
        pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
        ArrayList<PeptideValidatorRunnable> peptideRunnables = new ArrayList<>(processingParameters.getnThreads());
        PeptideMatchesIterator peptideMatchesIterator = identification.getPeptideMatchesIterator(waitingHandler);
        for (int i = 1; i <= processingParameters.getnThreads(); i++) {
            PeptideValidatorRunnable runnable = new PeptideValidatorRunnable(peptideMatchesIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, waitingHandler, exceptionHandler, metrics);
            pool.submit(runnable);
            peptideRunnables.add(runnable);
        }
        if (waitingHandler.isRunCanceled()) {
            pool.shutdownNow();
            return;
        }
        pool.shutdown();
        if (!pool.awaitTermination(identification.getPeptideIdentification().size(), TimeUnit.MINUTES)) {
            throw new InterruptedException("Peptide matches validation timed out. Please contact the developers.");
        }
        HashMap<String, Integer> validatedTotalPeptidesPerFraction = new HashMap<>();
        ArrayList<Double> validatedPeptideLengths = new ArrayList<>();
        for (PeptideValidatorRunnable runnable : peptideRunnables) {
            HashMap<String, Integer> threadValidatedTotalPeptidesPerFraction = runnable.getValidatedTotalPeptidesPerFraction();
            for (String fraction : threadValidatedTotalPeptidesPerFraction.keySet()) {
                Integer nValidated = validatedTotalPeptidesPerFraction.get(fraction);
                if (nValidated == null) {
                    nValidated = 0;
                }
                nValidated += threadValidatedTotalPeptidesPerFraction.get(fraction);
                validatedTotalPeptidesPerFraction.put(fraction, nValidated);
            }
            validatedPeptideLengths.addAll(runnable.getValidatedPeptideLengths());
        }
        if (validatedPeptideLengths.size() >= 100) {
            NonSymmetricalNormalDistribution lengthDistribution = NonSymmetricalNormalDistribution.getRobustNonSymmetricalNormalDistribution(validatedPeptideLengths);
            metrics.setPeptideLengthDistribution(lengthDistribution);
        }
        metrics.setTotalPeptidesPerFraction(validatedTotalPeptidesPerFraction);
        if (projectType == ProjectType.protein) {
            // validate the proteins
            pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
            ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(waitingHandler);
            ArrayList<ProteinValidatorRunnable> proteinRunnables = new ArrayList<>(processingParameters.getnThreads());
            for (int i = 1; i <= processingParameters.getnThreads(); i++) {
                ProteinValidatorRunnable runnable = new ProteinValidatorRunnable(proteinMatchesIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, metrics, identificationParameters, waitingHandler, exceptionHandler);
                pool.submit(runnable);
                proteinRunnables.add(runnable);
            }
            if (waitingHandler.isRunCanceled()) {
                pool.shutdownNow();
                return;
            }
            pool.shutdown();
            if (!pool.awaitTermination(identification.getProteinIdentification().size(), TimeUnit.MINUTES)) {
                throw new InterruptedException("Protein matches validation timed out. Please contact the developers.");
            }
            long[] validatedTargetProteinKeys = proteinRunnables.stream().flatMap(runnable -> runnable.getValidatedProteinMatches().stream()).mapToLong(a -> a).toArray();
            metrics.setValidatedTargetProteinKeys(validatedTargetProteinKeys);
        }
    }
}
Also used : Arrays(java.util.Arrays) IdentificationFeaturesGenerator(com.compomics.util.experiment.identification.features.IdentificationFeaturesGenerator) SpectrumMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.SpectrumMatchesIterator) FractionParameters(com.compomics.util.parameters.identification.advanced.FractionParameters) TimeoutException(java.util.concurrent.TimeoutException) PsmFilter(com.compomics.util.experiment.identification.filtering.PsmFilter) WaitingHandler(com.compomics.util.waiting.WaitingHandler) ProteinFilterItem(com.compomics.util.experiment.identification.filtering.items.ProteinFilterItem) TargetDecoyResults(eu.isas.peptideshaker.scoring.targetdecoy.TargetDecoyResults) SpectrumProvider(com.compomics.util.experiment.mass_spectrometry.SpectrumProvider) PeptideAssumption(com.compomics.util.experiment.identification.spectrum_assumptions.PeptideAssumption) Set(java.util.Set) IdMatchValidationParameters(com.compomics.util.parameters.identification.advanced.IdMatchValidationParameters) Collectors(java.util.stream.Collectors) ProteinFilter(com.compomics.util.experiment.identification.filtering.ProteinFilter) Executors(java.util.concurrent.Executors) SearchParameters(com.compomics.util.parameters.identification.search.SearchParameters) IdentificationParameters(com.compomics.util.parameters.identification.IdentificationParameters) PSParameter(com.compomics.util.experiment.identification.peptide_shaker.PSParameter) ExceptionHandler(com.compomics.util.exceptions.ExceptionHandler) Metrics(com.compomics.util.experiment.identification.peptide_shaker.Metrics) ModificationMatch(com.compomics.util.experiment.identification.matches.ModificationMatch) PeptideFilter(com.compomics.util.experiment.identification.filtering.PeptideFilter) SpectrumIdentificationAssumption(com.compomics.util.experiment.identification.SpectrumIdentificationAssumption) TargetDecoyMap(eu.isas.peptideshaker.scoring.targetdecoy.TargetDecoyMap) PeptideUtils(com.compomics.util.experiment.identification.utils.PeptideUtils) HashMap(java.util.HashMap) PeptideMatch(com.compomics.util.experiment.identification.matches.PeptideMatch) NonSymmetricalNormalDistribution(com.compomics.util.math.statistics.distributions.NonSymmetricalNormalDistribution) ProteinDetailsProvider(com.compomics.util.experiment.io.biology.protein.ProteinDetailsProvider) TreeSet(java.util.TreeSet) SpectrumMatch(com.compomics.util.experiment.identification.matches.SpectrumMatch) ValidationQcParameters(com.compomics.util.parameters.identification.advanced.ValidationQcParameters) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ProteinMatch(com.compomics.util.experiment.identification.matches.ProteinMatch) ProteinMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator) InputMap(eu.isas.peptideshaker.scoring.maps.InputMap) ProjectType(com.compomics.util.parameters.peptide_shaker.ProjectType) ProcessingParameters(com.compomics.util.parameters.tools.ProcessingParameters) FilterItemComparator(com.compomics.util.experiment.filtering.FilterItemComparator) PeptideFilterItem(com.compomics.util.experiment.identification.filtering.items.PeptideFilterItem) ExecutorService(java.util.concurrent.ExecutorService) AssumptionFilter(com.compomics.util.experiment.identification.filtering.AssumptionFilter) MatchValidationLevel(com.compomics.util.experiment.identification.validation.MatchValidationLevel) AssumptionFilterItem(com.compomics.util.experiment.identification.filtering.items.AssumptionFilterItem) SequenceProvider(com.compomics.util.experiment.io.biology.protein.SequenceProvider) Identification(com.compomics.util.experiment.identification.Identification) Peptide(com.compomics.util.experiment.biology.proteins.Peptide) PeptideMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.PeptideMatchesIterator) AnnotationParameters(com.compomics.util.experiment.identification.spectrum_annotation.AnnotationParameters) TimeUnit(java.util.concurrent.TimeUnit) TreeMap(java.util.TreeMap) FastaParameters(com.compomics.util.experiment.io.biology.protein.FastaParameters) ProteinUtils(com.compomics.util.experiment.identification.utils.ProteinUtils) Filter(com.compomics.util.experiment.filtering.Filter) GeneMaps(com.compomics.util.experiment.biology.genes.GeneMaps) IdMatchValidationParameters(com.compomics.util.parameters.identification.advanced.IdMatchValidationParameters) HashMap(java.util.HashMap) AnnotationParameters(com.compomics.util.experiment.identification.spectrum_annotation.AnnotationParameters) ArrayList(java.util.ArrayList) TargetDecoyMap(eu.isas.peptideshaker.scoring.targetdecoy.TargetDecoyMap) SearchParameters(com.compomics.util.parameters.identification.search.SearchParameters) ValidationQcParameters(com.compomics.util.parameters.identification.advanced.ValidationQcParameters) TargetDecoyResults(eu.isas.peptideshaker.scoring.targetdecoy.TargetDecoyResults) ProteinMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator) TimeoutException(java.util.concurrent.TimeoutException) NonSymmetricalNormalDistribution(com.compomics.util.math.statistics.distributions.NonSymmetricalNormalDistribution) PeptideMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.PeptideMatchesIterator) AssumptionFilter(com.compomics.util.experiment.identification.filtering.AssumptionFilter) SpectrumMatchesIterator(com.compomics.util.experiment.identification.matches_iterators.SpectrumMatchesIterator) PsmFilter(com.compomics.util.experiment.identification.filtering.PsmFilter) ProteinFilter(com.compomics.util.experiment.identification.filtering.ProteinFilter) PeptideFilter(com.compomics.util.experiment.identification.filtering.PeptideFilter) AssumptionFilter(com.compomics.util.experiment.identification.filtering.AssumptionFilter) Filter(com.compomics.util.experiment.filtering.Filter) ExecutorService(java.util.concurrent.ExecutorService) PsmFilter(com.compomics.util.experiment.identification.filtering.PsmFilter)

Aggregations

IdentificationParameters (com.compomics.util.parameters.identification.IdentificationParameters)3 ProcessingParameters (com.compomics.util.parameters.tools.ProcessingParameters)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 TimeoutException (java.util.concurrent.TimeoutException)3 ExceptionHandler (com.compomics.util.exceptions.ExceptionHandler)2 Identification (com.compomics.util.experiment.identification.Identification)2 IdentificationFeaturesGenerator (com.compomics.util.experiment.identification.features.IdentificationFeaturesGenerator)2 ProteinMatch (com.compomics.util.experiment.identification.matches.ProteinMatch)2 ProteinMatchesIterator (com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator)2 Metrics (com.compomics.util.experiment.identification.peptide_shaker.Metrics)2 PSParameter (com.compomics.util.experiment.identification.peptide_shaker.PSParameter)2 ProteinUtils (com.compomics.util.experiment.identification.utils.ProteinUtils)2 MatchValidationLevel (com.compomics.util.experiment.identification.validation.MatchValidationLevel)2 ValidationQcParameters (com.compomics.util.parameters.identification.advanced.ValidationQcParameters)2 SearchParameters (com.compomics.util.parameters.identification.search.SearchParameters)2 IdentificationParametersInputBean (com.compomics.cli.identification_parameters.IdentificationParametersInputBean)1 ProjectParameters (com.compomics.util.experiment.ProjectParameters)1 GeneMaps (com.compomics.util.experiment.biology.genes.GeneMaps)1 ModificationProvider (com.compomics.util.experiment.biology.modifications.ModificationProvider)1