/** * Preprocess the spectra in a given folder and merge those with a different collision energy * (from the same compound). All results are saved!!!! * * @param folder the folder */ private void preprocessUnsorted(String folder, double mzabs, double mzppm) { // loop over all files in folder File f = new File(folder); File files[] = f.listFiles(); Arrays.sort(files); // create new folder String path = folder + "Merged"; new File(path).mkdir(); int temp = 0; Map<String, List<File>> pubchemToFiles = new HashMap<String, List<File>>(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { WrapperSpectrum spectrum = new WrapperSpectrum(files[i].toString()); if (pubchemToFiles.containsKey(Integer.toString(spectrum.getCID()))) pubchemToFiles.get(Integer.toString(spectrum.getCID())).add(files[i]); else { List<File> fileList = new ArrayList<File>(); fileList.add(files[i]); pubchemToFiles.put(Integer.toString(spectrum.getCID()), fileList); } System.out.println(files[i].toString() + spectrum.getCID()); } } for (String pubchemID : pubchemToFiles.keySet()) { String mergedNames = ""; Vector<WrapperSpectrum> spectra = new Vector<WrapperSpectrum>(); String lastFile = ""; for (File file : pubchemToFiles.get(pubchemID)) { WrapperSpectrum spectrum = new WrapperSpectrum(file.toString()); // no pubchem id given ... skip this spectrum! if (spectrum.getCID() == 0) continue; spectra.add(spectrum); mergedNames += file.getName().split("\\.")[0]; lastFile = file.toString(); } // now merge the peaks from the different collision energies Vector<Double> mergedPeaks = mergePeaks(spectra, mzabs, mzppm); // now write back those peaks into a new file String line = ""; try { BufferedReader reader = new BufferedReader(new FileReader(lastFile)); line += reader.readLine() + "\n"; while (line != null && !line.contains("PK$NUM_PEAK:")) { String currentLine = reader.readLine(); if (currentLine.contains("PK$NUM_PEAK:")) break; else line += currentLine + "\n"; } line += "PK$NUM_PEAK: " + mergedPeaks.size() + "\n"; line += "PK$PEAK: m/z int. rel.int.\n"; for (int k = 0; k < mergedPeaks.size(); k++) { // those intensities should be in sync with the merged peaks....quick and dirty... line += " " + mergedPeaks.get(k) + " " + this.newIntensity.get(k) + " " + this.newRelIntensity.get(k) + "\n"; } line += "//"; // write to file File outFile = new File(path + "/" + mergedNames + ".txt"); System.out.println(mergedNames); FileWriter out = new FileWriter(outFile); out.write(line); out.close(); } catch (FileNotFoundException e) { System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } } }
/** * Preprocess the spectra in a given folder and merge those with a different collision energy * (from the same compound). All results are saved!!!! * * @param folder the folder */ private void preprocess(String folder, double threshold) { // loop over all files in folder File f = new File(folder); File files[] = f.listFiles(); Arrays.sort(files); // create new folder String path = folder + "Merged"; new File(path).mkdir(); int temp = 0; for (int i = 0; i < (files.length - 1); i++) { if (files[i].isFile()) { WrapperSpectrum spectrum = new WrapperSpectrum(files[i].toString()); // no pubchem id given ... skip this spectrum! if (spectrum.getCID() == 0) continue; temp = i; Vector<WrapperSpectrum> spectra = new Vector<WrapperSpectrum>(); spectra.add(spectrum); // next file WrapperSpectrum spectrumTemp = new WrapperSpectrum(files[i + 1].toString()); // same InchI int j = 2; String mergedNames = files[i].getName().split("\\.")[0]; while (spectrum.getInchI().compareTo(spectrumTemp.getInchI()) == 0 && (i + j) <= files.length) // while(spectrum.getTrivialName().equals(spectrumTemp.getTrivialName()) && (i+j) <= // files.length) { // same InchI --> add to list spectra.add(spectrumTemp); mergedNames += files[i + (j - 1)].getName().split("\\.")[0]; // fix for last file if ((i + j) < files.length) spectrumTemp = new WrapperSpectrum(files[i + j].toString()); j++; } i = i + (j - 2); // now merge the peaks from the different collision energies Vector<Double> mergedPeaks = mergePeaks(spectra, threshold, 0.0); // now write back those peaks into a new file String line = ""; try { BufferedReader reader = new BufferedReader(new FileReader(files[temp].toString())); line += reader.readLine() + "\n"; while (line != null && !line.contains("AC$ANALYTICAL_CONDITION: COLLISION_ENERGY")) { line += reader.readLine() + "\n"; } line += "PK$NUM_PEAK: " + mergedPeaks.size() + "\n"; line += "PK$PEAK: m/z int. rel.int.\n"; for (int k = 0; k < mergedPeaks.size(); k++) { // those intensities should be in sync with the merged peaks....quick and dirty... line += " " + mergedPeaks.get(k) + " " + this.newIntensity.get(k) + " " + this.newRelIntensity.get(k) + "\n"; } // write to file File outFile = new File(path + "/" + mergedNames + ".txt"); System.out.println(mergedNames); FileWriter out = new FileWriter(outFile); out.write(line); out.close(); } catch (FileNotFoundException e) { System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } } } }