Search in sources :

Example 1 with LocalCSVDatabase

use of de.ipbhalle.metfraglib.database.LocalCSVDatabase in project MetFragRelaunched by ipb-halle.

the class GetRankOfCandidateCSV method main.

/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    if (args.length < 3) {
        System.out.println("usage: progname csv propname=value prop1=weight1 [prop2=weight2 ...] [true|false] [filename]");
        System.exit(1);
    }
    String resultCSVFilename = args[0];
    String toSearchPropertyName = args[1].trim().split("=")[0];
    String toSearchPropertyValue = args[1].trim().split("=")[1];
    String[] toSearchPropertyValues = toSearchPropertyValue.split("/");
    String filename = "";
    if (args[args.length - 2].trim().equals("true")) {
        outputSortedList = true;
        filename = args[args.length - 1].trim();
    } else if (args[args.length - 1].trim().equals("true")) {
        outputSortedList = true;
    } else {
        outputSortedList = false;
    }
    MetFragGlobalSettings settings = new MetFragGlobalSettings();
    settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, resultCSVFilename);
    LocalCSVDatabase db = new LocalCSVDatabase(settings);
    ArrayList<String> identifiers = null;
    try {
        identifiers = db.getCandidateIdentifiers();
    } catch (MultipleHeadersFoundInInputDatabaseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    CandidateList candidates = db.getCandidateByIdentifier(identifiers);
    ArrayList<String> scoringPropertyNames = new ArrayList<String>();
    HashMap<String, Double> scorePropertyToWeight = new HashMap<String, Double>();
    ArrayList<Integer> indexesOfCorrectMolecules = new ArrayList<Integer>();
    for (int i = 2; i < args.length; i++) {
        String[] tmp = args[i].split("=");
        if (tmp.length == 1)
            continue;
        if (tmp[0].equals("CombinedReferenceScore")) {
            scorePropertyToWeight.put(tmp[0], Double.parseDouble(tmp[2]));
            combinedReferenceScoreValues = tmp[1].trim().split(",");
        } else
            scorePropertyToWeight.put(tmp[0], Double.parseDouble(tmp[1]));
        scoringPropertyNames.add(tmp[0]);
    }
    double[] scorePropertyToMaximumValue = new double[scorePropertyToWeight.size()];
    for (int i = 0; i < scorePropertyToMaximumValue.length; i++) scorePropertyToMaximumValue[i] = Integer.MIN_VALUE;
    HashMap<String, Double> inchiKeysToFinalScore = new HashMap<String, Double>();
    HashMap<String, String> inchiKeysToInChI = new HashMap<String, String>();
    int found = -1;
    for (int i = 0; i < candidates.getNumberElements(); i++) {
        String currentInChIKey1 = (String) candidates.getElement(i).getProperty(VariableNames.INCHI_KEY_1_NAME);
        String currentPropertyValue = (String) candidates.getElement(i).getProperty(toSearchPropertyName);
        if (currentPropertyValue == null)
            continue;
        inchiKeysToFinalScore.put(currentInChIKey1, (double) Integer.MIN_VALUE);
        inchiKeysToInChI.put(currentInChIKey1, (String) candidates.getElement(i).getProperty(VariableNames.INCHI_NAME));
        for (int l = 0; l < toSearchPropertyValues.length; l++) {
            if (currentPropertyValue.compareTo(toSearchPropertyValues[l]) == 0) {
                found = l;
                indexesOfCorrectMolecules.add(i);
            }
        }
        for (int l = 0; l < scoringPropertyNames.size(); l++) {
            double curVal = 0.0;
            /*
				 * added for combined candidate score
				 */
            if (scoringPropertyNames.get(l).equals("CombinedReferenceScore")) {
                curVal = getCombinedScoreValue(candidates.getElement(i));
            } else {
                curVal = Double.parseDouble((String) candidates.getElement(i).getProperty(scoringPropertyNames.get(l)));
            }
            if (scorePropertyNamesForLog.contains(scoringPropertyNames.get(l)) && curVal != 0.0) {
                curVal = Math.log(curVal);
            }
            if (scorePropertyToMaximumValue[l] < curVal)
                scorePropertyToMaximumValue[l] = curVal;
        }
    }
    boolean[] negativeScore = new boolean[scoringPropertyNames.size()];
    for (int l = 0; l < scoringPropertyNames.size(); l++) {
        if (scorePropertyToMaximumValue[l] == 0.0)
            scorePropertyToMaximumValue[l] = 1.0;
        else if (scorePropertyToMaximumValue[l] < 0) {
            negativeScore[l] = true;
            scorePropertyToMaximumValue[l] = 1.0 / Math.abs(scorePropertyToMaximumValue[l]);
        }
    }
    if (found == -1 && !outputSortedList) {
        System.out.println(toSearchPropertyValue + " not found in " + resultCSVFilename);
        System.exit(0);
    }
    HashMap<String, Integer> inchikeyToIndex = new HashMap<String, Integer>();
    for (int j = 0; j < candidates.getNumberElements(); j++) {
        double curScore = 0.0;
        for (int l = 0; l < scoringPropertyNames.size(); l++) {
            double currentValue = 0.0;
            if (scoringPropertyNames.get(l).equals("CombinedReferenceScore")) {
                currentValue = getCombinedScoreValue(candidates.getElement(j));
                candidates.getElement(j).setProperty("CombinedReferenceScore", currentValue);
            } else
                currentValue = Double.parseDouble((String) candidates.getElement(j).getProperty(scoringPropertyNames.get(l)));
            if (negativeScore[l]) {
                curScore += ((1.0 / Math.abs(currentValue)) / scorePropertyToMaximumValue[l]) * scorePropertyToWeight.get(scoringPropertyNames.get(l));
            } else if (!scorePropertyNamesForTakeRawValue.contains(scoringPropertyNames.get(l)))
                curScore += (currentValue / scorePropertyToMaximumValue[l]) * scorePropertyToWeight.get(scoringPropertyNames.get(l));
            else
                curScore += (currentValue) * scorePropertyToWeight.get(scoringPropertyNames.get(l));
        }
        String currentInChIKey = (String) candidates.getElement(j).getProperty(VariableNames.INCHI_KEY_1_NAME);
        if (inchiKeysToFinalScore.containsKey(currentInChIKey)) {
            double value = inchiKeysToFinalScore.get(currentInChIKey);
            if (value < curScore) {
                inchiKeysToFinalScore.put(currentInChIKey, curScore);
                inchikeyToIndex.put(currentInChIKey, j);
            }
        } else {
            try {
                inchiKeysToFinalScore.put(currentInChIKey, curScore);
                inchikeyToIndex.put(currentInChIKey, j);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    int rank = 1;
    double wc = 0;
    double bc = 0;
    ArrayList<String> correctInChIKeys = new ArrayList<String>();
    for (int i = 0; i < indexesOfCorrectMolecules.size(); i++) {
        correctInChIKeys.add((String) candidates.getElement(indexesOfCorrectMolecules.get(i)).getProperty(VariableNames.INCHI_KEY_1_NAME));
    }
    int indexOfCorrect = -1;
    String inchikeyMaximalScore = "";
    double scoreOfCorrect = (double) Integer.MIN_VALUE;
    for (int i = 0; i < correctInChIKeys.size(); i++) {
        double currentScore = inchiKeysToFinalScore.get(correctInChIKeys.get(i));
        if (currentScore > scoreOfCorrect) {
            inchikeyMaximalScore = correctInChIKeys.get(i);
            indexOfCorrect = inchikeyToIndex.get(correctInChIKeys.get(i));
            scoreOfCorrect = currentScore;
        }
    }
    for (String inchiKey : inchiKeysToFinalScore.keySet()) {
        double currentScore = inchiKeysToFinalScore.get(inchiKey);
        if (currentScore > scoreOfCorrect) {
            rank++;
        }
        if (currentScore == scoreOfCorrect && !correctInChIKeys.contains(inchiKey))
            rank++;
        if (currentScore > scoreOfCorrect)
            bc++;
        if (currentScore < scoreOfCorrect)
            wc++;
    }
    double rrp = 1.0;
    if (inchiKeysToFinalScore.size() != 1)
        rrp = 0.5 * (1.0 - (bc - wc) / (double) ((inchiKeysToFinalScore.size() - 1)));
    if (found != -1) {
        if (!outputSortedList) {
            String values = scoringPropertyNames.get(0) + "=" + candidates.getElement(indexOfCorrect).getProperty(scoringPropertyNames.get(0)) + "";
            String maximalValues = "Max" + scoringPropertyNames.get(0) + "=" + scorePropertyToMaximumValue[0];
            for (int i = 1; i < scoringPropertyNames.size(); i++) {
                values += " " + scoringPropertyNames.get(i) + "=" + candidates.getElement(indexOfCorrect).getProperty(scoringPropertyNames.get(i));
                maximalValues += " Max" + scoringPropertyNames.get(i) + "=" + scorePropertyToMaximumValue[i];
            }
            System.out.println(new File(resultCSVFilename).getName() + " " + toSearchPropertyValue + " " + rank + " " + (inchiKeysToFinalScore.size()) + " " + candidates.getElement(indexOfCorrect).getProperty(VariableNames.IDENTIFIER_NAME) + " " + candidates.getElement(indexOfCorrect).getProperty("NoExplPeaks") + " " + candidates.getElement(indexOfCorrect).getProperty("NumberPeaksUsed") + " " + rrp + " " + bc + " " + wc + " " + values + " " + inchikeyMaximalScore + " " + maximalValues);
        } else {
            String maximalValues = "Max" + scoringPropertyNames.get(0) + "=" + scorePropertyToMaximumValue[0];
            for (int i = 1; i < scoringPropertyNames.size(); i++) {
                maximalValues += " Max" + scoringPropertyNames.get(i) + "=" + scorePropertyToMaximumValue[i];
            }
            java.util.Iterator<?> it = inchiKeysToFinalScore.keySet().iterator();
            java.util.ArrayList<Double> scores = new java.util.ArrayList<Double>();
            java.util.ArrayList<String> inchikeys = new java.util.ArrayList<String>();
            String values = "";
            while (it.hasNext()) {
                String currentInChIKey = (String) it.next();
                double currentScore = inchiKeysToFinalScore.get(currentInChIKey);
                int currentIndex = addSorted(scores, currentScore);
                inchikeys.add(currentIndex, currentInChIKey);
                double curScore = Double.parseDouble((String) candidates.getElement(indexOfCorrect).getProperty(scoringPropertyNames.get(0)));
                if (negativeScore[0]) {
                    curScore = 1.0 / Math.abs(curScore);
                }
                values = scoringPropertyNames.get(0) + "=" + curScore + "";
                for (int i = 1; i < scoringPropertyNames.size(); i++) {
                    curScore = Double.parseDouble((String) candidates.getElement(indexOfCorrect).getProperty(scoringPropertyNames.get(i)));
                    if (negativeScore[i]) {
                        curScore = 1.0 / Math.abs(curScore);
                    }
                    values += " " + scoringPropertyNames.get(i) + "=" + curScore;
                }
            }
            if (filename.length() == 0) {
                for (int i = 0; i < scores.size(); i++) {
                    System.out.println(inchiKeysToInChI.get(inchikeys.get(i)) + " " + scores.get(i) + " " + inchikeys.get(i) + " " + values + " " + maximalValues);
                }
            } else {
                java.io.BufferedWriter bwriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(filename)));
                for (int i = 0; i < scores.size(); i++) {
                    bwriter.write(inchiKeysToInChI.get(inchikeys.get(i)) + " " + scores.get(i) + " " + inchikeys.get(i) + " " + values + " " + maximalValues);
                    bwriter.newLine();
                }
                bwriter.close();
            }
        }
    } else if (outputSortedList) {
        java.util.Iterator<?> it = inchiKeysToFinalScore.keySet().iterator();
        java.util.ArrayList<Double> scores = new java.util.ArrayList<Double>();
        java.util.ArrayList<String> inchikeys = new java.util.ArrayList<String>();
        while (it.hasNext()) {
            String currentInChIKey = (String) it.next();
            double currentScore = inchiKeysToFinalScore.get(currentInChIKey);
            int currentIndex = addSorted(scores, currentScore);
            inchikeys.add(currentIndex, currentInChIKey);
        }
        if (filename.length() == 0) {
            for (int i = 0; i < scores.size(); i++) {
                System.out.println(inchiKeysToInChI.get(inchikeys.get(i)) + " " + scores.get(i) + " " + inchikeys.get(i));
            }
        } else {
            java.io.BufferedWriter bwriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(filename)));
            for (int i = 0; i < scores.size(); i++) {
                bwriter.write(inchiKeysToInChI.get(inchikeys.get(i)) + " " + scores.get(i) + " " + inchikeys.get(i));
                bwriter.newLine();
            }
            bwriter.close();
        }
    } else {
        System.out.println(toSearchPropertyValue + " not found in " + resultCSVFilename);
    }
}
Also used : MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) LocalCSVDatabase(de.ipbhalle.metfraglib.database.LocalCSVDatabase) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) File(java.io.File) File(java.io.File)

Example 2 with LocalCSVDatabase

use of de.ipbhalle.metfraglib.database.LocalCSVDatabase 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 3 with LocalCSVDatabase

use of de.ipbhalle.metfraglib.database.LocalCSVDatabase in project MetFragRelaunched by ipb-halle.

the class CombineResultsForAnnotation method getMatchingCandidate.

/**
 * @param metfragFiles
 * @param id
 * @param inchikey1
 * @return
 */
public static ICandidate getMatchingCandidate(File[] metfragFiles, String id, String inchikey1) {
    for (int i = 0; i < metfragFiles.length; i++) {
        if (metfragFiles[i].getName().startsWith(id)) {
            MetFragGlobalSettings settings = new MetFragGlobalSettings();
            settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, metfragFiles[i].getAbsolutePath());
            IDatabase db = null;
            if (metfragFiles[i].getName().endsWith("csv"))
                db = new LocalCSVDatabase(settings);
            else
                db = new LocalPSVDatabase(settings);
            ArrayList<String> identifiers = null;
            try {
                identifiers = db.getCandidateIdentifiers();
            } catch (MultipleHeadersFoundInInputDatabaseException e1) {
                e1.printStackTrace();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            CandidateList candidates = null;
            try {
                candidates = db.getCandidateByIdentifier(identifiers);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            for (int ii = 0; ii < candidates.getNumberElements(); ii++) {
                if (((String) candidates.getElement(ii).getProperty(VariableNames.INCHI_KEY_1_NAME)).equals(inchikey1)) {
                    return candidates.getElement(ii);
                }
            }
        }
    }
    return null;
}
Also used : IDatabase(de.ipbhalle.metfraglib.interfaces.IDatabase) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) LocalPSVDatabase(de.ipbhalle.metfraglib.database.LocalPSVDatabase) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) LocalCSVDatabase(de.ipbhalle.metfraglib.database.LocalCSVDatabase) IOException(java.io.IOException) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException)

Example 4 with LocalCSVDatabase

use of de.ipbhalle.metfraglib.database.LocalCSVDatabase in project MetFragRelaunched by ipb-halle.

the class WriteMetFragToMZtab method main.

public static void main(String[] args) {
    if (!getArgs(args)) {
        System.err.println("Error reading parameters.");
        System.exit(1);
    }
    String metfragFolder = argsHash.get("metfragFolder");
    int numberCandidates = Integer.parseInt(argsHash.get("numberCandidates"));
    String output = argsHash.get("output");
    // check metfrag result folder
    File resultFolder = new File(metfragFolder);
    if (!resultFolder.exists()) {
        System.err.println(resultFolder.getAbsolutePath() + " not found.");
        System.exit(2);
    }
    // get files to convert to mztab
    File[] files = resultFolder.listFiles();
    // mztab feature table
    FeatureTable candidateTable = MSDKObjectBuilder.getFeatureTable("candidateTable", DataPointStoreFactory.getMemoryDataStore());
    // define columns
    FeatureTableColumn<Integer> idColumn = MSDKObjectBuilder.getIdFeatureTableColumn();
    FeatureTableColumn<Double> mzColumn = MSDKObjectBuilder.getMzFeatureTableColumn();
    FeatureTableColumn<ChromatographyInfo> chromatographyInfoColumn = MSDKObjectBuilder.getChromatographyInfoFeatureTableColumn();
    FeatureTableColumn<List<IonAnnotation>> ionAnnotationColumn = MSDKObjectBuilder.getIonAnnotationFeatureTableColumn();
    FeatureTableColumn<Integer> chargeColumn = MSDKObjectBuilder.getChargeFeatureTableColumn();
    // add columns
    candidateTable.addColumn(idColumn);
    candidateTable.addColumn(mzColumn);
    candidateTable.addColumn(chromatographyInfoColumn);
    candidateTable.addColumn(ionAnnotationColumn);
    candidateTable.addColumn(chargeColumn);
    // current row number of feature table
    int rownumber = 1;
    for (int i = 0; i < files.length; i++) {
        MetFragGlobalSettings settings = new MetFragGlobalSettings();
        settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, files[i].getAbsolutePath());
        IDatabase db = null;
        if (files[i].getName().endsWith("csv"))
            db = new LocalCSVDatabase(settings);
        else
            db = new LocalPSVDatabase(settings);
        Float rt = 0.0f;
        Double mz = 0.0;
        try {
            String[] tmp = files[i].getName().split("_");
            if (tmp.length == 1)
                throw new Exception();
            rt = Float.parseFloat(tmp[0]);
            mz = Double.parseDouble(tmp[1]);
        } catch (Exception e) {
            System.out.println(files[i].getName() + " has no rt and mz information. Check file name.");
        }
        ArrayList<String> identifiers = null;
        try {
            identifiers = db.getCandidateIdentifiers();
        } catch (MultipleHeadersFoundInInputDatabaseException e1) {
            e1.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        CandidateList candidates = null;
        try {
            candidates = db.getCandidateByIdentifier(identifiers);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        int candidateIndex = 0;
        // get candidates and store them in the FeatureTable
        while (candidateIndex < numberCandidates && candidateIndex < candidates.getNumberElements()) {
            ICandidate candidate = candidates.getElement(candidateIndex);
            FeatureTableRow currentRow = MSDKObjectBuilder.getFeatureTableRow(candidateTable, rownumber);
            FeatureTableColumn<Object> column;
            // Add common data to columns
            // Common column: Id
            column = candidateTable.getColumn(ColumnName.ID, null);
            currentRow.setData(column, Integer.valueOf(rownumber));
            // Common column: m/z
            column = candidateTable.getColumn(ColumnName.MZ, null);
            currentRow.setData(column, mz);
            // Annotation
            column = candidateTable.getColumn(ColumnName.IONANNOTATION, null);
            List<IonAnnotation> ionAnnotations = new ArrayList<IonAnnotation>();
            IonAnnotation ionAnnotation = MSDKObjectBuilder.getIonAnnotation();
            ionAnnotation.setAnnotationId(candidate.getIdentifier());
            try {
                ionAnnotation.setChemicalStructure(candidate.getAtomContainer());
                ionAnnotation.setFormula(MolecularFormulaManipulator.getMolecularFormula(candidate.getAtomContainer()));
                ionAnnotation.setInchiKey(InChIGeneratorFactory.getInstance().getInChIGenerator(candidate.getAtomContainer()).getInchiKey());
            } catch (Exception e) {
                candidateIndex++;
                continue;
            }
            ionAnnotation.setDescription((String) candidate.getProperty(VariableNames.COMPOUND_NAME_NAME));
            ionAnnotations.add(ionAnnotation);
            // ionAnnotation.setExpectedMz(metfrag_settings.get(VariableNames.PRECURSOR_NEUTRAL_MASS_NAME));
            currentRow.setData(column, ionAnnotations);
            // RT
            if (rt != null) {
                ChromatographyInfo cgInfo = MSDKObjectBuilder.getChromatographyInfo1D(SeparationType.LC, rt);
                FeatureTableColumn<ChromatographyInfo> rtcolumn = candidateTable.getColumn("Chromatography Info", null, ChromatographyInfo.class);
                currentRow.setData(rtcolumn, cgInfo);
            }
            // Add row to feature table
            candidateTable.addRow(currentRow);
            rownumber++;
            candidateIndex++;
        }
    }
    // write out mzTab file
    File outputFile = new File(output);
    MzTabFileExportMethod method = new MzTabFileExportMethod(candidateTable, outputFile, true);
    try {
        method.execute();
    } catch (MSDKException e) {
        e.printStackTrace();
        System.err.println("Could not write mzTab file.");
    }
}
Also used : MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) ArrayList(java.util.ArrayList) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) ChromatographyInfo(io.github.msdk.datamodel.rawdata.ChromatographyInfo) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ArrayList(java.util.ArrayList) List(java.util.List) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) MzTabFileExportMethod(io.github.msdk.io.mztab.MzTabFileExportMethod) FeatureTable(io.github.msdk.datamodel.featuretables.FeatureTable) IDatabase(de.ipbhalle.metfraglib.interfaces.IDatabase) MSDKException(io.github.msdk.MSDKException) LocalCSVDatabase(de.ipbhalle.metfraglib.database.LocalCSVDatabase) MSDKException(io.github.msdk.MSDKException) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) LocalPSVDatabase(de.ipbhalle.metfraglib.database.LocalPSVDatabase) FeatureTableRow(io.github.msdk.datamodel.featuretables.FeatureTableRow) File(java.io.File) IonAnnotation(io.github.msdk.datamodel.ionannotations.IonAnnotation)

Example 5 with LocalCSVDatabase

use of de.ipbhalle.metfraglib.database.LocalCSVDatabase in project MetFragRelaunched by ipb-halle.

the class GetRankOfCandidateMultipleCSV method main.

/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    boolean argCorrect = getArgs(args);
    if (!argCorrect) {
        System.err.println("run: progname csv='csv' inchikey1='inchikey1' scorenames='score1[,score2,...]' weights='weightfile' [transform=false]");
        System.exit(1);
    }
    String csv = argsHash.get("csv");
    String inchikey1 = argsHash.get("inchikey1");
    String scorenames = argsHash.get("scorenames");
    String weightfile = argsHash.get("weights");
    transformScores = Boolean.parseBoolean(argsHash.get("transform"));
    groupedCandidates = new java.util.Hashtable<String, GroupedCandidate>();
    scorenameToMaximum = new java.util.Hashtable<String, Double>();
    MetFragGlobalSettings settings = new MetFragGlobalSettings();
    settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, csv);
    LocalCSVDatabase db = new LocalCSVDatabase(settings);
    ArrayList<String> identifiers = null;
    try {
        identifiers = db.getCandidateIdentifiers();
    } catch (MultipleHeadersFoundInInputDatabaseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    CandidateList candidates = db.getCandidateByIdentifier(identifiers);
    String[] scoringPropertyNames = scorenames.split(",");
    for (String scoreName : scoringPropertyNames) scorenameToMaximum.put(scoreName, 0.0);
    for (int i = 0; i < candidates.getNumberElements(); i++) {
        String currentInChIKey1 = (String) candidates.getElement(i).getProperty(VariableNames.INCHI_KEY_1_NAME);
        double[] scores = new double[scoringPropertyNames.length];
        // get score and check for maximum
        for (int k = 0; k < scores.length; k++) {
            scores[k] = Double.parseDouble((String) candidates.getElement(i).getProperty(scoringPropertyNames[k]));
            if (transformScores && scoresToTransform.contains(scoringPropertyNames[k])) {
                if (scores[k] != 0.0)
                    scores[k] = 1.0 / (-1.0 * Math.log(scores[k]));
            }
            if (scores[k] > scorenameToMaximum.get(scoringPropertyNames[k]))
                scorenameToMaximum.put(scoringPropertyNames[k], scores[k]);
        }
        // group candidates by inchikey
        if (groupedCandidates.containsKey(currentInChIKey1)) {
            // add to existing
            groupedCandidates.get(currentInChIKey1).add((String) candidates.getElement(i).getProperty(VariableNames.IDENTIFIER_NAME), scores, i);
        } else {
            // create new
            GroupedCandidate gc = new GetRankOfCandidateMultipleCSV().new GroupedCandidate(currentInChIKey1);
            if (currentInChIKey1.equals(inchikey1))
                correctGroup = gc;
            gc.add((String) candidates.getElement(i).getProperty(VariableNames.IDENTIFIER_NAME), scores, i);
            groupedCandidates.put(currentInChIKey1, gc);
        }
    }
    // if correct not found
    if (correctGroup == null) {
        System.out.println("inchikey1=" + inchikey1 + " not found in " + csv);
        System.exit(0);
    }
    // store maximal scores
    double[] maximumscores = new double[scoringPropertyNames.length];
    for (int i = 0; i < scoringPropertyNames.length; i++) {
        maximumscores[i] = scorenameToMaximum.get(scoringPropertyNames[i]);
    }
    double[][] weights = readWeights(weightfile);
    for (int w = 0; w < weights.length; w++) {
        int rank = 0;
        double wc = 0;
        double bc = 0;
        // get final score of correct candidate
        double correctFinalScore = correctGroup.getBestMaximumScore(weights[w], maximumscores);
        java.util.Enumeration<String> keys = groupedCandidates.keys();
        // calculate ranking values
        while (keys.hasMoreElements()) {
            GroupedCandidate currentGroup = groupedCandidates.get(keys.nextElement());
            double currentScore = currentGroup.getBestMaximumScore(weights[w], maximumscores);
            if (currentScore >= correctFinalScore)
                rank++;
            if (currentScore > correctFinalScore)
                bc++;
            if (currentScore < correctFinalScore)
                wc++;
        }
        // calculate RRP of correct
        double rrp = getRRP(bc, wc, groupedCandidates.size());
        int indexOfCorrect = correctGroup.getBestIndex();
        // define output values
        String values = scoringPropertyNames[0] + "=" + candidates.getElement(indexOfCorrect).getProperty(scoringPropertyNames[0]) + "";
        String maximalValues = "Max" + scoringPropertyNames[0] + "=" + scorenameToMaximum.get(scoringPropertyNames[0]);
        for (int i = 1; i < scoringPropertyNames.length; i++) {
            values += " " + scoringPropertyNames[i] + "=" + candidates.getElement(indexOfCorrect).getProperty(scoringPropertyNames[i]);
            maximalValues += " Max" + scoringPropertyNames[i] + "=" + scorenameToMaximum.get(scoringPropertyNames[i]);
        }
        String weightString = "weights=" + weights[w][0];
        for (int i = 1; i < weights[w].length; i++) weightString += "," + weights[w][i];
        // print output
        System.out.println(new File(csv).getName() + " " + inchikey1 + " " + rank + " " + groupedCandidates.size() + " " + correctGroup.getBestIdentifier() + " " + candidates.getElement(indexOfCorrect).getProperty("NoExplPeaks") + " " + candidates.getElement(indexOfCorrect).getProperty("NumberPeaksUsed") + " " + rrp + " " + bc + " " + wc + " " + values + " " + maximalValues + " " + weightString);
    }
}
Also used : MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) LocalCSVDatabase(de.ipbhalle.metfraglib.database.LocalCSVDatabase) IOException(java.io.IOException) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) File(java.io.File)

Aggregations

LocalCSVDatabase (de.ipbhalle.metfraglib.database.LocalCSVDatabase)7 MultipleHeadersFoundInInputDatabaseException (de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException)7 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)7 LocalPSVDatabase (de.ipbhalle.metfraglib.database.LocalPSVDatabase)5 IDatabase (de.ipbhalle.metfraglib.interfaces.IDatabase)5 MetFragGlobalSettings (de.ipbhalle.metfraglib.settings.MetFragGlobalSettings)5 File (java.io.File)5 ArrayList (java.util.ArrayList)4 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)3 Settings (de.ipbhalle.metfraglib.settings.Settings)3 IOException (java.io.IOException)3 FastBitArray (de.ipbhalle.metfraglib.FastBitArray)2 FingerprintGroup (de.ipbhalle.metfraglib.substructure.FingerprintGroup)2 MassToFingerprintGroupList (de.ipbhalle.metfraglib.substructure.MassToFingerprintGroupList)2 MassToFingerprintGroupListCollection (de.ipbhalle.metfraglib.substructure.MassToFingerprintGroupListCollection)2 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 Hashtable (java.util.Hashtable)2 IPeak (de.ipbhalle.metfraglib.interfaces.IPeak)1 IPeakListReader (de.ipbhalle.metfraglib.interfaces.IPeakListReader)1