<commit_before>package org.bbop.apollo

import org.bbop.apollo.sequence.Strand
import org.codehaus.groovy.grails.web.json.JSONObject
import grails.converters.JSON
import org.bbop.apollo.gwt.shared.FeatureStringEnum
import java.io.*;
import java.util.*;
import java.util.zip.GZIPOutputStream;
//import groovy.transform.CompileStatic
//import grails.compiler.GrailsCompileStatic


//@CompileStatic
//@GrailsCompileStatic
public class FastaHandlerService {

    private File file;
    private PrintWriter out;
    private Mode mode;
    private int numResiduesPerLine = 60;

    def sequenceService
    def transcriptService
    def featurePropertyService

    public enum Mode {
        READ,
        WRITE
    }
    
    public enum Format {
        TEXT,
        GZIP
    }
    
//    public FastaHandler(String path, Mode mode) throws IOException {
//        this(path, mode, Format.TEXT);
//    }
//
//    public FastaHandlerService(String path, Mode mode, Format format) throws IOException {
//        numResiduesPerLine = 60;
//        this.mode = mode;
//        file = new File(path);
//        file.createNewFile();
//        if (mode == Mode.READ) {
//            if (!file.canRead()) {
//                throw new IOException("Cannot read FASTA file: " + file.getAbsolutePath());
//            }
//        }
//        if (mode == Mode.WRITE) {
//            if (!file.canWrite()) {
//                throw new IOException("Cannot write FATA to: " + file.getAbsolutePath());
//            }
//            switch (format) {
//            case Format.TEXT:
//                out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
//                break;
//            case Format.GZIP:
//                out = new PrintWriter(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))));
//            }
//        }
//    }
    
    public FastaHandlerService() {
    }
    
    public void close() {
        if (mode == Mode.READ) {
            //TODO
        }
        else if (mode == Mode.WRITE) { 
            out.close();
        }
    }
    
    public void writeFeatures(Collection<Feature> features, String seqType, Set<String> metaDataToExport, String path, Mode mode, Format format,String region) throws IOException {
        this.mode = mode
        file = new File(path)
        file.createNewFile()
        if(mode == Mode.READ) {
            if (!file.canRead()) {
                throw new IOException("Cannot read FASTA file: " + file.getAbsolutePath())
            }
        }
        if(mode == Mode.WRITE) {
            if(!file.canWrite()) {
                throw new IOException("Cannot write FASTA to file: " + file.getAbsolutePath())
            }
            switch(format) {
                case Format.TEXT:
                    out = new PrintWriter(new BufferedWriter(new FileWriter(file)))
                    break
                case Format.GZIP:<commit_msg>fixed integration test<commit_after>package org.bbop.apollo

import org.bbop.apollo.sequence.Strand
import org.codehaus.groovy.grails.web.json.JSONObject
import grails.converters.JSON
import org.bbop.apollo.gwt.shared.FeatureStringEnum
import java.io.*;
import java.util.*;
import java.util.zip.GZIPOutputStream;
//import groovy.transform.CompileStatic
//import grails.compiler.GrailsCompileStatic


//@CompileStatic
//@GrailsCompileStatic
public class FastaHandlerService {

    private File file;
    private PrintWriter out;
    private Mode mode;
    private int numResiduesPerLine = 60;

    def sequenceService
    def transcriptService
    def featurePropertyService

    public enum Mode {
        READ,
        WRITE
    }
    
    public enum Format {
        TEXT,
        GZIP
    }
    
//    public FastaHandler(String path, Mode mode) throws IOException {
//        this(path, mode, Format.TEXT);
//    }
//
//    public FastaHandlerService(String path, Mode mode, Format format) throws IOException {
//        numResiduesPerLine = 60;
//        this.mode = mode;
//        file = new File(path);
//        file.createNewFile();
//        if (mode == Mode.READ) {
//            if (!file.canRead()) {
//                throw new IOException("Cannot read FASTA file: " + file.getAbsolutePath());
//            }
//        }
//        if (mode == Mode.WRITE) {
//            if (!file.canWrite()) {
//                throw new IOException("Cannot write FATA to: " + file.getAbsolutePath());
//            }
//            switch (format) {
//            case Format.TEXT:
//                out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
//                break;
//            case Format.GZIP:
//                out = new PrintWriter(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))));
//            }
//        }
//    }
    
    public FastaHandlerService() {
    }
    
    public void close() {
        if (mode == Mode.READ) {
            //TODO
        }
        else if (mode == Mode.WRITE) { 
            out.close();
        }
    }
    
    public void writeFeatures(Collection<Feature> features, String seqType, Set<String> metaDataToExport, String path, Mode mode, Format format,String region = null ) throws IOException {
        this.mode = mode
        file = new File(path)
        file.createNewFile()
        if(mode == Mode.READ) {
            if (!file.canRead()) {
                throw new IOException("Cannot read FASTA file: " + file.getAbsolutePath())
            }
        }
        if(mode == Mode.WRITE) {
            if(!file.canWrite()) {
                throw new IOException("Cannot write FASTA to file: " + file.getAbsolutePath())
            }
            switch(format) {
                case Format.TEXT:
                    out = new PrintWriter(new BufferedWriter(new FileWriter(file)))
                    break
                case Format.GZIP:
{'fraction_non_alphanumeric': 0.1131206278792015, 'fraction_numerical': 0.001364954785872718, 'mean_word_length': 1.8373668925459825, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136944', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1582, 'n_words': 388}