Search in sources :

Example 1 with TopDownPrecursorCandidate

use of de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate in project MetFragRelaunched by ipb-halle.

the class OnlineChemSpiderDatabase method getCandidateByIdentifier.

/**
 */
public ICandidate getCandidateByIdentifier(String identifier) {
    MassSpecAPIStub stub = null;
    try {
        stub = this.initMassSpecAPIStub();
    } catch (Exception e) {
        this.logger.error("Error: Could not perform database query. This could be caused by a temporal database timeout. Try again later.");
        return null;
    }
    GetExtendedMolCompoundInfoArray gemcia = new GetExtendedMolCompoundInfoArray();
    com.chemspider.www.MassSpecAPIStub.ArrayOfInt aoi_msas = new com.chemspider.www.MassSpecAPIStub.ArrayOfInt();
    int[] csids = { Integer.parseInt(identifier) };
    aoi_msas.set_int(csids);
    gemcia.setCSIDs(aoi_msas);
    gemcia.setIncludeExternalReferences(true);
    gemcia.setIncludeReferenceCounts(true);
    EMolType eMolType = EMolType.e2D;
    gemcia.setEMolType(eMolType);
    GetExtendedMolCompoundInfoArrayResponse gemciar = null;
    gemcia.setToken(this.chemSpiderToken);
    try {
        gemciar = stub.getExtendedMolCompoundInfoArray(gemcia);
    } catch (RemoteException e) {
        this.logger.error("Error: Could not perform database query. This could be caused by a temporal database timeout. Try again later.");
        return null;
    }
    ExtendedMolCompoundInfo[] emci = gemciar.getGetExtendedMolCompoundInfoArrayResult().getExtendedMolCompoundInfo();
    if (emci == null || emci.length == 0)
        return null;
    String smiles = emci[0].getSMILES();
    String compoundName = emci[0].getCommonName();
    if (smiles != null)
        smiles = smiles.replace("\n", "").replace("\r", "");
    else
        smiles = "";
    if (compoundName != null)
        compoundName = compoundName.replace("\n", "").replace("\r", "");
    else
        compoundName = "";
    ICandidate precursorCandidate = new TopDownPrecursorCandidate(emci[0].getInChI(), String.valueOf(emci[0].getCSID()));
    precursorCandidate.setProperty(VariableNames.INCHI_KEY_1_NAME, emci[0].getInChIKey().split("-")[0]);
    precursorCandidate.setProperty(VariableNames.INCHI_KEY_2_NAME, emci[0].getInChIKey().split("-")[1]);
    precursorCandidate.setProperty(VariableNames.INCHI_KEY_NAME, emci[0].getInChIKey());
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_XLOGP_NAME, emci[0].getXLogP());
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_ALOGP_NAME, emci[0].getALogP());
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_NUMBER_EXTERNAL_REFERENCES_NAME, emci[0].getExternalReferences() == null ? 0d : (double) emci[0].getExternalReferences().getExtRef().length);
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_DATA_SOURCE_COUNT, (double) emci[0].getDataSourceCount());
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_NUMBER_PUBMED_REFERENCES_NAME, (double) emci[0].getPubMedCount());
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_REFERENCE_COUNT, (double) emci[0].getReferenceCount());
    precursorCandidate.setProperty(VariableNames.CHEMSPIDER_RSC_COUNT, (double) emci[0].getRSCCount());
    precursorCandidate.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, this.processFormula(emci[0].getMF()));
    precursorCandidate.setProperty(VariableNames.SMILES_NAME, smiles);
    precursorCandidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, emci[0].getMonoisotopicMass());
    precursorCandidate.setProperty(VariableNames.COMPOUND_NAME_NAME, compoundName);
    return precursorCandidate;
}
Also used : ExtendedMolCompoundInfo(com.chemspider.www.MassSpecAPIStub.ExtendedMolCompoundInfo) TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) MassSpecAPIStub(com.chemspider.www.MassSpecAPIStub) EMolType(com.chemspider.www.MassSpecAPIStub.EMolType) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) RemoteException(java.rmi.RemoteException) GetExtendedMolCompoundInfoArrayResponse(com.chemspider.www.MassSpecAPIStub.GetExtendedMolCompoundInfoArrayResponse) GetExtendedMolCompoundInfoArray(com.chemspider.www.MassSpecAPIStub.GetExtendedMolCompoundInfoArray)

Example 2 with TopDownPrecursorCandidate

use of de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate in project MetFragRelaunched by ipb-halle.

the class OnlineChemSpiderRestDatabase method generateCandidate.

protected ICandidate generateCandidate(JSONObject jsonObject) {
    try {
        String smiles = ((String) jsonObject.get("smiles")).replaceAll("\\n", "");
        Double mass = (Double) jsonObject.get("monoisotopicMass");
        Long dataSourceCount = (Long) jsonObject.get("dataSourceCount");
        String commonName = (String) jsonObject.get("commonName");
        Long referenceCount = (Long) jsonObject.get("referenceCount");
        String formula = (String) jsonObject.get("formula");
        Long pubMedCount = (Long) jsonObject.get("pubMedCount");
        Long rscCount = (Long) jsonObject.get("rscCount");
        String id = String.valueOf((long) jsonObject.get("id"));
        String[] inchi = null;
        try {
            inchi = MoleculeFunctions.getInChIInfoFromSmiles(smiles);
        } catch (Exception e) {
            return null;
        }
        ICandidate candidate = new TopDownPrecursorCandidate(inchi[0], id);
        candidate.setProperty(VariableNames.INCHI_KEY_NAME, inchi[1]);
        String[] tmp = inchi[1].split("-");
        candidate.setProperty(VariableNames.INCHI_KEY_1_NAME, tmp[0]);
        candidate.setProperty(VariableNames.INCHI_KEY_2_NAME, tmp[1]);
        candidate.setProperty(VariableNames.INCHI_KEY_3_NAME, tmp[2]);
        candidate.setProperty(VariableNames.CHEMSPIDER_DATA_SOURCE_COUNT, (double) Math.toIntExact(dataSourceCount));
        candidate.setProperty(VariableNames.CHEMSPIDER_NUMBER_PUBMED_REFERENCES_NAME, (double) Math.toIntExact(pubMedCount));
        candidate.setProperty(VariableNames.CHEMSPIDER_REFERENCE_COUNT, (double) Math.toIntExact(referenceCount));
        candidate.setProperty(VariableNames.CHEMSPIDER_RSC_COUNT, (double) Math.toIntExact(rscCount));
        candidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, mass);
        candidate.setProperty(VariableNames.COMPOUND_NAME_NAME, commonName);
        candidate.setProperty(VariableNames.SMILES_NAME, smiles);
        candidate.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, this.processFormula(formula));
        return candidate;
    } catch (Exception e) {
        return null;
    }
}
Also used : TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) ClientProtocolException(org.apache.http.client.ClientProtocolException) DatabaseIdentifierNotFoundException(de.ipbhalle.metfraglib.exceptions.DatabaseIdentifierNotFoundException) IOException(java.io.IOException) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) ParseException(org.json.simple.parser.ParseException) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate)

Example 3 with TopDownPrecursorCandidate

use of de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate in project MetFragRelaunched by ipb-halle.

the class LocalCSVDatabase method readCandidatesFromFile.

/**
 * @throws MultipleHeadersFoundInInputDatabaseException
 * @throws IOException
 */
protected void readCandidatesFromFile() throws Exception {
    this.candidates = new java.util.ArrayList<ICandidate>();
    java.io.File f = new java.io.File((String) this.settings.get(VariableNames.LOCAL_DATABASE_PATH_NAME));
    java.util.List<String> propertyNames = new java.util.ArrayList<String>();
    BufferedReader reader = null;
    if (f.isFile()) {
        reader = new BufferedReader(new FileReader(f));
        CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
        java.util.Iterator<?> it = parser.getHeaderMap().keySet().iterator();
        java.util.HashMap<String, String> nameToInputName = new java.util.HashMap<String, String>();
        nameToInputName.put(VariableNames.IDENTIFIER_NAME_3, VariableNames.IDENTIFIER_NAME);
        nameToInputName.put(VariableNames.IDENTIFIER_NAME_2, VariableNames.IDENTIFIER_NAME);
        nameToInputName.put(VariableNames.IDENTIFIER_NAME, VariableNames.IDENTIFIER_NAME);
        nameToInputName.put(VariableNames.MONOISOTOPIC_MASS_NAME_2, VariableNames.MONOISOTOPIC_MASS_NAME);
        nameToInputName.put(VariableNames.MONOISOTOPIC_MASS_NAME, VariableNames.MONOISOTOPIC_MASS_NAME);
        nameToInputName.put(VariableNames.INCHI_NAME_2, VariableNames.INCHI_NAME);
        nameToInputName.put(VariableNames.INCHI_NAME, VariableNames.INCHI_NAME);
        nameToInputName.put(VariableNames.MOLECULAR_FORMULA_NAME_2, VariableNames.MOLECULAR_FORMULA_NAME);
        nameToInputName.put(VariableNames.MOLECULAR_FORMULA_NAME, VariableNames.MOLECULAR_FORMULA_NAME);
        nameToInputName.put(VariableNames.SMILES_NAME_2, VariableNames.SMILES_NAME);
        nameToInputName.put(VariableNames.SMILES_NAME, VariableNames.SMILES_NAME);
        nameToInputName.put(VariableNames.INCHI_KEY_NAME_2, VariableNames.INCHI_KEY_NAME);
        nameToInputName.put(VariableNames.INCHI_KEY_NAME, VariableNames.INCHI_KEY_NAME);
        nameToInputName.put(VariableNames.COMPOUND_NAME_NAME_2, VariableNames.COMPOUND_NAME_NAME);
        nameToInputName.put(VariableNames.COMPOUND_NAME_NAME, VariableNames.COMPOUND_NAME_NAME);
        String[] possibleIdentifierNames = { VariableNames.IDENTIFIER_NAME_3, VariableNames.IDENTIFIER_NAME_2, VariableNames.IDENTIFIER_NAME };
        String[] possibleInChINames = { VariableNames.INCHI_NAME_2, VariableNames.INCHI_NAME };
        java.util.HashMap<String, Boolean> nameToWasFound = new java.util.HashMap<String, Boolean>();
        java.util.Iterator<String> keys = nameToInputName.keySet().iterator();
        while (keys.hasNext()) nameToWasFound.put(keys.next(), Boolean.valueOf(false));
        while (it.hasNext()) {
            String colname = (String) it.next();
            propertyNames.add(colname);
            if (nameToInputName.containsKey(colname))
                nameToWasFound.put(colname, Boolean.valueOf(true));
        }
        String properIdentifierName = "";
        String properInChIName = "";
        for (String name : possibleIdentifierNames) {
            if (nameToWasFound.get(name)) {
                properIdentifierName = name;
                break;
            }
        }
        for (String name : possibleInChINames) {
            if (nameToWasFound.get(name)) {
                properInChIName = name;
                break;
            }
        }
        if (properIdentifierName.equals("")) {
            logger.error("Error: No Identifier column defined.");
            parser.close();
            reader.close();
            throw new Exception();
        }
        if (possibleInChINames.equals("")) {
            logger.error("Error: No InChI column defined.");
            parser.close();
            reader.close();
            throw new Exception();
        }
        int index = 0;
        for (CSVRecord record : parser) {
            index++;
            String identifier = record.get(properIdentifierName);
            if (identifier == null)
                continue;
            identifier = identifier.trim();
            if (identifier.equals("-") || identifier.equals("NO_MATCH"))
                continue;
            ICandidate precursorCandidate = new TopDownPrecursorCandidate(record.get(properInChIName), identifier + "|" + index);
            keys = nameToWasFound.keySet().iterator();
            for (String curKey : this.preparedPropertyNames) {
                if (nameToWasFound.get(curKey)) {
                    String inputName = nameToInputName.get(curKey);
                    if (!precursorCandidate.hasDefinedProperty(inputName))
                        precursorCandidate.setProperty(inputName, record.get(curKey));
                }
            }
            for (int ii = 0; ii < propertyNames.size(); ii++) {
                String colname = propertyNames.get(ii);
                if (!precursorCandidate.hasDefinedProperty(colname)) {
                    precursorCandidate.setProperty(colname, record.get(colname));
                }
            }
            if (!precursorCandidate.hasDefinedProperty(VariableNames.MONOISOTOPIC_MASS_NAME)) {
                try {
                    precursorCandidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, precursorCandidate.getMolecularFormula().getMonoisotopicMass());
                } catch (AtomTypeNotKnownFromInputListException e) {
                    continue;
                }
            } else {
                precursorCandidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, Double.parseDouble((String) precursorCandidate.getProperty(VariableNames.MONOISOTOPIC_MASS_NAME)));
            }
            if (!this.addInChIFromSmiles(precursorCandidate))
                continue;
            if (!this.addSMILESFromInChI(precursorCandidate))
                continue;
            if (!this.addInChIKeyFromSmiles(precursorCandidate))
                continue;
            if (!this.setInChIValues(precursorCandidate))
                continue;
            if (this.checkFilter(precursorCandidate)) {
                this.identifiers.add(precursorCandidate.getIdentifier());
                this.candidates.add(precursorCandidate);
            }
        }
        parser.close();
        reader.close();
        return;
    }
    throw new Exception();
}
Also used : ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) FileReader(java.io.FileReader) TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) AtomTypeNotKnownFromInputListException(de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException) AtomTypeNotKnownFromInputListException(de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException) IOException(java.io.IOException) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) CSVParser(org.apache.commons.csv.CSVParser) BufferedReader(java.io.BufferedReader) CSVRecord(org.apache.commons.csv.CSVRecord)

Example 4 with TopDownPrecursorCandidate

use of de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate in project MetFragRelaunched by ipb-halle.

the class LocalChEBIDatabase method getCandidateByIdentifier.

public ICandidate getCandidateByIdentifier(String identifier) throws DatabaseIdentifierNotFoundException {
    String fields = this.CID_COLUMN_NAME + "," + this.INCHI_COLUMN_NAME + "," + this.INCHIKEY1_COLUMN_NAME + "," + this.INCHIKEY2_COLUMN_NAME + "," + this.SMILES_COLUMN_NAME + "," + this.MASS_COLUMN_NAME;
    if (this.COMPOUND_NAME_COLUMN_NAME != null && this.COMPOUND_NAME_COLUMN_NAME.length() != 0)
        fields += "," + this.COMPOUND_NAME_COLUMN_NAME;
    String query = "SELECT " + fields + " from " + this.TABLE_NAME + " where " + this.CID_COLUMN_NAME + " =\'" + identifier + "\';";
    ;
    ResultSet rs = this.submitQuery(query);
    if (rs == null)
        return null;
    ArrayList<String> inchis = new ArrayList<String>();
    ArrayList<String> inChIKeys1 = new ArrayList<String>();
    ArrayList<String> inChIKeys2 = new ArrayList<String>();
    ArrayList<String> formulas = new ArrayList<String>();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> smiles = new ArrayList<String>();
    ArrayList<Double> masses = new ArrayList<Double>();
    try {
        while (rs.next()) {
            inchis.add(rs.getString(this.INCHI_COLUMN_NAME));
            inChIKeys1.add(rs.getString(this.INCHIKEY1_COLUMN_NAME));
            inChIKeys2.add(rs.getString(this.INCHIKEY2_COLUMN_NAME));
            smiles.add(rs.getString(this.SMILES_COLUMN_NAME));
            masses.add(rs.getDouble(this.MASS_COLUMN_NAME));
            formulas.add(rs.getString(this.INCHI_COLUMN_NAME).split("/")[1]);
            if (rs.getString(this.COMPOUND_NAME_COLUMN_NAME) != null)
                names.add(rs.getString(this.COMPOUND_NAME_COLUMN_NAME));
            else
                names.add("NA");
        }
        rs.close();
        this.statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    ICandidate candidate = new TopDownPrecursorCandidate(inchis.get(0), identifier);
    candidate.setProperty(VariableNames.INCHI_KEY_1_NAME, inChIKeys1.get(0));
    candidate.setProperty(VariableNames.INCHI_KEY_2_NAME, inChIKeys2.get(0));
    candidate.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, formulas.get(0));
    candidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, masses.get(0));
    candidate.setProperty(VariableNames.SMILES_NAME, smiles.get(0));
    candidate.setProperty(VariableNames.COMPOUND_NAME_NAME, names.get(0));
    return candidate;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate)

Example 5 with TopDownPrecursorCandidate

use of de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate in project MetFragRelaunched by ipb-halle.

the class LocalChEBIDatabase method getCandidateByIdentifier.

/**
 */
public CandidateList getCandidateByIdentifier(ArrayList<String> identifiers) {
    if (identifiers.size() == 0)
        return new CandidateList();
    /*	String query = "SELECT " + this.CID_COLUMN_NAME + ", " 
				+ this.INCHI_COLUMN_NAME + "," + this.INCHIKEY1_COLUMN_NAME + "," 
				+ this.INCHIKEY2_COLUMN_NAME + " from " + this.TABLE_NAME + " where " 
				+ this.CID_COLUMN_NAME + " =\'" + identifiers.get(0) + "\'"; */
    String fields = this.CID_COLUMN_NAME + "," + this.INCHI_COLUMN_NAME + "," + this.INCHIKEY1_COLUMN_NAME + "," + this.INCHIKEY2_COLUMN_NAME + "," + this.SMILES_COLUMN_NAME + "," + this.MASS_COLUMN_NAME;
    if (this.COMPOUND_NAME_COLUMN_NAME != null && this.COMPOUND_NAME_COLUMN_NAME.length() != 0)
        fields += "," + this.COMPOUND_NAME_COLUMN_NAME;
    String query = "SELECT " + fields + " from " + this.TABLE_NAME + " where " + this.CID_COLUMN_NAME + " in (\'" + identifiers.get(0) + "\'";
    for (String cid : identifiers) query += ",\'" + cid + "\'";
    query += ");";
    ResultSet rs = this.submitQuery(query);
    if (rs == null)
        return new CandidateList();
    CandidateList candidates = new CandidateList();
    try {
        while (rs.next()) {
            ICandidate candidate = new TopDownPrecursorCandidate(rs.getString(this.INCHI_COLUMN_NAME), rs.getString(this.CID_COLUMN_NAME));
            candidate.setProperty(VariableNames.INCHI_KEY_1_NAME, rs.getString(this.INCHIKEY1_COLUMN_NAME));
            candidate.setProperty(VariableNames.INCHI_KEY_2_NAME, rs.getString(this.INCHIKEY2_COLUMN_NAME));
            candidate.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, rs.getString(this.INCHI_COLUMN_NAME).split("/")[1]);
            candidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, rs.getDouble(this.MASS_COLUMN_NAME));
            candidate.setProperty(VariableNames.SMILES_NAME, rs.getString(this.SMILES_COLUMN_NAME));
            if (rs.getString(this.COMPOUND_NAME_COLUMN_NAME) != null)
                candidate.setProperty(VariableNames.COMPOUND_NAME_NAME, rs.getString(this.COMPOUND_NAME_COLUMN_NAME));
            else
                candidate.setProperty(VariableNames.COMPOUND_NAME_NAME, "NA");
            candidates.addElement(candidate);
        }
        rs.close();
        this.statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return candidates;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate)

Aggregations

TopDownPrecursorCandidate (de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate)34 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)33 SQLException (java.sql.SQLException)13 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)12 ResultSet (java.sql.ResultSet)12 IOException (java.io.IOException)9 AtomTypeNotKnownFromInputListException (de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException)8 ArrayList (java.util.ArrayList)8 MultipleHeadersFoundInInputDatabaseException (de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException)6 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)6 CDKException (org.openscience.cdk.exception.CDKException)5 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 InputStream (java.io.InputStream)4 MalformedURLException (java.net.MalformedURLException)3 JSONArray (org.json.simple.JSONArray)3 JSONObject (org.json.simple.JSONObject)3 JSONParser (org.json.simple.parser.JSONParser)3 ExtendedMolCompoundInfo (com.chemspider.www.MassSpecAPIStub.ExtendedMolCompoundInfo)2 DatabaseIdentifierNotFoundException (de.ipbhalle.metfraglib.exceptions.DatabaseIdentifierNotFoundException)2