-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseGFF.java
More file actions
292 lines (277 loc) · 9.79 KB
/
Copy pathparseGFF.java
File metadata and controls
292 lines (277 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package scripts;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/***********************************************
* Input:
* 1. GFF file with Genbank (GB) identifiers
* 2. DBHitTable exported from Basic annoDB: species D. citri, nr, rank <=25
* TW sequence can only have one location, but multiple sequences can have the same location
* 3. Scaffold names -- mapping from the genome site of 'scaffoldxxx' to name used in GFF
*
* Output: Location file
*
* http://www.ncbi.nlm.nih.gov/genome/annotation_euk/Diaphorina_citri/100/
* This annotation should be referred to as NCBI Diaphorina citri Annotation Release 100
*/
public class parseGFF {
// input
static private String gffFile="blast/Dc_top_level.gff3";
static private String hitFile="blast/DBhitTable25.csv";
static private String scafFile="blast/scaffold_names";
// output
static private String locFile="blast/Locations.txt";
static private String scafCntFile="blast/Scaffold_counts.txt";
static private String multFile="blast/multiMap.txt";
static private PrintWriter multiWriter;
public static void main(String[] args) {
try {
multiWriter = new PrintWriter(new FileWriter(multFile));
readHit();
readScaffold();
readGFF();
findBest();
writeLocs();
System.out.println("+++ Done");
multiWriter.close();
}
catch ( Throwable err ) {err.printStackTrace();System.exit(-1);}
}
/*************************************************************
* Multiple twName can have the same gbName (transcripts of the same gene?)
* but twName can only have on gbName.
* For each twName, go through hits and find the one with the best evalue.
*/
private static void findBest() {
try {
System.out.println("+++ Find best mapping");
}
catch ( Throwable err ) {err.printStackTrace();System.exit(-1);}
}
/*******************************************************
* name supercontig_name:start-end(strand)
*/
private static void writeLocs() {
try {
System.out.println("+++ Write " + locFile);
PrintWriter writer = new PrintWriter(new FileWriter(locFile));
scafCntMap.clear();
for (String twName : twMap.keySet()) {
TW tw = twMap.get(twName);
GB gb = gbMap.get(tw.bestGB);
writer.format("%-10s %s:%d-%d(%s)\n", twName, gb.scaffold, gb.start, gb.end, gb.strand);
if (!scafCntMap.containsKey(gb.scaffold)) scafCntMap.put(gb.scaffold,1);
else scafCntMap.put(gb.scaffold, scafCntMap.get(gb.scaffold)+1);
}
writer.close();
System.out.println("+++ Write " + scafCntFile);
int cnt=1;
writer = new PrintWriter(new FileWriter(scafCntFile));
for (String key : scafCntMap.keySet())
writer.format("%4d. %15s %4d\n", cnt++, key, scafCntMap.get(key));
writer.close();
}
catch ( Throwable err ) {err.printStackTrace();System.exit(-1);}
}
/**********************************************
* NW_007377440.1 Gnomon CDS 70765 70945 . - 0
* ID=cds1;Parent=rna1;Dbxref=GeneID:103504972,Genbank:XP_008467534.1;
* Name=XP_008467534.1;gbkey=CDS;gene=LOC103504972;product=spondin-1-like;
* protein_id=XP_008467534.1
*/
static private void readGFF() {
System.out.println("+++Read " + gffFile);
File path = new File(gffFile);
if ( !(path.exists()) ) {
System.err.println(gffFile + " does not exist");
System.exit(-1);
}
try {
BufferedReader reader = new BufferedReader ( new FileReader ( gffFile ) );
String line;
int cntRead=0, cntProcess=0, cntNoGB=0, cntGB=0;
Pattern namePat = Pattern.compile("Genbank:(XP_.*);Name");
while ((line = reader.readLine()) != null) {
cntRead++;
String[] tok = line.split("\t");
if (tok == null || tok.length < 9) continue;
if (!tok[2].equals("CDS")) continue;
Matcher m = namePat.matcher(tok[8]);
if (!m.find()) continue;
cntProcess++; // have XP_
String gbName=m.group(1);
if (!gbMap.containsKey(gbName)) {
cntNoGB++;
continue;
}
GB gb = gbMap.get(gbName);
if (gb.scaffold=="") {
String sc="";
if (scafNameMap.containsKey(tok[0])) sc=scafNameMap.get(tok[0]);
else {
System.err.println("No " + tok[0] + " scaffold");
System.exit(0);
}
gb.scaffold = sc;
gb.start = Integer.parseInt(tok[3]);
gb.end = Integer.parseInt(tok[4]);
gb.strand = tok[6];
cntGB++;
if (!scafCntMap.containsKey(gb.scaffold)) scafCntMap.put(gb.scaffold,1);
else scafCntMap.put(gb.scaffold, scafCntMap.get(gb.scaffold)+1);
}
}
reader.close();
System.out.println(cntRead + " reads");
System.out.println(cntProcess + " processed");
System.out.println(cntNoGB + " no GB");
System.out.println(cntGB + " GB");
System.out.println(scafCntMap.size() + " scaffolds");
scafNameMap.clear();
}
catch ( Throwable err ) {err.printStackTrace();System.exit(-1);}
}
/*********************************************
* 1,DcAN_07268,NP_001284621.1,6.0E-120,Diaphorinacitri,1
* 2,DcAN_00699,XP_008472795.1,8.0E-55,Diaphorina citri,2
*/
static private void readHit() {
System.out.println("+++Read " + hitFile);
File path = new File(hitFile);
if ( !(path.exists()) ) {
System.err.println(hitFile + " does not exist");
System.exit(-1);
}
try {
BufferedReader reader = new BufferedReader ( new FileReader ( hitFile ) );
String line;
int cntRead=0, cntProcess=0, cntDupGB=0, cntDupTW=0;
while ((line = reader.readLine()) != null) {
cntRead++;
if ( line.length() == 0 || line.charAt(0) == '#' ) continue;
String[] tok = line.split(",");
if (tok == null || tok.length < 6) continue;
if (!tok[2].startsWith("XP_")) continue;
if (!tok[4].contains("Diaphorina citri")) continue;
cntProcess++;
String twName = tok[1];
String gbName = tok[2];
double eval = Double.parseDouble(tok[3]);
int rank = Integer.parseInt(tok[5]);
TW tw=null;
if (twMap.containsKey(twName)) {
tw = twMap.get(twName);
tw.add(gbName, eval, rank);
cntDupTW++;
}
else {
tw = new TW(twName, gbName, eval, rank);
twMap.put(twName, tw);
}
if (gbMap.containsKey(gbName)) {
GB gb = gbMap.get(gbName);
gb.add(tw);
cntDupGB++;
}
else {
GB gb = new GB(gbName, tw);
gbMap.put(gbName, gb);
}
}
reader.close();
System.out.println(cntRead + " reads");
System.out.println(cntProcess + " processed");
System.out.println(cntDupGB + " duplicate GB identifiers");
System.out.println(cntDupTW + " duplicate TW identifiers");
System.out.println(gbMap.size() + " unique GB identifiers");
System.out.println(twMap.size() + " unique TW identifiers");
}
catch ( Throwable err ) {err.printStackTrace();System.exit(-1);}
}
/***************************************************
* Diaci psyllid genome assembly version 1.1 scaffold1 NW_007377440.1 KI472552.1 GPS_004273110.1
*/
static private void readScaffold() {
System.out.println("+++Read " + scafFile);
File path = new File(scafFile);
if ( !(path.exists()) ) {
System.err.println(scafFile + " does not exist");
System.exit(-1);
}
try {
BufferedReader reader = new BufferedReader ( new FileReader ( scafFile ) );
String line;
int cntRead=0, cntProcess=0;
Pattern name1Pat = Pattern.compile("(scaffold\\d+)");
Pattern name2Pat = Pattern.compile("(NW_\\d+.\\d+)");
while ((line = reader.readLine()) != null) {
cntRead++;
if ( line.length() == 0 || line.charAt(0) == '#' ) continue;
Matcher m = name1Pat.matcher(line);
if (!m.find()) {
System.err.println(line);
System.exit(0);
}
String scaffold = m.group(1);
m = name2Pat.matcher(line);
if (!m.find()) {
System.err.println(line);
System.exit(0);
}
String gbScaff = m.group(1);
scafNameMap.put(gbScaff, scaffold);
cntProcess++;
}
reader.close();
System.out.println(cntRead + " reads");
System.out.println(cntProcess + " processed");
}
catch ( Throwable err ) {err.printStackTrace();System.exit(-1);}
}
static private class GB {
public GB (String n, TW t) {
name = n;
map.add(t);
}
public void add(TW t) {
map.add(t);
}
String name="", scaffold="", strand="";
int start, end;
ArrayList <TW> best = new ArrayList <TW> ();
ArrayList <TW> map = new ArrayList <TW> ();
}
static private class TW {
public TW (String n, String g, double d, int r) {
name = n;
gb.add(g);
rank.add(r);
eval.add(d);
bestGB=g; // may be changed if duplicates
bestEval=d;
}
public void add( String g, double d, int r) {
gb.add(g);
rank.add(r);
eval.add(d);
if (d<bestEval) {
multiWriter.print(name + " " + bestGB + " " + bestEval + " " + g + " " + d + "\n");
bestGB=g;
bestEval=d;
}
}
String name;
String bestGB;
double bestEval;
ArrayList <String> gb = new ArrayList <String> ();
ArrayList <Integer> rank = new ArrayList <Integer> ();
ArrayList <Double> eval = new ArrayList <Double> ();
}
static HashMap <String, GB> gbMap = new HashMap <String, GB> ();
static HashMap <String, TW> twMap = new HashMap <String, TW> ();
static TreeMap <String, Integer> scafCntMap = new TreeMap<String, Integer> ();
static HashMap <String, String> scafNameMap = new HashMap<String, String> ();
}