Search in sources :

Example 1 with CandidateListWriterCSV

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;
}
Also used : DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent) CandidateListWriterSDF(de.ipbhalle.metfraglib.writer.CandidateListWriterSDF) IWriter(de.ipbhalle.metfraglib.interfaces.IWriter) CandidateListWriterXLS(de.ipbhalle.metfraglib.writer.CandidateListWriterXLS) File(java.io.File) CandidateListWriterCSV(de.ipbhalle.metfraglib.writer.CandidateListWriterCSV) File(java.io.File) UploadedSuspectListFile(de.ipbhalle.metfragweb.datatype.UploadedSuspectListFile) DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent)

Example 2 with CandidateListWriterCSV

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);
}
Also used : IPeak(de.ipbhalle.metfraglib.interfaces.IPeak) IDatabase(de.ipbhalle.metfraglib.interfaces.IDatabase) IWriter(de.ipbhalle.metfraglib.interfaces.IWriter) DefaultPeakList(de.ipbhalle.metfraglib.list.DefaultPeakList) LocalCSVDatabase(de.ipbhalle.metfraglib.database.LocalCSVDatabase) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) IPeakListReader(de.ipbhalle.metfraglib.interfaces.IPeakListReader) CandidateListWriterCSV(de.ipbhalle.metfraglib.writer.CandidateListWriterCSV) LocalPSVDatabase(de.ipbhalle.metfraglib.database.LocalPSVDatabase) SettingsChecker(de.ipbhalle.metfraglib.parameter.SettingsChecker) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) MultipleHeadersFoundInInputDatabaseException(de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException) CandidateListWriterPSV(de.ipbhalle.metfraglib.writer.CandidateListWriterPSV) MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) Settings(de.ipbhalle.metfraglib.settings.Settings)

Example 3 with CandidateListWriterCSV

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;
}
Also used : DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent) CandidateListWriterSDF(de.ipbhalle.metfraglib.writer.CandidateListWriterSDF) IWriter(de.ipbhalle.metfraglib.interfaces.IWriter) CandidateListWriterXLS(de.ipbhalle.metfraglib.writer.CandidateListWriterXLS) File(java.io.File) CandidateListWriterCSV(de.ipbhalle.metfraglib.writer.CandidateListWriterCSV) File(java.io.File) UploadedSuspectListFile(de.ipbhalle.metfragweb.datatype.UploadedSuspectListFile) DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent)

Aggregations

IWriter (de.ipbhalle.metfraglib.interfaces.IWriter)3 CandidateListWriterCSV (de.ipbhalle.metfraglib.writer.CandidateListWriterCSV)3 CandidateListWriterSDF (de.ipbhalle.metfraglib.writer.CandidateListWriterSDF)2 CandidateListWriterXLS (de.ipbhalle.metfraglib.writer.CandidateListWriterXLS)2 UploadedSuspectListFile (de.ipbhalle.metfragweb.datatype.UploadedSuspectListFile)2 File (java.io.File)2 DefaultStreamedContent (org.primefaces.model.DefaultStreamedContent)2 LocalCSVDatabase (de.ipbhalle.metfraglib.database.LocalCSVDatabase)1 LocalPSVDatabase (de.ipbhalle.metfraglib.database.LocalPSVDatabase)1 MultipleHeadersFoundInInputDatabaseException (de.ipbhalle.metfraglib.exceptions.MultipleHeadersFoundInInputDatabaseException)1 IDatabase (de.ipbhalle.metfraglib.interfaces.IDatabase)1 IPeak (de.ipbhalle.metfraglib.interfaces.IPeak)1 IPeakListReader (de.ipbhalle.metfraglib.interfaces.IPeakListReader)1 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)1 DefaultPeakList (de.ipbhalle.metfraglib.list.DefaultPeakList)1 SettingsChecker (de.ipbhalle.metfraglib.parameter.SettingsChecker)1 MetFragGlobalSettings (de.ipbhalle.metfraglib.settings.MetFragGlobalSettings)1 Settings (de.ipbhalle.metfraglib.settings.Settings)1 CandidateListWriterPSV (de.ipbhalle.metfraglib.writer.CandidateListWriterPSV)1