File: gatekeeperDumpFASTQ.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 (513 lines) | stat: -rw-r--r-- 15,468 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

/******************************************************************************
 *
 *  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/gkpStoreDumpFASTQ.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-NOV-21 to 2015-JUN-03
 *      are Copyright 2014-2015 Battelle National Biodefense Institute, and
 *      are subject to the BSD 3-Clause License
 *
 *    Brian P. Walenz beginning on 2015-NOV-23
 *      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 "AS_UTL_decodeRange.H"
#include "AS_UTL_fileIO.H"
#include "AS_UTL_fasta.H"

#include "clearRangeFile.H"

//  Write sequence in multiple formats.  This used to write to four fastq files, the .1, .2, .paired and .unmated.
//  It's left around for future expansion to .fastq and .bax.h5.
//
class libOutput {
public:
  libOutput(char const *outPrefix, char const *outSuffix, char const *libName = NULL) {
    strcpy(_p, outPrefix);

    if (outSuffix[0])
      snprintf(_s, FILENAME_MAX, ".%s", outSuffix);
    else
      _s[0] = 0;

    if (libName)
      strncpy(_n, libName, FILENAME_MAX-1);
    else
      _n[0] = 0;

    _WRITER = NULL;
    _FASTA  = NULL;
    _FASTQ  = NULL;
  };

  ~libOutput() {
    if (_WRITER)
      delete _WRITER;
  };

  FILE  *getFASTQ(void) {

    if (_FASTQ)
      return(_FASTQ);

    return(openFASTQ());
  };

  FILE  *openFASTQ(void) {
    char  N[FILENAME_MAX];

    if (_n[0])
      snprintf(N, FILENAME_MAX, "%s.%s.fastq%s", _p, _n, _s);
    else
      snprintf(N, FILENAME_MAX, "%s.fastq%s", _p, _s);

    if ((_p[0] == '-') && (_p[1] == 0)) {
      snprintf(N, FILENAME_MAX, "(stdout)");
      _FASTQ = stdout;
    }

    else {
      _WRITER = new compressedFileWriter(N);
      _FASTQ  = _WRITER->file();
    }

    return(_FASTQ);
  };


  FILE  *getFASTA(void) {

    if (_FASTA)
      return(_FASTA);

    return(openFASTA());
  };

  FILE  *openFASTA(void) {
    char  N[FILENAME_MAX];

    if (_n[0])
      snprintf(N, FILENAME_MAX, "%s.%s.fasta%s", _p, _n, _s);
    else
      snprintf(N, FILENAME_MAX, "%s.fasta%s", _p, _s);

    if ((_p[0] == '-') && (_p[1] == 0)) {
      snprintf(N, FILENAME_MAX, "(stdout)");
      _FASTA = stdout;
    }

    else {
      _WRITER = new compressedFileWriter(N);
      _FASTA  = _WRITER->file();
    }

    return(_FASTA);
  };

private:
  char   _p[FILENAME_MAX];
  char   _s[FILENAME_MAX];
  char   _n[FILENAME_MAX];

  compressedFileWriter  *_WRITER;
  FILE                  *_FASTA;
  FILE                  *_FASTQ;
};




char *
scanPrefix(char *prefix) {
  int32 len = strlen(prefix);

  if ((len > 3) && (strcasecmp(prefix + len - 3, ".gz") == 0)) {
    prefix[len-3] = 0;
    return(prefix + len - 2);
  }

  if ((len > 4) && (strcasecmp(prefix + len - 4, ".bz2") == 0)) {
    prefix[len-4] = 0;
    return(prefix + len - 3);
  }

  if ((len > 3) && (strcasecmp(prefix + len - 3, ".xz") == 0)) {
    prefix[len-3] = 0;
    return(prefix + len - 2);
  }

  return(prefix + len);
}




int
main(int argc, char **argv) {
  char            *gkpStoreName      = NULL;
  uint32           gkpStorePart      = UINT32_MAX;

  char            *outPrefix         = NULL;
  char            *outSuffix         = NULL;

  char            *clrName           = NULL;

  uint32           libToDump         = 0;

  uint32           bgnID             = 1;
  uint32           endID             = UINT32_MAX;

  bool             dumpRaw           = false;
  bool             dumpCorrected     = false;
  bool             dumpTrimmed       = false;

  bool             dumpAllReads      = false;
  bool             dumpAllBases      = false;
  bool             dumpOnlyDeleted   = false;

  bool             dumpFASTQ         = true;
  bool             dumpFASTA         = false;

  bool             withLibName       = true;
  bool             withReadName      = true;

  argc = AS_configure(argc, argv);

  int arg = 1;
  int err = 0;
  while (arg < argc) {
    if        (strcmp(argv[arg], "-G") == 0) {
      gkpStoreName = argv[++arg];

      if ((arg+1 < argc) && (argv[arg+1][0] != '-'))
        gkpStorePart = atoi(argv[++arg]);

    } else if (strcmp(argv[arg], "-o") == 0) {
      outPrefix = argv[++arg];
      outSuffix = scanPrefix(outPrefix);


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


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


    } else if (strcmp(argv[arg], "-b") == 0) {   //  DEPRECATED!
      bgnID = atoi(argv[++arg]);

    } else if (strcmp(argv[arg], "-e") == 0) {   //  DEPRECATED!
      endID  = atoi(argv[++arg]);

    } else if (strcmp(argv[arg], "-r") == 0) {
      AS_UTL_decodeRange(argv[++arg], bgnID, endID);

    } else if (strcmp(argv[arg], "-raw") == 0) {
      dumpRaw = true;

    } else if (strcmp(argv[arg], "-corrected") == 0) {
      dumpCorrected = true;

    } else if (strcmp(argv[arg], "-trimmed") == 0) {
      dumpTrimmed = true;

    } else if (strcmp(argv[arg], "-allreads") == 0) {
      dumpAllReads    = true;

    } else if (strcmp(argv[arg], "-allbases") == 0) {
      dumpAllBases    = true;

    } else if (strcmp(argv[arg], "-onlydeleted") == 0) {
      dumpOnlyDeleted = true;
      dumpAllReads    = true;  //  Otherwise we won't report the deleted reads!


    } else if (strcmp(argv[arg], "-fastq") == 0) {
      dumpFASTQ       = true;
      dumpFASTA       = false;

    } else if (strcmp(argv[arg], "-fasta") == 0) {
      dumpFASTQ       = false;
      dumpFASTA       = true;

    } else if (strcmp(argv[arg], "-nolibname") == 0) {
      withLibName     = false;

    } else if (strcmp(argv[arg], "-noreadname") == 0) {
      withReadName    = false;


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

  if (gkpStoreName == NULL)
    err++;
  if (outPrefix == NULL)
    err++;
  if (err) {
    fprintf(stderr, "usage: %s [...] -o fastq-prefix -g gkpStore\n", argv[0]);
    fprintf(stderr, "  -G gkpStore\n");
    fprintf(stderr, "  -o fastq-prefix     write files fastq-prefix.(libname).fastq, ...\n");
    fprintf(stderr, "                      if fastq-prefix is '-', all sequences output to stdout\n");
    fprintf(stderr, "                      if fastq-prefix ends in .gz, .bz2 or .xz, output is compressed\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -fastq              output is FASTQ format (with extension .fastq, default)\n");
    fprintf(stderr, "  -fasta              output is FASTA format (with extension .fasta)\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -nolibname          don't include the library name in the output file name\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -noreadname         don't include the read name in the sequence header.  header will be:\n");
    fprintf(stderr, "                        '>original-name id=<gkpID> clr=<bgn>,<end>   with names\n");
    fprintf(stderr, "                        '>read<gkpID> clr=<bgn>,<end>                without names\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -l libToDump        output only read in library number libToDump\n");
    fprintf(stderr, "  -r id[-id]          output only the single read 'id', or the specified range of ids\n");
    fprintf(stderr, "\n");
    fprintf(stderr, " The default is to dump the latest version of each read.  You can force it to dump:\n");
    fprintf(stderr, "  -raw                Dump only raw reads.\n");
    fprintf(stderr, "  -corrected          Dump only corrected reads.\n");
    fprintf(stderr, "  -trimmed            Dump only trimmed reads.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, " An Overlap Based Trimming clear range file can be supplied (BUT NOT WIDELY TESTED):\n");
    fprintf(stderr, "  -c clearFile        clear range file from OBT modules\n");
    fprintf(stderr, "  -allreads           if a clear range file, lower case mask the deleted reads\n");
    fprintf(stderr, "  -allbases           if a clear range file, lower case mask the non-clear bases\n");
    fprintf(stderr, "  -onlydeleted        if a clear range file, only output deleted reads (the entire read)\n");

    if (gkpStoreName == NULL)
      fprintf(stderr, "ERROR: no gkpStore (-G) supplied.\n");
    if (outPrefix == NULL)
      fprintf(stderr, "ERROR: no output prefix (-o) supplied.\n");

    exit(1);
  }

  gkStore        *gkpStore  = gkStore::gkStore_open(gkpStoreName, gkStore_readOnly, gkpStorePart);
  uint32          numReads  = gkpStore->gkStore_getNumReads();
  uint32          numLibs   = gkpStore->gkStore_getNumLibraries();

  clearRangeFile *clrRange  = (clrName == NULL) ? NULL : new clearRangeFile(clrName, gkpStore);

  if (bgnID < 1)
    bgnID = 1;

  if (numReads < endID)
    endID = numReads;

  if (endID < bgnID)
    fprintf(stderr, "No reads to dump; reversed ranges make no sense: bgn=" F_U32 " end=" F_U32 "??\n", bgnID, endID), exit(1);

  if ((dumpRaw == true) && (gkpStore->gkStore_getNumRawReads() == 0))
    fprintf(stderr, "No raw reads in store.\n"), exit(1);

  if ((dumpCorrected == true) && (gkpStore->gkStore_getNumCorrectedReads() == 0))
    fprintf(stderr, "No corrected reads in store.\n"), exit(1);

  if ((dumpTrimmed == true) && (gkpStore->gkStore_getNumTrimmedReads() == 0))
    fprintf(stderr, "No trimmed reads in store.\n"), exit(1);



  if (gkpStorePart == UINT32_MAX)
    fprintf(stderr, "Dumping reads from %u to %u (inclusive).\n", bgnID, endID);
  else
    fprintf(stderr, "Dumping reads from %u to %u (inclusive) from partition %u.\n", bgnID, endID, gkpStorePart);


  libOutput   **out = new libOutput * [numLibs + 1];

  //  Allocate outputs.  If withLibName == false, all reads will artificially be in lib zero, the
  //  other files won't ever be created.  Otherwise, the zeroth file won't ever be created.

  out[0] = new libOutput(outPrefix, outSuffix, NULL);

  for (uint32 i=1; i<=numLibs; i++)
    out[i] = new libOutput(outPrefix, outSuffix, gkpStore->gkStore_getLibrary(i)->gkLibrary_libraryName());

  //  Grab a new readData, and iterate through reads to dump.

  gkReadData   *readData  = new gkReadData;

  char         *qltString = new char [AS_MAX_READLEN + 1];

  for (uint32 rid=bgnID; rid<=endID; rid++) {
    gkRead      *read   = gkpStore->gkStore_getRead(rid);

    if ((read == NULL) ||
        (gkpStore->gkStore_readInPartition(rid) == false))
      continue;

    uint32       libID  = (withLibName == false) ? 0 : read->gkRead_libraryID();

    uint32       flen   = read->gkRead_sequenceLength();

    if (dumpRaw == true)
      flen = read->gkRead_rawLength();

    if (dumpCorrected == true)
      flen = read->gkRead_correctedLength();

    if (dumpTrimmed == true)
      flen = read->gkRead_trimmedLength();

    uint32       lclr   = 0;
    uint32       rclr   = flen;
    bool         ignore = false;

    //  If a clear range file is supplied, grab the clear range.  If it hasn't been set, the default
    //  is the entire read.

    if (clrRange) {
      lclr   = clrRange->bgn(rid);
      rclr   = clrRange->end(rid);
      ignore = clrRange->isDeleted(rid);
    }

    //  Abort if we're not dumping anything from this read

    if (((libToDump != 0) && (libID == libToDump)) ||            //   - not in a library we care about
        ((dumpAllReads == false) && (ignore == true)) ||         //   - deleted, and not dumping all reads
        ((dumpOnlyDeleted == true) && (ignore == false)))        //   - not deleted, but only reporting deleted reads
      continue;

    //  If the read length is zero, then the read has been removed from this set.

    if ((dumpAllReads == false) && (flen == 0))
      continue;

    //  And if we're told to ignore the read, and here, then the read was deleted and we're printing
    //  all reads.  Reset the clear range to the whole read, the clear range is invalid.

    if (ignore) {
      lclr = 0;
      rclr = flen;
    }

    uint32  clen = rclr - lclr;

    //  Grab the _latest_ sequence and quality.

    gkpStore->gkStore_loadReadData(read, readData);

    char   *name = readData->gkReadData_getName();

    char   *seq  = readData->gkReadData_getSequence();
    uint8  *qlt8 = readData->gkReadData_getQualities();
    char   *qlt  = qltString;

    //  Grab the specified sequence and quality, if specified.

    if (dumpRaw == true) {
      seq  = readData->gkReadData_getRawSequence();
      qlt8 = readData->gkReadData_getRawQualities();
    }

    if (dumpCorrected == true) {
      seq  = readData->gkReadData_getCorrectedSequence();
      qlt8 = readData->gkReadData_getCorrectedQualities();
    }

    if (dumpTrimmed == true) {
      seq  = readData->gkReadData_getTrimmedSequence();
      qlt8 = readData->gkReadData_getTrimmedQualities();
    }

    //  Soft mask not-clear bases.

    if (dumpAllBases == true) {
      for (uint32 i=0; i<lclr; i++)
        seq[i] += (seq[i] >= 'A') ? 'a' - 'A' : 0;

      for (uint32 i=lclr; i<rclr; i++)
        seq[i] += (seq[i] >= 'A') ? 0 : 'A' - 'a';

      for (uint32 i=rclr; i<flen; i++)
        seq[i] += (seq[i] >= 'A') ? 'a' - 'A' : 0;

      lclr = 0;
      rclr = flen;
    }

    //  Create the QV string.

    for (uint32 i=0; i<read->gkRead_sequenceLength(); i++)
      qlt[i] = '!' + qlt8[i];

    //  Chop off the ends we're not printing.

    seq += lclr;
    qlt += lclr;

    seq[clen] = 0;
    qlt[clen] = 0;

    //  Print the read.

    if (dumpFASTA)  //  Dear GCC:  I'm NOT ambiguous
      if ((withReadName == true) && (name != NULL))
        AS_UTL_writeFastA(out[libID]->getFASTA(), seq, clen, 0,
                        ">%s id=" F_U32 " clr=" F_U32 "," F_U32 "\n",
                        name, rid, lclr, rclr);
      else
        AS_UTL_writeFastA(out[libID]->getFASTA(), seq, clen, 0,
                          ">read" F_U32 " clr=" F_U32 "," F_U32 "\n",
                          rid, lclr, rclr);

    if (dumpFASTQ)  //  Dear GCC:  I'm NOT ambiguous
      if ((withReadName == true) && (name != NULL))
        AS_UTL_writeFastQ(out[libID]->getFASTQ(), seq, clen, qlt, clen,
                          "@%s id=" F_U32 " clr=" F_U32 "," F_U32 "\n",
                          name,
                          rid, lclr, rclr);
      else
        AS_UTL_writeFastQ(out[libID]->getFASTQ(), seq, clen, qlt, clen,
                          "@read" F_U32 " clr=" F_U32 "," F_U32 "\n",
                          rid, lclr, rclr);
  }

  delete clrRange;

  delete readData;

  delete [] qltString;

  for (uint32 i=0; i<=numLibs; i++)
    delete out[i];
  delete [] out;

  gkpStore->gkStore_close();

  exit(0);
}