use of de.ipbhalle.metfraglib.writer.CandidateListWriterCSV in project MetFragRelaunched by ipb-halle.
the class UserOutputDataHandler method createResultsToDownload.
/**
* @param format
* @param errorMessages
* @return
*/
public org.primefaces.model.StreamedContent createResultsToDownload(MetFragResultsContainer metfragResults, String format, Messages errorMessages) {
System.out.println("downloadResults " + format);
IWriter candidateWriter = null;
String filePath = "";
org.primefaces.model.StreamedContent resource = new org.primefaces.model.DefaultStreamedContent(System.in, "text/csv", "MetFragWeb_Candidates." + format);
try {
java.io.File folder = new java.io.File(this.beanSettingsContainer.getRootSessionFolder() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "downloads");
System.out.println("creating folder " + folder.getAbsolutePath());
boolean folderExists = true;
if (!folder.exists())
folderExists = folder.mkdirs();
if (!folderExists)
throw new Exception();
String mimetype = "";
if (format.equals("csv")) {
candidateWriter = new CandidateListWriterCSV();
mimetype = "text/csv";
} else if (format.equals("xls")) {
candidateWriter = new CandidateListWriterXLS();
mimetype = "application/vnd.ms-excel";
} else if (format.equals("sdf")) {
candidateWriter = new CandidateListWriterSDF();
mimetype = "chemical/x-mdl-sdfile";
} else {
System.out.println("Error: Unknown format " + format);
}
candidateWriter.write(metfragResults.getScoredCandidateList(), "MetFragWeb_Results", folder.getAbsolutePath());
filePath = folder.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "MetFragWeb_Results." + format;
resource = new org.primefaces.model.DefaultStreamedContent(new java.io.FileInputStream(new java.io.File(filePath)), mimetype, "MetFragWeb_Candidates." + format);
errorMessages.removeKey("buttonDownloadResultsError");
} catch (Exception e) {
e.printStackTrace();
errorMessages.setMessage("buttonDownloadResultsError", "Error when downloading candidates.");
return resource;
}
System.out.println("success " + resource.getContentType() + " " + resource.getName());
return resource;
}
use of de.ipbhalle.metfraglib.writer.CandidateListWriterCSV in project MetFragRelaunched by ipb-halle.
the class AddMissingNonExplainedPeaks method main.
public static void main(String[] args) throws Exception {
String paramfile = args[0];
String resultfile = args[1];
String outputfile = args[2];
Settings settings = getSettings(paramfile);
settings.set(VariableNames.LOCAL_DATABASE_PATH_NAME, resultfile);
IPeakListReader peakListReader = (IPeakListReader) Class.forName((String) settings.get(VariableNames.METFRAG_PEAK_LIST_READER_NAME)).getConstructor(Settings.class).newInstance(settings);
SettingsChecker settingsChecker = new SettingsChecker();
if (!settingsChecker.check(settings)) {
System.err.println("Problems reading settings");
return;
}
settings.set(VariableNames.PEAK_LIST_NAME, peakListReader.read());
IDatabase db = null;
String dbFilename = (String) settings.get(VariableNames.LOCAL_DATABASE_PATH_NAME);
if (dbFilename.endsWith("psv"))
db = new LocalPSVDatabase(settings);
else
db = new LocalCSVDatabase(settings);
ArrayList<String> ids = null;
try {
ids = db.getCandidateIdentifiers();
} catch (MultipleHeadersFoundInInputDatabaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CandidateList candidates = null;
try {
candidates = db.getCandidateByIdentifier(ids);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (candidates.getNumberElements() == 0) {
System.out.println("No candidates found in " + (String) settings.get(VariableNames.LOCAL_DATABASE_PATH_NAME));
return;
}
DefaultPeakList peaklist = (DefaultPeakList) settings.get(VariableNames.PEAK_LIST_NAME);
for (int i = 0; i < candidates.getNumberElements(); i++) {
String explPeaks = (String) candidates.getElement(i).getProperty("ExplPeaks");
String[] explPeaksArray = explPeaks.split(";");
Double[] explPeaksMasses = null;
if (!explPeaks.equals("NA"))
explPeaksMasses = getDoubleArrayFromPeakList(explPeaksArray);
String nonExplPeaksString = "";
for (int k = 0; k < peaklist.getNumberElements(); k++) {
if (explPeaks.equals("NA")) {
nonExplPeaksString += ((IPeak) peaklist.getElement(k)).getMass() + ";";
} else if (!isContained(((IPeak) peaklist.getElement(k)).getMass(), explPeaksMasses)) {
nonExplPeaksString += ((IPeak) peaklist.getElement(k)).getMass() + ";";
}
}
if (nonExplPeaksString.length() == 0)
nonExplPeaksString = "NA";
if (nonExplPeaksString.endsWith(";"))
nonExplPeaksString = nonExplPeaksString.substring(0, nonExplPeaksString.length() - 1);
candidates.getElement(i).setProperty("NonExplainedMasses", nonExplPeaksString);
}
IWriter writer = null;
if (outputfile.endsWith("psv"))
writer = new CandidateListWriterPSV();
else
writer = new CandidateListWriterCSV();
writer.write(candidates, outputfile);
}
use of de.ipbhalle.metfraglib.writer.CandidateListWriterCSV in project MetFragRelaunched by ipb-halle.
the class UserOutputDataHandler method createCandidatesToDownload.
/**
* @param format
* @param errorMessages
* @return
*/
public org.primefaces.model.StreamedContent createCandidatesToDownload(String format, Messages errorMessages) {
System.out.println("downloadCandidates " + format);
IWriter candidateWriter = null;
String filePath = "";
org.primefaces.model.StreamedContent resource = new org.primefaces.model.DefaultStreamedContent(System.in, "text/csv", "MetFragWeb_Candidates." + format);
try {
java.io.File folder = new java.io.File(this.beanSettingsContainer.getRootSessionFolder() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "downloads");
System.out.println("creating folder " + folder.getAbsolutePath());
boolean folderExists = true;
if (!folder.exists())
folderExists = folder.mkdirs();
if (!folderExists)
throw new Exception();
String mimetype = "";
if (format.equals("csv")) {
candidateWriter = new CandidateListWriterCSV();
mimetype = "text/csv";
} else if (format.equals("xls")) {
candidateWriter = new CandidateListWriterXLS();
mimetype = "application/vnd.ms-excel";
} else if (format.equals("sdf")) {
candidateWriter = new CandidateListWriterSDF();
mimetype = "chemical/x-mdl-sdfile";
} else {
System.out.println("Error: Unknown format " + format);
}
candidateWriter.write(this.beanSettingsContainer.getRetrievedCandidateList(), "MetFragWeb_Candidates", folder.getAbsolutePath());
filePath = folder.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "MetFragWeb_Candidates." + format;
resource = new org.primefaces.model.DefaultStreamedContent(new java.io.FileInputStream(new java.io.File(filePath)), mimetype, "MetFragWeb_Candidates." + format);
errorMessages.removeKey("buttonDownloadCompoundsError");
} catch (Exception e) {
errorMessages.setMessage("buttonDownloadCompoundsError", "Error when downloading candidates.");
return resource;
}
System.out.println("success " + resource.getContentType() + " " + resource.getName());
return resource;
}
Aggregations