use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class DownloadEntriesFromPubChem method downloadFromCandidateFile.
public static void downloadFromCandidateFile(String filenameIn, String filenameOut) {
MetFragGlobalSettings settingsIn = new MetFragGlobalSettings();
settingsIn.set(VariableNames.LOCAL_DATABASE_PATH_NAME, filenameIn);
LocalPSVDatabase dbIn = new LocalPSVDatabase(settingsIn);
ArrayList<String> identifiers = null;
try {
identifiers = dbIn.getCandidateIdentifiers();
} catch (MultipleHeadersFoundInInputDatabaseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
CandidateList candidates = dbIn.getCandidateByIdentifier(identifiers);
String[] ids = new String[candidates.getNumberElements()];
for (int i = 0; i < ids.length; i++) ids[i] = candidates.getElement(i).getIdentifier();
MetFragGlobalSettings settingsPubChem = new MetFragGlobalSettings();
settingsPubChem.set(VariableNames.PRECURSOR_DATABASE_IDS_NAME, ids);
OnlineExtendedPubChemDatabase pubchemDB = new OnlineExtendedPubChemDatabase(settingsPubChem);
try {
identifiers = pubchemDB.getCandidateIdentifiers();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
CandidateList candidatesPubChem = null;
try {
candidatesPubChem = pubchemDB.getCandidateByIdentifier(identifiers);
} catch (Exception e1) {
e1.printStackTrace();
}
for (int i = 0; i < candidatesPubChem.getNumberElements(); i++) {
String identifier = candidatesPubChem.getElement(i).getIdentifier();
try {
ICandidate currentCandidate = dbIn.getCandidateByIdentifier(identifier);
currentCandidate.setProperty(VariableNames.PUBCHEM_NUMBER_PUBMED_REFERENCES_NAME, candidatesPubChem.getElement(i).getProperty(VariableNames.PUBCHEM_NUMBER_PUBMED_REFERENCES_NAME));
currentCandidate.setProperty(VariableNames.PUBCHEM_NUMBER_PATENTS_NAME, candidatesPubChem.getElement(i).getProperty(VariableNames.PUBCHEM_NUMBER_PATENTS_NAME));
} catch (DatabaseIdentifierNotFoundException e) {
e.printStackTrace();
}
}
CandidateListWriterPSV writer = new CandidateListWriterPSV();
String filename = filenameOut.replaceAll(".*\\/", "").replaceAll("\\..*$", "");
String path = filenameOut.replaceAll(filename + "\\..*$", "");
try {
writer.write(candidates, filename, path);
} catch (Exception e) {
e.printStackTrace();
}
}
use of de.ipbhalle.metfraglib.list.CandidateList 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.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterXLS method writeFile.
public boolean writeFile(File xlsFile, 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;
java.util.ArrayList<Integer> correctIndeces = new java.util.ArrayList<Integer>();
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));
try {
scoredCandidate.initialisePrecursorCandidate();
} catch (Exception e) {
continue;
}
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("ExplPeaks", peaksExplained);
scoredCandidate.setProperty("FormulasOfExplPeaks", sumFormulasOfFragmentsExplainedPeaks);
scoredCandidate.setProperty("NumberPeaksUsed", numberOfPeaksUsed);
scoredCandidate.setProperty("NoExplPeaks", countExplainedPeaks);
}
scoredCandidate.resetPrecursorMolecule();
correctIndeces.add(i);
}
boolean withImages = false;
xlsFile.createNewFile();
WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
WritableSheet sheet = workbook.createSheet("MetFrag result list", 0);
WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
WritableCellFormat arial10format = new WritableCellFormat(arial10font);
try {
arial10font.setBoldStyle(WritableFont.BOLD);
} catch (WriteException e1) {
System.out.println("Warning: Could not set WritableFont");
}
int numberCells = 0;
java.util.Map<String, Integer> labels = new java.util.HashMap<String, Integer>();
int columnWidthAdd = withImages ? 3 : 0;
int rowHeightAdd = withImages ? 9 : 1;
List<RenderedImage> molImages = null;
if (withImages) {
molImages = convertMoleculesToImages(candidateList);
for (int i = 0; i < molImages.size(); i++) {
// File imageFile = new File(resultspath + fileSep + fileName+
// "_" +i+".png");
File imageFile = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
imageFile.deleteOnExit();
if (ImageIO.write(molImages.get(i), "png", imageFile)) {
WritableImage wi = new WritableImage(0, (i * rowHeightAdd) + 1, columnWidthAdd, rowHeightAdd, imageFile);
sheet.addImage(wi);
}
}
}
for (int i = 0; i < correctIndeces.size(); i++) {
java.util.Hashtable<String, Object> properties = candidateList.getElement(correctIndeces.get(i)).getProperties();
Iterator<String> propNames = properties.keySet().iterator();
while (propNames.hasNext()) {
String propName = (String) propNames.next();
if (!labels.containsKey(propName)) {
labels.put(propName, Integer.valueOf(numberCells));
try {
sheet.addCell(new Label(labels.get(propName) + columnWidthAdd, 0, propName, arial10format));
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
numberCells++;
}
try {
String prop = String.valueOf(properties.get(propName));
if (prop.trim().length() == 0)
prop = "NA";
if (propName.equals(VariableNames.IDENTIFIER_NAME))
prop = prop.replaceAll("\\|[0-9]+", "");
sheet.addCell(new Label(labels.get(propName) + columnWidthAdd, (i * rowHeightAdd) + 1, prop));
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
workbook.write();
workbook.close();
return true;
}
use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterZippedPSV 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;
if (candidateList == null || candidateList.getNumberElements() == 0) {
writeDefaultHeader(file);
return false;
}
String[] lines = new String[candidateList.getNumberElements()];
String heading = "";
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.Enumeration<String> keys = scoredCandidate.getProperties().keys();
if (keys.hasMoreElements()) {
String key = keys.nextElement();
if (i == 0)
heading += key;
lines[i] = "" + checkEmptyProperty(scoredCandidate.getProperty(key));
}
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (i == 0)
heading += "|" + key;
lines[i] += "|" + checkEmptyProperty(scoredCandidate.getProperty(key));
}
}
java.io.BufferedWriter bwriter;
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();
this.zipFile(file);
file.delete();
return true;
}
use of de.ipbhalle.metfraglib.list.CandidateList in project MetFragRelaunched by ipb-halle.
the class CandidateWriterXLS method writeFile.
@Override
public boolean writeFile(File xlsFile, IList list, Settings settings) throws Exception {
CandidateList candidateList = null;
boolean isScoredCandidate = false;
if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
candidateList = (ScoredCandidateList) list;
isScoredCandidate = true;
}
if (list instanceof CandidateList) {
candidateList = (CandidateList) list;
}
if (candidateList == null || candidateList.getNumberElements() == 0)
return false;
ICandidate candidate = candidateList.getElement(0);
if (settings != null)
candidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
candidate.initialisePrecursorCandidate();
xlsFile.createNewFile();
WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
WritableSheet sheet1 = workbook.createSheet("MetFrag Candidate Result", 0);
WritableFont arial10fontBold = new WritableFont(WritableFont.ARIAL, 10);
arial10fontBold.setBoldStyle(WritableFont.BOLD);
WritableCellFormat arial10formatBold = new WritableCellFormat(arial10fontBold);
WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
arial10font.setBoldStyle(WritableFont.NO_BOLD);
WritableCellFormat arial10format = new WritableCellFormat(arial10font);
sheet1.addCell(new Label(0, 0, VariableNames.IDENTIFIER_NAME, arial10formatBold));
sheet1.addCell(new Label(1, 0, candidate.getIdentifier().replaceAll("\\|[0-9]+", ""), arial10format));
java.util.Hashtable<String, Object> properties = candidate.getProperties();
java.util.Enumeration<?> keys = properties.keys();
int propertyRow = 1;
while (keys.hasMoreElements()) {
String currentKey = (String) keys.nextElement();
sheet1.addCell(new Label(0, propertyRow, currentKey, arial10formatBold));
String value = "";
Object obj = properties.get(currentKey);
if (obj instanceof java.lang.Double)
value = String.valueOf((Double) obj);
else if (obj instanceof java.lang.Integer)
value = String.valueOf((Integer) obj);
else if (obj instanceof java.lang.String)
value = (String) obj;
sheet1.addCell(new Label(1, propertyRow, value, arial10format));
propertyRow++;
}
java.util.List<RenderedImage> molFragmentImages = null;
RenderedImage molImage = this.convertMoleculeToImages(candidate);
File imageFile = File.createTempFile("file", ".png", new File(Constants.OS_TEMP_DIR));
imageFile.deleteOnExit();
int rowHeightImage = 10;
int colWidthImage = 3;
int fragmentColumns = 1;
sheet1.addCell(new Label(0, propertyRow + 1, "Precursor", arial10formatBold));
if (ImageIO.write(molImage, "png", imageFile)) {
WritableImage wi = new WritableImage(0, propertyRow + 2, colWidthImage, rowHeightImage, imageFile);
sheet1.addImage(wi);
}
if (isScoredCandidate) {
molFragmentImages = this.convertMoleculeFragmentsToImages(candidate);
if (molFragmentImages.size() > 0) {
sheet1.addCell(new Label(0, (propertyRow + 4) + rowHeightImage, "Fragments", arial10formatBold));
}
int imagesWritten = 1;
int fragmentRow = 0;
for (int i = 0; i < molFragmentImages.size(); i++) {
File imageFileFragment = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
imageFileFragment.deleteOnExit();
if (ImageIO.write(molFragmentImages.get(i), "png", imageFileFragment)) {
int column = (((imagesWritten - 1) % fragmentColumns)) * (colWidthImage + 3);
if ((imagesWritten - 1) % fragmentColumns == 0)
fragmentRow++;
WritableImage wi = new WritableImage(column, (propertyRow + 5) + (rowHeightImage * fragmentRow), colWidthImage, rowHeightImage, imageFileFragment);
sheet1.addImage(wi);
FragmentMassToPeakMatch match = (FragmentMassToPeakMatch) candidate.getMatchList().getElement(imagesWritten - 1);
sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow), "Fragment " + imagesWritten, arial10formatBold));
sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 2, "Formula", arial10formatBold));
sheet1.addCell(new Label(column + colWidthImage + 1, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 2, match.getModifiedFormulaStringOfBestMatchedFragment(candidate.getPrecursorMolecule()), arial10format));
sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 3, "Mass", arial10formatBold));
sheet1.addCell(new Label(column + colWidthImage + 1, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 3, String.valueOf(MathTools.round(match.getBestMatchFragmentMass())), arial10format));
sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 4, "Peak m/z", arial10formatBold));
sheet1.addCell(new Label(column + colWidthImage + 1, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 4, String.valueOf(match.getMatchedPeak().getMass()), arial10format));
imagesWritten++;
}
}
}
workbook.write();
workbook.close();
return true;
}
Aggregations