Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/gff/VcfToGff.java @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 68:5028fdace37b |
---|---|
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.ReadStats; | |
11 import shared.Timer; | |
12 import shared.Tools; | |
13 import structures.ByteBuilder; | |
14 import var2.VCFLine; | |
15 | |
16 /** | |
17 * Stripped out of GffLine into independent class. | |
18 * @author Brian Bushnell | |
19 * @date Sep 12, 2018 | |
20 * | |
21 */ | |
22 public class VcfToGff { | |
23 | |
24 /** Translates VCF to GFF */ | |
25 public static void main(String[] args){ | |
26 Timer t=new Timer(); | |
27 PrintStream outstream=System.err; | |
28 {//Preparse block for help, config files, and outstream | |
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 //Parse each argument | |
41 for(int i=0; i<args.length; i++){ | |
42 String arg=args[i]; | |
43 | |
44 //Break arguments into their constituent parts, in the form of "a=b" | |
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 //do nothing | |
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 {//Process parser fields | |
66 overwrite=ReadStats.overwrite=parser.overwrite; | |
67 append=ReadStats.append=parser.append; | |
68 } | |
69 | |
70 //Ensure output files can be written | |
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 //Ensure input files can be read | |
77 if(!Tools.testInputFiles(false, true, in)){ | |
78 throw new RuntimeException("\nCan't read some input files.\n"); | |
79 } | |
80 | |
81 //Ensure that no file was specified multiple times | |
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 /** Translates VCF to GFF */ | |
91 private static void translate(String in, String out, boolean overwrite, boolean append){ | |
92 //Create output FileFormat objects | |
93 FileFormat ffout=FileFormat.testOutput(out, FileFormat.GFF, "gff", true, overwrite, append, false); | |
94 | |
95 //Create input FileFormat objects | |
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 //skip | |
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 } |