-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFluAnalysis2.java
More file actions
1296 lines (1131 loc) · 42.4 KB
/
Copy pathFluAnalysis2.java
File metadata and controls
1296 lines (1131 loc) · 42.4 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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package teaspoon;
import teaspoon.adaptation.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Date;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
public class FluAnalysis2 {
Random generator = new Random();
public FluAnalysis2(){
}
public double[] GAPcounter(String Location, String Table,String Gene){
DataSet d = new DataSet(Location,Table,Gene);
double[] GapStore = new double[d.tablelength];
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
int i=0;
while(It.hasNext()){
SequenceInfo element = It.next();
GapStore[i] = element.Gap;
i++;
}
return GapStore;
}
public String[] Acc(String Location, String Table,String Gene){
DataSet d = new DataSet(Location,Table,Gene);
String[] GapStore = new String[d.tablelength];
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
int i=0;
while(It.hasNext()){
SequenceInfo element = It.next();
GapStore[i] = element.Accession;
i++;
}
return GapStore;
}
public int[] Datecounter(String Location, String Table, String Gene,String type){
DataSet d = new DataSet(Location,Table,Gene);
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
double x=0;
int[] dates = new int[2011-1930];
while(It.hasNext()){
SequenceInfo element = It.next();
x=element.Year-1931;
if(element.Genotype.equals(type) && element.Gap<5.0){
dates[(int)x]++;
}
}
x=1931;
return dates;
}
public int[] Datecounter2(String Location, String Table, String Gene,String type){
DataSet d = new DataSet(Location,Table,Gene);
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
double x=0;
int[] dates = new int[2011-1930];
while(It.hasNext()){
SequenceInfo element = It.next();
x=element.Year-1931;
if(element.Genotype.equals(type) || element.Genotype.equals(type+"a") || element.Genotype.equals(type+"b") || element.Genotype.equals(type+"c") || element.Genotype.equals(type+"d") || element.Genotype.equals(type+"e") || element.Genotype.equals(type+"f") || element.Genotype.equals(type+"g")){
if(element.Gap<5.0){
dates[(int)x]++;
}
}
}
x=1931;
return dates;
}
public int[] DatecounterContinentSplit(String Location, String Table, String Gene,String type){
DataSet d = new DataSet(Location,Table,Gene);
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
double x=0;
int[] dates = new int[2011-1930];
while(It.hasNext()){
SequenceInfo element = It.next();
x=element.Year-1931;
if(element.Genotype.equals(type) || element.Genotype.equals(type+"a") || element.Genotype.equals(type+"b") || element.Genotype.equals(type+"c") || element.Genotype.equals(type+"d") || element.Genotype.equals(type+"e") || element.Genotype.equals(type+"f") || element.Genotype.equals(type+"g")){
if(element.Gap<5.0){
if(element.Continent.equals("EU")){
dates[(int)x]++;
}
}
}
}
x=1931;
return dates;
}
public int[] Datecounter(String Location, String Table, String Gene,String[] type){
DataSet d = new DataSet(Location,Table,Gene);
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
double x=0;
int[] dates = new int[2011-1930];
while(It.hasNext()){
SequenceInfo element = It.next();
x=element.Year-1931;
for(int i=0;i<type.length;i++){
if(element.Genotype.equals(type[i]) && element.Gap<5.0){
// if(element.Genotype.equals(type[i])){
dates[(int)x]++;
}
}
}
x=1931;
return dates;
}
// runner which splits sequences
public void Runner(String Location, String Table, String Gene,String[] type,String[] Dates,int start,int stop,double nr,int which){
// inputs if which is 0 this prints SR sounds if 1 prints adap
// if stop is -1 use full length sequences, if -2 and -3 this does the N1 subsetting
int isNA=stop;
DataSet d = new DataSet(Location,Table,Gene);
final double[] low = {0.0,0.15};
final double[] mid = {0.15,0.75};
final double[] high = {0.75,1.0};
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
/***************** Fix ancestral sequence *****************************/
ArrayList<SequenceInfo> outans = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false;
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[0].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
if(flag2==true && element.Gap<5.0 && flag==true){ // filter dates and gap counts and types
outans.add(element);
}
}
int[][] ans1 = new int[outans.size()][d.DataMainFrame.get(0).Sequence.length];
for(int i=0;i<outans.size();i++){
ans1[i] = outans.get(i).Sequence;
}
int[] ans = consensusArray(ans1);
/************************** Specify MAIN SEQUENCE************************************************************ */
double dateAvg = 0; // calculate date average
for(int Z=1;Z<Dates.length;Z++){ // BIG loop through dates ************************ change date in loops
It = d.DataMainFrame.iterator();
double count=0;
dateAvg = 0;
ArrayList<int[]> Sequences = new ArrayList<int[]>();
ArrayList<String> Names = new ArrayList<String>();
ArrayList<SequenceInfo> out = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[Z].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
Sequences.add(element.Sequence); //saves sequence
Names.add(element.Accession); //saves name
out.add(element); //for output if needed
dateAvg+=element.DecimalDate; //calculates average date
count++;
}
}
// make integer matricies dates ********************************************************************************
dateAvg = dateAvg/count;
int[][] integer_matrix = new int[Sequences.size()][Sequences.get(0).length];
String[] accessions = new String[Names.size()];
// add sequences
Iterator<int[]> It2 = Sequences.iterator(); // converts object into a sequence matrix
int l=0;
while(It2.hasNext()){
int[] tmp = It2.next();
integer_matrix[l] = tmp ;
l++;
}
Iterator<String> It3 = Names.iterator(); //converts object into the accessions matrix
l=0;
while(It3.hasNext()){
String tmp = It3.next();
accessions[l] = tmp ;
l++;
}
if(stop<=-1){
stop = integer_matrix[0].length;
}
// adjust matrix size
int[][] mat = new int[integer_matrix.length][stop-start]; //start=51, stop=1035 for ha1, ha2 is start 1036 - end
int[] matans = new int[stop-start];
for(int i=0;i<integer_matrix.length;i++){
int p=0;
for(int j=start;j<stop;j++){
mat[i][p] = integer_matrix[i][j];
matans[p] = ans[j];
p++;
}
}
Neuraminidase Nd = new Neuraminidase();
if(isNA==-2){ //this is N1
Methods m = new Methods();
int[][] mat2 = m.Subsetter(mat, Nd.badlisth1n1_NoStopCodon, 1);
int[] matans2 = m.Subsetter(matans, Nd.badlisth1n1_NoStopCodon, 1);
Williamson3bin ws = new Williamson3bin(mat2,matans2);
ws.williamson3bin_method(nr,low, mid, high);
if(which==1){
System.out.print(dateAvg + "\t" + ws.integer_matrix.length + "\t" + ws.low_R+"\t"+ws.low_S + "\t" + ws.Neut_R+"\t"+ws.Neut_S + "\t" + ws.High_R+"\t"+ws.High_S+"\n");
} else {
System.out.print(dateAvg + "\t" + ws.LowA+ "\t" + ws.MidA+ "\t" + ws.Adapt + "\n" );
}
}
else if(isNA==-3){ //this is N1
Methods m = new Methods();
int[][] mat3 = m.Subsetter(mat, Nd.badlisth1n1_NoStopCodon, 0);
int[] matans3 = m.Subsetter(matans, Nd.badlisth1n1_NoStopCodon, 0);
Williamson3bin ws = new Williamson3bin(mat3,matans3);
ws.williamson3bin_method(nr,low, mid, high);
if(which==1){
System.out.print(dateAvg + "\t" + ws.integer_matrix.length + "\t" + ws.low_R+"\t"+ws.low_S + "\t" + ws.Neut_R+"\t"+ws.Neut_S + "\t" + ws.High_R+"\t"+ws.High_S+"\n");
} else {
System.out.print(dateAvg + "\t" + ws.LowA+ "\t" + ws.MidA+ "\t" + ws.Adapt + "\n" );
}
} else { // this is for everything else
Williamson3bin ws = new Williamson3bin(mat,matans);
ws.williamson3bin_method(nr,low, mid, high);
if(which==1){
System.out.print(dateAvg + "\t" + ws.integer_matrix.length + "\t" + ws.low_R+"\t"+ws.low_S + "\t" + ws.Neut_R+"\t"+ws.Neut_S + "\t" + ws.High_R+"\t"+ws.High_S+"\n");
} else {
System.out.print(dateAvg + "\t" + ws.LowA+ "\t" + ws.MidA+ "\t" + ws.Adapt + "\n" );
}
}
}
}
public void WriteSequencesGeno(String outloc,String Location, String Table, String Gene,String[] type,String[] Dates,int start,int stop,double nr,int Num,int[] list,int oneorzero,String Geno){
int WhichSub=stop;
DataSet d = new DataSet(Location,Table,Gene);
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
ArrayList<SequenceInfo> out = new ArrayList<SequenceInfo>();
for(int Z=0;Z<Dates.length;Z++){ // BIG loop through dates ************************ change date in loops
It = d.DataMainFrame.iterator();
double count=0;
ArrayList<String> Names = new ArrayList<String>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[Z].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
if(WhichSub==-2){ //this is N1
Methods m = new Methods();
element.Sequence=m.Subsetter(element.Sequence, list, oneorzero);
}
Names.add(element.Accession); //saves name
System.out.println(element.Accession);
out.add(element); //for output if needed
count++;
}
}
}
// d.exportFASTA(outloc+"_"+Geno+"_"+Gene+"_"+Dates[Z]+".fa", out);
}
public void WriteSequences(String outloc,String Location, String Table, String Gene,String[] type,String[] Dates,int start,int stop,double nr,int Num,int[] list,int oneorzero,String Geno){
int WhichSub=stop;
WilliamsonOutputClass woc = new WilliamsonOutputClass(Dates.length,Num);
DataSet d = new DataSet(Location,Table,Gene);
final double[] low = {0.0,0.15};
final double[] mid = {0.15,0.75};
final double[] high = {0.75,1.0};
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
ArrayList<SequenceInfo> outans = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false;
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[0].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
if(flag2==true && element.Gap<5.0 && flag==true){ // filter dates and gap counts and types
if(WhichSub==-2){ //this is N1
Methods m = new Methods();
element.Sequence=m.Subsetter(element.Sequence, list, oneorzero);
}
outans.add(element);
}
}
d.exportFASTA(outloc+"_"+Geno+"_"+Gene+"_"+Dates[0]+".fa", outans);
for(int Z=1;Z<Dates.length;Z++){ // BIG loop through dates ************************ change date in loops
It = d.DataMainFrame.iterator();
double count=0;
ArrayList<int[]> Sequences = new ArrayList<int[]>();
ArrayList<String> Names = new ArrayList<String>();
ArrayList<SequenceInfo> out = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[Z].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
if(WhichSub==-2){ //this is N1
Methods m = new Methods();
element.Sequence=m.Subsetter(element.Sequence, list, oneorzero);
}
Names.add(element.Accession); //saves name
out.add(element); //for output if needed
count++;
}
}
d.exportFASTA(outloc+"_"+Geno+"_"+Gene+"_"+Dates[Z]+".fa", out);
}
}
// this is the main output file // needs to be double checked
public WilliamsonOutputClass RunnerBS(String Location, String Table, String Gene,String[] type,String[] Dates,int start,int stop,double nr,int Num,int[] list,int oneorzero){
int WhichSub=stop;
WilliamsonOutputClass woc = new WilliamsonOutputClass(Dates.length,Num);
DataSet d = new DataSet(Location,Table,Gene);
final double[] low = {0.0,0.15};
final double[] mid = {0.15,0.75};
final double[] high = {0.75,1.0};
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
/***************** Fix ancestral sequence *****************************/
ArrayList<SequenceInfo> outans = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false;
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[0].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
if(flag2==true && element.Gap<5.0 && flag==true){ // filter dates and gap counts and types
outans.add(element);
}
}
int[][] ans1 = new int[outans.size()][d.DataMainFrame.get(0).Sequence.length];
for(int i=0;i<outans.size();i++){
ans1[i] = outans.get(i).Sequence;
}
int[] ans = consensusArray(ans1);
for(int run=0;run<Num;run++){
// boot strap stuff
int[] sampler = new int[ans.length/3];
int numsites=0;
if(WhichSub==-1){
sampler = new int[ans.length/3];
for(int x=0;x<sampler.length;x++){
int randint = generator.nextInt(sampler.length-1);
sampler[x] = randint;
}
} else if(WhichSub==-2){
for(int j=0;j<list.length;j++){if(list[j]==oneorzero){numsites++;}}
sampler = new int[(numsites)];
for(int x=0;x<sampler.length;x++){
int randint = generator.nextInt(sampler.length-1);
sampler[x] = randint;
}
}
/************************** Specify MAIN SEQUENCE************************************************************ */
double dateAvg = 0; // calculate date average
for(int Z=0;Z<Dates.length;Z++){ // BIG loop through dates ************************ change date in loops
It = d.DataMainFrame.iterator();
double count=0;
dateAvg = 0;
ArrayList<int[]> Sequences = new ArrayList<int[]>();
ArrayList<String> Names = new ArrayList<String>();
ArrayList<SequenceInfo> out = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[Z].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
Sequences.add(element.Sequence); //saves sequence
Names.add(element.Accession); //saves name
out.add(element); //for output if needed
dateAvg+=element.DecimalDate; //calculates average date
count++;
}
}
// make integer matricies dates ********************************************************************************
dateAvg = dateAvg/count;
int[][] integer_matrix = new int[Sequences.size()][Sequences.get(0).length];
String[] accessions = new String[Names.size()];
// add sequences
Iterator<int[]> It2 = Sequences.iterator(); // converts object into a sequence matrix
int l=0;
while(It2.hasNext()){
int[] tmp = It2.next();
integer_matrix[l] = tmp ;
l++;
}
Iterator<String> It3 = Names.iterator(); //converts object into the accessions matrix
l=0;
while(It3.hasNext()){
String tmp = It3.next();
accessions[l] = tmp ;
l++;
}
if(stop<=-1){
stop = integer_matrix[0].length;
}
// adjust matrix size
int[][] mat = new int[integer_matrix.length][stop-start]; //start=51, stop=1035 for ha1, ha2 is start 1036 - end
int[] matans = new int[stop-start];
for(int i=0;i<integer_matrix.length;i++){
int p=0;
for(int j=start;j<stop;j++){
mat[i][p] = integer_matrix[i][j];
matans[p] = ans[j];
p++;
}
}
if(WhichSub==-2){ //this is N1
Methods m = new Methods();
int[][] mat2 = m.Subsetter(mat, list, oneorzero);
int[] matans2 = m.Subsetter(matans, list, oneorzero);
Williamson3bin ww = new Williamson3bin(mat2,matans2); //******
Store s = ww.CreateBlocks(3,mat2[0].length,sampler); //******
Williamson3bin w = new Williamson3bin(s.RandomisedIntegerMatrix,s.RandomisedIntegerAncestral);
w.williamson3bin_method(nr,low, mid, high);
if(run==0 && Z==0){
woc.Low_S.add(Double.valueOf(0.0));
woc.Low_R.add(Double.valueOf(0.0));
woc.Mid_S.add(Double.valueOf(0.0));
woc.Mid_R.add(Double.valueOf(0.0));
woc.High_S.add(Double.valueOf(0.0));
woc.High_R.add(Double.valueOf(0.0));
woc.Fix_S.add(Double.valueOf(0.0));
woc.Fix_R.add(Double.valueOf(0.0));
woc.dateavg.add(dateAvg);
woc.LowA.add(Double.valueOf(0.0));
woc.MidA.add(Double.valueOf(0.0));
woc.HighA.add(Double.valueOf(0.0));
woc.FixA.add(Double.valueOf(0.0));
woc.TotalA.add(Double.valueOf(0.0));
woc.numSamples.add(mat.length);
woc.neut=nr;
woc.L=matans2.length;
}
else if(run==0){
Williamson3bin ws = new Williamson3bin(mat2,matans2);
ws.williamson3bin_method(nr,low, mid, high);
woc.Low_S.add(ws.low_S);
woc.Low_R.add(ws.low_R);
woc.Mid_S.add(ws.Neut_S);
woc.Mid_R.add(ws.Neut_R);
woc.High_S.add(ws.High_S);
woc.High_R.add(ws.High_R);
woc.Fix_S.add(ws.Fix_S);
woc.Fix_R.add(ws.Fix_R);
woc.dateavg.add(dateAvg);
woc.LowA.add(ws.LowA);
woc.MidA.add(ws.MidA);
woc.HighA.add(ws.HighA);
woc.FixA.add(ws.FixA);
woc.TotalA.add(ws.Adapt);
woc.numSamples.add(mat.length);
woc.neut=nr;
woc.L=matans2.length;
}
woc.BS.get(Z)[run]=w.Adapt;
}else { // this is for everything else
Williamson3bin ww = new Williamson3bin(mat,matans); //******
Store s = ww.CreateBlocks(3,mat[0].length,sampler); //******
Williamson3bin w = new Williamson3bin(s.RandomisedIntegerMatrix,s.RandomisedIntegerAncestral);
w.williamson3bin_method(nr,low, mid, high);
if(run==0 && Z==0){
woc.Low_S.add(Double.valueOf(0.0));
woc.Low_R.add(Double.valueOf(0.0));
woc.Mid_S.add(Double.valueOf(0.0));
woc.Mid_R.add(Double.valueOf(0.0));
woc.High_S.add(Double.valueOf(0.0));
woc.High_R.add(Double.valueOf(0.0));
woc.Fix_S.add(Double.valueOf(0.0));
woc.Fix_R.add(Double.valueOf(0.0));
woc.dateavg.add(dateAvg);
woc.LowA.add(Double.valueOf(0.0));
woc.MidA.add(Double.valueOf(0.0));
woc.HighA.add(Double.valueOf(0.0));
woc.FixA.add(Double.valueOf(0.0));
woc.TotalA.add(Double.valueOf(0.0));
woc.numSamples.add(mat.length);
woc.neut=nr;
woc.L=matans.length;
}
else if(run==0){
Williamson3bin ws = new Williamson3bin(mat,matans);
ws.williamson3bin_method(nr,low, mid, high);
woc.Low_S.add(ws.low_S);
woc.Low_R.add(ws.low_R);
woc.Mid_S.add(ws.Neut_S);
woc.Mid_R.add(ws.Neut_R);
woc.High_S.add(ws.High_S);
woc.High_R.add(ws.High_R);
woc.Fix_S.add(ws.Fix_S);
woc.Fix_R.add(ws.Fix_R);
woc.dateavg.add(dateAvg);
woc.LowA.add(ws.LowA);
woc.MidA.add(ws.MidA);
woc.HighA.add(ws.HighA);
woc.FixA.add(ws.FixA);
woc.TotalA.add(ws.Adapt);
woc.numSamples.add(mat.length);
woc.neut=nr;
woc.L=matans.length;
}
woc.BS.get(Z)[run]=w.Adapt;
}
}
}
for(int l=0;l<woc.BS.size();l++){
Arrays.sort(woc.BS.get(l));
}
double lc= 0.05*Num; double hc= 0.95*Num;
for(int l=0;l<woc.BS.size();l++){
woc.LC.add(woc.BS.get(l)[(int)lc]);
woc.HC.add(woc.BS.get(l)[(int)hc]);
}
return woc;
}
public WilliamsonOutputClass GenericRunner(String Location,String[] Dates,double nr,int Num){
DataSet data = new DataSet(Location);
System.out.println("Analysis on "+Location);
//ancestral sequence
WilliamsonOutputClass woc = new WilliamsonOutputClass(Dates.length,Num);
final double[] low = {0.0,0.15};
final double[] mid = {0.15,0.75};
final double[] high = {0.75,1.0};
///////******************************************************* Generate Ans**************************
ArrayList<int[]> Ans = new ArrayList<int[]>();
String[] temp = Dates[0].split("_");
for(int i=0;i<data.taxon_matrix.length;i++){
String line = data.taxon_matrix[i];
for(int x=0;x<temp.length;x++){
if(line.matches(".*"+String.valueOf(temp[x]))){
Ans.add(data.integer_matrix[i]);
}
}
}
int[][] ansMat = new int[Ans.size()][Ans.get(0).length];
for(int i=1;i<Ans.size();i++){
ansMat[i]=Ans.get(i);
}
Read_main r = new Read_main();
int[] ans = r.consensusArray(ansMat); // this is the ancestral sequence
///////******************************************************* Generate Main**************************
// DO main sequence
for(int run=0;run<Num;run++){
for(int d=0;d<Dates.length;d++){ // cycle through all time points
temp = Dates[d].split("_"); // split clumped dates
double dateAvg=0; //initialise date counter
double k=0;
ArrayList<int[]> Seq = new ArrayList<int[]>();
for(int i=0;i<data.taxon_matrix.length;i++){
String line = data.taxon_matrix[i];
for(int x=0;x<temp.length;x++){
if(line.matches("(?i).*_"+String.valueOf(temp[x])+".*")){
String[] split = line.split("_");
dateAvg+=(double) Double.valueOf(split[split.length-1]);
k++;
Seq.add(data.integer_matrix[i]);
}
}
}
int[][] seq = new int[Seq.size()][Seq.get(0).length];
for(int i=0;i<Seq.size();i++){
seq[i]=Seq.get(i);
}
// update the teaspoon.adaptation.Williamson Output class
if(run==0){
Williamson3bin ws = new Williamson3bin(seq,ans);
ws.williamson3bin_method(nr,low, mid, high);
woc.Low_S.add(ws.low_S);
woc.Low_R.add(ws.low_R);
woc.Mid_S.add(ws.Neut_S);
woc.Mid_R.add(ws.Neut_R);
woc.High_S.add(ws.High_S);
woc.High_R.add(ws.High_R);
woc.Fix_S.add(ws.Fix_S);
woc.Fix_R.add(ws.Fix_R);
woc.dateavg.add(dateAvg/k);
woc.LowA.add(ws.LowA);
woc.MidA.add(ws.MidA);
woc.HighA.add(ws.HighA);
woc.FixA.add(ws.FixA);
woc.TotalA.add(ws.Adapt);
woc.numSamples.add(seq.length);
woc.neut=nr;
woc.L=ans.length;
}else{
///////******************************************************* Generate Bootstraps**************************
// generate a new sample set
int[] sampler = new int[ans.length/3];
for(int x=0;x<sampler.length;x++){
int randint = generator.nextInt(sampler.length-1);
sampler[x] = randint;
}
Williamson3bin ww = new Williamson3bin(seq,ans); //******
Store s = ww.CreateBlocks(3,seq[0].length,sampler); //******
Williamson3bin w = new Williamson3bin(s.RandomisedIntegerMatrix,s.RandomisedIntegerAncestral);
w.williamson3bin_method(nr,low, mid, high);
woc.BS.get(d)[run]=w.Adapt;
}
}
}
return woc;
}
public void RunnerTS(String Location, String Table, String Gene,String[] type,String[] Dates,int start,int stop,double nr,int which){
DataSet d = new DataSet(Location,Table,Gene);
final double[] low = {0.0,0.15};
final double[] mid = {0.15,0.75};
final double[] high = {0.75,1.0};
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
/************************** Specify MAIN SEQUENCE************************************************************ */
double dateAvg = 0; // calculate date average
for(int Z=1;Z<Dates.length;Z++){ // BIG loop through dates ************************ change date in loops
It = d.DataMainFrame.iterator();
double count=0;
dateAvg = 0;
ArrayList<int[]> Sequences = new ArrayList<int[]>();
ArrayList<String> Names = new ArrayList<String>();
ArrayList<SequenceInfo> out = new ArrayList<SequenceInfo>();
ArrayList<int[]> Sequences2 = new ArrayList<int[]>();
ArrayList<String> Names2 = new ArrayList<String>();
ArrayList<SequenceInfo> out2 = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
// find main sequences
boolean flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates[Z].split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
Sequences.add(element.Sequence); //saves sequence
Names.add(element.Accession); //saves name
out.add(element); //for output if needed
dateAvg+=element.DecimalDate; //calculates average date
count++;
}
// find ancestral sequences
flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
delimiter = "-";
temp = Dates[Z-1].split(delimiter);
flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
Sequences2.add(element.Sequence); //saves sequence
}
}
// make integer matricies dates ********************************************************************************
dateAvg = dateAvg/count;
int[][] integer_matrix = new int[Sequences.size()][Sequences.get(0).length];
String[] accessions = new String[Names.size()];
// add sequences
Iterator<int[]> It2 = Sequences.iterator(); // converts object into a sequence matrix
int l=0;
while(It2.hasNext()){
int[] tmp = It2.next();
integer_matrix[l] = tmp ;
l++;
}
Iterator<String> It3 = Names.iterator(); //converts object into the accessions matrix
l=0;
while(It3.hasNext()){
String tmp = It3.next();
accessions[l] = tmp ;
l++;
}
if(stop==-1){
stop = integer_matrix[0].length;
}
// make ancestreal matricies dates ********************************************************************************
int[][] integer_ancestral = new int[Sequences2.size()][Sequences2.get(0).length];
Iterator<int[]> It4 = Sequences2.iterator(); // converts object into a sequence matrix
l=0;
while(It4.hasNext()){
int[] tmp = It4.next();
integer_ancestral[l] = tmp ;
l++;
}
int[] ans = consensusArray(integer_ancestral);
// adjust matrix size
int[][] mat = new int[integer_matrix.length][stop-start]; //start=51, stop=1035 for ha1, ha2 is start 1036 - end
int[] matans = new int[stop-start];
for(int i=0;i<integer_matrix.length;i++){
int p=0;
for(int j=start;j<stop;j++){
mat[i][p] = integer_matrix[i][j];
matans[p] = ans[j];
p++;
}
}
Williamson3bin ws = new Williamson3bin(mat,matans);
ws.williamson3bin_method(nr,low, mid, high);
if(which==1){
System.out.print(dateAvg + "\t" + ws.integer_matrix.length + "\t" + ws.low_R+"\t"+ws.low_S + "\t" + ws.Neut_R+"\t"+ws.Neut_S + "\t" + ws.High_R+"\t"+ws.High_S+"\n");
} else {
// System.out.print(dateAvg + "\t" + ws.Adapt + "\n" );
System.out.print(dateAvg + "\t" + ws.LowA+ "\t" + ws.MidA+ "\t" + ws.Adapt + "\n" );
}
}
}
public int[] consensusArray(int[][] integer_matrix){
Methods preprocess = new Methods();
int[] consensus = new int[integer_matrix[0].length];
double[] counter = new double[5];
for(int site=0;site<integer_matrix[0].length;site++){
// count numbers
counter[0] = preprocess.num_of_base(integer_matrix, 1, site);
counter[1] = preprocess.num_of_base(integer_matrix, 2, site);
counter[2] = preprocess.num_of_base(integer_matrix, 3, site);
counter[3] = preprocess.num_of_base(integer_matrix, 4, site);
counter[4] = preprocess.num_of_base(integer_matrix, 5, site);
int length = counter.length;
double max = -1;
int position = 0;
for (int i = 0; i < length; i++) {
if (counter[i]>max) {
max = counter[i]; // update max
position = (i+1);
}
}
//after the loop, min contains the minimum value,
//position contains its position inside the array
consensus[site] = position;
}
return consensus;
}
public void Extract(String Location, String Table, String Gene,String[] type,String Dates,String outname){
DataSet d = new DataSet(Location,Table,Gene);
Iterator<SequenceInfo> It = d.DataMainFrame.iterator();
ArrayList<SequenceInfo> out = new ArrayList<SequenceInfo>();
while(It.hasNext()){
SequenceInfo element = It.next();
boolean flag = false; // first flag checks through genotypes
for(int f=0;f<type.length;f++){
if(element.Genotype.equals(type[f])){ // fiter genotypes
flag=true;
}
}
String delimiter = "-";
String[] temp = Dates.split(delimiter);
boolean flag2 = false; // second flag checks through dates
for(int n=0;n<temp.length;n++){
if(element.Year==Double.valueOf(temp[n])){
flag2=true;
}
}
// adds the sequences we want according to date,genotype and gap uncertainty
if(element.Gap<5.0 && flag==true && flag2==true){ // filter sequences with gap count more than five
out.add(element);
}
}
d.exportFASTA(outname, out);
}
public void print(WilliamsonOutputClass woc,int type,String outp){
if(type==1){ // output adaptation and percentiles
for(int i=0;i<woc.BS.size();i++){
System.out.println(woc.dateavg.get(i) + "\t" + woc.LC.get(i) + "\t" + woc.TotalA.get(i) + "\t" + woc.HC.get(i) );
}
}else if(type==2){ // out put silent and replace stats
for(int i=0;i<woc.BS.size();i++){
System.out.print(woc.dateavg.get(i) + "\t" + woc.numSamples.get(i) + "\t" + woc.Low_R.get(i)+"\t"+woc.Low_S.get(i) + "\t" + woc.Mid_R.get(i)+"\t"+woc.Mid_S.get(i) + "\t" + woc.High_R.get(i)+"\t"+woc.High_S.get(i)+"\n");
}
}else if(type==3){ // output all adaptation from all ranges
for(int i=0;i<woc.BS.size();i++){
System.out.print(woc.dateavg.get(i) + "\t" + woc.LowA.get(i)+ "\t" + woc.MidA.get(i)+ "\t" + woc.TotalA.get(i) + "\n" );
}
}else if(type==4){ // output just top adaptations
for(int i=0;i<woc.BS.size();i++){
System.out.print(woc.dateavg.get(i) + "\t" + woc.TotalA.get(i) + "\n" );
}
} else if(type==5) { // output silent and replacement from mid neutral bin
for(int i=0;i<woc.BS.size();i++){
System.out.print(woc.dateavg.get(i) + "\t" + woc.Mid_R.get(i)+"\t"+woc.Mid_S.get(i) + "\n" );
}
}else if(type==6) { // output bootstraps
for(int j=0;j<woc.BS.get(0).length;j++){
for(int i=0;i<woc.BS.size();i++){
System.out.print(woc.BS.get(i)[j]+"\t");
}