package com.github.sparkcaller;

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import picard.vcf.MergeVcfs;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class Utils {
    /*
    Get all the files in the folder 'pathToFolder' ending with 'fileExtension', and return them as an ArrayList.
    */
    public static ArrayList<File> getFilesInFolder(String pathToFolder, String fileExtension) {
        File folder = new File(pathToFolder);
        File[] listOfFiles = folder.listFiles();

        if (listOfFiles != null) {
            ArrayList<File> bamFiles = new ArrayList<File>();

            for (File file : listOfFiles) {
                if (file.isFile() && file.getName().endsWith(fileExtension)) {
                    bamFiles.add(file);
                }
            }
            return bamFiles;
        }

        System.err.println(String.format("The folder: %s is either empty or was not found!", folder.getName()));
        return null;
    }

    public static String removeExtenstion(String filepath, String ext) {
        return filepath.substring(0, filepath.length() - (ext.length()+1));
    }

    /*
    Merges all the VCF files specified in the list 'vcfFiles' and returns the filename of the output file.
    */
    public static String mergeVCFFiles(List<File> vcfFiles, String outputFileName) {
        MergeVcfs mergeEngine = new MergeVcfs();
        mergeEngine.INPUT = vcfFiles;

        // Only pass the output file as an argument,
        // as it is more efficient to set the input files directly in the object.
        String outputArgs[] = {"O=" + outputFileName};

        mergeEngine.instanceMain(outputArgs);
        return outputFileName;
    }

    public static Properties loadConfigFile(String configFilepath) {
        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream(configFilepath);
            prop.load(input);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return prop;
    }

    public static List<File> splitVcf(JavaSparkContext sparkContext, String pathToInputVcf) throws IOException {
        JavaRDD<String> vcfFile = sparkContext.textFile(pathToInputVcf);
        JavaRDD<String> vcfHeader = vcfFile.filter(line -> line.startsWith("#"));
        JavaRDD<String> vcfLines = vcfFile.subtract(vcfHeader);


        // Get the first word in the line, as this maps to the chromosome.
        JavaPairRDD<String, Iterable<String>> vcfChromosomesRDD = vcfLines.groupBy(line -> line.split("\t", 0)[0]);
        List<String> chromosomes = vcfChromosomesRDD.keys().distinct().collect();
        List<File> vcfFilesByChromosome = new ArrayList<>();

        for (String chromosome: chromosomes) {
            JavaRDD<String> chromosomeRDD = getRddByChromosome(vcfChromosomesRDD, chromosome);

            // Sort the variants by position.
            chromosomeRDD = chromosomeRDD.sortBy(line -> Long.parseLong(line.split("\t")[1]), true, 1);

            // Make sure that each file has a header.
            List<String> chromosomeVcfLines = vcfHeader.union(chromosomeRDD).collect();

            File chromosomeOutputFile = new File(Utils.removeExtenstion(pathToInputVcf, "vcf") + "-" + chromosome + ".vcf");
            FileWriter newVcfFile = new FileWriter(chromosomeOutputFile);

            for (String line : chromosomeVcfLines) {
                newVcfFile.write(line + "\n");
            }

            newVcfFile.close();
            vcfFilesByChromosome.add(chromosomeOutputFile);
        }

        return vcfFilesByChromosome;
    }

    public static JavaRDD<String> getRddByChromosome(JavaPairRDD<String, Iterable<String>> chromosomes, String chromosome) {
        // Get the RDD which has the desired 'chromosome' as the key,
        // then get all variants connected to this chromosome and return it as a single RDD.
        return chromosomes.filter(rddChromosome -> rddChromosome._1().equals(chromosome)).values().flatMap(line -> line);
    }

    public static File moveToDir(File fileToCopy, String targetPath) {
        File outputFile = new File(targetPath, fileToCopy.getName());
        fileToCopy.renameTo(outputFile);

        return outputFile;
    }

    public static ArrayList<String> possibleStringToArgs(String maybeString) {
        if (maybeString == null) {
            return null;
        }

        String[] args = maybeString.split("\\s+");
        return new ArrayList<>(Arrays.asList(args));
    }
}

@@ -30,177 +30,8 @@
 r;%0A%0A
-import org.apache.spark.api.java.JavaPairRDD;%0Aimport org.apache.spark.api.java.JavaRDD;%0Aimport org.apache.spark.api.java.JavaSparkContext;%0Aimport picard.vcf.MergeVcfs;%0A%0A
 impo
@@ -101,31 +101,8 @@
 ys;%0A
-import java.util.List;%0A
 impo
@@ -1105,3131 +1105,577 @@
     
-/*%0A    Merges all the VCF files specified in the list 'vcfFiles' and returns the filename of the output file.%0A    */%0A    public static String mergeVCFFiles(List%3CFile%3E vcfFiles, String outputFileName) %7B%0A        MergeVcfs mergeEngine = new MergeVcfs();%0A        mergeEngine.INPUT = vcfFiles;%0A%0A        // Only pass the output file as an argument,%0A        // as it is more efficient to set the input files directly in the object.%0A        String outputArgs%5B%5D = %7B%22O=%22 + outputFileName%7D;%0A%0A        mergeEngine.instanceMain(outputArgs);%0A        return outputFileName;%0A    %7D%0A%0A    public static Properties loadConfigFile(String configFilepath) %7B%0A        Properties prop = new Properties();%0A        InputStream input = null;%0A%0A        try %7B%0A            input = new FileInputStream(configFilepath);%0A            prop.load(input);%0A        %7D catch (IOException ex) %7B%0A            ex.printStackTrace();%0A        %7D finally %7B%0A            if (input != null) %7B%0A                try %7B%0A                    input.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D%0A%0A        return prop;%0A    %7D%0A%0A    public static List%3CFile%3E splitVcf(JavaSparkContext sparkContext, String pathToInputVcf) throws IOException %7B%0A        JavaRDD%3CString%3E vcfFile = sparkContext.textFile(pathToInputVcf);%0A        JavaRDD%3CString%3E vcfHeader = vcfFile.filter(line -%3E line.startsWith(%22#%22));%0A        JavaRDD%3CString%3E vcfLines = vcfFile.subtract(vcfHeader);%0A%0A%0A        // Get the first word in the line, as this maps to the chromosome.%0A        JavaPairRDD%3CString, Iterable%3CString%3E%3E vcfChromosomesRDD = vcfLines.groupBy(line -%3E line.split(%22%5Ct%22, 0)%5B0%5D);%0A        List%3CString%3E chromosomes = vcfChromosomesRDD.keys().distinct().collect();%0A        List%3CFile%3E vcfFilesByChromosome = new ArrayList%3C%3E();%0A%0A        for (String chromosome: chromosomes) %7B%0A            JavaRDD%3CString%3E chromosomeRDD = getRddByChromosome(vcfChromosomesRDD, chromosome);%0A%0A            // Sort the variants by position.%0A            chromosomeRDD = chromosomeRDD.sortBy(line -%3E Long.parseLong(line.split(%22%5Ct%22)%5B1%5D), true, 1);%0A%0A            // Make sure that each file has a header.%0A            List%3CString%3E chromosomeVcfLines = vcfHeader.union(chromosomeRDD).collect();%0A%0A            File chromosomeOutputFile = new File(Utils.removeExtenstion(pathToInputVcf, %22vcf%22) + %22-%22 + chromosome + %22.vcf%22);%0A            FileWriter newVcfFile = new FileWriter(chromosomeOutputFile);%0A%0A            for (String line : chromosomeVcfLines) %7B%0A                newVcfFile.write(line + %22%5Cn%22);%0A            %7D%0A%0A            newVcfFile.close();%0A            vcfFilesByChromosome.add(chromosomeOutputFile);%0A        %7D%0A%0A        return vcfFilesByChromosome;%0A    %7D%0A%0A    public static JavaRDD%3CString%3E getRddByChromosome(JavaPairRDD%3CString, Iterable%3CString%3E%3E chromosomes, String chromosome) %7B%0A        // Get the RDD which has the desired 'chromosome' as the key,%0A        // then get all variants connected to this chromosome and return it as a single RDD.%0A        return chromosomes.filter(rddChromosome -%3E rddChromosome._1().equals(chromosome)).values().flatMap(line -%3E line)
+public static Properties loadConfigFile(String configFilepath) %7B%0A        Properties prop = new Properties();%0A        InputStream input = null;%0A%0A        try %7B%0A            input = new FileInputStream(configFilepath);%0A            prop.load(input);%0A        %7D catch (IOException ex) %7B%0A            ex.printStackTrace();%0A        %7D finally %7B%0A            if (input != null) %7B%0A                try %7B%0A                    input.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D%0A%0A        return prop
 ;%0A  

Remove the function used to merge and split VCF files, as they are no longer needed.