<commit_before>/*
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * MZmine 2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */

package net.sf.mzmine.modules.peaklistmethods.io.csvexport;

import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.regex.Pattern;

import net.sf.mzmine.datamodel.Feature;
import net.sf.mzmine.datamodel.Feature.FeatureStatus;
import net.sf.mzmine.datamodel.PeakIdentity;
import net.sf.mzmine.datamodel.PeakList;
import net.sf.mzmine.datamodel.PeakListRow;
import net.sf.mzmine.datamodel.RawDataFile;
import net.sf.mzmine.parameters.ParameterSet;
import net.sf.mzmine.taskcontrol.AbstractTask;
import net.sf.mzmine.taskcontrol.TaskStatus;
import net.sf.mzmine.util.RangeUtils;

class CSVExportTask extends AbstractTask {

    private PeakList[] peakLists;
    private int processedRows = 0, totalRows = 0;

    // parameter values
    private File fileName;
    private String plNamePattern = "{}";
    private String fieldSeparator;
    private ExportRowCommonElement[] commonElements;
    private String[] identityElements;
    private ExportRowDataFileElement[] dataFileElements;
    private Boolean exportAllIDs;
    private String idSeparator;

    CSVExportTask(ParameterSet parameters) {

        this.peakLists = parameters.getParameter(CSVExportParameters.peakLists)
                .getValue().getMatchingPeakLists();
        fileName = parameters.getParameter(CSVExportParameters.filename)
                .getValue();
        fieldSeparator = parameters.getParameter(
                CSVExportParameters.fieldSeparator).getValue();
        commonElements = parameters.getParameter(
                CSVExportParameters.exportCommonItems).getValue();
        identityElements = parameters.getParameter(
                CSVExportParameters.exportIdentityItems).getValue();
        dataFileElements = parameters.getParameter(
                CSVExportParameters.exportDataFileItems).getValue();
        exportAllIDs = parameters.getParameter(
                CSVExportParameters.exportAllIDs).getValue();
        idSeparator = parameters.getParameter(
                CSVExportParameters.idSeparator).getValue();
    }

    public double getFinishedPercentage() {
        if (totalRows == 0) {
            return 0;
        }
        return (double) processedRows / (double) totalRows;
    }

    public String getTaskDescription() {
        return "Exporting peak list(s) " 
                + Arrays.toString(peakLists) + " to CSV file(s)";
    }

    public void run() {

        setStatus(TaskStatus.PROCESSING);

        // Shall export several files?
        boolean substitute = fileName.getPath().contains(plNamePattern);

        // Total number of rows
        for (PeakList peakList: peakLists) {
            totalRows += peakList.getNumberOfRows();
        }

        // Process peak lists
        for (PeakList peakList: peakLists) {

            // Filename
            File curFile = fileName;
            if (substitute) {
                curFile = new File(fileName.getPath().replaceAll(
                        Pattern.quote(plNamePattern), peakList.getName()));
            }

            // Open file
            FileWriter writer;
            try {
                writer = new FileWriter(curFile);
            } catch (Exception e) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Could not open file " + curFile 
                        + " for writing.");
                return;
            }

            exportPeakList(peakList, writer, curFile);

            // Close file
            try {
                writer.close();
            } catch (Exception e) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Could not close file " + curFile);
                return;
            }
        }

        if (getStatus() == TaskStatus.PROCESSING)
            setStatus(TaskStatus.FINISHED);

    }

    private void exportPeakList(PeakList peakList, FileWriter writer, 
            File fileName) {

        RawDataFile rawDataFiles[] = peakList.getRawDataFiles();

        // Buffer for writing
        StringBuffer line = new StringBuffer();

        // Write column headers
        // Common elements
        int length = commonElements.length;
        String name;
        for (int i = 0; i < length; i++) {
            name = commonElements[i].toString();
            name = name.replace("Export ", "");<commit_msg>CSVExportTask: fix - keep treating only one PL if no substitution pattern found<commit_after>/*
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * MZmine 2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */

package net.sf.mzmine.modules.peaklistmethods.io.csvexport;

import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.regex.Pattern;

import net.sf.mzmine.datamodel.Feature;
import net.sf.mzmine.datamodel.Feature.FeatureStatus;
import net.sf.mzmine.datamodel.PeakIdentity;
import net.sf.mzmine.datamodel.PeakList;
import net.sf.mzmine.datamodel.PeakListRow;
import net.sf.mzmine.datamodel.RawDataFile;
import net.sf.mzmine.parameters.ParameterSet;
import net.sf.mzmine.taskcontrol.AbstractTask;
import net.sf.mzmine.taskcontrol.TaskStatus;
import net.sf.mzmine.util.RangeUtils;

class CSVExportTask extends AbstractTask {

    private PeakList[] peakLists;
    private int processedRows = 0, totalRows = 0;

    // parameter values
    private File fileName;
    private String plNamePattern = "{}";
    private String fieldSeparator;
    private ExportRowCommonElement[] commonElements;
    private String[] identityElements;
    private ExportRowDataFileElement[] dataFileElements;
    private Boolean exportAllIDs;
    private String idSeparator;

    CSVExportTask(ParameterSet parameters) {

        this.peakLists = parameters.getParameter(CSVExportParameters.peakLists)
                .getValue().getMatchingPeakLists();
        fileName = parameters.getParameter(CSVExportParameters.filename)
                .getValue();
        fieldSeparator = parameters.getParameter(
                CSVExportParameters.fieldSeparator).getValue();
        commonElements = parameters.getParameter(
                CSVExportParameters.exportCommonItems).getValue();
        identityElements = parameters.getParameter(
                CSVExportParameters.exportIdentityItems).getValue();
        dataFileElements = parameters.getParameter(
                CSVExportParameters.exportDataFileItems).getValue();
        exportAllIDs = parameters.getParameter(
                CSVExportParameters.exportAllIDs).getValue();
        idSeparator = parameters.getParameter(
                CSVExportParameters.idSeparator).getValue();
    }

    public double getFinishedPercentage() {
        if (totalRows == 0) {
            return 0;
        }
        return (double) processedRows / (double) totalRows;
    }

    public String getTaskDescription() {
        return "Exporting peak list(s) " 
                + Arrays.toString(peakLists) + " to CSV file(s)";
    }

    public void run() {

        setStatus(TaskStatus.PROCESSING);

        // Shall export several files?
        boolean substitute = fileName.getPath().contains(plNamePattern);

        // Total number of rows
        for (PeakList peakList: peakLists) {
            totalRows += peakList.getNumberOfRows();
        }

        // Process peak lists
        for (PeakList peakList: peakLists) {

            // Filename
            File curFile = fileName;
            if (substitute) {
                curFile = new File(fileName.getPath().replaceAll(
                        Pattern.quote(plNamePattern), peakList.getName()));
            }

            // Open file
            FileWriter writer;
            try {
                writer = new FileWriter(curFile);
            } catch (Exception e) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Could not open file " + curFile 
                        + " for writing.");
                return;
            }

            exportPeakList(peakList, writer, curFile);

            // Close file
            try {
                writer.close();
            } catch (Exception e) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Could not close file " + curFile);
                return;
            }

            // If peak list substitution pattern wasn't found, 
            // treat one peak list only
            if (!substitute)
                break;
        }

        if (getStatus() == TaskStatus.PROCESSING)
            setStatus(TaskStatus.FINISHED);

    }

    private void exportPeakList(PeakList peakList, FileWriter writer, 
            File fileName) {

        RawDataFile rawDataFiles[] = peakList.getRawDataFiles();

        // Buffer for writing
        StringBuffer line = new StringBuffer();

        // Write column headers
        // Common elements
        int length = commonElements.length;
        String name;
        for (int i = 0; i < length; i++) {
            name = commonElements[i].toString();
            name = name.replace("Export ", "");
{'fraction_non_alphanumeric': 0.08517499762875842, 'fraction_numerical': 0.005690979797021721, 'mean_word_length': 2.337765115542893, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773631', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2667, 'n_words': 758}