use of de.ipbhalle.metfraglib.interfaces.IWriter 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.interfaces.IWriter 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.interfaces.IWriter in project MetFragRelaunched by ipb-halle.
the class CommandLineTool method main.
/**
* @param args
*/
public static void main(String[] args) {
/*
* read in commandline arguments
*/
java.util.Hashtable<String, String> commandLineArguments = processCommandLineArguments(args);
if (commandLineArguments == null) {
System.exit(1);
}
if (printHelp) {
printHelp();
System.exit(0);
}
File parameterFile = null;
MetFragGlobalSettings settings = null;
if (commandLineArguments.containsKey(VariableNames.PARAMETER_FILE_NAME)) {
parameterFile = new File((String) commandLineArguments.get(VariableNames.PARAMETER_FILE_NAME));
if (!parameterFile.exists()) {
logger.error("Parameter file " + parameterFile.getAbsolutePath() + " not found!");
System.exit(1);
}
if (!parameterFile.canRead()) {
logger.error("Parameter file " + parameterFile.getAbsolutePath() + " has no read permissions!");
System.exit(1);
}
if (!parameterFile.isFile()) {
logger.error("Parameter file " + parameterFile.getAbsolutePath() + " is no regular file!");
System.exit(1);
}
/*
* read settings
*/
try {
settings = MetFragGlobalSettings.readSettings(parameterFile, logger);
} catch (Exception e) {
logger.error("Error reading parameter file " + parameterFile);
System.exit(1);
}
}
/*
* read additional parameters
*/
try {
if (settings == null) {
settings = MetFragGlobalSettings.readSettings(commandLineArguments, logger);
} else {
MetFragGlobalSettings.readSettings(commandLineArguments, settings, logger);
}
} catch (Exception e2) {
logger.error(e2.getMessage());
logger.error("Error reading settings");
System.exit(1);
}
// check settings with SettingsChecker
SettingsChecker settingsChecker = new SettingsChecker();
if (!settingsChecker.check(settings)) {
System.exit(2);
}
/*
* load hd plugin
*/
HydrogenDeuteriumPlugin hdPlugin = new HydrogenDeuteriumPlugin();
if (!hdPlugin.load(settings))
System.exit(3);
// init the MetFrag process
CombinedMetFragProcess mp = new CombinedMetFragProcess(settings);
// retrieve candidates from database
try {
boolean candidatesRetrieved = mp.retrieveCompounds();
if (!candidatesRetrieved)
throw new Exception();
} catch (Exception e1) {
e1.printStackTrace();
logger.error("Error when retrieving compounds.");
System.exit(4);
}
// run the MetFrag process -> in silico fragmentation, fragment-peak-assignment, scoring
try {
mp.run();
} catch (Exception e1) {
e1.printStackTrace();
logger.error("Error when processing compounds.");
System.exit(5);
}
// fetch the scored candidate list
CandidateList scoredCandidateList = mp.getCandidateList();
/*
* store the results
*/
try {
/*
* store candidates in your specified format
*/
if (settings.get(VariableNames.METFRAG_CANDIDATE_WRITER_NAME) != null) {
String[] candidateWriterNames = (String[]) settings.get(VariableNames.METFRAG_CANDIDATE_WRITER_NAME);
for (int k = 0; k < candidateWriterNames.length; k++) {
IWriter candidateWriter = (IWriter) Class.forName(ClassNames.getClassNameOfCandidateListWriter(candidateWriterNames[k])).getConstructor().newInstance();
try {
Object ResultsFile = settings.get(VariableNames.STORE_RESULTS_FILE_NAME);
if (ResultsFile != null)
candidateWriter.writeFile(new File((String) ResultsFile), scoredCandidateList, settings);
else
candidateWriter.write(scoredCandidateList, (String) settings.get(VariableNames.SAMPLE_NAME), (String) settings.get(VariableNames.STORE_RESULTS_PATH_NAME), settings);
} catch (Exception e) {
e.printStackTrace();
logger.error("Error: Could not write candidate file.");
System.exit(6);
}
}
}
/*
* store candidates fragments in your specified format
*/
if (settings.get(VariableNames.METFRAG_CANDIDATE_FRAGMENT_WRITER_NAME) != null) {
IWriter candidateWriter = (IWriter) Class.forName(ClassNames.getClassNameOfFragmentListWriter((String) settings.get(VariableNames.METFRAG_CANDIDATE_FRAGMENT_WRITER_NAME))).getConstructor().newInstance();
try {
candidateWriter.write(scoredCandidateList, (String) settings.get(VariableNames.SAMPLE_NAME), (String) settings.get(VariableNames.STORE_RESULTS_PATH_NAME), settings);
} catch (Exception e) {
logger.error(e.getMessage());
logger.error("Error: Could not write fragment files.");
System.exit(7);
}
}
/*
* store candidates in your specified image format
*/
if (settings.get(VariableNames.METFRAG_CANDIDATE_IMAGE_GENERATOR_NAME) != null) {
IWriter imageWriter = (IWriter) Class.forName((String) settings.get(VariableNames.METFRAG_IMAGE_WRITER_NAME)).getConstructor().newInstance();
IImageGenerator imageGenerator = (IImageGenerator) Class.forName((String) settings.get(VariableNames.METFRAG_CANDIDATE_IMAGE_GENERATOR_NAME)).getConstructor().newInstance();
for (int i = 0; i < scoredCandidateList.getNumberElements(); i++) {
try {
RenderedImage renderedImage = imageGenerator.generateImage(scoredCandidateList.getElement(i));
DefaultList imageList = new DefaultList();
imageList.addElement(renderedImage);
imageWriter.write(imageList, (String) settings.get(VariableNames.SAMPLE_NAME) + "_" + scoredCandidateList.getElement(i).getIdentifier(), (String) settings.get(VariableNames.STORE_RESULTS_PATH_NAME));
} catch (Exception e) {
logger.error("Error: Could not write candidate image files.");
System.exit(8);
}
}
}
/*
* store candidate fragments in your specified image format
*/
if (settings.get(VariableNames.METFRAG_FRAGMENT_IMAGE_GENERATOR_NAME) != null) {
IWriter imageWriter = (IWriter) Class.forName((String) settings.get(VariableNames.METFRAG_IMAGE_WRITER_NAME)).getConstructor().newInstance();
IImageGenerator imageGenerator = (IImageGenerator) Class.forName((String) settings.get(VariableNames.METFRAG_FRAGMENT_IMAGE_GENERATOR_NAME)).getConstructor().newInstance();
for (int i = 0; i < scoredCandidateList.getNumberElements(); i++) {
MatchList matchList = scoredCandidateList.getElement(i).getMatchList();
for (int j = 0; j < matchList.getNumberElements(); j++) {
try {
scoredCandidateList.getElement(i).initialisePrecursorCandidate();
RenderedImage renderedImage = imageGenerator.generateImage(scoredCandidateList.getElement(i).getPrecursorMolecule(), (DefaultBitArrayFragment) matchList.getElement(j).getBestMatchedFragment());
DefaultList imageList = new DefaultList();
imageList.addElement(renderedImage);
imageWriter.write(imageList, (String) settings.get(VariableNames.SAMPLE_NAME) + "_" + scoredCandidateList.getElement(i).getIdentifier() + "_fragment_id" + (j + 1), (String) settings.get(VariableNames.STORE_RESULTS_PATH_NAME));
scoredCandidateList.getElement(i).resetPrecursorMolecule();
} catch (Exception e) {
logger.error("Error: Could not write fragment image files.");
System.exit(9);
}
}
}
}
} catch (InstantiationException e) {
e.printStackTrace();
System.exit(10);
} catch (IllegalAccessException e) {
e.printStackTrace();
System.exit(10);
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.exit(10);
} catch (InvocationTargetException e) {
e.printStackTrace();
System.exit(10);
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.exit(10);
} catch (SecurityException e) {
e.printStackTrace();
System.exit(10);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(10);
}
}
use of de.ipbhalle.metfraglib.interfaces.IWriter in project MetFragRelaunched by ipb-halle.
the class MetFragRestService method startMetFrag.
@Async
public static String startMetFrag(CombinedMetFragProcess mp, MetFragGlobalSettings settings, String resultFolderName) throws IOException {
// retrieve candidates from database
boolean candidatesRetrieved = false;
try {
candidatesRetrieved = mp.retrieveCompounds();
mp.run();
CandidateList scoredCandidateList = mp.getCandidateList();
if (settings.get(VariableNames.METFRAG_CANDIDATE_WRITER_NAME) != null) {
String[] candidateWriterNames = (String[]) settings.get(VariableNames.METFRAG_CANDIDATE_WRITER_NAME);
for (int k = 0; k < candidateWriterNames.length; k++) {
IWriter candidateWriter = (IWriter) Class.forName(ClassNames.getClassNameOfCandidateListWriter(candidateWriterNames[k])).getConstructor().newInstance();
try {
Object ResultsFile = settings.get(VariableNames.STORE_RESULTS_FILE_NAME);
if (ResultsFile != null)
candidateWriter.writeFile(new File((String) ResultsFile), scoredCandidateList, settings);
else
candidateWriter.write(scoredCandidateList, (String) settings.get(VariableNames.SAMPLE_NAME), (String) settings.get(VariableNames.STORE_RESULTS_PATH_NAME), settings);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
writeStatus("ERROR", (String) settings.get(VariableNames.SAMPLE_NAME), resultFolderName);
return "Error when fetching candidates.";
}
if (!candidatesRetrieved) {
writeStatus("ERROR", (String) settings.get(VariableNames.SAMPLE_NAME), resultFolderName);
return "Error when fetching candidates.";
} else {
writeStatus("SUCCESS", (String) settings.get(VariableNames.SAMPLE_NAME), resultFolderName);
return (String) settings.get(VariableNames.SAMPLE_NAME);
}
}
use of de.ipbhalle.metfraglib.interfaces.IWriter 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