Search in sources :

Example 1 with SettingsChecker

use of de.ipbhalle.metfraglib.parameter.SettingsChecker in project MetFragRelaunched by ipb-halle.

the class MetFragRestController method process.

/**
 * runs a metfrag query
 *
 * @param args
 * @return
 * @throws CouldNotWriteStatusException
 * @throws CouldNotCreateProcessException
 */
@RequestMapping(method = RequestMethod.POST, value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public ResponseEntity<Resource<ProcessAssembler>> process(@RequestBody ProcessArguments args) throws CouldNotWriteStatusException, CouldNotCreateProcessException {
    File resFolder;
    String processid;
    try {
        resFolder = Files.createTempDirectory("java.io.tmpdir").toFile();
        processid = resFolder.getName();
        try {
            MetFragGlobalSettings settings = args.getSettingsObject(resFolder);
            // check settings
            SettingsChecker settingsChecker = new SettingsChecker();
            if (!settingsChecker.check(settings))
                throw new CouldNotCreateProcessException("Error: Corrupt parameters");
            logger.info("Storing in " + settings.get(VariableNames.STORE_RESULTS_PATH_NAME));
            CombinedMetFragProcess mp = new CombinedMetFragProcess(settings);
            this.writeStatus("RUNNING", processid);
            this.writeHost(processid);
            new Thread(() -> {
                System.out.println("staring run");
                try {
                    MetFragRestService.startMetFrag(mp, settings, resFolder.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }, "MyThread-" + processid).start();
        } catch (ParameterNotKnownException e) {
            e.printStackTrace();
            this.writeStatus("ERROR", processid);
            throw new CouldNotCreateProcessException("Error: Parameter not known");
        } catch (IOException e) {
            e.printStackTrace();
            throw new CouldNotCreateProcessException("Error: Could not write status");
        } catch (Exception e) {
            e.printStackTrace();
            this.writeStatus("ERROR", processid);
            throw new CouldNotCreateProcessException("Error: Unknown error");
        }
    } catch (IOException e) {
        throw new CouldNotWriteStatusException(e.getMessage());
    }
    Resource<ProcessAssembler> resource = new ProcessAssembler("process", processid).toResource();
    resource.add(linkTo(MetFragRestController.class).slash("process").withSelfRel());
    resource.add(linkTo(MetFragRestController.class).slash("status").slash(processid).withRel("status"));
    resource.add(linkTo(MetFragRestController.class).slash("host").slash(processid).withRel("host"));
    resource.add(linkTo(MetFragRestController.class).slash("result").slash(processid).withRel("result"));
    resource.add(linkTo(MetFragRestController.class).slash("resultzip").slash(processid).withRel("resultzip"));
    return new ResponseEntity<Resource<ProcessAssembler>>(resource, HttpStatus.CREATED);
}
Also used : CouldNotCreateProcessException(de.ipbhalle.exception.CouldNotCreateProcessException) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) CouldNotWriteStatusException(de.ipbhalle.exception.CouldNotWriteStatusException) IOException(java.io.IOException) ParameterNotKnownException(de.ipbhalle.metfraglib.exceptions.ParameterNotKnownException) CouldNotReadStatusException(de.ipbhalle.exception.CouldNotReadStatusException) CouldNotFetchResultsException(de.ipbhalle.exception.CouldNotFetchResultsException) ParameterNotKnownException(de.ipbhalle.metfraglib.exceptions.ParameterNotKnownException) CouldNotCreateProcessException(de.ipbhalle.exception.CouldNotCreateProcessException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CouldNotRemoveProcessException(de.ipbhalle.exception.CouldNotRemoveProcessException) ExecutionException(java.util.concurrent.ExecutionException) CouldNotReadHostException(de.ipbhalle.exception.CouldNotReadHostException) CouldNotWriteStatusException(de.ipbhalle.exception.CouldNotWriteStatusException) ResponseEntity(org.springframework.http.ResponseEntity) SettingsChecker(de.ipbhalle.metfraglib.parameter.SettingsChecker) File(java.io.File) ProcessAssembler(de.ipbhalle.model.ProcessAssembler) CombinedMetFragProcess(de.ipbhalle.metfraglib.process.CombinedMetFragProcess) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with SettingsChecker

use of de.ipbhalle.metfraglib.parameter.SettingsChecker in project MetFragRelaunched by ipb-halle.

the class PostCalculateScoreValuesAndRankFromResultFilePeakThreadFP method main.

public static void main(String[] args) throws Exception {
    getArgs(args);
    String paramfolder = (String) argsHash.get("parampath");
    String resfolder = (String) argsHash.get("resultpath");
    String outputfile = (String) argsHash.get("output");
    String alphaPeak = (String) argsHash.get("alphaPeak");
    String betaPeak = (String) argsHash.get("betaPeak");
    String alphaLoss = (String) argsHash.get("alphaLoss");
    String betaLoss = (String) argsHash.get("betaLoss");
    int numberThreads = Integer.parseInt(argsHash.get("threads"));
    double[][] weights = readWeights(argsHash.get("weights"));
    Boolean stdout = Boolean.parseBoolean(argsHash.get("stdout"));
    String outputtype = (String) argsHash.get("outputtype");
    Boolean negScores = Boolean.parseBoolean(argsHash.get("negscore"));
    String[] scoringPropertyNames = argsHash.get("scorenames").split(",");
    Boolean transformScores = Boolean.parseBoolean(argsHash.get("transform"));
    String outputfolder = (String) argsHash.get("outputfolder");
    String filter = (String) argsHash.get("filter");
    ALPHA_VALUE_PEAK = Double.parseDouble(alphaPeak);
    BETA_VALUE_PEAK = Double.parseDouble(betaPeak);
    ALPHA_VALUE_LOSS = Double.parseDouble(alphaLoss);
    BETA_VALUE_LOSS = Double.parseDouble(betaLoss);
    File _resfolder = new File(resfolder);
    File _paramfolder = new File(paramfolder);
    File[] resultFiles = _resfolder.listFiles();
    File[] paramFiles = null;
    if (filter.equals(""))
        paramFiles = _paramfolder.listFiles();
    else {
        if (filter.equals("pos")) {
            FileFilter fileFilter = new GetRankOfCandidateMultipleThreadCSV().new FileExtensionFilter("-01.txt");
            paramFiles = _paramfolder.listFiles(fileFilter);
        } else if (filter.equals("neg")) {
            FileFilter fileFilter = new GetRankOfCandidateMultipleThreadCSV().new FileExtensionFilter("-02.txt");
            paramFiles = _paramfolder.listFiles(fileFilter);
        }
    }
    ArrayList<ProcessThread> threads = new ArrayList<ProcessThread>();
    for (int i = 0; i < paramFiles.length; i++) {
        String id = paramFiles[i].getName().split("\\.")[0];
        int resultFileID = -1;
        for (int j = 0; j < resultFiles.length; j++) {
            if (resultFiles[j].getName().startsWith(id + ".")) {
                resultFileID = j;
                break;
            }
        }
        if (resultFileID == -1) {
            System.err.println(id + " not found as result.");
            continue;
        }
        Settings settings = getSettings(paramFiles[i].getAbsolutePath());
        settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, resultFiles[resultFileID].getAbsolutePath());
        settings.set(VariableNames.PEAK_FINGERPRINT_ANNOTATION_ALPHA_VALUE_NAME, ALPHA_VALUE_PEAK);
        settings.set(VariableNames.PEAK_FINGERPRINT_ANNOTATION_BETA_VALUE_NAME, BETA_VALUE_PEAK);
        settings.set(VariableNames.LOSS_FINGERPRINT_ANNOTATION_ALPHA_VALUE_NAME, ALPHA_VALUE_LOSS);
        settings.set(VariableNames.LOSS_FINGERPRINT_ANNOTATION_BETA_VALUE_NAME, BETA_VALUE_LOSS);
        SettingsChecker sc = new SettingsChecker();
        if (!sc.check(settings)) {
            System.err.println("Error checking settings for " + id);
            continue;
        }
        IPeakListReader peakListReader = (IPeakListReader) Class.forName((String) settings.get(VariableNames.METFRAG_PEAK_LIST_READER_NAME)).getConstructor(Settings.class).newInstance(settings);
        settings.set(VariableNames.PEAK_LIST_NAME, peakListReader.read());
        ProcessThread thread = new PostCalculateScoreValuesAndRankFromResultFilePeakThreadFP().new ProcessThread(settings, outputfile, resfolder, paramFiles[i].getAbsolutePath(), outputtype, negScores, weights, transformScores, scoringPropertyNames, stdout);
        threads.add(thread);
    }
    System.err.println("preparation finished");
    ExecutorService executer = Executors.newFixedThreadPool(numberThreads);
    for (ProcessThread thread : threads) {
        executer.execute(thread);
    }
    executer.shutdown();
    while (!executer.isTerminated()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    java.util.ArrayList<Integer> weightIndexesOfInterestPos = new java.util.ArrayList<Integer>();
    java.util.ArrayList<Integer> weightIndexesOfInterestNeg = new java.util.ArrayList<Integer>();
    for (int i = 0; i < weights.length; i++) {
        weightIndexesOfInterestPos.add(i);
        weightIndexesOfInterestNeg.add(i);
    }
    java.util.ArrayList<Integer> nextWeightIndexesOfInterestPos;
    java.util.ArrayList<Integer> nextWeightIndexesOfInterestNeg;
    int[] bestRanksPos = new int[10];
    int bestWeightIndexPos = -1;
    int[] bestRanksNeg = new int[10];
    int bestWeightIndexNeg = -1;
    for (int i = 1; i <= 10; i++) {
        // for pos queries
        int currentBestValuePos = 0;
        nextWeightIndexesOfInterestPos = new java.util.ArrayList<Integer>();
        for (int w = 0; w < weightIndexesOfInterestPos.size(); w++) {
            int thisBestValuePos = 0;
            for (ProcessThread thread : threads) {
                // get rank of query in thread obtained by using weight w
                if (thread.isPositiveQuery() && thread.getRanksForWeight()[weightIndexesOfInterestPos.get(w)] <= i)
                    thisBestValuePos++;
            }
            if (currentBestValuePos < thisBestValuePos) {
                currentBestValuePos = thisBestValuePos;
                nextWeightIndexesOfInterestPos.clear();
                nextWeightIndexesOfInterestPos.add(weightIndexesOfInterestPos.get(w));
            } else if (currentBestValuePos == thisBestValuePos)
                nextWeightIndexesOfInterestPos.add(weightIndexesOfInterestPos.get(w));
        }
        bestRanksPos[i - 1] = currentBestValuePos;
        bestWeightIndexPos = nextWeightIndexesOfInterestPos.get(0);
        weightIndexesOfInterestPos = nextWeightIndexesOfInterestPos;
        // for neg queries
        int currentBestValueNeg = 0;
        nextWeightIndexesOfInterestNeg = new java.util.ArrayList<Integer>();
        for (int w = 0; w < weightIndexesOfInterestNeg.size(); w++) {
            int thisBestValueNeg = 0;
            for (ProcessThread thread : threads) {
                // get rank of query in thread obtained by using weight w
                if (!thread.isPositiveQuery() && thread.getRanksForWeight()[weightIndexesOfInterestNeg.get(w)] <= i)
                    thisBestValueNeg++;
            }
            if (currentBestValueNeg < thisBestValueNeg) {
                currentBestValueNeg = thisBestValueNeg;
                nextWeightIndexesOfInterestNeg.clear();
                nextWeightIndexesOfInterestNeg.add(weightIndexesOfInterestNeg.get(w));
            } else if (currentBestValueNeg == thisBestValueNeg)
                nextWeightIndexesOfInterestNeg.add(weightIndexesOfInterestNeg.get(w));
        }
        bestRanksNeg[i - 1] = currentBestValueNeg;
        bestWeightIndexNeg = nextWeightIndexesOfInterestNeg.get(0);
        weightIndexesOfInterestNeg = nextWeightIndexesOfInterestNeg;
    }
    String bestWeightStringPos = weights[bestWeightIndexPos][0] + "";
    String bestWeightStringNeg = weights[bestWeightIndexNeg][0] + "";
    for (int i = 1; i < weights[bestWeightIndexPos].length; i++) {
        bestWeightStringPos += " " + weights[bestWeightIndexPos][i];
        bestWeightStringNeg += " " + weights[bestWeightIndexNeg][i];
    }
    String bestRankingsStringPos = bestRanksPos[0] + "";
    String bestRankingsStringNeg = bestRanksNeg[0] + "";
    for (int i = 1; i < bestRanksPos.length; i++) {
        bestRankingsStringPos += " " + bestRanksPos[i];
        bestRankingsStringNeg += " " + bestRanksNeg[i];
    }
    File folder = new File(outputfolder + Constants.OS_SPECIFIC_FILE_SEPARATOR + "best_values");
    if (!folder.exists())
        folder.mkdirs();
    String fileprefix = alphaPeak + "_" + betaPeak + "_" + alphaLoss + "_" + betaLoss;
    fileprefix = fileprefix.replaceAll("\\.", "");
    String path_pos = folder.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "pos" + Constants.OS_SPECIFIC_FILE_SEPARATOR;
    String path_neg = folder.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "neg" + Constants.OS_SPECIFIC_FILE_SEPARATOR;
    if (filter.equals("pos") || filter.equals(""))
        new File(path_pos).mkdirs();
    if (filter.equals("neg") || filter.equals(""))
        new File(path_neg).mkdirs();
    java.io.BufferedWriter bwriter_pos1 = null;
    java.io.BufferedWriter bwriter_pos2 = null;
    java.io.BufferedWriter bwriter_neg1 = null;
    java.io.BufferedWriter bwriter_neg2 = null;
    if (filter.equals("pos") || filter.equals(""))
        bwriter_pos1 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_pos + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_weights.txt")));
    if (filter.equals("pos") || filter.equals(""))
        bwriter_pos2 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_pos + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_tops.txt")));
    if (filter.equals("neg") || filter.equals(""))
        bwriter_neg1 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_neg + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_weights.txt")));
    if (filter.equals("neg") || filter.equals(""))
        bwriter_neg2 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_neg + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_tops.txt")));
    if (bwriter_pos1 != null)
        bwriter_pos1.write(bestWeightStringPos);
    if (bwriter_pos1 != null)
        bwriter_pos1.newLine();
    if (bwriter_pos2 != null)
        bwriter_pos2.write(bestRankingsStringPos);
    if (bwriter_pos2 != null)
        bwriter_pos2.newLine();
    if (bwriter_pos1 != null)
        bwriter_pos1.close();
    if (bwriter_pos2 != null)
        bwriter_pos2.close();
    if (bwriter_neg1 != null)
        bwriter_neg1.write(bestWeightStringNeg);
    if (bwriter_neg1 != null)
        bwriter_neg1.newLine();
    if (bwriter_neg2 != null)
        bwriter_neg2.write(bestRankingsStringNeg);
    if (bwriter_neg2 != null)
        bwriter_neg2.newLine();
    if (bwriter_neg1 != null)
        bwriter_neg1.close();
    if (bwriter_neg2 != null)
        bwriter_neg2.close();
}
Also used : ArrayList(java.util.ArrayList) IPeakListReader(de.ipbhalle.metfraglib.interfaces.IPeakListReader) FileFilter(java.io.FileFilter) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) Settings(de.ipbhalle.metfraglib.settings.Settings) GetRankOfCandidateMultipleThreadCSV(de.ipbhalle.metfrag.ranking.GetRankOfCandidateMultipleThreadCSV) SettingsChecker(de.ipbhalle.metfraglib.parameter.SettingsChecker) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Example 3 with SettingsChecker

use of de.ipbhalle.metfraglib.parameter.SettingsChecker in project MetFragRelaunched by ipb-halle.

the class PostCalculateScoreValuesFromResultFilePeakLossThreadFP method main.

public static void main(String[] args) throws Exception {
    getArgs(args);
    String paramfolder = (String) argsHash.get("parampath");
    String resfolder = (String) argsHash.get("resultpath");
    String outputfolder = (String) argsHash.get("output");
    String alphaPeak = (String) argsHash.get("alphaPeak");
    String betaPeak = (String) argsHash.get("betaPeak");
    String alphaLoss = (String) argsHash.get("alphaLoss");
    String betaLoss = (String) argsHash.get("betaLoss");
    int numberThreads = Integer.parseInt(argsHash.get("threads"));
    String fingerprinttype = (String) argsHash.get("fingerprinttype");
    ALPHA_VALUE_PEAK = Double.parseDouble(alphaPeak);
    BETA_VALUE_PEAK = Double.parseDouble(betaPeak);
    ALPHA_VALUE_LOSS = Double.parseDouble(alphaLoss);
    BETA_VALUE_LOSS = Double.parseDouble(betaLoss);
    File _resfolder = new File(resfolder);
    File _paramfolder = new File(paramfolder);
    File[] resultFiles = _resfolder.listFiles();
    File[] paramFiles = _paramfolder.listFiles();
    ArrayList<ProcessThread> threads = new ArrayList<ProcessThread>();
    for (int i = 0; i < paramFiles.length; i++) {
        String id = paramFiles[i].getName().split("\\.")[0];
        int resultFileID = -1;
        for (int j = 0; j < resultFiles.length; j++) {
            if (resultFiles[j].getName().startsWith(id + ".")) {
                resultFileID = j;
                break;
            }
        }
        if (resultFileID == -1) {
            System.out.println(id + " not found as result.");
            continue;
        }
        Settings settings = getSettings(paramFiles[i].getAbsolutePath());
        settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, resultFiles[resultFileID].getAbsolutePath());
        settings.set(VariableNames.PEAK_FINGERPRINT_ANNOTATION_ALPHA_VALUE_NAME, ALPHA_VALUE_PEAK);
        settings.set(VariableNames.PEAK_FINGERPRINT_ANNOTATION_BETA_VALUE_NAME, BETA_VALUE_PEAK);
        settings.set(VariableNames.LOSS_FINGERPRINT_ANNOTATION_ALPHA_VALUE_NAME, ALPHA_VALUE_LOSS);
        settings.set(VariableNames.LOSS_FINGERPRINT_ANNOTATION_BETA_VALUE_NAME, BETA_VALUE_LOSS);
        SettingsChecker sc = new SettingsChecker();
        if (!sc.check(settings)) {
            System.out.println("Error checking settings for " + id);
            continue;
        }
        IPeakListReader peakListReader = (IPeakListReader) Class.forName((String) settings.get(VariableNames.METFRAG_PEAK_LIST_READER_NAME)).getConstructor(Settings.class).newInstance(settings);
        settings.set(VariableNames.PEAK_LIST_NAME, peakListReader.read());
        ProcessThread thread = new PostCalculateScoreValuesFromResultFilePeakLossThreadFP().new ProcessThread(settings, outputfolder, fingerprinttype, resfolder);
        threads.add(thread);
    }
    System.out.println("preparation finished");
    ExecutorService executer = Executors.newFixedThreadPool(numberThreads);
    for (ProcessThread thread : threads) {
        executer.execute(thread);
    }
    executer.shutdown();
    while (!executer.isTerminated()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IPeakListReader(de.ipbhalle.metfraglib.interfaces.IPeakListReader) SettingsChecker(de.ipbhalle.metfraglib.parameter.SettingsChecker) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) Settings(de.ipbhalle.metfraglib.settings.Settings)

Example 4 with SettingsChecker

use of de.ipbhalle.metfraglib.parameter.SettingsChecker in project MetFragRelaunched by ipb-halle.

the class AddMissingNonExplainedPeaks method main.

public static void main(String[] args) throws Exception {
    String paramfile = args[0];
    String resultfile = args[1];
    String outputfile = args[2];
    Settings settings = getSettings(paramfile);
    settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, resultfile);
    IPeakListReader peakListReader = (IPeakListReader) Class.forName((String) settings.get(VariableNames.METFRAG_PEAK_LIST_READER_NAME)).getConstructor(Settings.class).newInstance(settings);
    SettingsChecker settingsChecker = new SettingsChecker();
    if (!settingsChecker.check(settings)) {
        System.err.println("Problems reading settings");
        return;
    }
    settings.set(VariableNames.PEAK_LIST_NAME, peakListReader.read());
    IDatabase db = null;
    String dbFilename = (String) settings.get(VariableNames.LOCAL_DATABASE_PATH_NAME);
    if (dbFilename.endsWith("psv"))
        db = new LocalPSVDatabase(settings);
    else
        db = new LocalCSVDatabase(settings);
    ArrayList<String> ids = null;
    try {
        ids = db.getCandidateIdentifiers();
    } catch (MultipleHeadersFoundInInputDatabaseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    CandidateList candidates = null;
    try {
        candidates = db.getCandidateByIdentifier(ids);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (candidates.getNumberElements() == 0) {
        System.out.println("No candidates found in " + (String) settings.get(VariableNames.LOCAL_DATABASE_PATH_NAME));
        return;
    }
    DefaultPeakList peaklist = (DefaultPeakList) settings.get(VariableNames.PEAK_LIST_NAME);
    for (int i = 0; i < candidates.getNumberElements(); i++) {
        String explPeaks = (String) candidates.getElement(i).getProperty("ExplPeaks");
        String[] explPeaksArray = explPeaks.split(";");
        Double[] explPeaksMasses = null;
        if (!explPeaks.equals("NA"))
            explPeaksMasses = getDoubleArrayFromPeakList(explPeaksArray);
        String nonExplPeaksString = "";
        for (int k = 0; k < peaklist.getNumberElements(); k++) {
            if (explPeaks.equals("NA")) {
                nonExplPeaksString += ((IPeak) peaklist.getElement(k)).getMass() + ";";
            } else if (!isContained(((IPeak) peaklist.getElement(k)).getMass(), explPeaksMasses)) {
                nonExplPeaksString += ((IPeak) peaklist.getElement(k)).getMass() + ";";
            }
        }
        if (nonExplPeaksString.length() == 0)
            nonExplPeaksString = "NA";
        if (nonExplPeaksString.endsWith(";"))
            nonExplPeaksString = nonExplPeaksString.substring(0, nonExplPeaksString.length() - 1);
        candidates.getElement(i).setProperty("NonExplainedMasses", nonExplPeaksString);
    }
    IWriter writer = null;
    if (outputfile.endsWith("psv"))
        writer = new CandidateListWriterPSV();
    else
        writer = new CandidateListWriterCSV();
    writer.write(candidates, outputfile);
}
Also used : IPeak(de.ipbhalle.metfraglib.interfaces.IPeak) IDatabase(de.ipbhalle.metfraglib.interfaces.IDatabase) IWriter(de.ipbhalle.metfraglib.interfaces.IWriter) DefaultPeakList(de.ipbhalle.metfraglib.list.DefaultPeakList) LocalCSVDatabase(de.ipbhalle.metfraglib.database.LocalCSVDatabase) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) IPeakListReader(de.ipbhalle.metfraglib.interfaces.IPeakListReader) CandidateListWriterCSV(de.ipbhalle.metfraglib.writer.CandidateListWriterCSV) LocalPSVDatabase(de.ipbhalle.metfraglib.database.LocalPSVDatabase) SettingsChecker(de.ipbhalle.metfraglib.parameter.SettingsChecker) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) CandidateListWriterPSV(de.ipbhalle.metfraglib.writer.CandidateListWriterPSV) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) Settings(de.ipbhalle.metfraglib.settings.Settings)

Example 5 with SettingsChecker

use of de.ipbhalle.metfraglib.parameter.SettingsChecker in project MetFragRelaunched by ipb-halle.

the class PostCalculateScoreValuesAndRankFromResultFilePeakLossMultipleThreadFP method main.

public static void main(String[] args) throws Exception {
    getArgs(args);
    String paramfolder = (String) argsHash.get("parampath");
    String resfolder = (String) argsHash.get("resultpath");
    String outputfile = (String) argsHash.get("output");
    int numberThreads = Integer.parseInt(argsHash.get("threads"));
    double[][] weights = readValues(argsHash.get("weights"));
    double[][] pseudos = readValues(argsHash.get("pseudos"));
    Boolean stdout = Boolean.parseBoolean(argsHash.get("stdout"));
    String outputtype = (String) argsHash.get("outputtype");
    Boolean negScores = Boolean.parseBoolean(argsHash.get("negscore"));
    String[] scoringPropertyNames = new String[] { "FragmenterScore", "AutomatedPeakFingerprintAnnotationScore", "AutomatedLossFingerprintAnnotationScore" };
    Boolean transformScores = Boolean.parseBoolean(argsHash.get("transform"));
    String outputfolder = (String) argsHash.get("outputfolder");
    File _resfolder = new File(resfolder);
    File _paramfolder = new File(paramfolder);
    File[] resultFiles = _resfolder.listFiles();
    File[] paramFiles = _paramfolder.listFiles();
    ArrayList<ProcessThread> threads = new ArrayList<ProcessThread>();
    for (int i = 0; i < paramFiles.length; i++) {
        String id = paramFiles[i].getName().split("\\.")[0];
        int resultFileID = -1;
        for (int j = 0; j < resultFiles.length; j++) {
            if (resultFiles[j].getName().startsWith(id + ".")) {
                resultFileID = j;
                break;
            }
        }
        if (resultFileID == -1) {
            System.err.println(id + " not found as result.");
            continue;
        }
        Settings settings = getSettings(paramFiles[i].getAbsolutePath());
        settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, resultFiles[resultFileID].getAbsolutePath());
        SettingsChecker sc = new SettingsChecker();
        if (!sc.check(settings)) {
            System.err.println("Error checking settings for " + id);
            continue;
        }
        IPeakListReader peakListReader = (IPeakListReader) Class.forName((String) settings.get(VariableNames.METFRAG_PEAK_LIST_READER_NAME)).getConstructor(Settings.class).newInstance(settings);
        settings.set(VariableNames.PEAK_LIST_NAME, peakListReader.read());
        ProcessThread thread = new PostCalculateScoreValuesAndRankFromResultFilePeakLossMultipleThreadFP().new ProcessThread(settings, outputfile, resfolder, paramFiles[i].getAbsolutePath(), outputtype, negScores, weights, transformScores, scoringPropertyNames, stdout, pseudos);
        threads.add(thread);
    }
    System.err.println("preparation finished");
    ExecutorService executer = Executors.newFixedThreadPool(numberThreads);
    for (ProcessThread thread : threads) {
        executer.execute(thread);
    }
    executer.shutdown();
    while (!executer.isTerminated()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    for (int kk = 0; kk < pseudos.length; kk++) {
        java.util.ArrayList<Integer> weightIndexesOfInterestPos = new java.util.ArrayList<Integer>();
        java.util.ArrayList<Integer> weightIndexesOfInterestNeg = new java.util.ArrayList<Integer>();
        for (int i = 0; i < weights.length; i++) {
            weightIndexesOfInterestPos.add(i);
            weightIndexesOfInterestNeg.add(i);
        }
        java.util.ArrayList<Integer> nextWeightIndexesOfInterestPos;
        java.util.ArrayList<Integer> nextWeightIndexesOfInterestNeg;
        int[] bestRanksPos = new int[10];
        int bestWeightIndexPos = -1;
        int[] bestRanksNeg = new int[10];
        int bestWeightIndexNeg = -1;
        for (int i = 1; i <= 10; i++) {
            // for pos queries
            int currentBestValuePos = 0;
            nextWeightIndexesOfInterestPos = new java.util.ArrayList<Integer>();
            for (int w = 0; w < weightIndexesOfInterestPos.size(); w++) {
                int thisBestValuePos = 0;
                for (ProcessThread thread : threads) {
                    // get rank of query in thread obtained by using weight w
                    if (thread.isPositiveQuery() && thread.getRanksForWeight(kk)[weightIndexesOfInterestPos.get(w)] <= i)
                        thisBestValuePos++;
                }
                if (currentBestValuePos < thisBestValuePos) {
                    currentBestValuePos = thisBestValuePos;
                    nextWeightIndexesOfInterestPos.clear();
                    nextWeightIndexesOfInterestPos.add(weightIndexesOfInterestPos.get(w));
                } else if (currentBestValuePos == thisBestValuePos)
                    nextWeightIndexesOfInterestPos.add(weightIndexesOfInterestPos.get(w));
            }
            bestRanksPos[i - 1] = currentBestValuePos;
            bestWeightIndexPos = nextWeightIndexesOfInterestPos.get(0);
            weightIndexesOfInterestPos = nextWeightIndexesOfInterestPos;
            // for neg queries
            int currentBestValueNeg = 0;
            nextWeightIndexesOfInterestNeg = new java.util.ArrayList<Integer>();
            for (int w = 0; w < weightIndexesOfInterestNeg.size(); w++) {
                int thisBestValueNeg = 0;
                for (ProcessThread thread : threads) {
                    // get rank of query in thread obtained by using weight w
                    if (!thread.isPositiveQuery() && thread.getRanksForWeight(kk)[weightIndexesOfInterestNeg.get(w)] <= i)
                        thisBestValueNeg++;
                }
                if (currentBestValueNeg < thisBestValueNeg) {
                    currentBestValueNeg = thisBestValueNeg;
                    nextWeightIndexesOfInterestNeg.clear();
                    nextWeightIndexesOfInterestNeg.add(weightIndexesOfInterestNeg.get(w));
                } else if (currentBestValueNeg == thisBestValueNeg)
                    nextWeightIndexesOfInterestNeg.add(weightIndexesOfInterestNeg.get(w));
            }
            bestRanksNeg[i - 1] = currentBestValueNeg;
            bestWeightIndexNeg = nextWeightIndexesOfInterestNeg.get(0);
            weightIndexesOfInterestNeg = nextWeightIndexesOfInterestNeg;
        }
        String bestWeightStringPos = weights[bestWeightIndexPos][0] + "";
        String bestWeightStringNeg = weights[bestWeightIndexNeg][0] + "";
        for (int i = 1; i < weights[bestWeightIndexPos].length; i++) {
            bestWeightStringPos += " " + weights[bestWeightIndexPos][i];
            bestWeightStringNeg += " " + weights[bestWeightIndexNeg][i];
        }
        String bestRankingsStringPos = bestRanksPos[0] + "";
        String bestRankingsStringNeg = bestRanksNeg[0] + "";
        for (int i = 1; i < bestRanksPos.length; i++) {
            bestRankingsStringPos += " " + bestRanksPos[i];
            bestRankingsStringNeg += " " + bestRanksNeg[i];
        }
        File folder = new File(outputfolder + Constants.OS_SPECIFIC_FILE_SEPARATOR + "best_values");
        if (!folder.exists())
            folder.mkdirs();
        String fileprefix = pseudos[kk][0] + "_" + pseudos[kk][1] + "_" + pseudos[kk][2] + "_" + pseudos[kk][3];
        fileprefix = fileprefix.replaceAll("\\.0E", "e").replaceAll("\\.", "");
        String path_pos = folder.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "pos" + Constants.OS_SPECIFIC_FILE_SEPARATOR;
        String path_neg = folder.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "neg" + Constants.OS_SPECIFIC_FILE_SEPARATOR;
        new File(path_pos).mkdirs();
        new File(path_neg).mkdirs();
        java.io.BufferedWriter bwriter_pos1 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_pos + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_weights.txt")));
        java.io.BufferedWriter bwriter_pos2 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_pos + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_tops.txt")));
        java.io.BufferedWriter bwriter_neg1 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_neg + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_weights.txt")));
        java.io.BufferedWriter bwriter_neg2 = new java.io.BufferedWriter(new java.io.FileWriter(new File(path_neg + Constants.OS_SPECIFIC_FILE_SEPARATOR + fileprefix + "_tops.txt")));
        bwriter_pos1.write(bestWeightStringPos);
        bwriter_pos1.newLine();
        bwriter_pos2.write(bestRankingsStringPos);
        bwriter_pos2.newLine();
        bwriter_pos1.close();
        bwriter_pos2.close();
        bwriter_neg1.write(bestWeightStringNeg);
        bwriter_neg1.newLine();
        bwriter_neg2.write(bestRankingsStringNeg);
        bwriter_neg2.newLine();
        bwriter_neg1.close();
        bwriter_neg2.close();
    }
}
Also used : ArrayList(java.util.ArrayList) IPeakListReader(de.ipbhalle.metfraglib.interfaces.IPeakListReader) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) Settings(de.ipbhalle.metfraglib.settings.Settings) SettingsChecker(de.ipbhalle.metfraglib.parameter.SettingsChecker) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Aggregations

SettingsChecker (de.ipbhalle.metfraglib.parameter.SettingsChecker)10 MetFragGlobalSettings (de.ipbhalle.metfraglib.settings.MetFragGlobalSettings)9 File (java.io.File)8 IPeakListReader (de.ipbhalle.metfraglib.interfaces.IPeakListReader)6 Settings (de.ipbhalle.metfraglib.settings.Settings)6 ArrayList (java.util.ArrayList)6 ExecutorService (java.util.concurrent.ExecutorService)5 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)4 CombinedMetFragProcess (de.ipbhalle.metfraglib.process.CombinedMetFragProcess)4 GetRankOfCandidateMultipleThreadCSV (de.ipbhalle.metfrag.ranking.GetRankOfCandidateMultipleThreadCSV)2 IWriter (de.ipbhalle.metfraglib.interfaces.IWriter)2 FileFilter (java.io.FileFilter)2 IOException (java.io.IOException)2 CouldNotCreateProcessException (de.ipbhalle.exception.CouldNotCreateProcessException)1 CouldNotFetchResultsException (de.ipbhalle.exception.CouldNotFetchResultsException)1 CouldNotReadHostException (de.ipbhalle.exception.CouldNotReadHostException)1 CouldNotReadStatusException (de.ipbhalle.exception.CouldNotReadStatusException)1 CouldNotRemoveProcessException (de.ipbhalle.exception.CouldNotRemoveProcessException)1 CouldNotWriteStatusException (de.ipbhalle.exception.CouldNotWriteStatusException)1 HydrogenDeuteriumPlugin (de.ipbhalle.metfrag.plugins.HydrogenDeuteriumPlugin)1