use of de.ipbhalle.metfraglib.list.MatchList in project MetFragRelaunched by ipb-halle.
the class SimulateSpectrumHDX method generateDeuteratedSpectrumNegative.
/**
* @param candidate
* @return
* @throws AtomTypeNotKnownFromInputListException
* @throws Exception
*/
public static double[][] generateDeuteratedSpectrumNegative(ICandidate candidate, int method) throws AtomTypeNotKnownFromInputListException, Exception {
int numberPositions = searchForDeuteriumExchangeablePositions((TopDownBitArrayPrecursor) candidate.getPrecursorMolecule()).length;
HDTopDownBitArrayPrecursor preHDX = new HDTopDownBitArrayPrecursor(candidate.getImplicitHydrogenAtomContainer(), (byte) numberPositions);
preHDX.preprocessPrecursor();
MatchList matchList = candidate.getMatchList();
double[][] spectrumHDX = null;
if (method == 3)
spectrumHDX = new double[matchList.getNumberElements() * 2][2];
else
spectrumHDX = new double[matchList.getNumberElements()][2];
for (int i = 0; i < matchList.getNumberElements(); i++) {
IFragment frag = matchList.getElement(i).getBestMatchedFragment();
IPeak peak = matchList.getElement(i).getMatchedPeak();
int[] setAtoms = ((DefaultBitArrayFragment) frag).getAtomsFastBitArray().getSetIndeces();
int numberDeuteriums = 0;
for (int k = 0; k < setAtoms.length; k++) {
numberDeuteriums += preHDX.getNumberDeuteriumsConnectedToAtomIndex(0, setAtoms[k]);
}
spectrumHDX[i][1] = peak.getAbsoluteIntensity();
// [M-D]-
if (method == 1) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[i][0] += Constants.HYDROGEN_MASS - Constants.getMonoisotopicMassOfAtom("D");
} else // [M]-
if (method == 2) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
// nothing to do
} else // [M]- [M-D]-
if (method == 3) {
spectrumHDX[i * 2][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[(i * 2) + 1][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[(i * 2) + 1][0] += Constants.HYDROGEN_MASS - Constants.getMonoisotopicMassOfAtom("D");
} else // predict based on deuteriums
if (method == 4) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
if (numberDeuteriums != 0)
spectrumHDX[i][0] += Constants.HYDROGEN_MASS - Constants.getMonoisotopicMassOfAtom("D");
} else // predict based on adduct
if (method == 5) {
byte adductTypeIndex = ((DefaultFragmentToPeakMatch) matchList.getElement(i)).getBestMatchedFragmentAdductTypeIndex();
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
if (adductTypeIndex == 1)
spectrumHDX[i][0] += Constants.getMonoisotopicMassOfAtom("D") - Constants.HYDROGEN_MASS;
}
}
return spectrumHDX;
}
use of de.ipbhalle.metfraglib.list.MatchList 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.MatchList 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.MatchList in project MetFragRelaunched by ipb-halle.
the class CandidateWriterXLS method convertMoleculeFragmentsToImages.
/**
* @param candidateList
* @return
* @throws Exception
*/
private java.util.List<RenderedImage> convertMoleculeFragmentsToImages(ICandidate candidate) throws Exception {
java.util.List<RenderedImage> molImages = new java.util.ArrayList<RenderedImage>();
de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator imageGenerator = new de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator();
imageGenerator.setBackgroundColor(new Color(1f, 1f, 1f, 1f));
imageGenerator.setImageHeight(200);
imageGenerator.setImageWidth(200);
MatchList matchList = candidate.getMatchList();
for (int i = 0; i < matchList.getNumberElements(); i++) {
RenderedImage renderedImage = imageGenerator.generateImage(candidate.getPrecursorMolecule(), matchList.getElement(i).getBestMatchedFragment());
molImages.add(renderedImage);
}
return molImages;
}
use of de.ipbhalle.metfraglib.list.MatchList 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;
}
Aggregations