| 1 |
package gff; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.ByteStreamWriter; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import shared.Parser; |
| 9 |
import shared.PreParser; |
| 10 |
import shared.Timer; |
| 11 |
import shared.Tools; |
| 12 |
import structures.ByteBuilder; |
| 13 |
import tracker.ReadStats; |
| 14 |
import var2.VCFLine; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class VcfToGff { |
| 23 |
|
| 24 |
|
| 25 |
public static void main(String[] args){ |
| 26 |
Timer t=new Timer(); |
| 27 |
PrintStream outstream=System.err; |
| 28 |
{ |
| 29 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 30 |
args=pp.args; |
| 31 |
outstream=pp.outstream; |
| 32 |
t.outstream=outstream; |
| 33 |
} |
| 34 |
|
| 35 |
Parser parser=new Parser(); |
| 36 |
String in=null; |
| 37 |
String out=null; |
| 38 |
boolean overwrite=true, append=false; |
| 39 |
|
| 40 |
|
| 41 |
for(int i=0; i<args.length; i++){ |
| 42 |
String arg=args[i]; |
| 43 |
|
| 44 |
|
| 45 |
String[] split=arg.split("="); |
| 46 |
String a=split[0].toLowerCase(); |
| 47 |
String b=split.length>1 ? split[1] : null; |
| 48 |
|
| 49 |
if(a.equals("in") || a.equals("vcf")){ |
| 50 |
in=b; |
| 51 |
}else if(a.equals("out") || a.equals("gff")){ |
| 52 |
out=b; |
| 53 |
}else if(parser.parse(arg, a, b)){ |
| 54 |
|
| 55 |
}else if(in==null && b==null && i==0 && Tools.canRead(arg)){ |
| 56 |
in=arg; |
| 57 |
}else if(in==null && b==null && i==1){ |
| 58 |
out=arg; |
| 59 |
}else{ |
| 60 |
outstream.println("Unknown parameter "+args[i]); |
| 61 |
assert(false) : "Unknown parameter "+args[i]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
{ |
| 66 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 67 |
append=ReadStats.append=parser.append; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 72 |
outstream.println((out==null)+", "+out); |
| 73 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n"); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if(!Tools.testInputFiles(false, true, in)){ |
| 78 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if(!Tools.testForDuplicateFiles(true, in, out)){ |
| 83 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 84 |
} |
| 85 |
|
| 86 |
translate(in, out, overwrite, append); |
| 87 |
t.stop("Time: \t"); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
private static void translate(String in, String out, boolean overwrite, boolean append){ |
| 92 |
|
| 93 |
FileFormat ffout=FileFormat.testOutput(out, FileFormat.GFF, "gff", true, overwrite, append, false); |
| 94 |
|
| 95 |
|
| 96 |
FileFormat ffin=FileFormat.testInput(in, FileFormat.VCF, "vcf", true, true); |
| 97 |
|
| 98 |
ByteFile bf=ByteFile.makeByteFile(ffin); |
| 99 |
ByteStreamWriter bsw=null; |
| 100 |
if(ffout!=null){ |
| 101 |
bsw=new ByteStreamWriter(ffout); |
| 102 |
bsw.start(); |
| 103 |
} |
| 104 |
|
| 105 |
ByteBuilder bb=new ByteBuilder(17000); |
| 106 |
bb.append("##gff-version 3\n"); |
| 107 |
String header="#seqid source type start end score strand phase attributes"; |
| 108 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 109 |
if(line.length>1){ |
| 110 |
if(line[0]=='#'){ |
| 111 |
if(Tools.startsWith(line, "##fileformat") || Tools.startsWith(line, "##FORMAT") || |
| 112 |
Tools.startsWith(line, "##INFO") || Tools.startsWith(line, "#CHROM POS")){ |
| 113 |
|
| 114 |
}else{ |
| 115 |
int i=1; |
| 116 |
while(i<line.length && line[i]=='#'){i++;} |
| 117 |
i--; |
| 118 |
bb.append(line, i, line.length-i); |
| 119 |
bb.nl(); |
| 120 |
} |
| 121 |
}else{ |
| 122 |
if(header!=null){ |
| 123 |
bb.append(header).append('\n'); |
| 124 |
header=null; |
| 125 |
} |
| 126 |
VCFLine vline=new VCFLine(line); |
| 127 |
GffLine gline=new GffLine(vline); |
| 128 |
gline.appendTo(bb); |
| 129 |
bb.nl(); |
| 130 |
} |
| 131 |
} |
| 132 |
if(bb.length()>=16384){ |
| 133 |
if(bsw!=null){ |
| 134 |
bsw.print(bb); |
| 135 |
} |
| 136 |
bb.clear(); |
| 137 |
} |
| 138 |
} |
| 139 |
if(bb.length()>0){ |
| 140 |
if(bsw!=null){ |
| 141 |
bsw.print(bb); |
| 142 |
} |
| 143 |
bb.clear(); |
| 144 |
} |
| 145 |
bf.close(); |
| 146 |
if(bsw!=null){bsw.poisonAndWait();} |
| 147 |
} |
| 148 |
|
| 149 |
} |