package com.github.sparkcaller;

import picard.vcf.MergeVcfs;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
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 ArrayList<String> possibleStringToArgs(String maybeString) {
        if (maybeString == null) {
            return null;
        }

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

@@ -37,111 +37,213 @@
 ort 
-picard.vcf.MergeVcfs;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;
+org.apache.avro.generic.GenericData;%0Aimport 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
 %0Aimp
@@ -254,27 +254,17 @@
 java.io.
-InputStream
+*
 ;%0Aimport
@@ -2508,32 +2508,1786 @@
 rn 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).sortBy(line -%3E Long.parseLong(line.split(%22%5Ct%22)%5B1%5D), true, 1);%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            int vcfFileSize = chromosomeVcfLines.size();%0A            for (int i = 0; i %3C vcfFileSize; i++) %7B%0A                newVcfFile.write(chromosomeVcfLines.get(i) + %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        return chromosomes.filter(rddChromosome -%3E rddChromosome._1().equals(chromosome)).values().flatMap(line -%3E line);%0A    %7D%0A%0A
     public stati

Add a utility function which can be used to split VCF files by chromosome.