use of de.ipbhalle.metfraglib.fragment.DefaultBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class CollectCommuniesAndFragmentPeaks method main.
public static void main(String[] args) {
MetFragGlobalSettings settings = new MetFragGlobalSettings();
settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, args[0]);
LocalPSVDatabase db = new LocalPSVDatabase(settings);
java.util.ArrayList<String> ids = null;
try {
ids = db.getCandidateIdentifiers();
} catch (MultipleHeadersFoundInInputDatabaseException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
CandidateList candidates = db.getCandidateByIdentifier(ids);
ArrayList<Double> masses = new ArrayList<Double>();
ArrayList<ArrayList<String>> vector_formulas = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> vector_smiles = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> vector_eawagids = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<Double>> vector_intensities = new ArrayList<ArrayList<Double>>();
ArrayList<Integer> occurences = new ArrayList<Integer>();
ArrayList<ArrayList<FingerPrintFragmentCollection>> matchingFragments = new ArrayList<ArrayList<FingerPrintFragmentCollection>>();
for (int i = 0; i < candidates.getNumberElements(); i++) {
// System.out.println(candidates.getElement(i).getIdentifier());
try {
candidates.getElement(i).initialisePrecursorCandidate();
} catch (AtomTypeNotKnownFromInputListException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
CommunityCalculation c = new CommunityCalculation((BitArrayPrecursor) candidates.getElement(i).getPrecursorMolecule());
DefaultBitArrayFragment[] communityFragments = c.getCommunityFragments();
String[] formulas = ((String) candidates.getElement(i).getProperties().get("FormulasOfExplPeaks")).split(";");
String eawagID = (String) candidates.getElement(i).getProperties().get("EawagID");
String[] smiles = ((String) candidates.getElement(i).getProperties().get("SmilesOfExplPeaks")).split(";");
String[] explPeaks = ((String) candidates.getElement(i).getProperties().get("ExplPeaks")).split(";");
boolean debug = false;
for (int k = 0; k < formulas.length; k++) {
String[] tmp_formula = formulas[k].split(":");
String[] tmp_smiles = smiles[k].split(":");
String[] tmp_intensity = explPeaks[k].split("_");
if (tmp_formula.length != 2 || tmp_smiles.length != 2 || tmp_intensity.length != 2)
continue;
double intensity = Double.parseDouble(tmp_intensity[1]);
if (intensity < intensityThreshold)
continue;
double mass = Double.parseDouble(tmp_formula[0]);
int index = containsDouble(masses, mass, 5.0, 0.001, debug);
if (index == -1) {
int addedIndex = addMassSorted(masses, mass, debug);
/*
* how often we have ssen this peak mass
* here: only once as initial add
*/
occurences.add(addedIndex, 1);
/*
* add the fragment
*/
addFragmentsAddPositionInitial(candidates.getElement(i).getPrecursorMolecule(), addedIndex, matchingFragments, communityFragments, candidates.getElement(i).getIdentifier());
ArrayList<String> tmp_formulas = new ArrayList<String>();
ArrayList<String> tmp_smiless = new ArrayList<String>();
ArrayList<Double> tmp_intensities = new ArrayList<Double>();
ArrayList<String> tmp_eawagids = new ArrayList<String>();
tmp_formulas.add(tmp_formula[1]);
tmp_smiless.add(tmp_smiles[1]);
tmp_intensities.add(Double.parseDouble(tmp_intensity[1]));
tmp_eawagids.add(eawagID);
vector_formulas.add(addedIndex, tmp_formulas);
vector_smiles.add(addedIndex, tmp_smiless);
vector_intensities.add(addedIndex, tmp_intensities);
vector_eawagids.add(addedIndex, tmp_eawagids);
} else {
masses.set(index, (masses.get(index) + mass) / 2.0);
occurences.set(index, occurences.get(index) + 1);
addFragmentsAddPosition(candidates.getElement(i).getPrecursorMolecule(), index, matchingFragments, communityFragments, candidates.getElement(i).getIdentifier());
vector_formulas.get(index).add(tmp_formula[1]);
vector_smiles.get(index).add(tmp_smiles[1]);
vector_intensities.get(index).add(Double.parseDouble(tmp_intensity[1]));
vector_eawagids.get(index).add(eawagID);
}
}
}
printMasses(masses);
java.io.BufferedWriter bwriter;
java.io.BufferedWriter smilesBwriter;
try {
smilesBwriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(args[2])));
bwriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(args[1])));
for (int i = 0; i < masses.size(); i++) {
// System.out.println(masses.get(i));
bwriter.write(masses.get(i) + "");
bwriter.newLine();
for (int k = 0; k < vector_formulas.get(i).size(); k++) {
// System.out.println("\t" + vector_eawagids.get(i).get(k) + ": " + vector_formulas.get(i).get(k) + " " + vector_smiles.get(i).get(k) + " " + vector_intensities.get(i).get(k));
bwriter.write("\t" + vector_eawagids.get(i).get(k) + ": " + vector_formulas.get(i).get(k) + " " + vector_smiles.get(i).get(k) + " " + vector_intensities.get(i).get(k));
smilesBwriter.write(vector_smiles.get(i).get(k));
smilesBwriter.newLine();
bwriter.newLine();
}
// System.out.print("\t");
bwriter.write("\t");
for (int k = 0; k < vector_formulas.get(i).size(); k++) {
// System.out.print(vector_eawagids.get(i).get(k) + ".png ");
bwriter.write(vector_eawagids.get(i).get(k) + ".png ");
}
// System.out.println();
bwriter.newLine();
bwriter.newLine();
// System.out.println();
}
bwriter.close();
smilesBwriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
java.io.BufferedWriter bwriterComms = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(args[3])));
for (int i = 0; i < matchingFragments.size(); i++) {
bwriterComms.write(masses.get(i) + " " + occurences.get(i));
bwriterComms.newLine();
ArrayList<FingerPrintFragmentCollection> collections = matchingFragments.get(i);
for (int j = 0; j < collections.size(); j++) {
FingerPrintFragmentCollection collection = collections.get(j);
bwriterComms.write("\t");
bwriterComms.write(collection.toString());
bwriterComms.newLine();
}
}
bwriterComms.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of de.ipbhalle.metfraglib.fragment.DefaultBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class CommunityCalculation method calculateCommunities.
private void calculateCommunities() {
String nodeString = "";
for (int i = 0; i < this.precursor.getNonHydrogenAtomCount(); i++) {
short[] connectedAtomIndexes = this.precursor.getConnectedAtomIndecesOfAtomIndex((short) i);
for (int k = 0; k < connectedAtomIndexes.length; k++) {
if (i < connectedAtomIndexes[k])
nodeString += "," + (i + 1) + "," + (connectedAtomIndexes[k] + 1);
}
}
nodeString = nodeString.substring(1);
String command1 = "g <- graph(c(" + nodeString + "),directed=F)";
String command2 = "comms <- fastgreedy.community(g)";
String command3 = "membership(comms)";
double[] membershipDouble = null;
if (Rsession.isSessionLoaded())
membershipDouble = Rsession.giveMemberships(command1, command2, command3);
this.memberships = new int[membershipDouble.length];
this.numberCommunities = 0;
for (int i = 0; i < membershipDouble.length; i++) {
this.memberships[i] = (int) membershipDouble[i] - 1;
if (this.memberships[i] + 1 > this.numberCommunities)
this.numberCommunities = this.memberships[i] + 1;
}
FastBitArray[] atomFastBitArrays = new FastBitArray[this.numberCommunities];
for (int i = 0; i < this.numberCommunities; i++) atomFastBitArrays[i] = new FastBitArray(this.memberships.length, false);
for (int i = 0; i < this.memberships.length; i++) {
atomFastBitArrays[this.memberships[i]].set(i);
}
this.communityFragments = new DefaultBitArrayFragment[this.numberCommunities];
for (int i = 0; i < this.communityFragments.length; i++) {
this.communityFragments[i] = new DefaultBitArrayFragment(this.precursor, atomFastBitArrays[i]);
}
}
use of de.ipbhalle.metfraglib.fragment.DefaultBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterSDF method writeFile.
public boolean writeFile(File file, IList list, Settings settings) throws Exception {
IAtomContainerSet set = new AtomContainerSet();
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++) {
ICandidate candidate = candidateList.getElement(i);
if (settings != null)
candidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
try {
candidate.initialisePrecursorCandidate();
} catch (Exception e) {
continue;
}
IAtomContainer candidateAtomContainer = null;
try {
candidateAtomContainer = candidate.getAtomContainer();
} catch (Exception e1) {
System.err.println("Error saving: " + candidateList.getElement(i).getIdentifier());
continue;
}
ICandidate scoredCandidate = candidateList.getElement(i);
if (scoredCandidate.getMatchList() != null) {
MatchList matchList = scoredCandidate.getMatchList();
int countExplainedPeaks = 0;
for (int l = 0; l < matchList.getNumberElements(); l++) {
try {
matchList.getElement(l).getMatchedPeak().getIntensity();
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
countExplainedPeaks++;
}
candidateAtomContainer.setProperty("NumberPeaksExplained", countExplainedPeaks);
}
String peaksExplained = "";
String sumFormulasOfFragmentsExplainedPeaks = "";
String fragmentAtomArrays = "";
String fragmentBondArrays = "";
String fragmentBrokenBondArrays = "";
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 + ";";
fragmentAtomArrays += ((DefaultBitArrayFragment) scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment()).getAtomsFastBitArray() + ";";
fragmentBondArrays += ((DefaultBitArrayFragment) scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment()).getBondsFastBitArray() + ";";
fragmentBrokenBondArrays += ((DefaultBitArrayFragment) scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment()).getBrokenBondsFastBitArray() + ";";
}
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 (fragmentAtomArrays.length() != 0)
fragmentAtomArrays = fragmentAtomArrays.substring(0, fragmentAtomArrays.length() - 1);
if (fragmentBondArrays.length() != 0)
fragmentBondArrays = fragmentBondArrays.substring(0, fragmentBondArrays.length() - 1);
if (fragmentBrokenBondArrays.length() != 0)
fragmentBrokenBondArrays = fragmentBrokenBondArrays.substring(0, fragmentBrokenBondArrays.length() - 1);
if (sumFormulasOfFragmentsExplainedPeaks.length() == 0)
sumFormulasOfFragmentsExplainedPeaks = "NA";
candidateAtomContainer.setProperty("PeaksExplained", peaksExplained);
candidateAtomContainer.setProperty("FragmentMolecularFormulas", sumFormulasOfFragmentsExplainedPeaks);
}
for (java.util.Enumeration<String> keys = candidate.getProperties().keys(); keys.hasMoreElements(); ) {
String key = keys.nextElement();
String propertyValue = checkEmptyProperty(candidate.getProperty(key)).toString();
if (key.equals(VariableNames.IDENTIFIER_NAME))
propertyValue = propertyValue.replaceAll("\\|[0-9]+", "");
candidateAtomContainer.setProperty(key, propertyValue);
}
candidateAtomContainer.setProperty("NumberPeaksUsed", numberOfPeaksUsed);
MoleculeFunctions.prepareAtomContainer(candidateAtomContainer, true);
MoleculeFunctions.convertExplicitToImplicitHydrogens(candidateAtomContainer);
set.addAtomContainer(candidateAtomContainer);
}
SDFWriter writer = new SDFWriter(new FileWriter(file));
writer.write(set);
writer.close();
return true;
}
Aggregations