File: AnalyzeFlowCell.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (1811 lines) | stat: -rwxr-xr-x 59,960 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
package hiseq;

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import aligner.SideChannel3;
import barcode.Barcode;
import barcode.BarcodeStats;
import bloom.BloomFilter;
import bloom.KmerCountAbstract;
import bloom.PolyFilter;
import dna.AminoAcid;
import dna.Data;
import fileIO.ByteFile;
import fileIO.ByteStreamWriter;
import fileIO.FileFormat;
import fileIO.ReadWrite;
import jgi.BBMerge;
import jgi.CalcTrueQuality;
import jgi.Dedupe;
import shared.Parse;
import shared.Parser;
import shared.PreParser;
import shared.Shared;
import shared.Timer;
import shared.Tools;
import shared.TrimRead;
import stream.ConcurrentReadInputStream;
import stream.ConcurrentReadOutputStream;
import stream.FASTQ;
import stream.FastaReadInputStream;
import stream.Read;
import stream.SamLine;
import stream.SamLineStreamer;
import structures.AtomicStringNum;
import structures.ByteBuilder;
import structures.IntList;
import structures.ListNum;
import structures.LongList;
import template.Accumulator;
import template.ThreadWaiter;

/**
 * Analyzes a flow cell for low-quality areas.
 * Removes reads in the low-quality areas.
 * 
 * @author Brian Bushnell
 * @date August 31, 2016
 *
 */
public class AnalyzeFlowCell implements Accumulator<AnalyzeFlowCell.ProcessThread> {
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/
	
	/**
	 * Code entrance from the command line.
	 * @param args Command line arguments
	 */
	public static void main(String[] args){
		Timer t=new Timer();
		AnalyzeFlowCell x=new AnalyzeFlowCell(args);
		x.process(t);
		
		//Close the print stream if it was redirected
		Shared.closeStream(x.outstream);
	}
	
	/**
	 * Constructor.
	 * @param args Command line arguments
	 */
	public AnalyzeFlowCell(String[] args){
		
		{//Preparse block for help, config files, and outstream
			PreParser pp=new PreParser(args, getClass(), false);
			args=pp.args;
			outstream=pp.outstream;
		}
		
		//Set shared static variables
		ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
		ReadWrite.setZipThreads(Shared.threads());
		
		Parser parser=parse(args);
		
		if(gToN || discardG){MicroTile.TRACK_CYCLES=true;}
		
		{//Process parser fields
			Parser.processQuality();
			
			maxReads=parser.maxReads;
			
			overwrite=parser.overwrite;
			append=parser.append;
			setInterleaved=parser.setInterleaved;
			
			in1=parser.in1;
			in2=parser.in2;

			out1=parser.out1;
			out2=parser.out2;
			
			extin=parser.extin;
			extout=parser.extout;
			

			trimq=parser.trimq;
			trimE=parser.trimE();
			minlen=parser.minReadLength;
			trimLeft=parser.qtrimLeft;
			trimRight=parser.qtrimRight;
		}
		

		Read.VALIDATE_IN_CONSTRUCTOR=true;
		align=(align || alignOut!=null) && samInput==null && dumpIn==null;
		
		if(recalibrate) {
			CalcTrueQuality.initializeMatrices();
		}
//		if(mapper!=null) {
//			assert(mapper.getMap().size()>0) : refPath+", "+alignK+", "+minIdentity;
//		}
		checkFiles();
		
		//Create output FileFormat objects
		ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false);
		ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false);
		ffoutbad=FileFormat.testOutput(outbad, FileFormat.FASTQ, extout, true, overwrite, append, false);

		//Create input FileFormat objects
		ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true);
		ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true);
	}
	
	/*--------------------------------------------------------------*/
	/*----------------    Initialization Helpers    ----------------*/
	/*--------------------------------------------------------------*/
	
	private Parser parse(String[] args){
		//Create a parser object
		Parser parser=new Parser();
		parser.qtrimRight=trimRight;
		parser.trimq=trimq;
		parser.minReadLength=minlen;
		
		//Parse each argument
		for(int i=0; i<args.length; i++){
			String arg=args[i];
			
			//Break arguments into their constituent parts, in the form of "a=b"
			String[] split=arg.split("=");
			String a=split[0].toLowerCase();
			String b=split.length>1 ? split[1] : null;
			
			if(a.equals("verbose")){
				verbose=Parse.parseBoolean(b);
			}else if(a.equals("outb") || a.equals("outbad")){
				outbad=b;
			}else if(a.equals("sam") || a.equals("insam") || 
					a.equals("samin") || a.equals("saminput")){
				samInput=b;
			}else if(a.equals("sammt")){
//				processSamMT=Parse.parseBoolean(b);
			}else if(a.equals("divisor") || a.equals("size")){
				targetX=targetY=Tile.xSize=Tile.ySize=Parse.parseIntKMG(b);
			}else if(a.equals("xdivisor") || a.equals("xsize") || a.equals("x")){
				targetX=Tile.xSize=Parse.parseIntKMG(b);
			}else if(a.equals("ydivisor") || a.equals("ysize") || a.equals("y")){
				targetY=Tile.ySize=Parse.parseIntKMG(b);
			}else if(a.equals("target") || a.equals("targetreads")){
				targetAverageReads=Parse.parseIntKMG(b);
			}else if(a.equals("targetalignedreads") || a.equals("alignedreads")){
				targetAlignedReads=Parse.parseIntKMG(b);
			}else if(a.equals("dump") || a.equals("dumpout") || a.equals("outdump")){
				dumpOut=b;
			}else if(a.equals("indump") || a.equals("ind") || a.equals("dumpin")){
				dumpIn=b;
			}else if(a.equals("filterout") || a.equals("filterlist") || a.equals("coordinates")
					 || a.equals("coords") || a.equals("coordsout") || a.equals("coordinatesout")){
				coordsOut=b;
			}else if(a.equals("loadkmers") || a.equals("usekmers")){
				loadKmers=Parse.parseBoolean(b);
			}else if(a.equals("loadthreads")){
				loadThreads=Integer.parseInt(b);
			}else if(a.equals("fillthreads")){
				fillThreads=Integer.parseInt(b);
			}else if(a.equals("minprob")){
				minProb=Float.parseFloat(b);
			}else if(a.equals("bits") || a.equals("cbits")){
				cbits=Integer.parseInt(b);
			}else if(a.equals("hashes")){
				hashes=Integer.parseInt(b);
			}
			
			else if(a.equals("smooth") || a.equals("fixspikes")){
				if(!Tools.startsWithDigit(b)) {
					smoothDepths=Parse.parseBoolean(b) ? 3 : 0;
				}else {
					smoothDepths=Integer.parseInt(b);
				}
			}else if(a.equals("deblur") || a.equals("deconvolute")){
				deblurDepths=Parse.parseBoolean(b);
			}
			
			else if(a.equals("blur") || a.equals("blurtiles") || a.equals("smoothtiles")){
				blurTiles=Parse.parseBoolean(b);
			}
			
			else if(a.equals("recalibrate") || a.equals("recal")) {
				recalibrate=Parse.parseBoolean(b);
			}
			
			else if(a.equals("minpolyg")){
				MicroTile.MIN_POLY_G=Integer.parseInt(b);
			}else if(a.equals("trackcycles")){
				MicroTile.TRACK_CYCLES=Parse.parseBoolean(b);
			}else if(a.equals("extra")){
				if(b!=null) {
					for(String s : b.split(",")) {
						extra.add(s);
					}
				}
			}else if(a.equals("expectedbarcodes") || a.equals("expected") || a.equals("barcodes") || a.equals("barcodesin")){
				expectedBarcodes=b;
			}else if(a.equals("barcodesout") || a.equals("barcodecounts") || a.equals("counts")){
				barcodeCounts=b;
			}
			
			else if(a.equals("merge")){
				merge=Parse.parseBoolean(b);
			}else if(a.equals("strict")){
				strictmerge=Parse.parseBoolean(b);
			}else if(a.equals("loose")){
				strictmerge=!Parse.parseBoolean(b);
			}
			
			else if(a.equals("alignout") || a.equals("sideout") || a.equals("phixout") || a.equals("outphix") || 
					a.equals("outsam") || a.equals("samout")){
				alignOut=b;
			}else if(a.equals("align")){
				align=Parse.parseBoolean(b);
			}else if(a.equals("ref") || a.equals("alignref") || a.equals("sideref")){
				alignRef=b;
			}else if(a.equals("alignk") || a.equals("sidek") || a.equals("alignk1") || a.equals("sidek1")){
				alignK1=Integer.parseInt(b);
			}else if(a.equals("alignk2") || a.equals("sidek2")){
				alignK2=Integer.parseInt(b);
			}else if(a.equals("alignminid") || a.equals("alignminid1") || 
					a.equals("sideminid") || a.equals("sideminid1") || a.equals("minid") || a.equals("minid1")){
				alignMinid1=Float.parseFloat(b);
			}else if(a.equals("alignminid2") || a.equals("sideminid2") || a.equals("minid2")){
				alignMinid2=Float.parseFloat(b);
			}else if(a.equals("alignmm1") || a.equals("alignmidmask1") || a.equals("sidemm1") || 
					a.equals("sidemidmask1")){
				alignMM1=Integer.parseInt(b);
			}else if(a.equals("alignmm2") || a.equals("alignmidmask2") || a.equals("sidemm2") || 
					a.equals("sidemidmask2")){
				alignMM2=Integer.parseInt(b);
			}
			
			else if(a.equals("lqo") || a.equals("lowqualityonly")){
				discardOnlyLowQuality=Parse.parseBoolean(b);
			}else if(a.equals("dmult")){
				dmult=Float.parseFloat(b);
			}else if(a.equals("idmaskread")){
				idmask_read=Integer.parseInt(b);
			}else if(a.equals("idmaskwrite")){
				idmask_write=Integer.parseInt(b);
			}else if(a.equals("allkmers")){
//				kmersPerRead=(Parse.parseBoolean(b) ? 0 : 1);
				boolean x=Parse.parseBoolean(b);
				idmask_write=(x ? 0 : 15);
			}
//			else if(a.equals("kmersperread")){
//				kmersPerRead=Integer.parseInt(b);
//			}
			
			else if(a.equals("parse_flag_goes_here")){
				//Set a variable here
			}else if(TileDump.parseStatic(arg, a, b)){
				//do nothing
			}else if(parser.parse(arg, a, b)){//Parse standard flags in the parser
				//do nothing
			}else if(b==null && new File(arg).exists()){
				extra.add(b);
			}else{
				outstream.println("Unknown parameter "+args[i]);
				assert(false) : "Unknown parameter "+args[i];
				//				throw new RuntimeException("Unknown parameter "+args[i]);
			}
		}
		return parser;
	}
	
	private void checkFiles(){
		doPoundReplacement();
		adjustInterleaving();
		checkFileExistence();
		checkStatics();
	}
	
	private void doPoundReplacement(){
		//Do input file # replacement
		if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){
			in2=in1.replace("#", "2");
			in1=in1.replace("#", "1");
		}

		//Do output file # replacement
		if(out1!=null && out2==null && out1.indexOf('#')>-1){
			out2=out1.replace("#", "2");
			out1=out1.replace("#", "1");
		}
		
		//Ensure there is an input file
		if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}

		//Ensure out2 is not set without out1
		if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");}
	}
	
	private void checkFileExistence(){
		
		//Ensure output files can be written
		if(!Tools.testOutputFiles(overwrite, append, false, out1, out2, outbad, dumpOut, barcodeCounts)){
			outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2+", "+outbad);
			throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+", "+outbad+", "+dumpOut+"\n");
		}

		//Ensure input files can be read
		if(!Tools.testInputFiles(false, true, in1, in2, samInput, dumpIn, expectedBarcodes)){
			throw new RuntimeException("\nCan't read some input files.\n");  
		}

		//Ensure that no file was specified multiple times
		if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2, outbad, samInput, 
				dumpIn, dumpOut, expectedBarcodes, barcodeCounts)){
			throw new RuntimeException("\nSome file names were specified multiple times.\n");
		}
	}
	
	private void adjustInterleaving(){
		//Adjust interleaved detection based on the number of input files
		if(in2!=null){
			if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");}
			FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
		}

		//Adjust interleaved settings based on number of output files
		if(!setInterleaved){
			assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n";
			if(in2!=null){ //If there are 2 input streams.
				FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
				outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
			}else{ //There is one input stream.
				if(out2!=null){
					FASTQ.FORCE_INTERLEAVED=true;
					FASTQ.TEST_INTERLEAVED=false;
					outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
				}
			}
		}
	}
	
	private static void checkStatics(){
		//Adjust the number of threads for input file reading
		if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
			ByteFile.FORCE_MODE_BF2=true;
		}
		
		assert(FastaReadInputStream.settingsOK());
	}
	
	/*--------------------------------------------------------------*/
	/*----------------         Outer Methods        ----------------*/
	/*--------------------------------------------------------------*/

	/** Create read streams and process all data */
	public void process(Timer t){
		
		//Reset counters
		readsProcessed=0;
		basesProcessed=0;
		
		if(dumpIn==null){
			barcodeStats=loadBarcodes(expectedBarcodes);
			if(barcodeCounts!=null) {
				barcodeMap=new ConcurrentHashMap<String, AtomicStringNum>();
			}
			if(loadKmers){loadKmers();}
			flowcell=new FlowCell(k);
			fillTiles();
			
			bloomFilter=null;//Clearing before widen saves memory
			ArrayList<MicroTile> mtList;
//			if(!processSamMT) {loadSam_ST(samInput);}
			Timer t2=new Timer();
			boolean showTime=false;
			synchronized(flowcell) {
				mtList=flowcell.calcStats();
				if(showTime) {t2.stopAndStart("calcStats: ");}
				if(flowcell.avgReads<targetAverageReads){
					flowcell=flowcell.widenToTargetReads(targetAverageReads);
					if(showTime) {t2.stopAndStart("Widen: ");}
					mtList=flowcell.toList();
					if(showTime) {t2.stopAndStart("toList: ");}
				}
				if(blurTiles) {
					flowcell.blur();
					if(showTime) {t2.stopAndStart("Blur: ");}
				}
				//Temporarily widen to calculate a regression
				if(flowcell.avgAlignedReads<targetAlignedReads && flowcell.readsAligned>targetAlignedReads) {
					final int oldX=Tile.xSize, oldY=Tile.ySize;
					FlowCell temp=flowcell.widenToTargetAlignedReads(targetAlignedReads);
					temp.calcStats();
					flowcell.uniqueToReadErrorRateFormula=temp.uniqueToReadErrorRateFormula;
					flowcell.uniqueToBaseErrorRateFormula=temp.uniqueToBaseErrorRateFormula;
					Tile.xSize=oldX;
					Tile.ySize=oldY;
					if(showTime) {t2.stopAndStart("Widen2: ");}
				}
//				if(MicroTile.trackDepth) {flowcell.summarizeDepth();}
			}

			long readsToDiscard=TileDump.markTiles(flowcell, mtList, outstream);

			if(dumpOut!=null){
				flowcell.dump(dumpOut, overwrite);
				if(showTime) {t2.stopAndStart("Dump: ");}
			}
			if(barcodeCounts!=null) {
				dumpBarcodes(barcodeMap.values(), barcodeCounts, overwrite);
				if(showTime) {t2.stopAndStart("Dump Barcodes: ");}
			}
		}else{
			flowcell=new FlowCell(dumpIn);
//			if(loadKmers){loadKmers();}
			if(targetX>Tile.xSize || targetY>Tile.ySize) {
				flowcell=flowcell.widen(targetX, targetY, true);
			}
			if(flowcell.avgReads<targetAverageReads){
				flowcell.calcStats();//May be necessary for calculating average reads
				flowcell=flowcell.widenToTargetReads(targetAverageReads);
			}
			if(blurTiles) {flowcell.blur();}
			ArrayList<MicroTile> mtList=flowcell.calcStats();
			long readsToDiscard=TileDump.markTiles(flowcell, mtList, outstream);
		}
		System.err.println("Avg quality:     \t"+Tools.format("%.3f", flowcell.avgQuality));
		System.err.println("STDev quality:   \t"+Tools.format("%.4f", flowcell.stdQuality));
		System.err.println("Avg uniqueness:  \t"+Tools.format("%.4f", flowcell.avgUnique));
		System.err.println("STDev uniqueness:\t"+Tools.format("%.4f", flowcell.stdUnique));
		System.err.println("Avg depth:       \t"+Tools.format("%.4f", flowcell.avgDepth));
		System.err.println("STDev depth:     \t"+Tools.format("%.4f", flowcell.stdDepth));
		System.err.println("Avg poly-G:      \t"+Tools.format("%.4f", flowcell.avgPolyG));
		System.err.println("STDev poly-G:    \t"+Tools.format("%.4f", flowcell.stdPolyG));
		System.err.println("Alignment Rate:  \t"+Tools.format("%.8f", flowcell.alignmentRate()));
		System.err.println("Base Error Rate: \t"+Tools.format("%.8f", flowcell.baseErrorRate()));
		
		if(sidechannel!=null && sidechannel.samOut) {Data.unloadAll();}
		
		processReads(t);
	}

	/** Create read streams and process all data */
	void loadKmers(){
		Timer t2=new Timer();
		outstream.print("Loading kmers:  \t");
		
		loadThreads=Tools.min(loadThreads, Shared.threads());
		final int oldMCT=KmerCountAbstract.MAX_COUNT_THREADS;
		final float oldProb=KmerCountAbstract.minProb;
//		KmerCountAbstract.KMERS_PER_READ=kmersPerRead;
		KmerCountAbstract.IDMASK=idmask_write;
		KmerCountAbstract.minProb=minProb;
		KmerCountAbstract.CANONICAL=true;
		
		if(loadThreads>1) {
			if(idmask_write>7) {
				KmerCountAbstract.MAX_COUNT_THREADS=loadThreads;
			}
			bloomFilter=new BloomFilter(in1, in2, extra, k, k, cbits, hashes, 
					1, true, false, false, 0.65f);
			bloomFilter.filter.shutdown();
		}else {
			//Create a read input stream
			final ConcurrentReadInputStream cris;
			{
				cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2);
				cris.start(); //Start the stream
				if(verbose){outstream.println("Started cris");}
			}
			boolean paired=cris.paired();
			//Process the read stream
			loadKmersInner(cris);
			
//			if(verbose){outstream.println("Finished; closing streams.");}
			
			//Close the read streams
			errorState|=ReadWrite.closeStreams(cris);
			bloomFilter.filter.shutdown();
		}
		
		KmerCountAbstract.KMERS_PER_READ=0;
		KmerCountAbstract.IDMASK=0;
		KmerCountAbstract.MAX_COUNT_THREADS=oldMCT;
		KmerCountAbstract.minProb=oldProb;
		
		double used=bloomFilter.filter.usedFraction();
		long unique=(long)bloomFilter.filter.estimateUniqueKmersFromUsedFraction(hashes, used);
		System.err.println(String.format("Bloom Occupancy:\t%.2f%%", 100*used));
		System.err.println("Unique Kmers:   \t"+unique);
		
		t2.stop();
		outstream.println(t2);
	}

	/** Create read streams and process all data */
	void fillTiles(){
		
		//Create a read input stream
		final ConcurrentReadInputStream cris;
		cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2);
		cris.start(); //Start the stream
		if(verbose){outstream.println("Started cris");}
		boolean paired=cris.paired();
		
		SamLineStreamer ss=null;
		if(samInput!=null && processSamMT) {
			outstream.println("Loading sam file.");
			FileFormat ff=FileFormat.testInput(samInput, FileFormat.SAM, null, true, false);
			final int streamerThreads=Tools.min(4, Shared.threads());
			ss=new SamLineStreamer(ff, streamerThreads, false, maxReads);
			ss.start();
		}
		

		if(align) {
			sidechannel=new SideChannel3(alignRef, alignOut, null, alignK1, alignK2, 
					alignMinid1, alignMinid2, alignMM1, alignMM2, overwrite, ordered);
			sidechannel.start();
		}else {
			sidechannel=null;
		}
		
		//Process the read stream
		fillTilesInner(cris, ss);
		
		if(verbose){outstream.println("Finished; closing streams.");}
		
		//Close the read streams
		errorState|=ReadWrite.closeStreams(cris);
		if(sidechannel!=null) {errorState|=sidechannel.shutdown();}
	}
	
//	/** Singlethreaded version. */
//	private void loadSam_ST(String fname) {
//		if(fname==null || processSamMT) {return;}
//		Timer t=new Timer();
//		outstream.println("Loading sam file.");
//		final SamReadStreamer ss;
//		FileFormat ff=FileFormat.testInput(fname, FileFormat.SAM, null, true, false);
//		final int streamerThreads=Tools.min(4, Shared.threads());
//		
//		ss=new SamReadStreamer(ff, streamerThreads, false, maxReads);
//		ss.start();
//
//		ListNum<Read> ln=ss.nextList();
//		ArrayList<Read> reads=(ln==null ? null : ln.list);
//		final IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();
//
//		while(ln!=null && reads!=null && reads.size()>0){
//
//			for(int idx=0; idx<reads.size(); idx++){
//				Read r=reads.get(idx);
//				assert(r.mate==null);
//				processSamLine(r, ihp);
//			}
//			ln=ss.nextList();
//			reads=(ln==null ? null : ln.list);
//		}
//		t.stopAndPrint();
//	}
//	
//	private void processSamLine(Read r, IlluminaHeaderParser2 ihp) {
//		if(r==null){return;}
//		final SamLine sl=r.samline;
////		if(!sl.mapped() && !sl.nextMapped()) {return;} //Probably not PhiX
//		if(!sl.mapped()) {return;}//TODO: Track unmapped info; requires modifying dump format
//		
//		
//		if(!sl.mapped() || r.bases==null || r.match==null) {return;}
//		final int pairnum=sl.pairnum();
//		assert(sl.strand()==r.strand());
//		
////		final boolean needsFixing=(varMap!=null && Read.containsVars(r.match));
//		
//		if(r.shortmatch()){r.toLongMatchString(false);}
//		final byte[] match=r.match;
//
//		int subs=0, inss=0, dels=0;
////		final AtomicLongArray matchCounts=lane.matchCounts[pairnum];
////		final AtomicLongArray subCounts=lane.subCounts[pairnum];
//		for(int mpos=0, qpos=0; mpos<match.length; mpos++){
//			byte m=match[mpos];
//			if(m=='m'){
////				matchCounts.incrementAndGet(qpos);
//				qpos++;
//			}else if(m=='S' || m=='N'){
////				subCounts.incrementAndGet(qpos);
//				subs++;
//				qpos++;
//			}else if(m=='I'){
////				subCounts.incrementAndGet(qpos);
//				inss++;
//				qpos++;
//			}else if(m=='X' || m=='Y' || m=='C'){
//				qpos++;
//			}else if(m=='D'){
//				dels++;
//			}else{
//				assert(false) : "Unhandled symbol "+m;
//			}
//		}
//		
//		final MicroTile mt;
//		final Lane lane;
//		ihp.parse(r.id);
//		final int lnum=ihp.lane(), tile=ihp.tile(), x=ihp.xPos(), y=ihp.yPos();
//		synchronized(flowcell) {
//			lane=flowcell.getLane(lnum);
//			mt=lane.getMicroTile(tile, x, y);
//		}
//
//		synchronized(mt) {
//			mt.alignedReadCount++;
//			mt.alignedBaseCount+=r.countAlignedBases();
//			mt.readErrorCount+=((subs+inss+dels>0) ? 1 : 0);
//			mt.baseErrorCount+=(subs+inss);
//			mt.readInsCount+=(inss>0 ? 1 : 0);
//			mt.readDelCount+=(dels>0 ? 1 : 0);
//		}
//	}
	
	//This version is not really any faster,
	//though it does use 15% less CPU-time.
	private void processSamLine(SamLine sl, IlluminaHeaderParser2 ihp) {
		if(sl==null){return;}
//		if(!sl.mapped() && !sl.nextMapped()) {return;} //Probably not PhiX
		if(!sl.mapped()) {return;}//TODO: Track unmapped info; requires modifying dump format
		
		if(!sl.mapped() || sl.seq==null || !sl.hasCigar()) {return;}
		final int pairnum=sl.pairnum();
		
//		final boolean needsFixing=(varMap!=null && Read.containsVars(r.match));
		
		final byte[] shortmatch=sl.toShortMatch(false);
		final byte[] match=Read.toLongMatchString(shortmatch);

		int subs=0, inss=0, dels=0;
		//Atomics are too slow here
//		final AtomicLongArray matchCounts=lane.matchCounts[pairnum];
//		final AtomicLongArray subCounts=lane.subCounts[pairnum];
		for(int mpos=0, qpos=0; mpos<match.length; mpos++){
			byte m=match[mpos];
			if(m=='m'){
//				matchCounts.incrementAndGet(qpos);
				qpos++;
			}else if(m=='S' || m=='N'){
//				subCounts.incrementAndGet(qpos);
				subs++;
				qpos++;
			}else if(m=='I'){
//				subCounts.incrementAndGet(qpos);
				inss++;
				qpos++;
			}else if(m=='X' || m=='Y' || m=='C'){
				qpos++;
			}else if(m=='D'){
				dels++;
			}else{
				assert(false) : "Unhandled symbol "+m;
			}
		}
		
		final MicroTile mt;
		final Lane lane;
		ihp.parse(sl.qname);
		final int lnum=ihp.lane(), tile=ihp.tile(), x=ihp.xPos(), y=ihp.yPos();
		synchronized(flowcell) {
			lane=flowcell.getLane(lnum);
			mt=lane.getMicroTile(tile, x, y);
		}

		synchronized(mt) {
			mt.alignedReadCount++;
			mt.alignedBaseCount+=Read.countAlignedBases(match);
			mt.readErrorCount+=((subs+inss+dels>0) ? 1 : 0);
			mt.baseErrorCount+=(subs+inss);
			mt.readInsCount+=(inss>0 ? 1 : 0);
			mt.readDelCount+=(dels>0 ? 1 : 0);
		}
	}

	/** Create read streams and process all data */
	void processReads(Timer t){
		
		if(ffout1!=null || ffoutbad!=null || coordsOut!=null){
			Timer t2=new Timer();
			outstream.print("Filtering reads:\t");

			//Create a read input stream
			final ConcurrentReadInputStream cris;
			{
				Read.VALIDATE_IN_CONSTRUCTOR=true;
				cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2);
				cris.start(); //Start the stream
				if(verbose){outstream.println("Started cris");}
			}
			boolean paired=cris.paired();
			//		if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));}

			//Optionally read output streams
			final ConcurrentReadOutputStream ros, rosb;
			final int buff=4;
			if(ffout1!=null){
				ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, buff, null, false);
				ros.start(); //Start the stream
			}else{ros=null;}
			
			if(ffoutbad!=null){
				rosb=ConcurrentReadOutputStream.getStream(ffoutbad, null, null, null, buff, null, false);
				rosb.start(); //Start the stream
			}else{rosb=null;}
			
			final ByteStreamWriter coords=(coordsOut==null ? null : 
				ByteStreamWriter.makeBSW(coordsOut, overwrite, append, true));
			
			//Process the read stream
			processInner(cris, ros, rosb, coords);

			if(verbose){outstream.println("Finished; closing streams.");}

			//Close the read streams
			errorState|=ReadWrite.closeStreams(cris, ros, rosb);
			
			if(coords!=null) {
				errorState=coords.poisonAndWait()|errorState;
			}

			t2.stop();
			outstream.println(t2);
		}
		
		//Report timing and results
		{
			t.stop();
			lastReadsOut=readsProcessed-readsDiscarded;
			outstream.println();
			outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
			
			if(ffout1!=null || ffoutbad!=null || coordsOut!=null){

				String rpstring=Tools.padKMB(readsDiscarded, 8);
				String bpstring=Tools.padKMB(basesDiscarded, 8);
				String gpstring=Tools.padKMB(gsTransformedToN, 8);
				outstream.println();
				outstream.println("Reads Discarded:    "+rpstring+" \t"+Tools.format("%.3f%%", readsDiscarded*100.0/readsProcessed));
				outstream.println("Bases Discarded:    "+bpstring+" \t"+Tools.format("%.3f%%", basesDiscarded*100.0/basesProcessed));
				if(gToN){outstream.println("Gs Masked By N:     "+gpstring+" \t"+Tools.format("%.3f%%", gsTransformedToN*100.0/basesProcessed));}
				outstream.println();
			}
		}
		
		//Throw an exception of there was an error in a thread
		if(errorState){
			throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
		}
	}
	
	/** Iterate through the reads */
	void processInner(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros, 
			final ConcurrentReadOutputStream rosb, final ByteStreamWriter coords){
		
		//Do anything necessary prior to processing
		readsProcessed=0;
		basesProcessed=0;
		final IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();
		final ByteBuilder bb=new ByteBuilder();
		{
			//Grab the first ListNum of reads
			ListNum<Read> ln=cris.nextList();
			//Grab the actual read list from the ListNum
			ArrayList<Read> reads=(ln!=null ? ln.list : null);
			
			//Check to ensure pairing is as expected
			if(reads!=null && !reads.isEmpty()){
				Read r=reads.get(0);
				assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
			}
			
			//As long as there is a nonempty read list...
			while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
				if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}

				ArrayList<Read> keepList=new ArrayList<Read>(reads.size());
				ArrayList<Read> tossList=new ArrayList<Read>(4);
				
				//Loop through each read in the list
				for(int idx=0; idx<reads.size(); idx++){
					final Read r1=reads.get(idx);
					final Read r2=r1.mate;
					
					//Track the initial length for statistics
					final int initialLength1=r1.length();
					final int initialLength2=(r1.mateLength());
					
					//Increment counters
					readsProcessed+=r1.pairCount();
					basesProcessed+=initialLength1+initialLength2;
					
					boolean keep=processReadPair(r1, r2);
					if(keep){
						keepList.add(r1);
					}else{
						tossList.add(r1);
						readsDiscarded+=r1.pairCount();
						basesDiscarded+=initialLength1+initialLength2;
					}
				}
				
				//Output reads to the output stream
				if(ros!=null){ros.add(keepList, ln.id);}
				if(rosb!=null){rosb.add(tossList, ln.id);}
				if(coords!=null) {
					bb.clear();
					for(Read r : tossList) {
						ihp.parse(r);
						ihp.appendCoordinates(bb);
						bb.nl();
					}
					if(!bb.isEmpty()) {coords.print(bb);}
				}
				
				//Notify the input stream that the list was used
				cris.returnList(ln);
				if(verbose){outstream.println("Returned a list.");}
				
				//Fetch a new list
				ln=cris.nextList();
				reads=(ln!=null ? ln.list : null);
			}
			
			//Notify the input stream that the final list was used
			if(ln!=null){
				cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
			}
		}
		
		//Do anything necessary after processing
		
	}
	
	/** Iterate through the reads */
	public void loadKmersInner(final ConcurrentReadInputStream cris){
		
		bloomFilter=new BloomFilter(k, k, cbits, hashes, 1, true, false, true, 0.7f);
		
		{
			//Grab the first ListNum of reads
			ListNum<Read> ln=cris.nextList();
			//Grab the actual read list from the ListNum
			ArrayList<Read> reads=(ln!=null ? ln.list : null);
			
			//Check to ensure pairing is as expected
			if(reads!=null && !reads.isEmpty()){
				Read r=reads.get(0);
				assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
			}
			
			LongList kmers=new LongList(300);
			//As long as there is a nonempty read list...
			while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
				if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}

				for(int idx=0; idx<reads.size(); idx++){
					final Read r1=reads.get(idx);
					final Read r2=r1.mate;

					loadKmers(r1, kmers);
					loadKmers(r2, kmers);
				}
				
				//Notify the input stream that the list was used
				cris.returnList(ln);
				if(verbose){outstream.println("Returned a list.");}
				
				//Fetch a new list
				ln=cris.nextList();
				reads=(ln!=null ? ln.list : null);
			}
			
			//Notify the input stream that the final list was used
			if(ln!=null){
				cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
			}
		}
	}
	
	private void loadKmers(Read r, LongList kmers) {
		if(r==null || r.length()<k) {return;}
//		if(!randy.nextBoolean()) {return;}//Speed optimization, I guess
		
		kmers.clear();
		BloomFilter.toKmers(r, kmers, k, 0, 0, true);
		final int idmod=(int)((r.numericID+3)&idmask_write);
		for(int i=0; i<kmers.size; i++) {
			if((i&idmask_write)==idmod) {
				long kmer=kmers.array[i];
				bloomFilter.filter.increment(kmer);
			}
		}
	}
	
	/** Iterate through the reads */
	public void fillTilesInner(final ConcurrentReadInputStream cris, final SamLineStreamer ss){
		Timer t2=new Timer();
		
		if(merge && loadKmers) {fillThreads=Tools.max(fillThreads, fillThreadsM);}
		fillThreads=Tools.min(fillThreads, Shared.threads());
		outstream.print("Filling tiles with "+fillThreads+" threads:  \t");
		spawnThreads(cris, ss);
		
		t2.stop();
		outstream.println(t2);
	}
	
	private void processTileKmers(Read r, IntList klist, IntList blist) {
		fillDepthList(r, klist);
		if(smoothDepths>3) {smooth5(klist);}
		else if(smoothDepths>=3) {smooth3(klist);}
		if(deblurDepths) {deblur(klist, blist);}
	}
	
	//Samples all kmers
	private void fillDepthList(Read r, IntList depths) {
		if(r==null || r.length()<k) {return;}
		final byte[] bases=r.bases;
		final int shift=2*k;
		final int shift2=shift-2;
		final long mask=(-1L)>>>(64-shift);

		long kmer=0, rkmer=0;
		
		depths.clear();
		
		final int idmod=(int)(r.numericID&idmask_read);
		
		//TODO: Debranch using 2 loops; one for 1st k-1 bases, and one for the rest.
		for(int i=0, len=0; i<bases.length; i++) {
			final byte b=bases[i];
			final long x=AminoAcid.baseToNumber[b];
			final long x2=AminoAcid.baseToComplementNumber[b];
			kmer=((kmer<<2)|x)&mask;
			rkmer=x<0 ? 0 : ((rkmer>>>2)|(x2<<shift2))&mask;
			
			len=(x<0 ? 0 : len+1);
			rkmer=(x<0 ? 0 : rkmer);//is this necessary? TODO: Check.
//			if(x<0){len=0; rkmer=0;}else{len++;}//old branchy version
			if(i>=k && ((i&idmask_read)==idmod)) {
				final long key=toKey(kmer, rkmer);
				final int value=(len>=k ? bloomFilter.getCount(key) : 0);
				depths.add(value);
			}
		}
	}
	
	private void smooth3(IntList list) {
		if(list.size<3) {return;}
		final int max=list.size-1;
		{
			int a=list.get(0), b=list.get(1), c=list.get(2);
			a=Tools.min(a, b, c);
			list.set(0, a);
		}
		{
			int a=list.get(max), b=list.get(max-1), c=list.get(max-2);
			a=Tools.min(a, b, c);
			list.set(0, a);
		}
		for(int i=1; i<max; i++) {
			int a=list.get(i-1), b=list.get(i), c=list.get(i+1);
			list.set(i, Tools.min(b, Tools.max(a, c)));
		}
	}
	
	private void smooth5(IntList list) {
		if(list.size<5) {return;}
		final int max=list.size-1;
		{
			int a=list.get(0), b=list.get(1), c=list.get(2), d=list.get(3);
			a=Tools.min(a, b, c);
			b=Tools.min(b, Tools.max(a, Tools.min(c, d)));
			list.set(0, a);
			list.set(1, b);
		}
		{
			int a=list.get(max), b=list.get(max-1), c=list.get(max-2), d=list.get(max-3);
			a=Tools.min(a, b, c);
			b=Tools.min(b, Tools.max(a, Tools.min(c, d)));
			list.set(0, a);
			list.set(1, b);
		}
		for(int i=2; i<max-1; i++) {
			int a=list.get(i-2), b=list.get(i-1), c=list.get(i), d=list.get(i+1), e=list.get(i+2);
			int left=Tools.min(a, b);
			int right=Tools.min(d, e);
			list.set(i, Tools.min(c, Tools.max(left, right)));
		}
	}
	
	void deblur(IntList klist, IntList blist) {
		blist.clearFull();
		blist.set(klist.size+k-1, 0);
		for(int i=0; i<klist.size; i++) {
			int kdepth=klist.get(i);
			//Since max is only 3 this could be much more efficient, without a double loop
			if(kdepth>0) {
				for(int j=i, max=i+k; j<max; j++) {
					blist.set(j, Tools.max(blist.get(i), kdepth));
				}
			}
		}
	}
	
//	//Samples multiple kmers
//	private void processTileKmersSampled(Read r, MicroTile mt, int samples) {
//		if(r.length()<=3*k+1) {samples=1;}
//		samples=Tools.min(samples, r.length()-k2);
//		final int cutoff=(kmersPerRead<1 ? 2 : 1);
//		
//		long depthSum=0;
//		for(int i=0; i<samples; i++) {
//			int value=getKmerCount(r.bases, getPos(r.numericID+74+k3*i, r.length()));
//			depthSum+=value;
//			if(value>=cutoff) {mt.hits++;}
//			else {mt.misses++;}
//		}
//		mt.depthSum+=depthSum;
//	}
//	
//	private int getKmerCount(byte[] bases, int pos) {
//		final int lim=bases.length-k2;
//		pos=pos%(bases.length-k2);
//		assert(pos>=0 && pos<=lim);
//		final long kmer=toKmer(bases, pos, k);
//		if(kmer<0) {return 0;}
//		final long key=toKey(kmer);
//		final int value=bloomFilter.getCount(key);
////		if(maxReads==1) {System.err.println("Got "+value+" for kmer "+key);}
//		return value;
//	}
//	
//	private int getPos(long id, int len) {
//		return (deterministic ? (int)((id)%(len-k2)) : randy.nextInt(len-k2));
//	}
	
	private final long toKey(long kmer) {
		return Tools.max(kmer, AminoAcid.reverseComplementBinaryFast(kmer, k));
//		return (kmersPerRead==1 || kmer==-1 ? kmer : 
//			Tools.max(kmer, AminoAcid.reverseComplementBinaryFast(kmer, k)));
	}
	
	private final long toKey(long kmer, long rkmer) {
		return Tools.max(kmer, rkmer);
	}
	
	/*--------------------------------------------------------------*/
	/*----------------         Inner Methods        ----------------*/
	/*--------------------------------------------------------------*/
	
	boolean processReadPair(final Read r1, final Read r2){
		boolean passes=processReadPair_inner(r1, r2);
		if(passes){return true;}
		if(trimq>0){
			TrimRead.trimFast(r1, trimLeft, trimRight, trimq, trimE, 0);
			if(r2!=null){TrimRead.trimFast(r2, trimLeft, trimRight, trimq, trimE, 0);}
			return r1.length()>=minlen && (r2==null || r2.length()>=minlen);
		}else{
			return false;
		}
	}
	
	/**
	 * Process a single read pair.
	 * @param r1 Read 1
	 * @param r2 Read 2 (may be null)
	 * @return True if the reads should be kept, false if they should be discarded.
	 */
	boolean processReadPair_inner(final Read r1, final Read r2){
		
		MicroTile mt=flowcell.getMicroTile(r1.id);
		if(mt==null){
			if(!warned){
				outstream.println("\nWarning - a read was found with no corresponding MicroTile:\n"+r1.id);
				warned=true;
			}
			return true;
		}
		if(mt.discard<discardLevel){return true;}
		if(!discardOnlyLowQuality){return false;}
		
		if(shouldDiscard(r1, mt)) {return false;}
		if(gToN){gsTransformedToN+=doGToN(r1, mt);}
		
		if(shouldDiscard(r2, mt)) {return false;}
		if(gToN){gsTransformedToN+=doGToN(r2, mt);}
		
		return true;
	}
	
	private boolean shouldDiscard(Read r, MicroTile mt) {
		if(r==null || r.length()<1) {return false;}
		final int len=r.length();
		double qual=r.avgQualityByProbabilityDouble(true, len);
		double prob=100*r.probabilityErrorFree(true, len);
		if(qual<=flowcell.avgQuality-(dmult*TileDump.qDeviations*flowcell.stdQuality)){return true;}
		if(prob<=flowcell.avgErrorFree-(dmult*TileDump.eDeviations*flowcell.stdErrorFree)){return true;}
		if(PolyFilter.polymerLen(r.bases, (byte)'G', 0.16f)>15) {return true;}
		if(discardG && shouldDiscardG(r, mt)){return true;}
		return false;
	}
	
	private boolean shouldDiscardG(Read r, MicroTile mt){
		final byte[] bases=r.bases;
		final float[] gArray=mt.tracker.cycleAverages[2];
		
		final float thresh=(float)(flowcell.avgG+Tools.max(TileDump.gDeviations*flowcell.stdG, 
				flowcell.avgG*TileDump.gFraction, TileDump.gAbs));
		for(int i=0; i<bases.length; i++){
			byte b=bases[i];
			if(b=='G' && gArray[i]>thresh){
				return true;
			}
		}
		return false;
	}
	
	private int doGToN(Read r, MicroTile mt){
		if(r==null || r.length()<1) {return 0;}
		final byte[] bases=r.bases;
		final byte[] quals=r.quality;
		final float[] gArray=mt.tracker.cycleAverages[2];
		
		final float thresh=(float)(flowcell.avgG+Tools.max(TileDump.gDeviations*flowcell.stdG, 
				flowcell.avgG*TileDump.gFraction, TileDump.gAbs));
		int changes=0;
		for(int i=0; i<bases.length; i++){
			byte b=bases[i];
			if(b=='G' && gArray[i]>thresh){
				bases[i]='N';
				changes++;
				if(quals!=null){quals[i]=0;}
			}
		}
		return changes;
	}
		
	/*--------------------------------------------------------------*/
	/*----------------        Helper Methods        ----------------*/
	/*--------------------------------------------------------------*/
	
	/**
	 * Generate a kmer from specified start location
	 * @param bases
	 * @param start
	 * @param klen kmer length
	 * @return kmer
	 */
	private static final long toKmer(final byte[] bases, final int start, final int klen){
		final int stop=start+klen;
		assert(stop<=bases.length) : klen+", "+bases.length;
		long kmer=0;
		
		for(int i=start; i<stop; i++){
			final byte b=bases[i];
			final long x=Dedupe.baseToNumber[b];
			kmer=((kmer<<2)|x);
		}
		return kmer;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------           Barcodes           ----------------*/
	/*--------------------------------------------------------------*/
	
	BarcodeStats loadBarcodes(String expectedBarcodesFile) {
		if(delimiter<0) {
			delimiter=(byte)ffin1.barcodeDelimiter();
			barcodesPerRead=ffin1.barcodesPerRead();
		}
		BarcodeStats bs=new BarcodeStats(delimiter, barcodesPerRead, extin);
		if(bs.length1<1 && bs.length2<1) {
			bs.length1=ffin1.barcodeLength(1);
			bs.length2=ffin1.barcodeLength(2);
		}
		if(expectedBarcodesFile!=null) {
			bs.loadBarcodeList(expectedBarcodesFile, barcodesPerRead>1 ? delimiter : 0, false, false);
		}
		return bs;
	}
	
	private static void dumpBarcodes(Collection<AtomicStringNum> counts, String fname, boolean overwrite) {
		System.err.println("Writing barcode counts.");
		if(fname==null || counts==null) {return;}
		ArrayList<AtomicStringNum> list=new ArrayList<AtomicStringNum>(counts);
		Collections.sort(list);
		Collections.reverse(list);
		long sum=0;
		for(AtomicStringNum asn : list) {sum+=asn.n.get();}
		ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, true);
		bsw.print("#Barcodes\t").print(sum).nl();
		bsw.print("#Unique\t").print(list.size()).nl();
		bsw.print("#Code\tCount\n");
		for(AtomicStringNum count : list) {
			bsw.print(count.s).tab().print(count.n.get()).nl();
		}
		bsw.poisonAndWait();
	}
	
	/*--------------------------------------------------------------*/
	/*----------------       Thread Management      ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Spawn process threads */
	private void spawnThreads(final ConcurrentReadInputStream cris, final SamLineStreamer ss){
		
		//Do anything necessary prior to processing
		
		//Increases concurrency by making flowcell copies
		//2 seems to be sufficient when merging is used. 
		FlowCell[] fca=new FlowCell[Tools.mid(1, (merge || loadKmers ? 2 : 3), (fillThreads+4)/8)];
		fca[0]=flowcell;
		for(int i=1; i<fca.length; i++) {fca[i]=new FlowCell(k);}
		
		//Fill a list with ProcessThreads
		ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(fillThreads);
		for(int i=0; i<fillThreads; i++){
			alpt.add(new ProcessThread(cris, ss, i, fca[(i%fca.length)]));
		}
		//Start the threads and wait for them to finish
		boolean success=ThreadWaiter.startAndWait(alpt, this);
		errorState&=!success;
		
		//Do anything necessary after processing
		
		//Combine flowcell copies
		synchronized(flowcell) {
			for(int i=1; i<fca.length; i++) {
				synchronized(fca[i]) {
					flowcell.add(fca[i]);
				}
			}
		}
	}
	
	@Override
	public final void accumulate(ProcessThread pt){
		synchronized(pt) {
			readsProcessed+=pt.readsProcessedT;
			basesProcessed+=pt.basesProcessedT;
			errorState|=(!pt.success);
			synchronized(flowcell) {
				flowcell.xMin=(flowcell.xMin<0 ? pt.xmin : Tools.min(pt.xmin, flowcell.xMin));
				flowcell.xMax=(flowcell.xMax<0 ? pt.xmax : Tools.max(pt.xmax, flowcell.xMax));
				flowcell.yMin=(flowcell.yMin<0 ? pt.ymin : Tools.min(pt.ymin, flowcell.yMin));
				flowcell.yMax=(flowcell.yMax<0 ? pt.ymax : Tools.max(pt.ymax, flowcell.yMax));
				flowcell.tMin=(flowcell.tMin<0 ? pt.tmin : Tools.min(pt.tmin, flowcell.tMin));
				flowcell.tMax=(flowcell.tMax<0 ? pt.tmax : Tools.max(pt.tmax, flowcell.tMax));
				if(pt.flowcellName!=null) {
					assert(flowcell.name==null || flowcell.name.equals(pt.flowcellName));
					flowcell.name=pt.flowcellName;
				}
//				for(Lane lane : flowcell.lanes) {
//					if(lane!=null) {
//						for(int pairnum=0; pairnum<2; pairnum++) {
//							long[] counts=pt.laneDepthCounts[lane.lane][pairnum];
//							long[] sums=pt.laneDepthSums[lane.lane][pairnum];
//							for(int i=0; i<counts.length; i++) {
//								lane.depthCounts[pairnum].addAndGet(i, counts[i]);
//								lane.depthSums[pairnum].addAndGet(i, sums[i]);
//							}
//						}
//					}
//				}
			}
		}
	}
	
	@Override
	public final boolean success(){return !errorState;}	
	@Override
	public final ReadWriteLock rwlock() {return rwlock;}
	private final ReadWriteLock rwlock=new ReentrantReadWriteLock();
	
	class ProcessThread extends Thread {
		
		//Constructor
		ProcessThread(final ConcurrentReadInputStream cris_, final SamLineStreamer ss_, final int tid_, final FlowCell flowcell_){
			cris=cris_;
			ss=ss_;
			tid=tid_;
			flowcellT=flowcell_;
		}
		
		//Called by start()
		@Override
		public void run(){
			//Do anything necessary prior to processing
			
			//Process the reads
			processInner();
			
			//Do anything necessary after processing
			
			//Indicate successful exit status
			success=true;
		}
		
		/** Iterate through the reads */
		void processInner(){
			
			//Grab the first ListNum of reads
			ListNum<Read> ln=cris.nextList();

			//As long as there is a nonempty read list...
			while(ln!=null && ln.size()>0){
//				if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} //Disabled due to non-static access
				
				processList(ln);
				
				//Notify the input stream that the list was used
				cris.returnList(ln);
//				if(verbose){outstream.println("Returned a list.");} //Disabled due to non-static access
				
				//Fetch a new list
				ln=cris.nextList();
			}

			//Notify the input stream that the final list was used
			if(ln!=null){
				cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
			}
			
			if(ss!=null) {processSam();}
		}
		
		void processList(ListNum<Read> ln){

			//Grab the actual read list from the ListNum
			final ArrayList<Read> reads=ln.list;
			
			//Loop through each read in the list
			for(int idx=0; idx<reads.size(); idx++){
				final Read r1=reads.get(idx);
				final Read r2=r1.mate;
				
				//Validate reads in worker threads
				if(!r1.validated()){r1.validate(true);}
				if(r2!=null && !r2.validated()){r2.validate(true);}

				//Track the initial length for statistics
				final int initialLength1=r1.length();
				final int initialLength2=r1.mateLength();

				//Increment counters
				readsProcessedT+=r1.pairCount();
				basesProcessedT+=initialLength1+initialLength2;
				
				processReadPair(r1, r2);
			}
			if(sidechannel!=null) {sidechannel.writeByStatus(reads, ln.id);}
		}
		
		/**
		 * Process a read or a read pair.
		 * @param r1 Read 1
		 * @param r2 Read 2 (may be null)
		 */
		void processReadPair(final Read r1, final Read r2){
			final int cutoff=(idmask_write<=3 && cbits>1) ? 2 : 1;
			if(recalibrate) {
				CalcTrueQuality.recalibrate(r1);
				CalcTrueQuality.recalibrate(r2);
			}
			
			ihp.parse(r1.id);
			final int lnum=ihp.lane(), tile=ihp.tile(), x=ihp.xPos(), y=ihp.yPos();
			xmin=Tools.min(x, xmin);
			xmax=Tools.max(x, xmax);
			ymin=Tools.min(y, ymin);
			ymax=Tools.max(y, ymax);
			tmin=Tools.min(tile, tmin);
			tmax=Tools.max(tile, tmax);
			if(flowcellName==null) {
				flowcellName=ihp.machine()+":"+ihp.run()+":"+ihp.flowcell();
			}
			
			long hits1=0, hits2=0, misses=0, depthSum=0;
			if(loadKmers){//All kmer processing is outside of the sync block
				processTileKmers(r1, kmerDepths0, baseDepths0);
				processTileKmers(r2, kmerDepths1, baseDepths1);
				
				IntList list0=(deblurDepths ? baseDepths0 : kmerDepths0);
//				long[] counts0=laneDepthCounts[lnum][0];
//				long[] sums0=laneDepthSums[lnum][0];
				for(int i=0; i<list0.size; i++) {
					int d=list0.get(i);
					int hit=(d>=cutoff ? 1 : 0);
					hits1+=hit;
					misses+=(hit^1);//This is clever.  No conditionals!
					depthSum+=d;
					//TODO: This atomic increment is super slow; replace it with locals.
//					lane.depthSums[0].addAndGet(i, d);
//					lane.depthCounts[0].incrementAndGet(i);
//					counts0[i]++;
//					sums0[i]+=d;
				}
//				assert(sums0[0]==0) : Arrays.toString(counts0)+"\n"+Arrays.toString(sums0);
				IntList list1=(deblurDepths ? baseDepths1 : kmerDepths1);
//				long[] counts1=laneDepthCounts[lnum][1];
//				long[] sums1=laneDepthSums[lnum][1];
				for(int i=0; i<list1.size; i++) {
					int d=list1.get(i);
					int hit=(d>=cutoff ? 1 : 0);
					hits2+=hit;
					misses+=(hit^1);
					depthSum+=d;
//					lane.depthSums[1].addAndGet(i, d);
//					lane.depthCounts[1].incrementAndGet(i);
//					counts1[i]++;
//					sums1[i]+=d;
				}
			}
			
			int merged=0;
			int insert=0;
			int overlap=0;
			int mergeErrors=0;
			if(merge && r2!=null) {
				if(strictmerge) {
					insert=BBMerge.findOverlapStrict(r1, r2, false);
				}else {
					insert=BBMerge.findOverlapLoose(r1, r2, false);
				}
				if(insert>0) {
					merged=2;
					overlap=Tools.min(insert, r1.length()+r2.length()-insert);
					mergeErrors=BBMerge.countErrors(r1.bases, r2.bases, insert);
				}else {insert=0;}
			}
			
			if(sidechannel!=null) {
				sidechannel.map(r1, r2);
			}
			
			int bchdist=0;
			int barcodePolymers=0;
			if(barcodeStats!=null) {
				String code=ihp.barcode();
				if(!barcodeStats.expectedCodeList.isEmpty()) {
					bchdist=barcodeStats.calcHdist(code);
				}
				barcodePolymers=Barcode.countPolymers(code);
//				assert(barcodePolymers==0 && !code.contains("GGGGGGGG")) : code+", "+barcodePolymers;
				if(barcodeMap!=null) {
					AtomicStringNum count=barcodeMap.get(code);
					if(count==null) {
						count=new AtomicStringNum(code, 0);
						barcodeMap.put(code, count);
					}
					count.increment();
				}
			}
			
			//Changes hits to misses if the read was a poly-G read.
			//Unwise for short-insert libraries.
			//Probably not necessary, either
			if(changePolyGHitsToMisses && r1!=null && r1.discarded()) {misses+=hits1; hits1=0;}
			if(changePolyGHitsToMisses && r2!=null && r2.discarded()) {misses+=hits2; hits2=0;}
			
			final MicroTile mt;
			synchronized(flowcellT) {
				mt=flowcellT.getMicroTile(lnum, tile, x, y, true);
			}
			
//			boolean addQuick=true; //This has a very minor impact on speed, <5% at t=64
//			double readQualityByProbSum=0, probErrorFreeSum=0, baseErrorProbSum=0;
//			int alignedReadCount=0, alignedBaseCount=0, readErrorCount=0, baseErrorCount=0, readInsCount=0, readDelCount=0;
//			if(addQuick){//Moving things out of the synchronized block to improve threading; changes add to addQuick
//				if(r1!=null) {
//					int len=r1.length();
//					readQualityByProbSum+=r1.avgQualityByProbabilityDouble(true, len);
//					probErrorFreeSum+=100*r1.probabilityErrorFree(true, len);
//					baseErrorProbSum+=r1.expectedErrors(true, len);
//					
//					if(r1.match!=null) {
//						int bc=r1.countAlignedBases();
//						if(bc>0) {
//							alignedReadCount++;
//							alignedBaseCount+=bc;
//							int errors=r1.countErrors();
//							readErrorCount+=(errors>0 ? 1 : 0);
//							baseErrorCount+=errors;
//							int[] mSCNID=Read.countMatchEvents(r1.match);
//							readInsCount+=(mSCNID[4]>0 ? 1 : 0);
//							readDelCount+=(mSCNID[5]>0 ? 1 : 0);
//						}
//					}else if(r1.samline!=null && r1.samline.mapped()) {
//						alignedReadCount++;
//					}
//					
//				}
//				if(r2!=null) {
//					int len=r2.length();
//					readQualityByProbSum+=r2.avgQualityByProbabilityDouble(true, len);
//					probErrorFreeSum+=100*r2.probabilityErrorFree(true, len);
//					baseErrorProbSum+=r2.expectedErrors(true, len);
//					
//					if(r2.match!=null) {
//						int bc=r2.countAlignedBases();
//						if(bc>0) {
//							alignedReadCount++;
//							alignedBaseCount+=bc;
//							int errors=r2.countErrors();
//							readErrorCount+=(errors>0 ? 1 : 0);
//							baseErrorCount+=errors;
//							int[] mSCNID=Read.countMatchEvents(r2.match);
//							readInsCount+=(mSCNID[4]>0 ? 1 : 0);
//							readDelCount+=(mSCNID[5]>0 ? 1 : 0);
//						}
//					}else if(r2.samline!=null && r2.samline.mapped()) {
//						alignedReadCount++;
//					}
//				}
//			}
			
			synchronized(mt) {
//				if(addQuick) {
//					mt.addQuick(r1);
//					mt.addQuick(r2);
//
//					mt.readQualityByProbSum+=readQualityByProbSum;
//					mt.probErrorFreeSum+=probErrorFreeSum;
//					mt.baseErrorProbSum+=baseErrorProbSum;
//
//					mt.alignedReadCount+=alignedReadCount;
//					mt.alignedBaseCount+=alignedBaseCount;
//					mt.readErrorCount+=readErrorCount;
//					mt.baseErrorCount+=baseErrorCount;
//					mt.readInsCount+=readInsCount;
//					mt.readDelCount+=readDelCount;
//				}else {
					mt.add(r1);
					mt.add(r2);
//				}
				
				mt.hits+=(hits1+hits2);
				mt.misses+=misses;
				mt.depthSum+=depthSum;
				
				mt.barcodes+=barcodesPerRead;
				mt.barcodeHDistSum+=bchdist;
				mt.barcodePolymers+=barcodePolymers;
				
				mt.mergedReads+=merged;
				mt.insertSum+=insert;
				mt.overlapSum+=overlap;
				mt.mergeErrorSum+=mergeErrors;
			}
		}
		
		private void processSam() {
			ListNum<SamLine> ln=ss.nextLines();
			ArrayList<SamLine> reads=(ln==null ? null : ln.list);
			final IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();

			while(ln!=null && reads!=null && reads.size()>0){

				for(int idx=0; idx<reads.size(); idx++){
					SamLine sl=reads.get(idx);
					processSamLine(sl, ihp);
				}
				ln=ss.nextLines();
				reads=(ln==null ? null : ln.list);
			}
		}

		/** Number of reads processed by this thread */
		protected long readsProcessedT=0;
		/** Number of bases processed by this thread */
		protected long basesProcessedT=0;

		int xmin=Integer.MAX_VALUE, ymin=Integer.MAX_VALUE, tmin=Integer.MAX_VALUE;
		int xmax=-1, ymax=-1, tmax=-1;
		public String flowcellName=null;
		
		private IntList kmerDepths0=new IntList(151);
		private IntList kmerDepths1=new IntList(151);
		private IntList baseDepths0=new IntList(151);
		private IntList baseDepths1=new IntList(151);

//		long[][][] laneDepthSums=new long[9][2][500];
//		long[][][] laneDepthCounts=new long[9][2][500];
		
		private IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();
		
		/** True only if this thread has completed successfully */
		boolean success=false;

		/** Shared input stream */
		private final ConcurrentReadInputStream cris;
		/** Optional sam input stream */
		private final SamLineStreamer ss;
		/** Thread ID */
		final int tid;
		final FlowCell flowcellT;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------            Fields            ----------------*/
	/*--------------------------------------------------------------*/

	/** Primary input file path */
	private String in1=null;
	/** Secondary input file path */
	private String in2=null;
	
	private ArrayList<String> extra=new ArrayList<String>();

	/** Primary output file path */
	private String out1=null;
	/** Secondary output file path */
	private String out2=null;

	/** Discard output file path */
	private String outbad=null;

	/** Optional aligned reads (e.g. PhiX) */
	private String samInput=null;
	private final boolean processSamMT=true;
	
	/** Override input file extension */
	private String extin=null;
	/** Override output file extension */
	private String extout=null;
	
	/*--------------------------------------------------------------*/	
	
//	private boolean pound=true;
	private String dumpOut=null;
	private String dumpIn=null;
	private String coordsOut=null;
	
	/*--------------------------------------------------------------*/	
	
	private byte delimiter='+';
	private int barcodesPerRead=2;
	private String expectedBarcodes;
	private String barcodeCounts;
	private BarcodeStats barcodeStats;
	
	private ConcurrentHashMap<String, AtomicStringNum> barcodeMap;
	
	/*--------------------------------------------------------------*/
	/*----------------         Side Channel         ----------------*/
	/*--------------------------------------------------------------*/
	
	boolean align=false;
	String alignOut=null;
	String alignRef="phix";
	float alignMinid1=0.66f;
	float alignMinid2=0.56f;
	int alignK1=17; //Phix is unique down to k=13 solid; unsure about gapped
	int alignK2=13;
	int alignMM1=1;
	int alignMM2=1;
	SideChannel3 sidechannel;
	
	/*--------------------------------------------------------------*/	

	/** Number of reads processed */
	public long readsProcessed=0;
	/** Number of bases processed */
	public long basesProcessed=0;

	/** Number of reads discarded */
	public long readsDiscarded=0;
	/** Number of bases discarded */
	public long basesDiscarded=0;
	
	protected long gsTransformedToN=0; 
	
	/** Quit after processing this many input reads; -1 means no limit */
	private long maxReads=-1;
	
	/** Whether interleaved was explicitly set. */
	private boolean setInterleaved=false;

	//Test this; may need to be higher.  Was 16, now 24.
	//Seems unused though, since switching to a mask...
	//And Perlmutter seems to saturate at ~2000% CPU
	private int loadThreads=24;
	//32 was not quite enough on Perlmutter; maybe OK on Dori though
	//64 is not enough with merge enabled
	private int fillThreads=64;
	//128 threads only ran at 5600% utilization...  may be too many.
	private static final int fillThreadsM=96;//threads when merge is enabled
	private BloomFilter bloomFilter;

	int idmask_read=7;
	int idmask_write=15;
	private int smoothDepths=0;
	private boolean deblurDepths=false;
	
	private boolean blurTiles=false;
	
	private boolean loadKmers=true;
//	private int kmersPerRead=0;//for loading
	private float minProb=0;
	private boolean deterministic=true;
	private int cbits=2;
	private int hashes=2;
	
	private boolean recalibrate=false;
	private boolean merge=false;
	private boolean strictmerge=false;
	
	private int targetAverageReads=1600;
	private int targetAlignedReads=250;
	private int targetX=Tile.xSize;
	private int targetY=Tile.ySize;
	
	private static final int k=31;
	
	private FlowCell flowcell;
	
	private boolean changePolyGHitsToMisses=false;
	
	private float dmult=-0.2f;
	
	private boolean discardOnlyLowQuality=true;
	private int discardLevel=1;
	private boolean gToN=false;
	private boolean discardG=false;
	
	private int minlen=30;
	private float trimq=-1;
	private final float trimE;
	private boolean trimLeft=false;
	private boolean trimRight=true;
	
	private boolean warned=false;
	
	/*--------------------------------------------------------------*/
	/*----------------         Final Fields         ----------------*/
	/*--------------------------------------------------------------*/

	/** Primary input file */
	private final FileFormat ffin1;
	/** Secondary input file */
	private final FileFormat ffin2;
	
	/** Primary output file */
	private final FileFormat ffout1;
	/** Secondary output file */
	private final FileFormat ffout2;
	
	/** Output for discarded reads */
	private final FileFormat ffoutbad;
	
	/*--------------------------------------------------------------*/
	/*----------------        Common Fields         ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Number of reads output in the last run */
	public static long lastReadsOut;
	/** Print status messages to this output stream */
	private PrintStream outstream=System.err;
	/** Print verbose messages */
	public static boolean verbose=false;
	/** True if an error was encountered */
	public boolean errorState=false;
	/** Overwrite existing output files */
	private boolean overwrite=true;
	/** Append to existing output files */
	private boolean append=false;
	/** Output reads in input order (possibly todo) */
	private final boolean ordered=false;
	
}