jpayne@68: package gff; jpayne@68: jpayne@68: import java.io.PrintStream; jpayne@68: jpayne@68: import fileIO.ByteFile; jpayne@68: import fileIO.ByteStreamWriter; jpayne@68: import fileIO.FileFormat; jpayne@68: import shared.Parser; jpayne@68: import shared.PreParser; jpayne@68: import shared.ReadStats; jpayne@68: import shared.Timer; jpayne@68: import shared.Tools; jpayne@68: import structures.ByteBuilder; jpayne@68: import var2.VCFLine; jpayne@68: jpayne@68: /** jpayne@68: * Stripped out of GffLine into independent class. jpayne@68: * @author Brian Bushnell jpayne@68: * @date Sep 12, 2018 jpayne@68: * jpayne@68: */ jpayne@68: public class VcfToGff { jpayne@68: jpayne@68: /** Translates VCF to GFF */ jpayne@68: public static void main(String[] args){ jpayne@68: Timer t=new Timer(); jpayne@68: PrintStream outstream=System.err; jpayne@68: {//Preparse block for help, config files, and outstream jpayne@68: PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); jpayne@68: args=pp.args; jpayne@68: outstream=pp.outstream; jpayne@68: t.outstream=outstream; jpayne@68: } jpayne@68: jpayne@68: Parser parser=new Parser(); jpayne@68: String in=null; jpayne@68: String out=null; jpayne@68: boolean overwrite=true, append=false; jpayne@68: jpayne@68: //Parse each argument jpayne@68: for(int i=0; i1 ? split[1] : null; jpayne@68: jpayne@68: if(a.equals("in") || a.equals("vcf")){ jpayne@68: in=b; jpayne@68: }else if(a.equals("out") || a.equals("gff")){ jpayne@68: out=b; jpayne@68: }else if(parser.parse(arg, a, b)){ jpayne@68: //do nothing jpayne@68: }else if(in==null && b==null && i==0 && Tools.canRead(arg)){ jpayne@68: in=arg; jpayne@68: }else if(in==null && b==null && i==1){ jpayne@68: out=arg; jpayne@68: }else{ jpayne@68: outstream.println("Unknown parameter "+args[i]); jpayne@68: assert(false) : "Unknown parameter "+args[i]; jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: {//Process parser fields jpayne@68: overwrite=ReadStats.overwrite=parser.overwrite; jpayne@68: append=ReadStats.append=parser.append; jpayne@68: } jpayne@68: jpayne@68: //Ensure output files can be written jpayne@68: if(!Tools.testOutputFiles(overwrite, append, false, out)){ jpayne@68: outstream.println((out==null)+", "+out); jpayne@68: throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n"); jpayne@68: } jpayne@68: jpayne@68: //Ensure input files can be read jpayne@68: if(!Tools.testInputFiles(false, true, in)){ jpayne@68: throw new RuntimeException("\nCan't read some input files.\n"); jpayne@68: } jpayne@68: jpayne@68: //Ensure that no file was specified multiple times jpayne@68: if(!Tools.testForDuplicateFiles(true, in, out)){ jpayne@68: throw new RuntimeException("\nSome file names were specified multiple times.\n"); jpayne@68: } jpayne@68: jpayne@68: translate(in, out, overwrite, append); jpayne@68: t.stop("Time: \t"); jpayne@68: } jpayne@68: jpayne@68: /** Translates VCF to GFF */ jpayne@68: private static void translate(String in, String out, boolean overwrite, boolean append){ jpayne@68: //Create output FileFormat objects jpayne@68: FileFormat ffout=FileFormat.testOutput(out, FileFormat.GFF, "gff", true, overwrite, append, false); jpayne@68: jpayne@68: //Create input FileFormat objects jpayne@68: FileFormat ffin=FileFormat.testInput(in, FileFormat.VCF, "vcf", true, true); jpayne@68: jpayne@68: ByteFile bf=ByteFile.makeByteFile(ffin); jpayne@68: ByteStreamWriter bsw=null; jpayne@68: if(ffout!=null){ jpayne@68: bsw=new ByteStreamWriter(ffout); jpayne@68: bsw.start(); jpayne@68: } jpayne@68: jpayne@68: ByteBuilder bb=new ByteBuilder(17000); jpayne@68: bb.append("##gff-version 3\n"); jpayne@68: String header="#seqid source type start end score strand phase attributes"; jpayne@68: for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ jpayne@68: if(line.length>1){ jpayne@68: if(line[0]=='#'){ jpayne@68: if(Tools.startsWith(line, "##fileformat") || Tools.startsWith(line, "##FORMAT") || jpayne@68: Tools.startsWith(line, "##INFO") || Tools.startsWith(line, "#CHROM POS")){ jpayne@68: //skip jpayne@68: }else{ jpayne@68: int i=1; jpayne@68: while(i=16384){ jpayne@68: if(bsw!=null){ jpayne@68: bsw.print(bb); jpayne@68: } jpayne@68: bb.clear(); jpayne@68: } jpayne@68: } jpayne@68: if(bb.length()>0){ jpayne@68: if(bsw!=null){ jpayne@68: bsw.print(bb); jpayne@68: } jpayne@68: bb.clear(); jpayne@68: } jpayne@68: bf.close(); jpayne@68: if(bsw!=null){bsw.poisonAndWait();} jpayne@68: } jpayne@68: jpayne@68: }