Search in sources :

Example 1 with PeakListSaveHandler

use of net.sf.mzmine.modules.projectmethods.projectsave.PeakListSaveHandler in project mzmine2 by mzmine.

the class XMLExportTask method run.

/**
 * @see java.lang.Runnable#run()
 */
public void run() {
    setStatus(TaskStatus.PROCESSING);
    // Shall export several files?
    boolean substitute = fileName.getPath().contains(plNamePattern);
    // Process feature lists
    for (int i = 0; i < peakLists.length; i++) {
        PeakList peakList = peakLists[i];
        File curFile = fileName;
        try {
            // Filename
            if (substitute) {
                // Cleanup from illegal filename characters
                String cleanPlName = peakList.getName().replaceAll("[^a-zA-Z0-9.-]", "_");
                // Substitute
                String newFilename = fileName.getPath().replaceAll(Pattern.quote(plNamePattern), cleanPlName);
                curFile = new File(newFilename);
            }
            // Open file
            FileWriter writer;
            try {
                writer = new FileWriter(curFile);
            } catch (Exception e) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Could not open file " + curFile + " for writing.");
                return;
            }
            logger.info("Started saving feature list " + peakList.getName());
            // write the saving file
            FileOutputStream fos = new FileOutputStream(curFile);
            OutputStream finalStream = fos;
            if (compression) {
                @SuppressWarnings("resource") ZipOutputStream zos = new ZipOutputStream(fos);
                zos.setLevel(9);
                zos.putNextEntry(new ZipEntry(fileName.getName()));
                finalStream = zos;
            }
            Hashtable<RawDataFile, String> dataFilesIDMap = new Hashtable<RawDataFile, String>();
            for (RawDataFile file : peakList.getRawDataFiles()) {
                dataFilesIDMap.put(file, file.getName());
            }
            PeakListSaveHandler peakListSaveHandler = new PeakListSaveHandler(finalStream, dataFilesIDMap);
            peakListSaveHandlers[i] = peakListSaveHandler;
            peakListSaveHandler.savePeakList(peakList);
            finalStream.close();
        } catch (Exception e) {
            /* we may already have set the status to CANCELED */
            if (getStatus() == TaskStatus.PROCESSING) {
                setStatus(TaskStatus.ERROR);
            }
            setErrorMessage(e.toString());
            e.printStackTrace();
            return;
        }
        logger.info("Finished saving " + peakList.getName());
        setStatus(TaskStatus.FINISHED);
    }
}
Also used : Hashtable(java.util.Hashtable) FileWriter(java.io.FileWriter) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) PeakListSaveHandler(net.sf.mzmine.modules.projectmethods.projectsave.PeakListSaveHandler) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) PeakList(net.sf.mzmine.datamodel.PeakList) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 OutputStream (java.io.OutputStream)1 Hashtable (java.util.Hashtable)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 PeakList (net.sf.mzmine.datamodel.PeakList)1 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)1 PeakListSaveHandler (net.sf.mzmine.modules.projectmethods.projectsave.PeakListSaveHandler)1