-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodons.java
489 lines (430 loc) · 15.4 KB
/
Codons.java
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import java.io.*;
import java.util.*;
public class Codons{
private Hashtable<String, Character> codon2amino;
private Hashtable<Character, UsageCounter> amino2UsageCounter;
private SubstitutionMatrix sm;
private int[] atcgNonCoding = new int[5]; //noncoding = all - coding;
private int[] atcgCoding = new int[5]; /* 0:A, 1:T, 2:C, 3:G, 4:X */
private int[] atcgALL = new int[5];
public SubstitutionMatrix getSM(){
return this.sm;
}
/* This calculates codon usage and */
public static void main(String[] args){
if(args.length >= 4){
String[] ptts = new String[args.length-3];
for(int i=3;i<args.length;i++){
ptts[i-3] = args[i];
}
new Codons(args[0], args[2]).runCodonUsageCalculator(args[1], ptts);
}else{
System.err.println("USAGE: java Codons <codon file> <multiFasta> <substitutionMatrix> <ptt.1> ... <ptt.n> (AT LEAST 1 .ptt file requires)");
}
}
private void runCodonUsageCalculator(String fasta, String[] ptts){
this.calculateCodonUsage(new FastaReader().parseFasta(fasta), new PttParser().parseAll(ptts));
}
/*
* codonfile is a tab-delimited file where 1st column is codon triplet in UPPER characters
* and 2nd column is a amino acid symbol in UPPER character.
* symbol, * is used to denote a STOP codon
*
*/
public Codons(String codonfile, String smFile){
this.sm = new SubstitutionMatrixParser().parse(smFile);
this.codon2amino = new Hashtable<String, Character>();
this.amino2UsageCounter = new Hashtable<Character, UsageCounter>();
this.loadHash(codonfile);
}
private void loadHash(String codonfile){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(codonfile));
String curLine = "";
while((curLine=br.readLine())!=null){
String[] tokens = curLine.split("\\t"); /* tokens[0] --> codon, tokens[1] --> amino */
Character curChar = new Character(tokens[1].charAt(0));
this.codon2amino.put(tokens[0], curChar);
UsageCounter cur = this.amino2UsageCounter.get(curChar);
if(cur == null){
// System.out.println("Here");
this.amino2UsageCounter.put(curChar, new UsageCounter(tokens[0]));
}else{
//System.out.println("there");
this.amino2UsageCounter.get(curChar).addCodon(tokens[0]);
}
}
br.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public boolean isConservative(SubstitutionMatrix sm, int threshold, boolean greater, char origAmino, char aAmino){
int dist = sm.getDistance(origAmino, aAmino);
if(greater){
if(dist < threshold)
return false;
}else{
if(dist > threshold)
return false;
}
return true;
}
public void calculateCodonUsage(Hashtable<String, Seq> seqHash, Hashtable<String, ArrayList<Gene>> geneHash){
Enumeration<String> keys = seqHash.keys(); /* get names for all contigs */
ArrayList<String> names = new ArrayList<String>(); /* Contig Names */
int syn=0;
int nonsyn=0;
int cons=0;
int noncons=0;
int total=0;
int nonsense = 0;
int other = 0;
while(keys.hasMoreElements())
names.add(keys.nextElement());
/* for each contig */
for(int i=0; i<names.size();i++){
Seq curSeq = seqHash.get(names.get(i));
ArrayList<Gene> genes = geneHash.get(names.get(i));
if(genes != null){
this.atcgALL = Seq.getATCGXArray(curSeq.getSeq().toString(), this.atcgALL);
/* for each gene in contig i */
for(int j=0; j<genes.size();j++){
//System.err.print("Processing:\t" + genes.get(j).getGName());
String geneSeq = genes.get(j).getGeneSequence(curSeq);
if(genes.get(j).getGName().equals("prfB")){
if(names.get(i).equals("ecoli")){
String tmp1 = new Gene("ecoli", 3033206, 3034228, false, "prfB").getGeneSequence(curSeq);
String tmp2 = new Gene("ecoli", 3034230, 3034304, false, "prfB").getGeneSequence(curSeq);
geneSeq = tmp2 + tmp1;
}else if(names.get(i).equals("K12MG1655_Chr1")){
String tmp1 = new Gene("K12MG1655_Chr1", 3033206, 3034228, false, "prfB").getGeneSequence(curSeq);
String tmp2 = new Gene("K12MG1655_Chr1", 3034230, 3034304, false, "prfB").getGeneSequence(curSeq);
geneSeq = tmp2 + tmp1;
}
else if(names.get(i).equals("IAI1_Chr1")){
//3094283-3094357
//3094281-3093259
String tmp1 = new Gene("IAI1_Chr1", 3093259, 3094281, false, "prfB").getGeneSequence(curSeq);
String tmp2 = new Gene("IAI1_Chr1", 3094283, 3094357, false, "prfB").getGeneSequence(curSeq);
geneSeq = tmp2 + tmp1;
}else if(names.get(i).equals("ED1a_Chr1")){
//3301720-3301794
//3300696-3301718
String tmp1 = new Gene("ED1a_Chr1", 3300696, 3301718, false, "prfB").getGeneSequence(curSeq);
String tmp2 = new Gene("ED1a_Chr1", 3301720, 3301794, false, "prfB").getGeneSequence(curSeq);
geneSeq = tmp2 + tmp1;
}
}
//3033206..3034228,3034230..3034304
//System.err.println("\t" + geneSeq.length());
this.atcgCoding = Seq.getATCGXArray(geneSeq, this.atcgCoding);
for(int k=0;k<geneSeq.length();k=k+3){
//total=total+3;
//System.err.println("\t" + geneSeq.length());
String curCodon = geneSeq.substring(k,k+3).toUpperCase();
char curAmino = this.toAmino(curCodon);
this.amino2UsageCounter.get(new Character(curAmino)).update(curCodon);
for(int l=0;l<3;l++){
/* current base in reference is curCodon.charAt(l) */
char[] bases = getOthers(curCodon.charAt(l));
for(int m=0;m<bases.length;m++){
total++;
char preAmino = this.toAmino(curCodon);
char afterAmino = this.toAmino(makeAlternate(curCodon, bases[m],l));
//System.out.println(curCodon + "\t" + makeAlternate(curCodon, bases[m], l));
if(preAmino != '*' && afterAmino == '*'){ // if nonsense mutations
nonsense++;
}else{ // else
other++;
}
if(preAmino == afterAmino ){
syn++;
int mutType = calcMutType(curCodon.charAt(l), bases[m]);
this.updateSynConSyn(mutType, true);
}else{
//System.out.println(".");
nonsyn++;
if(Character.isUpperCase(curCodon.charAt(l)) && Character.isUpperCase(bases[m]))
;
else
System.out.println("#####################\n#####################\n###################");
int mutType = calcMutType(curCodon.charAt(l), bases[m]);
this.updateSynConSyn(mutType, false);
if(isConservative(this.sm, 0, true, preAmino, afterAmino)){
cons++;
this.updateWithMutType(mutType, true);//spectrumForConservative[mutType]++;
}else{
noncons++;
this.updateWithMutType(mutType, false);//spectrumForNonCons[mutType]++;
}
}
//moving this block inside nonsyn block to just count for nonsyn changes 7/6/12
/*
if(isConservative(this.sm, 0, true, preAmino, afterAmino))
cons++;
else
noncons++;
*/
}
}
}
}
}
}
System.out.println("#Syn\t#NonSyn\t#Cons\t#NonCons\t#Total");
System.out.println(syn + "\t" + nonsyn + "\t" +cons + "\t" + noncons + "\t" + total);
System.out.println("\n#nonsense\t#others");
System.out.println(nonsense + "\t" + other);
System.out.println("Break down for Syns");
System.out.println("AT > GC : \t" + this.spectrumForSyn[0]);
System.out.println("GC > AT : \t" + this.spectrumForSyn[1]);
System.out.println("AT > TA : \t" + this.spectrumForSyn[2]);
System.out.println("GC > TA : \t" + this.spectrumForSyn[3]);
System.out.println("AT > CG : \t" + this.spectrumForSyn[4]);
System.out.println("GC > CG : \t" + this.spectrumForSyn[5]);
System.out.println("Break down for non-syn");
System.out.println("AT > GC : \t" + this.spectrumForNonSyn[0]);
System.out.println("GC > AT : \t" + this.spectrumForNonSyn[1]);
System.out.println("AT > TA : \t" + this.spectrumForNonSyn[2]);
System.out.println("GC > TA : \t" + this.spectrumForNonSyn[3]);
System.out.println("AT > CG : \t" + this.spectrumForNonSyn[4]);
System.out.println("GC > CG : \t" + this.spectrumForNonSyn[5]);
System.out.println("Break down for conservatives");
System.out.println("AT > GC : \t" + this.spectrumForConservative[0]);
System.out.println("GC > AT : \t" + this.spectrumForConservative[1]);
System.out.println("AT > TA : \t" + this.spectrumForConservative[2]);
System.out.println("GC > TA : \t" + this.spectrumForConservative[3]);
System.out.println("AT > CG : \t" + this.spectrumForConservative[4]);
System.out.println("GC > CG : \t" + this.spectrumForConservative[5]);
System.out.println("Break down for non-conservative");
System.out.println("AT > GC : \t" + this.spectrumForNonCons[0]);
System.out.println("GC > AT : \t" + this.spectrumForNonCons[1]);
System.out.println("AT > TA : \t" + this.spectrumForNonCons[2]);
System.out.println("GC > TA : \t" + this.spectrumForNonCons[3]);
System.out.println("AT > CG : \t" + this.spectrumForNonCons[4]);
System.out.println("GC > CG : \t" + this.spectrumForNonCons[5]);
//printATGCCountForNonCoding(seqHash, names, geneHash);
printCodonUsage();
this.updateNonCodingATCGXCounts();
System.out.println(">>>>>>>>>>>>>> ATCGX count. ALL, Coding, NonCoding(ALL-Coding)");
this.printATCGXCounts();
}
public void printATCGXCounts(){
int sumALL = 0;
int sumCoding = 0;
int sumNonCoding = 0;
for(int i=0; i<this.atcgALL.length;i++){
sumALL += this.atcgALL[i];
sumCoding += this.atcgCoding[i];
sumNonCoding += this.atcgNonCoding[i];
}
System.out.println("\ttotal\tA\tT\tC\tG\tN");
System.out.print("ALL\t"+sumALL);
for(int i=0; i<this.atcgALL.length;i++)
System.out.print("\t" + this.atcgALL[i]);
System.out.print("\nCODING\t" + sumCoding);
for(int i=0; i<this.atcgCoding.length;i++)
System.out.print("\t" + this.atcgCoding[i]);
System.out.print("\nNONCOD\t" + sumNonCoding);
for(int i=0; i<this.atcgNonCoding.length;i++)
System.out.print("\t" + this.atcgNonCoding[i]);
System.out.println();
}
public void updateNonCodingATCGXCounts(){
for(int i=0;i<this.atcgALL.length;i++)
this.atcgNonCoding[i] = this.atcgALL[i] - this.atcgCoding[i];
}
public void printATGCCountForNonCoding(Hashtable<String, Seq> seqHash, ArrayList<String> names, Hashtable<String, ArrayList<Gene>> geneHash){
Seq curSeq;
ArrayList<Gene> curGenes;
int[] atcgxArray = new int[5];
int sum = 0;
for(int i=0; i<names.size(); i++){
curSeq = seqHash.get(names.get(i));
curGenes = geneHash.get(names.get(i));
if(curGenes != null){
int start = 1;
int prevEnd = 0;
for(int j=0;j<curGenes.size();j++){
int curSt = curGenes.get(j).getStart();
int curEnd = curGenes.get(j).getEnd();
if(curSt <= prevEnd)
;//System.out.println(curSt + ".." + curEnd);
if(curSt > start){
//System.out.println(start + "\t" + (curSt-1));
atcgxArray = curSeq.getATCGXArray(start, curSt-1, atcgxArray);
sum += (curSt - start);
}
start = curEnd+1;
prevEnd = curEnd;
}
if(start <= curSeq.getSeqLength()){
//System.out.println(start + "\t" + curSeq.getSeqLength());
atcgxArray = curSeq.getATCGXArray(start, curSeq.getSeqLength(), atcgxArray);
sum += (curSeq.getSeqLength()+1 - start);
}
System.out.println(sum);
System.out.println("A\tT\tC\tG\tX");
System.out.println(atcgxArray[0] + "\t" + atcgxArray[1] + "\t" + atcgxArray[2] + "\t" + atcgxArray[3] +"\t" + atcgxArray[4] );
//seqHash.get(names.get(i)).printATCGCounts();
}
}
}
public static String makeAlternate(String codon, char c, int pos){
//System.out.print(codon + "\t" + c + "\t" + pos);
char[] tmp = new char[3];
for(int i=0; i<3;i++){
if(i==pos)
tmp[i]=c;
else
tmp[i]=codon.charAt(i);
}
//System.out.println("\t" + tmp[0]+tmp[1]+tmp[2]);
return "" + tmp[0]+tmp[1]+tmp[2];
}
public static char[] getOthers(char c){
char[] tmp2 = new char[3];
if(c == 'A'){
char[] tmp = new char[] {'T','G','C'};
tmp2 = tmp;
}
else if(c == 'T'){
char[] tmp = new char[] {'A','G','C'};
tmp2 = tmp;
}else if(c == 'G'){
char[] tmp = new char[] {'A','T','C'};
tmp2 = tmp;
}else if(c == 'C'){
char[] tmp = new char[] {'A','T','G'};
tmp2 = tmp;
}else
System.err.println("WTF");
return tmp2;
}
public void printCodonUsage(){
Enumeration<Character> aminos = amino2UsageCounter.keys();
while(aminos.hasMoreElements()){
Character curChar = aminos.nextElement();
this.amino2UsageCounter.get(curChar).calculateUsage(curChar.charValue());
}
}
public char toAmino(String triplet){
Character ch = this.codon2amino.get(triplet);
if(ch == null){
System.err.println(triplet);
return '!';
//System.exit(1);
}
return ch.charValue();
/*if(ch !=null)
return ch.charValue();
else
return '@';*/
}
/* RETURNS 1 if Syn, 0 if non-syn, -1, if NG due to bases other than atgc */
/*public int isSyn(String t1, String t2){
//System.out.println(t1 + "\t" + t2);
char c1 = this.toAmino(t1);
char c2 = this.toAmino(t2);
if(c1 == '!' || c2 == '!')
return -1;
if(c1 == c2)
return 1;
else
return 0;
}*/
public MutType isSyn(String t1, String t2){
//System.out.println(t1 + "\t" + t2);
char c1 = this.toAmino(t1);
char c2 = this.toAmino(t2);
if(c1 == '!' || c2 == '!')
return new MutType(-1,c1,c2,t1,t2);
if(c1 == c2)
return new MutType(1,c1,c2,t1,t2);
else
return new MutType(0,c1,c2,t1,t2);
}
/* ADDED to KEEP TRACK of more detailed COUNTS */
/* length : 6, the order is AT > GC , GC > AT , AT > TA , GC > TA , AT > CG , GC > CG */
private int[] spectrumForConservative = new int[6]; //only
private int[] spectrumForNonCons = new int[6];
private int[] spectrumForSyn = new int[6];
private int[] spectrumForNonSyn = new int[6];
private int calcMutType(char pre, char post){
if(pre == 'A'){
if(post == 'C')
return 3;
else if(post == 'G')
return -2;
else if(post == 'T')
return 1;
else
return 0;
}else if(pre == 'C'){
if(post == 'A')
return 2;
else if(post == 'G')
return 4;
else if(post == 'T')
return -1;
else
return 0;
}else if(pre == 'G'){
if(post == 'A')
return -1;
else if(post == 'C')
return 4;
else if(post == 'T')
return 2;
else
return 0;
}else if(pre == 'T'){
if(post == 'A')
return 1;
else if(post == 'C')
return -2;
else if(post == 'G')
return 3;
else
return 0;
}else
return 0;
}
private void updateSynConSyn(int mutType, boolean syn){
if(syn){
if(mutType < 0)
this.spectrumForSyn[mutType+2]++; //transistions are -2 and -1
else if(mutType > 0)
this.spectrumForSyn[mutType+1]++; //transversions are 1 through 4
else
System.out.println("FUCKKKKKKKKKKKK");
}else{
if(mutType < 0)
this.spectrumForNonSyn[mutType+2]++; //transistions are -2 and -1
else if(mutType > 0)
this.spectrumForNonSyn[mutType+1]++; //transversions are 1 through 4
else
System.out.println("FUCKKKKKKKKKKKK!!!!");
}
}
private void updateWithMutType(int mutType, boolean cons){
if(cons){
if(mutType < 0)
this.spectrumForConservative[mutType+2]++; //transistions are -2 and -1
else if(mutType > 0)
this.spectrumForConservative[mutType+1]++; //transversions are 1 through 4
else
System.out.println("FUCKKKKKKKKKKKK");
}else{
if(mutType < 0)
this.spectrumForNonCons[mutType+2]++; //transistions are -2 and -1
else if(mutType > 0)
this.spectrumForNonCons[mutType+1]++; //transversions are 1 through 4
else
System.out.println("FUCKKKKKKKKKKKK!!!!");
}
//return mutType;
}
}