use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class HDCandidateListWriterPSV method writeFile.
public boolean writeFile(File file, IList list, Settings settings) throws Exception {
CandidateList candidateList = null;
int numberOfPeaksUsed = 0;
if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
candidateList = (ScoredCandidateList) list;
numberOfPeaksUsed = ((ScoredCandidateList) list).getNumberPeaksUsed();
}
if (list instanceof CandidateList) {
candidateList = (CandidateList) list;
}
if (candidateList == null)
return false;
for (int i = 0; i < candidateList.getNumberElements(); i++) {
int countExplainedPeaks = 0;
ICandidate scoredCandidate = candidateList.getElement(i);
if (settings != null)
scoredCandidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
scoredCandidate.initialisePrecursorCandidate();
if (scoredCandidate.getMatchList() != null) {
MatchList matchList = scoredCandidate.getMatchList();
for (int l = 0; l < matchList.getNumberElements(); l++) {
try {
matchList.getElement(l).getMatchedPeak().getIntensity();
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
countExplainedPeaks++;
}
}
String peaksExplained = "";
String sumFormulasOfFragmentsExplainedPeaks = "";
if (scoredCandidate.getMatchList() != null) {
for (int ii = 0; ii < scoredCandidate.getMatchList().getNumberElements(); ii++) {
try {
double intensity = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity();
peaksExplained += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + "_" + intensity + ";";
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
String formula = scoredCandidate.getMatchList().getElement(ii).getModifiedFormulaStringOfBestMatchedFragment(scoredCandidate.getPrecursorMolecule());
sumFormulasOfFragmentsExplainedPeaks += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + ":" + formula + ";";
}
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";
scoredCandidate.setProperty(VariableNames.EXPLAINED_PEAKS_COLUMN, peaksExplained);
scoredCandidate.setProperty(VariableNames.FORMULAS_OF_PEAKS_EXPLAINED_COLUMN, sumFormulasOfFragmentsExplainedPeaks);
scoredCandidate.setProperty(VariableNames.NUMBER_PEAKS_USED_COLUMN, numberOfPeaksUsed);
scoredCandidate.setProperty(VariableNames.NUMBER_EXPLAINED_PEAKS_COLUMN, countExplainedPeaks);
}
}
java.util.Hashtable<String, java.util.ArrayList<ICandidate>> hdGroupedCandidates = new java.util.Hashtable<String, java.util.ArrayList<ICandidate>>();
String[] lines = new String[candidateList.getNumberElements()];
String heading = "";
for (int i = 0; i < candidateList.getNumberElements(); i++) {
ICandidate candidate = candidateList.getElement(i);
if (hdGroupedCandidates.containsKey((String) candidate.getProperty(VariableNames.HD_GROUP_FLAG_NAME)))
((java.util.ArrayList<ICandidate>) hdGroupedCandidates.get((String) candidate.getProperty(VariableNames.HD_GROUP_FLAG_NAME))).add(candidate);
else {
java.util.ArrayList<ICandidate> vec = new java.util.ArrayList<ICandidate>();
vec.add(candidate);
hdGroupedCandidates.put((String) candidate.getProperty(VariableNames.HD_GROUP_FLAG_NAME), vec);
}
candidate.getProperties().remove(VariableNames.HD_GROUP_FLAG_NAME);
}
java.util.ArrayList<String> propertyNames = new java.util.ArrayList<String>();
java.util.Iterator<?> it = (java.util.Iterator<?>) hdGroupedCandidates.keys();
while (it.hasNext()) {
String currentGroup = (String) it.next();
java.util.ArrayList<ICandidate> vec = hdGroupedCandidates.get(currentGroup);
int originalCandidate = -1;
int maxPropertySize = 0;
// find original candidate properties
for (int i = 0; i < vec.size(); i++) {
if (vec.get(i).getProperties().size() > maxPropertySize) {
originalCandidate = i;
maxPropertySize = vec.get(i).getProperties().size();
}
}
// fill in missing properties
java.util.Hashtable<String, Object> properties = vec.get(originalCandidate).getProperties();
for (int i = 0; i < vec.size(); i++) {
ICandidate currentCandidate = vec.get(i);
java.util.Iterator<?> prop_it = (java.util.Iterator<?>) properties.keys();
while (prop_it.hasNext()) {
String key = (String) prop_it.next();
if (!propertyNames.contains(key))
propertyNames.add(key);
if (currentCandidate.getProperty(key) == null)
currentCandidate.setProperty(key, properties.get(key));
}
}
}
for (int i = 0; i < candidateList.getNumberElements(); i++) {
ICandidate scoredCandidate = candidateList.getElement(i);
if (propertyNames.size() >= 1) {
String key = propertyNames.get(0);
if (i == 0)
heading += key;
lines[i] = "" + checkEmptyProperty(scoredCandidate.getProperty(key), key);
}
for (int k = 1; k < propertyNames.size(); k++) {
String key = propertyNames.get(k);
if (i == 0)
heading += "|" + key;
String prop = checkEmptyProperty(scoredCandidate.getProperty(key), key).toString();
lines[i] += "|" + prop;
}
}
java.io.BufferedWriter bwriter = new java.io.BufferedWriter(new FileWriter(file));
bwriter.write(heading);
bwriter.newLine();
for (int i = 0; i < lines.length; i++) {
bwriter.write(lines[i]);
bwriter.newLine();
}
bwriter.close();
return true;
}
use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterExtendedFragmentsXLS method convertMoleculesAndFragmentToImages.
/**
* @param candidateList
* @return
*/
private List<List<RenderedImage>> convertMoleculesAndFragmentToImages(CandidateList candidateList) throws Exception {
List<List<RenderedImage>> molImages = new ArrayList<List<RenderedImage>>();
de.ipbhalle.metfraglib.imagegenerator.StandardSingleStructureImageGenerator imageGenerator = new de.ipbhalle.metfraglib.imagegenerator.StandardSingleStructureImageGenerator();
imageGenerator.setImageHeight(200);
imageGenerator.setImageWidth(200);
for (int i = 0; i < candidateList.getNumberElements(); i++) {
List<RenderedImage> currentList = new ArrayList<RenderedImage>();
RenderedImage renderedImage = imageGenerator.generateImage(candidateList.getElement(i));
if (renderedImage != null) {
currentList.add(renderedImage);
} else
currentList.add(new java.awt.image.BufferedImage(imageGenerator.getImageWidth(), imageGenerator.getImageHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB));
if (renderedImage != null) {
MatchList matchList = candidateList.getElement(i).getMatchList();
for (int k = 0; k < matchList.getNumberElements(); k++) {
currentList.add(imageGenerator.generateImage(candidateList.getElement(i).getPrecursorMolecule(), matchList.getElement(k).getBestMatchedFragment()));
}
}
molImages.add(currentList);
}
return molImages;
}
use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterLossFragmentSmilesPSV method writeFile.
public boolean writeFile(File file, IList list, Settings settings) throws Exception {
CandidateList candidateList = null;
int numberOfPeaksUsed = 0;
if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
candidateList = (ScoredCandidateList) list;
numberOfPeaksUsed = ((ScoredCandidateList) list).getNumberPeaksUsed();
}
if (list instanceof CandidateList) {
candidateList = (CandidateList) list;
}
if (candidateList == null)
return false;
StringBuilder heading = new StringBuilder();
this.removeCandidateProperties(candidateList.getElement(0));
java.util.Enumeration<String> keys = candidateList.getElement(0).getProperties().keys();
if (keys.hasMoreElements()) {
String key = keys.nextElement();
heading.append(key);
}
while (keys.hasMoreElements()) {
String key = keys.nextElement();
heading.append("|");
heading.append(key);
}
heading.append("|");
heading.append("ExplPeaks");
heading.append("|");
heading.append("FormulasOfExplPeaks");
heading.append("|");
heading.append("FragmentFingerprintOfExplPeaks");
heading.append("|");
heading.append("NumberPeaksUsed");
heading.append("|");
heading.append("NoExplPeaks");
heading.append("|");
heading.append("LossFingerprintOfExplPeaks");
java.io.BufferedWriter bwriter = new java.io.BufferedWriter(new FileWriter(file));
bwriter.write(heading.toString());
bwriter.newLine();
Fingerprint fingerprint = new Fingerprint((String) settings.get(VariableNames.FINGERPRINT_TYPE_NAME));
for (int i = 0; i < candidateList.getNumberElements(); i++) {
StringBuilder line = new StringBuilder();
int countExplainedPeaks = 0;
ICandidate scoredCandidate = candidateList.getElement(i);
this.removeCandidateProperties(scoredCandidate);
if (settings != null)
scoredCandidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
scoredCandidate.initialisePrecursorCandidate();
if (scoredCandidate.getMatchList() != null) {
MatchList matchList = scoredCandidate.getMatchList();
for (int l = 0; l < matchList.getNumberElements(); l++) {
try {
matchList.getElement(l).getMatchedPeak().getIntensity();
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
countExplainedPeaks++;
}
}
keys = scoredCandidate.getProperties().keys();
if (keys.hasMoreElements()) {
String key = keys.nextElement();
line.append(scoredCandidate.getProperty(key));
}
while (keys.hasMoreElements()) {
String key = keys.nextElement();
line.append("|");
line.append(scoredCandidate.getProperty(key));
}
StringBuilder peaksExplained = new StringBuilder();
StringBuilder sumFormulasOfFragmentsExplainedPeaks = new StringBuilder();
StringBuilder fingerprintOfFragmentsExplainedPeaks = new StringBuilder();
if (scoredCandidate.getMatchList() != null) {
String[] matchedFormulas = new String[scoredCandidate.getMatchList().getNumberElements()];
double[] correctedMasses = new double[scoredCandidate.getMatchList().getNumberElements()];
for (int ii = 0; ii < scoredCandidate.getMatchList().getNumberElements(); ii++) {
try {
double intensity = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity();
peaksExplained.append(scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass());
peaksExplained.append("_");
peaksExplained.append(intensity);
peaksExplained.append(";");
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
String formula = scoredCandidate.getMatchList().getElement(ii).getModifiedFormulaStringOfBestMatchedFragment(scoredCandidate.getPrecursorMolecule());
double mass = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass();
if ((Boolean) settings.get(VariableNames.CORRECT_MASSES_FOR_FINGERPRINT_ANNOTATION_NAME)) {
matchedFormulas[ii] = formula;
correctedMasses[ii] = MathTools.round(calculateMassOfFormula(formula));
mass = correctedMasses[ii];
}
sumFormulasOfFragmentsExplainedPeaks.append(scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass());
sumFormulasOfFragmentsExplainedPeaks.append(":");
sumFormulasOfFragmentsExplainedPeaks.append(formula);
sumFormulasOfFragmentsExplainedPeaks.append(";");
// get fragment of explained peak
IFragment frag = scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment();
String fp = null;
try {
IAtomContainer con = fingerprint.getNormalizedAtomContainer(scoredCandidate.getPrecursorMolecule(), frag);
fp = fingerprint.getNormalizedFingerprint(con);
} catch (Exception e) {
continue;
}
fingerprintOfFragmentsExplainedPeaks.append(mass);
fingerprintOfFragmentsExplainedPeaks.append(":");
fingerprintOfFragmentsExplainedPeaks.append(fp);
fingerprintOfFragmentsExplainedPeaks.append(";");
}
line.append("|");
line.append(peaksExplained.length() == 0 ? "NA" : peaksExplained.substring(0, peaksExplained.length() - 1));
line.append("|");
line.append(sumFormulasOfFragmentsExplainedPeaks.length() == 0 ? "NA" : sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1));
line.append("|");
line.append(fingerprintOfFragmentsExplainedPeaks.length() == 0 ? "NA" : fingerprintOfFragmentsExplainedPeaks.substring(0, fingerprintOfFragmentsExplainedPeaks.length() - 1));
line.append("|");
line.append(numberOfPeaksUsed);
line.append("|");
line.append(countExplainedPeaks);
// add loss information
if (settings != null) {
String lossesFingerprints = createLossAnnotations(scoredCandidate.getPrecursorMolecule(), scoredCandidate.getMatchList(), settings, correctedMasses, fingerprint);
line.append("|");
line.append(lossesFingerprints);
}
}
bwriter.write(line.toString());
bwriter.newLine();
}
bwriter.close();
return true;
}
use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class ConvertMultipleMonaMSPtoMetFragRecord method checkEntries.
public static java.util.ArrayList<Entry> checkEntries(java.util.ArrayList<Entry> entries, String addparamsfile) {
if (entries.size() == 0)
return entries;
MetFragGlobalSettings settings;
try {
boolean[] inchikey_id_missmatches = new boolean[entries.size()];
String[] alternative_pubchem_id = new String[entries.size()];
settings = MetFragGlobalSettings.readSettings(new File(addparamsfile), null);
LocalMetChemDatabase lmcd = new LocalMetChemDatabase(settings);
// check inchikeys
String inchikeyset = "'" + entries.get(0).inchikey.split("-")[0] + "'";
for (int i = 1; i < entries.size(); i++) {
inchikeyset += ",'" + entries.get(i).inchikey.split("-")[0] + "'";
}
CandidateList candidates = lmcd.getCandidateListByQuery("2", "c.inchi_key_1 in (" + inchikeyset + ")");
for (int i = 0; i < entries.size(); i++) {
inchikey_id_missmatches[i] = true;
boolean inchikeyfound = false;
String inchikey1 = entries.get(i).inchikey.split("-")[0];
LinkedList<String> alternative_pubchem_ids = new LinkedList<String>();
for (int j = 0; j < candidates.getNumberElements(); j++) {
if (inchikey1.equals((String) candidates.getElement(j).getProperty(VariableNames.INCHI_KEY_1_NAME))) {
inchikeyfound = true;
alternative_pubchem_ids.add(candidates.getElement(j).getIdentifier() + "|" + candidates.getElement(j).getProperty(VariableNames.INCHI_KEY_3_NAME));
if (candidates.getElement(j).getIdentifier().equals(entries.get(i).pubchemcid))
inchikey_id_missmatches[i] = false;
}
}
if (!inchikeyfound)
inchikey_id_missmatches[i] = true;
if (inchikey_id_missmatches[i] && alternative_pubchem_ids.size() != 0) {
java.util.Iterator<String> it = alternative_pubchem_ids.iterator();
while (it.hasNext()) {
String currentid = it.next();
if (currentid.endsWith("N"))
alternative_pubchem_id[i] = currentid.split("\\|")[0];
}
if (alternative_pubchem_id[i] == null || alternative_pubchem_id[i].equals(""))
alternative_pubchem_id[i] = alternative_pubchem_ids.getFirst().split("\\|")[0];
}
}
String cidString = "";
for (int i = 0; i < inchikey_id_missmatches.length; i++) {
if (inchikey_id_missmatches[i]) {
cidString += "'" + entries.get(i).pubchemcid + "',";
}
}
if (cidString.length() == 0)
return entries;
cidString = cidString.substring(0, cidString.length() - 1);
candidates = lmcd.getCandidateListByQuery("2", "s.accession in (" + cidString + ")");
for (int i = 0; i < inchikey_id_missmatches.length; i++) {
if (inchikey_id_missmatches[i]) {
System.out.println("inchikey pubchemid mismatch found: " + entries.get(i).sampleName + ": " + entries.get(i).inchikey + " " + entries.get(i).pubchemcid);
boolean cid_found = false;
for (int j = 0; j < candidates.getNumberElements(); j++) {
if (candidates.getElement(j).getIdentifier().equals(entries.get(i))) {
entries.get(i).inchikey = (String) candidates.getElement(j).getProperty(VariableNames.INCHI_KEY_1_NAME);
cid_found = true;
break;
}
}
if (!cid_found) {
if (alternative_pubchem_id[i] != null && !alternative_pubchem_id[i].equals("")) {
entries.get(i).pubchemcid = alternative_pubchem_id[i];
cid_found = true;
}
}
if (cid_found) {
System.out.println(" corrected " + entries.get(i).inchikey + " " + entries.get(i).pubchemcid);
inchikey_id_missmatches[i] = false;
}
}
}
// prepare new entry vector
java.util.ArrayList<Entry> newentries = new java.util.ArrayList<Entry>();
for (int i = 0; i < inchikey_id_missmatches.length; i++) {
if (!inchikey_id_missmatches[i])
newentries.add(entries.get(i));
}
return newentries;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class ConvertSDFtoCSV method main.
/**
* @param args
* @throws CDKException
*/
public static void main(String[] args) throws CDKException {
IteratingSDFReader reader;
CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
CandidateList candidates = new CandidateList();
try {
reader = new IteratingSDFReader(new java.io.FileReader(args[0]), DefaultChemObjectBuilder.getInstance());
while (reader.hasNext()) {
IAtomContainer molecule = reader.next();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
adder.addImplicitHydrogens(molecule);
AtomContainerManipulator.convertImplicitToExplicitHydrogens(molecule);
PrecursorCandidate candidate = new PrecursorCandidate((String) molecule.getProperty(VariableNames.INCHI_NAME), (String) molecule.getProperty(VariableNames.IDENTIFIER_NAME));
java.util.Iterator<Object> keys = molecule.getProperties().keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
if (molecule.getProperty(key) != null)
candidate.setProperty(key, molecule.getProperty(key));
}
candidates.addElement(candidate);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
CandidateListWriterPSV writer = new CandidateListWriterPSV();
try {
writer.write(candidates, args[1], args[2]);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations