File: gatekeeperCreate.C

package info (click to toggle)
canu 1.7.1+dfsg-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 7,680 kB
  • sloc: cpp: 66,708; perl: 13,682; ansic: 4,020; makefile: 627; sh: 472; python: 39
file content (767 lines) | stat: -rw-r--r-- 24,649 bytes parent folder | download
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

/******************************************************************************
 *
 *  This file is part of canu, a software program that assembles whole-genome
 *  sequencing reads into contigs.
 *
 *  This software is based on:
 *    'Celera Assembler' (http://wgs-assembler.sourceforge.net)
 *    the 'kmer package' (http://kmer.sourceforge.net)
 *  both originally distributed by Applera Corporation under the GNU General
 *  Public License, version 2.
 *
 *  Canu branched from Celera Assembler at its revision 4587.
 *  Canu branched from the kmer project at its revision 1994.
 *
 *  This file is derived from:
 *
 *    src/AS_GKP/gkpStoreCreate.C
 *
 *  Modifications by:
 *
 *    Brian P. Walenz from 2012-FEB-06 to 2013-AUG-01
 *      are Copyright 2012-2013 J. Craig Venter Institute, and
 *      are subject to the GNU General Public License version 2
 *
 *    Brian P. Walenz from 2014-OCT-09 to 2015-AUG-10
 *      are Copyright 2014-2015 Battelle National Biodefense Institute, and
 *      are subject to the BSD 3-Clause License
 *
 *    Brian P. Walenz beginning on 2015-NOV-08
 *      are a 'United States Government Work', and
 *      are released in the public domain
 *
 *  File 'README.licenses' in the root directory of this distribution contains
 *  full conditions and disclaimers for each license.
 */

#include "AS_global.H"
#include "gkStore.H"
#include "findKeyAndValue.H"
#include "AS_UTL_fileIO.H"


#undef  UPCASE  //  Don't convert lowercase to uppercase, special case for testing alignments.
#define UPCASE  //  Convert lowercase to uppercase.  Probably needed.



//  Canu doesn't really use QVs as of late 2015.  Only consensus is aware of them, but since read
//  correction doesn't output QVs, the QVs that consensus sees are totally bogus.  So, disable
//  storage of them - gatekeeperCreate will store a default QV for every read, and consensus will
//  process as usual.
//
#define  DO_NOT_STORE_QVs



//  Support fastq of fasta, even in the same file.
//  Eventually want to support bax.h5 natively.


uint32  validSeq[256] = {0};


uint32
loadFASTA(char                 *L,
          char                 *H,
          char                 *S,
          uint32               &Slen,
          uint8                *Q,
          compressedFileReader *F,
          FILE                 *errorLog,
          uint32               &nWARNS) {
  uint32  nLines = 0;     //  Lines read from the input
  uint32  nBases = 0;     //  Bases read from the input, used for reporting errors
  bool    valid  = true;

  //  We've already read the header.  It's in L.  But we want to use L to load the sequence, so the
  //  header is copied to H.  We need to do a copy anyway - we need to return the next header in L.
  //  We can either copy L to H now, or use H to read lines and copy H to L at the end.  We're stuck
  //  either way.

  strcpy(H, L + 1);

  //  Clear the sequence.

  S[0] = 0;
  Q[0] = 255;  //  Sentinel to tell gatekeeper to use the fixed QV value

  Slen = 0;

  //  Load sequence.  This is a bit tricky, since we need to peek ahead
  //  and stop reading before the next header is loaded.  Instead, we read the
  //  next line into what we'd read the header into outside here.

  fgets(L, AS_MAX_READLEN+1, F->file());  nLines++;
  chomp(L);

  //  Catch empty reads - reads with no sequence line at all.

  if (L[0] == '>') {
    fprintf(errorLog, "read '%s' is empty.\n", H);
    nWARNS++;
    return(nLines);
  }

  //  Copy in the sequence, as long as it is valid sequence.  If any invalid letters
  //  are found, set the base to 'N'.

  uint32  baseErrors = 0;

  while ((!feof(F->file())) && (L[0] != '>')) {
    nBases += strlen(L);  //  Could do this in the loop below, but it makes it ugly.

    for (uint32 i=0; (Slen < AS_MAX_READLEN) && (L[i] != 0); i++) {
      switch (L[i]) {
#ifdef UPCASE
        case 'a':   S[Slen] = 'A';  break;
        case 'c':   S[Slen] = 'C';  break;
        case 'g':   S[Slen] = 'G';  break;
        case 't':   S[Slen] = 'T';  break;
        case 'u':   S[Slen] = 'T';  break;
#else
        case 'a':   S[Slen] = 'a';  break;
        case 'c':   S[Slen] = 'c';  break;
        case 'g':   S[Slen] = 'g';  break;
        case 't':   S[Slen] = 't';  break;
        case 'u':   S[Slen] = 't';  break;
#endif
        case 'A':   S[Slen] = 'A';  break;
        case 'C':   S[Slen] = 'C';  break;
        case 'G':   S[Slen] = 'G';  break;
        case 'T':   S[Slen] = 'T';  break;
        case 'U':   S[Slen] = 'T';  break;
        case 'n':   S[Slen] = 'N';  break;
        case 'N':   S[Slen] = 'N';  break;
        default:
          baseErrors++;
          S[Slen]   = 'N';
          break;
      }

      Slen++;
    }

    //  Grab the next line.  It should be more sequence, or the next header, or eof.
    //  The last two are stop conditions for the while loop.

    L[0] = 0;

    fgets(L, AS_MAX_READLEN+1, F->file());  nLines++;
    chomp(L);
  }

  //  Terminate the sequence.

  S[Slen] = 0;

  //  Report errors.

  if (baseErrors > 0) {
    fprintf(errorLog, "read '%s' has " F_U32 " invalid base%s.  Converted to 'N'.\n",
            H, baseErrors, (baseErrors > 1) ? "s" : "");
    nWARNS++;
  }

  if (Slen == 0) {
    fprintf(errorLog, "read '%s' is empty.\n", H);
    nWARNS++;
  }

  if (Slen != nBases) {
    fprintf(errorLog, "read '%s' is too long; contains %u bases, but we can only handle %u.\n", H, nBases, AS_MAX_READLEN);
    nWARNS++;
  }

  //  Do NOT clear L, it contains the next header.

  return(nLines);
}



uint32
loadFASTQ(char                 *L,
          char                 *H,
          char                 *S,
          uint32               &Slen,
          uint8                *Q,
          compressedFileReader *F,
          FILE                 *errorLog,
          uint32               &nWARNS) {

  //  We've already read the header.  It's in L.

  strcpy(H, L + 1);

  //  Load sequence.

  S[0] = 0;
  Slen = 0;

  S[AS_MAX_READLEN+1-2] = 0;  //  If this is ever set, the read is probably longer than we can support.
  S[AS_MAX_READLEN+1-1] = 0;  //  This will always be zero; fgets() sets it.

  Q[AS_MAX_READLEN+1-2] = 0;  //  This too.
  Q[AS_MAX_READLEN+1-1] = 0;

  fgets(S, AS_MAX_READLEN+1, F->file());
  chomp(S);

  //  Check for long reads.  If found, read the rest of the line, and report an error.  The -1 (in
  //  the print) is because fgets() and strlen() will count the newline, which isn't a base.

  if ((S[AS_MAX_READLEN+1-2] != 0) && (S[AS_MAX_READLEN+1-2] != '\n')) {
    char    *overflow = new char [1048576];
    uint32   nBases   = AS_MAX_READLEN;

    do {
      overflow[1048576-2] = 0;
      overflow[1048576-1] = 0;
      fgets(overflow, 1048576, F->file());
      nBases += strlen(overflow);
    } while (overflow[1048576-2] != 0);

    fprintf(errorLog, "read '%s' is too long; contains %u bases, but we can only handle %u.\n", H, nBases-1, AS_MAX_READLEN);

    nWARNS++;

    delete [] overflow;
  }

  //  Check for and correct invalid bases.

  uint32 baseErrors = 0;

  for (uint32 i=0; (Slen < AS_MAX_READLEN) && (S[i] != 0); i++) {
    switch (S[i]) {
#ifdef UPCASE
      case 'a':   S[i] = 'A';  break;
      case 'c':   S[i] = 'C';  break;
      case 'g':   S[i] = 'G';  break;
      case 't':   S[i] = 'T';  break;
#else
      case 'a':                break;
      case 'c':                break;
      case 'g':                break;
      case 't':                break;
#endif
      case 'A':                break;
      case 'C':                break;
      case 'G':                break;
      case 'T':                break;
      case 'n':   S[i] = 'N';  break;
      case 'N':                break;
      default:
        S[i] = 'N';
        Q[i] = '!';  //  QV=0, ASCII=33
        baseErrors++;
        break;
    }

    Slen++;
  }

  if (baseErrors > 0) {
        fprintf(errorLog, "read '%s' has " F_U32 " invalid base%s.  Converted to 'N'.\n",
                L, baseErrors, (baseErrors > 1) ? "s" : "");
    nWARNS++;
  }

  //  Load the qv header, and then load the qvs themselves over the header.

  L[0] = 0;
  fgets(L, AS_MAX_READLEN+1, F->file());   //  NOTE: Using L, not Q, since L is char and Q is uint8
  fgets(L, AS_MAX_READLEN+1, F->file());
  chomp(L);

  //  As with the base, we need to suck in the rest of the longer-than-allowed QV string.  But we don't need to report it
  //  or do anything fancy, just advance the file pointer.

  if ((Q[AS_MAX_READLEN-1] != 0) && (Q[AS_MAX_READLEN-1] != '\n')) {
    char    *overflow = new char [1048576];

    do {
      overflow[1048576-2] = 0;
      overflow[1048576-1] = 0;
      fgets(overflow, 1048576, F->file());
    } while (overflow[1048576-2] != 0);

    delete [] overflow;
  }

  //  If we're not using QVs, just terminate the sequence.

  Q[0] = 255;  //  Sentinel to tell gatekeeper to use the fixed QV value

  //  But if we are storing QVs, check lengths and convert from letters to integers

#ifndef DO_NOT_STORE_QVs
  uint32   sLen = strlen(S);
  uint32   qLen = strlen(L);

  if (sLen < qLen) {
    fprintf(errorLog, "read '%s' sequence length %u quality length %u; quality values trimmed.\n",
            H, sLen, qLen);
    nWARNS++;
    L[sLen] = 0;
  }

  if (sLen > qLen) {
    fprintf(errorLog, "read '%s' sequence length %u quality length %u; sequence trimmed.\n",
            H, sLen, qLen);
    nWARNS++;
    S[qLen] = 0;
  }

  uint32 QVerrors = 0;

  for (uint32 i=0; L[i]; i++) {
    if (L[i] < '!') {  //  QV=0, ASCII=33
      L[i] = '!';
      QVerrors++;
    }

    if (L[i] > '!' + 60) {  //  QV=60, ASCII=93=']'
      L[i] = '!' + 60;
      QVerrors++;
    }

    Q[i] = L[i] - '!';
  }

  if (QVerrors > 0) {
    fprintf(errorLog, "read '%s' has " F_U32 " invalid QV%s.  Converted to min or max value.\n",
            L, QVerrors, (QVerrors > 1) ? "s" : "");
    nWARNS++;
  }
#endif

  //  Clear the lines, so we can load the next one.

  L[0] = 0;

  return(4);  //  FASTQ always reads exactly four lines
}





void
loadReads(gkStore    *gkpStore,
          gkLibrary  *gkpLibrary,
          uint32      gkpFileID,
          uint32      minReadLength,
          FILE       *nameMap,
          FILE       *loadLog,
          FILE       *errorLog,
          char       *fileName,
          uint32     &nWARNS,
          uint32     &nLOADED,
          uint64     &bLOADED,
          uint32     &nSKIPPED,
          uint64     &bSKIPPED) {
  char    *L = new char  [AS_MAX_READLEN + 1];  //  +1.  One for the newline, and one for the terminating nul.
  char    *H = new char  [AS_MAX_READLEN + 1];
  char    *S = new char  [AS_MAX_READLEN + 1];
  uint8   *Q = new uint8 [AS_MAX_READLEN + 1];

  uint32   Slen = 0;

  uint64   lineNumber = 1;

  fprintf(stderr, "\n");
  fprintf(stderr, "  Loading reads from '%s'\n", fileName);

  fprintf(loadLog, "nam " F_U32 " %s\n", gkpFileID, fileName);

  fprintf(loadLog, "lib preset=N/A");
  fprintf(loadLog,    " defaultQV=%u",            gkpLibrary->gkLibrary_defaultQV());
  fprintf(loadLog,    " isNonRandom=%s",          gkpLibrary->gkLibrary_isNonRandom()          ? "true" : "false");
  fprintf(loadLog,    " removeDuplicateReads=%s", gkpLibrary->gkLibrary_removeDuplicateReads() ? "true" : "false");
  fprintf(loadLog,    " finalTrim=%s",            gkpLibrary->gkLibrary_finalTrim()            ? "true" : "false");
  fprintf(loadLog,    " removeSpurReads=%s",      gkpLibrary->gkLibrary_removeSpurReads()      ? "true" : "false");
  fprintf(loadLog,    " removeChimericReads=%s",  gkpLibrary->gkLibrary_removeChimericReads()  ? "true" : "false");
  fprintf(loadLog,    " checkForSubReads=%s\n",   gkpLibrary->gkLibrary_checkForSubReads()     ? "true" : "false");

  compressedFileReader *F = new compressedFileReader(fileName);

  uint32   nFASTAlocal    = 0;  //  number of sequences read from disk
  uint32   nFASTQlocal    = 0;
  uint32   nWARNSlocal    = 0;

  uint32   nLOADEDAlocal  = 0;  //  Sequences actaully loaded into the store
  uint32   nLOADEDQlocal  = 0;

  uint64   bLOADEDAlocal  = 0;
  uint64   bLOADEDQlocal  = 0;

  uint32   nSKIPPEDAlocal = 0;  //  Sequences skipped because they are too short
  uint32   nSKIPPEDQlocal = 0;

  uint64   bSKIPPEDAlocal = 0;
  uint64   bSKIPPEDQlocal = 0;

  fgets(L, AS_MAX_READLEN+1, F->file());
  chomp(L);

  while (!feof(F->file())) {
    bool  isFASTA = false;
    bool  isFASTQ = false;

    if      (L[0] == '>') {
      lineNumber += loadFASTA(L, H, S, Slen, Q, F, errorLog, nWARNSlocal);
      isFASTA = true;
      nFASTAlocal++;
    }

    else if (L[0] == '@') {
      lineNumber += loadFASTQ(L, H, S, Slen, Q, F, errorLog, nWARNSlocal);
      isFASTQ = true;
      nFASTQlocal++;
    }

    else {
      fprintf(errorLog, "invalid read header '%.40s%s' in file '%s' at line " F_U64 ", skipping.\n",
              L, (strlen(L) > 80) ? "..." : "", fileName, lineNumber);
      L[0] = 0;
      nWARNSlocal++;
    }

    //  If S[0] isn't nul, we loaded a sequence and need to store it.

    if (Slen < minReadLength) {
      fprintf(errorLog, "read '%s' of length " F_U32 " in file '%s' at line " F_U64 " is too short, skipping.\n",
              H, Slen, fileName, lineNumber);

      if (isFASTA) {
        nSKIPPEDAlocal += 1;
        bSKIPPEDAlocal += Slen;
      }

      if (isFASTQ) {
        nSKIPPEDQlocal += 1;
        bSKIPPEDQlocal += Slen;
      }

      S[0] = 0;
      Q[0] = 0;
    }

    if (S[0] != 0) {
      gkReadData *readData = gkpStore->gkStore_addEmptyRead(gkpLibrary);

      readData->gkReadData_setName(H);
      readData->gkReadData_setBasesQuals(S, Q);

      gkpStore->gkStore_stashReadData(readData);

      delete readData;

      if (isFASTA) {
        nLOADEDAlocal += 1;
        bLOADEDAlocal += Slen;
      }

      if (isFASTQ) {
        nLOADEDQlocal += 1;
        bLOADEDQlocal += Slen;
      }

      fprintf(nameMap, F_U32"\t%s\n", gkpStore->gkStore_getNumReads(), H);
    }

    //  If L[0] is nul, we need to load the next line.  If not, the next line is the header (from
    //  the fasta loader).

    if (L[0] == 0) {
      fgets(L, AS_MAX_READLEN+1, F->file());  lineNumber++;
      chomp(L);
    }
  }

  delete    F;

  delete [] Q;
  delete [] S;
  delete [] H;
  delete [] L;

  lineNumber--;  //  The last fgets() returns EOF, but we still count the line.

  //  Write status to the screen

  fprintf(stderr, "    Processed " F_U64 " lines.\n", lineNumber);

  fprintf(stderr, "    Loaded " F_U64 " bp from:\n", bLOADEDAlocal + bLOADEDQlocal);
  if (nFASTAlocal > 0)
    fprintf(stderr, "      " F_U32 " FASTA format reads (" F_U64 " bp).\n", nFASTAlocal, bLOADEDAlocal);
  if (nFASTQlocal > 0)
    fprintf(stderr, "      " F_U32 " FASTQ format reads (" F_U64 " bp).\n", nFASTQlocal, bLOADEDQlocal);

  if (nWARNSlocal > 0)
    fprintf(stderr, "    WARNING: " F_U32 " reads issued a warning.\n", nWARNSlocal);

  if (nSKIPPEDAlocal > 0)
    fprintf(stderr, "    WARNING: " F_U32 " reads (%0.4f%%) with " F_U64 " bp (%0.4f%%) were too short (< " F_U32 "bp) and were ignored.\n",
            nSKIPPEDAlocal, 100.0 * nSKIPPEDAlocal / (nSKIPPEDAlocal + nLOADEDAlocal),
            bSKIPPEDAlocal, 100.0 * bSKIPPEDAlocal / (bSKIPPEDAlocal + bLOADEDAlocal),
            minReadLength);

  if (nSKIPPEDQlocal > 0)
    fprintf(stderr, "    WARNING: " F_U32 " reads (%0.4f%%) with " F_U64 " bp (%0.4f%%) were too short (< " F_U32 "bp) and were ignored.\n",
            nSKIPPEDQlocal, 100.0 * nSKIPPEDQlocal / (nSKIPPEDQlocal + nLOADEDQlocal),
            bSKIPPEDQlocal, 100.0 * bSKIPPEDQlocal / (bSKIPPEDQlocal + bLOADEDQlocal),
            minReadLength);

  //  Write status to HTML

  fprintf(loadLog, "dat " F_U32 " " F_U64 " " F_U32 " " F_U64 " " F_U32 " " F_U64 " " F_U32 " " F_U64 " " F_U32 "\n",
          nLOADEDAlocal, bLOADEDAlocal,
          nSKIPPEDAlocal, bSKIPPEDAlocal,
          nLOADEDQlocal, bLOADEDQlocal,
          nSKIPPEDQlocal, bSKIPPEDQlocal,
          nWARNSlocal);

  //  Add the just loaded numbers to the global numbers

  nWARNS   += nWARNSlocal;

  nLOADED  += nLOADEDAlocal + nLOADEDQlocal;
  bLOADED  += bLOADEDAlocal + bLOADEDQlocal;

  nSKIPPED += nSKIPPEDAlocal + nSKIPPEDQlocal;
  bSKIPPED += bSKIPPEDAlocal + bSKIPPEDQlocal;
};




int
main(int argc, char **argv) {
  char            *gkpStoreName      = NULL;
  char            *outPrefix         = NULL;
  gkStore_mode     mode              = gkStore_create;

  uint32           minReadLength     = 0;

  uint32           firstFileArg      = 0;

  char             errorLogName[FILENAME_MAX];
  char             loadLogName[FILENAME_MAX];
  char             nameMapName[FILENAME_MAX];

  argc = AS_configure(argc, argv);

  int arg = 1;
  int err = 0;
  while (arg < argc) {
    if        (strcmp(argv[arg], "-o") == 0) {  //  This previously used gkStore_append here, but if
      mode         = gkStore_create;            //  two instances of gatekeeperCreate were run in the
      gkpStoreName = argv[++arg];               //  same directory, they would clobber each other,
                                                //  generating a blobs file that is a mix of both.
    } else if (strcmp(argv[arg], "-a") == 0) {  //  So now the -c will fail if any trace of a store
      mode         = gkStore_extend;            //  exists in the output location, and -a will
      gkpStoreName = argv[++arg];               //  blindly add reads to an existing set of files

    } else if (strcmp(argv[arg], "-minlength") == 0) {
      minReadLength = atoi(argv[++arg]);

    } else if (strcmp(argv[arg], "--") == 0) {
      firstFileArg = arg++;
      break;

    } else if (argv[arg][0] == '-') {
      fprintf(stderr, "ERROR: unknown option '%s'\n", argv[arg]);
      err++;

    } else {
      firstFileArg = arg;
      break;
    }
    arg++;
  }

  if (gkpStoreName == NULL)
    err++;
  if (firstFileArg == 0)
    err++;

  if (err) {
    fprintf(stderr, "usage: %s [-minlength L] -o gkpStore input.gkp\n", argv[0]);
    fprintf(stderr, "  -o gkpStore            load raw reads into new gkpStore\n");
    fprintf(stderr, "  -minlength L           discard reads shorter than L\n");
    fprintf(stderr, "  \n");

    if (gkpStoreName == NULL)
      fprintf(stderr, "ERROR: no gkpStore (-o) supplied.\n");
    if (firstFileArg == 0)
      fprintf(stderr, "ERROR: no input files supplied.\n");

    exit(1);
  }


  gkStore     *gkpStore     = gkStore::gkStore_open(gkpStoreName, mode);
  gkRead      *gkpRead      = NULL;
  gkLibrary   *gkpLibrary   = NULL;
  uint32       gkpFileID    = 0;      //  Used for HTML output, an ID for each file loaded.

  uint32       inLineLen    = 1024;
  char         inLine[1024] = { 0 };

  validSeq['a'] = validSeq['c'] = validSeq['g'] = validSeq['t'] = validSeq['n'] = 1;
  validSeq['A'] = validSeq['C'] = validSeq['G'] = validSeq['T'] = validSeq['N'] = 1;

  errno = 0;

  snprintf(errorLogName, FILENAME_MAX, "%s/errorLog",    gkpStoreName);
  FILE    *errorLog = fopen(errorLogName, "w");
  if (errno)
    fprintf(stderr, "ERROR:  cannot open error file '%s': %s\n", errorLogName, strerror(errno)), exit(1);

  snprintf(loadLogName, FILENAME_MAX,   "%s/load.dat", gkpStoreName);
  FILE    *loadLog   = fopen(loadLogName,   "w");
  if (errno)
    fprintf(stderr, "ERROR:  cannot open uid map file '%s': %s\n", loadLogName, strerror(errno)), exit(1);

  snprintf(nameMapName, FILENAME_MAX,   "%s/readNames.txt", gkpStoreName);
  FILE    *nameMap   = fopen(nameMapName,   "w");
  if (errno)
    fprintf(stderr, "ERROR:  cannot open uid map file '%s': %s\n", nameMapName, strerror(errno)), exit(1);

  uint32  nERROR   = 0;  //  There aren't any errors, we just exit fatally if encountered.
  uint32  nWARNS   = 0;

  uint32  nLOADED  = 0;  //  Reads loaded
  uint64  bLOADED  = 0;  //  Bases loaded

  uint32  nSKIPPED = 0;
  uint64  bSKIPPED = 0;  //  Bases not loaded, too short

  for (; firstFileArg < argc; firstFileArg++) {
    fprintf(stderr, "\n");
    fprintf(stderr, "Starting file '%s'.\n", argv[firstFileArg]);

    compressedFileReader *inFile = new compressedFileReader(argv[firstFileArg]);
    char                 *line   = new char [10240];
    char                 *linekv = new char [10240];
    KeyAndValue           keyval;

    while (fgets(line, 10240, inFile->file()) != NULL) {
      chomp(line);
      strcpy(linekv, line);  //  keyval.find() modifies the input line, adding a nul byte to split the key and value.
      keyval.find(linekv);

      if (keyval.key() == NULL) {
        //  No key, so must be a comment or blank line
        continue;
      }

      if (strcasecmp(keyval.key(), "name") == 0) {
        gkpLibrary = gkpStore->gkStore_addEmptyLibrary(keyval.value());
        continue;
      }

      //  We'd better have a gkpLibrary defined, if not, the .gkp input file is incorrect.
      if (gkpLibrary == NULL) {
        fprintf(stderr, "WARNING: no 'name' tag in gkp input; creating library with name 'DEFAULT'.\n");
        gkpLibrary = gkpStore->gkStore_addEmptyLibrary(keyval.value());
        nWARNS++;
      }

      if        (strcasecmp(keyval.key(), "preset") == 0) {
        gkpLibrary->gkLibrary_parsePreset(keyval.value());

      } else if (strcasecmp(keyval.key(), "qv") == 0) {
        gkpLibrary->gkLibrary_setDefaultQV(keyval.value_double());

      } else if (strcasecmp(keyval.key(), "isNonRandom") == 0) {
        gkpLibrary->gkLibrary_setIsNonRandom(keyval.value_bool());

      } else if (strcasecmp(keyval.key(), "readType") == 0) {
        gkpLibrary->gkLibrary_setReadType(keyval.value());

      } else if (strcasecmp(keyval.key(), "removeDuplicateReads") == 0) {
        gkpLibrary->gkLibrary_setRemoveDuplicateReads(keyval.value_bool());

      } else if (strcasecmp(keyval.key(), "finalTrim") == 0) {
        gkpLibrary->gkLibrary_setFinalTrim(keyval.value());

      } else if (strcasecmp(keyval.key(), "removeSpurReads") == 0) {
        gkpLibrary->gkLibrary_setRemoveSpurReads(keyval.value_bool());

      } else if (strcasecmp(keyval.key(), "removeChimericReads") == 0) {
        gkpLibrary->gkLibrary_setRemoveChimericReads(keyval.value_bool());

      } else if (strcasecmp(keyval.key(), "checkForSubReads") == 0) {
        gkpLibrary->gkLibrary_setCheckForSubReads(keyval.value_bool());

      } else if (AS_UTL_fileExists(line, false, false)) {
        loadReads(gkpStore,
                  gkpLibrary,
                  gkpFileID++,
                  minReadLength,
                  nameMap,
                  loadLog,
                  errorLog,
                  line,
                  nWARNS, nLOADED, bLOADED, nSKIPPED, bSKIPPED);

      } else {
        fprintf(stderr, "ERROR:  option '%s' not recognized, and not a file of reads.\n", line);
        exit(1);
      }
    }

    delete    inFile;
    delete [] line;
    delete [] linekv;
  }

  gkpStore->gkStore_close();

  AS_UTL_closeFile(nameMap, nameMapName);
  AS_UTL_closeFile(errorLog, errorLogName);

  fprintf(stderr, "\n");
  fprintf(stderr, "Finished with:\n");
  fprintf(stderr, "  " F_U32 " warnings (bad base or qv, too short, too long)\n", nWARNS);
  fprintf(stderr, "\n");
  fprintf(stderr, "Loaded into store:\n");
  fprintf(stderr, "  " F_U64 " bp.\n",    bLOADED);
  fprintf(stderr, "  " F_U32 " reads.\n", nLOADED);
  fprintf(stderr, "\n");
  fprintf(stderr, "Skipped (too short):\n");
  fprintf(stderr, "  " F_U64 " bp (%.4f%%).\n",    bSKIPPED, (bSKIPPED + bLOADED > 0) ? (100.0 * bSKIPPED / (bSKIPPED + bLOADED)) : 0);
  fprintf(stderr, "  " F_U32 " reads (%.4f%%).\n", nSKIPPED, (nSKIPPED + nLOADED > 0) ? (100.0 * nSKIPPED / (nSKIPPED + nLOADED)) : 0);
  fprintf(stderr, "\n");
  fprintf(stderr, "\n");
  fprintf(loadLog, "sum " F_U32 " " F_U64 " " F_U32 " " F_U64 " " F_U32 "\n", nLOADED, bLOADED, nSKIPPED, bSKIPPED, nWARNS);

  AS_UTL_closeFile(loadLog, loadLogName);

  if (nERROR > 0)
    fprintf(stderr, "gatekeeperCreate did NOT finish successfully; too many errors.\n");

  if (bSKIPPED > 0.25 * (bSKIPPED + bLOADED))
    fprintf(stderr, "gatekeeperCreate did NOT finish successfully; too many bases skipped.  Check your reads.\n");

  if (nWARNS > 0.25 * (nLOADED))
    fprintf(stderr, "gatekeeperCreate did NOT finish successfully; too many warnings.  Check your reads.\n");

  if (nSKIPPED > 0.50 * (nLOADED))
    fprintf(stderr, "gatekeeperCreate did NOT finish successfully; too many short reads.  Check your reads!\n");

  if ((nERROR > 0) ||
      (bSKIPPED > 0.25 * (bSKIPPED + bLOADED)) ||
      (nWARNS   > 0.25 * (nSKIPPED + nLOADED)) ||
      (nSKIPPED > 0.50 * (nSKIPPED + nLOADED)))
    exit(0);

  fprintf(stderr, "gatekeeperCreate finished successfully.\n");

  exit(0);
}