use of de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException in project MetFragRelaunched by ipb-halle.
the class DownloadEntriesFromPubChem method downloadFromString.
public static void downloadFromString(String idString) {
String[] ids = idString.trim().split(",");
MetFragGlobalSettings settings = new MetFragGlobalSettings();
settings.set(VariableNames.PRECURSOR_DATABASE_IDS_NAME, ids);
OnlineExtendedPubChemDatabase db = new OnlineExtendedPubChemDatabase(settings);
CandidateList candidates = null;
try {
candidates = db.getCandidateByIdentifier(db.getCandidateIdentifiers());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < candidates.getNumberElements(); i++) {
ICandidate candidate = candidates.getElement(i);
try {
System.out.println(candidate.getIdentifier() + "|" + candidate.getInChI() + "|" + candidate.getMolecularFormula().toString() + "|" + candidate.getMolecularFormula().getMonoisotopicMass() + "|" + candidate.getProperty(VariableNames.PUBCHEM_XLOGP_NAME) + "|" + candidate.getProperty(VariableNames.INCHI_KEY_1_NAME) + "|" + candidate.getProperty(VariableNames.INCHI_KEY_2_NAME) + "|" + candidate.getProperty(VariableNames.PUBCHEM_NUMBER_PATENTS_NAME) + "|" + candidate.getProperty(VariableNames.PUBCHEM_NUMBER_PUBMED_REFERENCES_NAME) + "|" + candidate.getProperty(VariableNames.SMILES_NAME));
} catch (AtomTypeNotKnownFromInputListException e) {
e.printStackTrace();
}
}
}
use of de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterLossFragmentSmilesCompletePSV method calculateMassOfFormula.
private double calculateMassOfFormula(String formula) {
String part1 = formula.replaceAll("\\[([A-Za-z0-9]*).*\\].*", "$1");
String charge = formula.substring(formula.length() - 1);
java.util.ArrayList<String> elementsToAdd = new java.util.ArrayList<String>();
java.util.ArrayList<String> timesToAdd = new java.util.ArrayList<String>();
java.util.ArrayList<String> signsForAdd = new java.util.ArrayList<String>();
for (int i = 0; i < formula.length() - 1; i++) {
if (formula.charAt(i) == '+' || formula.charAt(i) == '-') {
signsForAdd.add(formula.charAt(i) + "");
boolean numberFinished = false;
String number = "";
String element = "";
for (int k = (i + 1); k < formula.length() - 1; k++) {
if (!numberFinished && Character.isDigit(formula.charAt(k)))
number += formula.charAt(k);
else if (!numberFinished && !Character.isDigit(formula.charAt(k))) {
if (number.equals(""))
number = "1";
numberFinished = true;
}
if (Character.isLowerCase(formula.charAt(k)) && !element.equals(""))
element += formula.charAt(k);
if (Character.isUpperCase(formula.charAt(k)) && element.equals(""))
element += formula.charAt(k);
if (Character.isUpperCase(formula.charAt(k)) && !element.equals(""))
break;
if (Character.isDigit(formula.charAt(k)) && numberFinished)
break;
if (!Character.isUpperCase(formula.charAt(k)) && !Character.isLowerCase(formula.charAt(k)) && !Character.isDigit(formula.charAt(k)))
break;
}
elementsToAdd.add(element);
timesToAdd.add(number);
}
}
double mass = 0.0;
try {
boolean isPositive = charge.equals("-") ? false : true;
double chargeMass = Constants.getChargeMassByType(isPositive);
ByteMolecularFormula bmf = new ByteMolecularFormula(part1);
for (int i = 0; i < elementsToAdd.size(); i++) {
byte atomIndex = (byte) Constants.ELEMENTS.indexOf(elementsToAdd.get(i));
short amount = Short.parseShort(signsForAdd.get(i) + timesToAdd.get(i));
bmf.changeNumberElementsFromByte(atomIndex, amount);
}
mass = bmf.getMonoisotopicMass() + chargeMass;
} catch (AtomTypeNotKnownFromInputListException e) {
e.printStackTrace();
}
return mass;
}
use of de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException in project MetFragRelaunched by ipb-halle.
the class MetfRag method scoreMoleculesAgainstSpectrum.
/**
* fragment and score molecules stored in a sdf file
*
* @param _pathToSDF
* @param _masses
* @param _intensities
* @param _exactMass
* @param _numberThreads
* @param _mzabs
* @param _mzppm
* @param _searchppm
* @param _posCharge
* @param _mode
* @param _treeDepth
* @return
*/
public static IAtomContainer[] scoreMoleculesAgainstSpectrum(IAtomContainer[] atomContainerArray, double[] _masses, double[] _intensities, double _exactMass, int _numberThreads, double _mzabs, double _mzppm, boolean _posCharge, int _mode, int _treeDepth, String[] scoreNames, double[] scoreWeights) {
Logger.getLogger("net.sf.jnati.deploy.artefact.ConfigManager").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.repository.ClasspathRepository").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.repository.LocalRepository").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.artefact.ManifestReader").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.NativeArtefactLocator").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.NativeLibraryLoader").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.resolver.ArtefactResolver").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.source.JarSource").setLevel(Level.ERROR);
double mzabs = _mzabs;
double mzppm = _mzppm;
double exactMass = _exactMass;
int treeDepth = _treeDepth;
int mode = _mode;
int numberThreads = _numberThreads;
boolean posCharge = _posCharge;
double[] masses = _masses;
double[] intensities = _intensities;
IAtomContainer[] resultMols = new IAtomContainer[0];
if (masses == null)
return resultMols;
if (intensities == null)
return resultMols;
if (masses.length != intensities.length)
return resultMols;
if (exactMass <= 0.0)
return resultMols;
if (numberThreads < -1 || numberThreads > 8)
return resultMols;
if (mzabs < 0.0 || mzppm < 0.0)
return resultMols;
if (mode != -1 && mode != 0 && mode != 1)
return resultMols;
if (treeDepth < 1 || treeDepth > 5)
return resultMols;
String peaksString = "";
if (masses.length > 0)
peaksString += masses[0] + " " + intensities[0];
for (int i = 1; i < masses.length; i++) {
peaksString += "\n" + masses[i] + " " + intensities[i];
}
Double[] scoreWeightsObject = new Double[scoreWeights.length];
for (int i = 0; i < scoreWeightsObject.length; i++) scoreWeightsObject[i] = Double.valueOf(scoreWeights[i]);
MetFragGlobalSettings settings = new MetFragGlobalSettings();
settings.set(VariableNames.MOLECULES_IN_MEMORY, atomContainerArray);
settings.set(VariableNames.PEAK_LIST_STRING_NAME, peaksString);
settings.set(VariableNames.METFRAG_DATABASE_TYPE_NAME, "LocalInMemoryDatabase");
settings.set(VariableNames.METFRAG_PEAK_LIST_READER_NAME, "de.ipbhalle.metfraglib.peaklistreader.FilteredStringTandemMassPeakListReader");
settings.set(VariableNames.METFRAG_SCORE_TYPES_NAME, scoreNames);
settings.set(VariableNames.METFRAG_SCORE_WEIGHTS_NAME, scoreWeightsObject);
settings.set(VariableNames.RELATIVE_MASS_DEVIATION_NAME, mzppm);
settings.set(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME, mzabs);
settings.set(VariableNames.IS_POSITIVE_ION_MODE_NAME, posCharge);
settings.set(VariableNames.PRECURSOR_ION_MODE_NAME, mode);
settings.set(VariableNames.PRECURSOR_NEUTRAL_MASS_NAME, exactMass);
settings.set(VariableNames.MAXIMUM_TREE_DEPTH_NAME, (byte) treeDepth);
CombinedMetFragProcess mp = new CombinedMetFragProcess(settings);
try {
mp.retrieveCompounds();
} catch (Exception e2) {
System.err.println("Error retrieving candidates");
e2.printStackTrace();
return new IAtomContainer[0];
}
try {
mp.run();
} catch (Exception e) {
System.err.println("Error running MetFrag process");
e.printStackTrace();
return new IAtomContainer[0];
}
SortedScoredCandidateList scoredCandidateList = (SortedScoredCandidateList) mp.getCandidateList();
resultMols = new IAtomContainer[scoredCandidateList.getNumberElements()];
int numberOfPeaksUsed = scoredCandidateList.getNumberPeaksUsed();
for (int i = 0; i < scoredCandidateList.getNumberElements(); i++) {
ICandidate candidate = scoredCandidateList.getElement(i);
IAtomContainer tmp = null;
try {
tmp = candidate.getAtomContainer();
MoleculeFunctions.prepareAtomContainer(tmp, false);
} catch (Exception e1) {
e1.printStackTrace();
continue;
}
if (candidate.getProperties().containsKey(VariableNames.INCHI_KEY_1_NAME) && candidate.getProperty(VariableNames.INCHI_KEY_1_NAME) != null)
tmp.setProperty(VariableNames.INCHI_KEY_1_NAME, candidate.getProperty(VariableNames.INCHI_KEY_1_NAME));
if (candidate.getProperties().containsKey(VariableNames.INCHI_KEY_2_NAME) && candidate.getProperty(VariableNames.INCHI_KEY_2_NAME) != null)
tmp.setProperty(VariableNames.INCHI_KEY_2_NAME, candidate.getProperty(VariableNames.INCHI_KEY_2_NAME));
if (candidate.getProperties().containsKey(VariableNames.INCHI_KEY_NAME) && candidate.getProperty(VariableNames.INCHI_KEY_NAME) != null)
tmp.setProperty(VariableNames.INCHI_KEY_NAME, candidate.getProperty(VariableNames.INCHI_KEY_NAME));
tmp.setProperty(VariableNames.IDENTIFIER_NAME, candidate.getIdentifier());
IMolecularFormula molFormula = MolecularFormulaManipulator.getMolecularFormula(tmp);
Double massDoubleOrig = null;
try {
massDoubleOrig = candidate.getMolecularFormula().getMonoisotopicMass();
} catch (AtomTypeNotKnownFromInputListException e) {
e.printStackTrace();
}
massDoubleOrig = (double) Math.round((massDoubleOrig) * 10000) / 10000;
tmp.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, massDoubleOrig);
tmp.setProperty(VariableNames.FINAL_SCORE_COLUMN_NAME, candidate.getProperty(VariableNames.FINAL_SCORE_COLUMN_NAME));
for (int ii = 0; ii < scoreNames.length; ii++) {
String scoreClassName = scoreNames[ii];
tmp.setProperty(scoreClassName, candidate.getProperty(scoreClassName));
tmp.setProperty(scoreClassName + "_Values", candidate.getProperty(scoreClassName + "_Values"));
}
if (candidate.getMatchList() != null)
tmp.setProperty(VariableNames.NUMBER_EXPLAINED_PEAKS_COLUMN, candidate.getMatchList().getNumberElements());
String peaksExplained = "";
String sumFormulasOfFragmentsExplainedPeaks = "";
if (candidate.getMatchList() != null) {
for (int ii = 0; ii < candidate.getMatchList().getNumberElements(); ii++) {
try {
peaksExplained += candidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + "_" + candidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity() + ";";
} catch (RelativeIntensityNotDefinedException e1) {
e1.printStackTrace();
}
sumFormulasOfFragmentsExplainedPeaks += candidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + ":" + candidate.getMatchList().getElement(ii).getBestMatchedFragment().getMolecularFormula(candidate.getPrecursorMolecule()) + ";";
}
if (sumFormulasOfFragmentsExplainedPeaks.length() != 0)
sumFormulasOfFragmentsExplainedPeaks = sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1);
if (peaksExplained.length() != 0)
peaksExplained = peaksExplained.substring(0, peaksExplained.length() - 1);
if (peaksExplained.length() == 0)
peaksExplained = "NA";
if (sumFormulasOfFragmentsExplainedPeaks.length() == 0)
sumFormulasOfFragmentsExplainedPeaks = "NA";
tmp.setProperty(VariableNames.EXPLAINED_PEAKS_COLUMN, peaksExplained);
tmp.setProperty(VariableNames.FORMULAS_OF_PEAKS_EXPLAINED_COLUMN, sumFormulasOfFragmentsExplainedPeaks);
}
tmp.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, MolecularFormulaManipulator.getString(molFormula));
tmp.setProperty(VariableNames.NUMBER_EXPLAINED_PEAKS_COLUMN, numberOfPeaksUsed);
resultMols[i] = tmp;
}
return resultMols;
}
use of de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException in project MetFragRelaunched by ipb-halle.
the class ByteMolecularFormula method parseMolecularFormula.
protected void parseMolecularFormula(String formula) throws AtomTypeNotKnownFromInputListException {
String splitableFormula = formula.trim().replaceAll("(\\[*[0-9]*[A-Z][a-z]{0,3}\\]*)([0-9]*)", "$1;$2\\|");
String[] parts = splitableFormula.split("\\|");
int numberElements = 0;
for (int i = 0; i < parts.length; i++) if (!parts[i].split(";")[0].equals("H") && !parts[i].split(";")[0].equals("D"))
numberElements++;
this.atomsAsIndeces = new byte[numberElements];
this.numberOfAtoms = new short[numberElements];
this.numberHydrogens = 0;
int index = 0;
for (int i = 0; i < parts.length; i++) {
String[] tmp = parts[i].split(";");
short count = 1;
if (tmp.length > 1 && tmp[1].length() != 0)
count = Short.parseShort(tmp[1]);
if (tmp[0].equals("H"))
this.numberHydrogens = count;
else if (tmp[0].equals("D"))
this.numberHydrogens = count;
else {
byte byteToAtomSymbol = (byte) Constants.ELEMENTS.indexOf(tmp[0]);
if (byteToAtomSymbol == 0 || byteToAtomSymbol == 1)
this.containsC = true;
if (byteToAtomSymbol == -1) {
throw new de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException(tmp[0] + " not found (" + formula + ")");
}
this.atomsAsIndeces[index] = byteToAtomSymbol;
this.numberOfAtoms[index] = count;
index++;
}
}
}
use of de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException in project MetFragRelaunched by ipb-halle.
the class SiriusNodePeakList method parseDotFile.
/**
* generate SiriusNodePeakList from dot file
*
* @param filename
* @return
*/
public void parseDotFile(String filename) {
java.util.Hashtable<String, SiriusNodePeak> nodes = new java.util.Hashtable<String, SiriusNodePeak>();
double maxIntensity = 0.0;
try {
java.io.BufferedReader breader = new java.io.BufferedReader(new java.io.FileReader(new java.io.File(filename)));
String line = breader.readLine();
while ((line = breader.readLine()) != null) {
line = line.trim();
if (line.length() == 1)
continue;
if (line.contains("->")) {
String[] tmp = line.split("\\s+");
SiriusNodePeak curChild = nodes.get(tmp[2].trim());
SiriusNodePeak curFather = nodes.get(tmp[0].trim());
try {
curChild.setLossFormula(new ByteMolecularFormula(tmp[3].replaceFirst("\\[label=\"", "").replace("\"];", "").trim()));
} catch (AtomTypeNotKnownFromInputListException e) {
e.printStackTrace();
}
curChild.setFather(curFather);
curFather.addChild(curChild);
} else {
String properties = line.replaceFirst(".*label=\"", "").replace("\"];", "");
String id = line.split("\\s+")[0].trim();
String[] tmp = properties.split("\\\\n");
String fragmentFormula = tmp[0];
double mass = Double.parseDouble(tmp[1].split("\\s+")[0].trim());
double intensity = 0.0;
try {
intensity = Double.parseDouble(properties.split("Intensity=")[1].split("\\\\n")[0]);
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
// precursor has no intensity
intensity = 0.0;
}
if (intensity > maxIntensity)
maxIntensity = intensity;
SiriusNodePeak curNode = null;
try {
curNode = new SiriusNodePeak(mass, fragmentFormula, intensity);
} catch (AtomTypeNotKnownFromInputListException e) {
e.printStackTrace();
}
curNode.setCompleteNodeLabel(properties);
curNode.setIntensity(intensity);
curNode.setSiriusID(id);
nodes.put(id, curNode);
/*
* add peak sorted
*/
this.addElement(curNode);
}
}
breader.close();
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
java.util.Enumeration<SiriusNodePeak> elems = nodes.elements();
while (elems.hasMoreElements()) {
SiriusNodePeak node = elems.nextElement();
try {
node.setIntensity((node.getIntensity() / maxIntensity) * 999.0);
} catch (RelativeIntensityNotDefinedException e) {
e.printStackTrace();
}
if (node.isRoot()) {
this.root = node;
}
if (node.isLeaf())
this.leaves.add(node);
}
}
Aggregations