File: merTrimApply.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 (200 lines) | stat: -rw-r--r-- 5,828 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

/******************************************************************************
 *
 *  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_MER/merTrimApply.C
 *
 *  Modifications by:
 *
 *    Brian P. Walenz from 2011-JAN-04 to 2013-AUG-01
 *      are Copyright 2011,2013 J. Craig Venter Institute, and
 *      are subject to the GNU General Public License version 2
 *
 *    Brian P. Walenz from 2014-OCT-09 to 2014-DEC-05
 *      are Copyright 2014 Battelle National Biodefense Institute, and
 *      are subject to the BSD 3-Clause License
 *
 *    Brian P. Walenz beginning on 2015-DEC-07
 *      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 "AS_PER_gkpStore.H"
#include "splitToWords.H"

#include "merTrimResult.H"


void
merTrimApply(char *gkpStoreName,
             char *listName,
             char *logName) {
  char  resultFileName[FILENAME_MAX] = {0};
  char  resultLine[1024] = {0};

  errno = 0;
  FILE *listFile = fopen(listName, "r");
  if (errno)
    fprintf(stderr, "Failed to open -L '%s' for reading: %s\n", listName, strerror(errno)), exit(1);

  errno = 0;
  FILE *logFile = (logName != NULL) ? fopen(logName, "w") : NULL;
  if (errno)
    fprintf(stderr, "Failed to open -l '%s' for writing: %s\n", logName, strerror(errno)), exit(1);

  gkStore *gkpStore = gkStore::gkStore_open(gkpStoreName, FALSE, TRUE);

  gkpStore->gkStore_enableClearRange(AS_READ_CLEAR_OBTINITIAL);
  gkpStore->gkStore_enableClearRange(AS_READ_CLEAR_TNT);
  gkpStore->gkStore_metadataCaching(true);

  bool            tntEnabled = false;

  //  Over every result file, read each line, change the clear range.

  gkFragment      gkf;
  mertrimResult   res;

  fgets(resultFileName, FILENAME_MAX, listFile);
  while (!feof(listFile)) {
    chomp(resultFileName);

    errno = 0;
    FILE *resultFile = fopen(resultFileName, "r");
    if (errno)
      fprintf(stderr, "Failed to open result file '%s' for reading: %s\n", resultFileName, strerror(errno)), exit(1);

    while (res.readResult(resultFile)) {
      gkpStore->gkStore_getFragment(res.readIID, &gkf, GKFRAGMENT_INF);

      if (res.chimer)
        gkf.gkFragment_setClearRegion(res.chmBgn, res.chmEnd, AS_READ_CLEAR_TNT);

      gkf.gkFragment_setClearRegion(res.clrBgn, res.clrEnd, AS_READ_CLEAR_OBTINITIAL);

      if (res.deleted)
        gkpStore->gkStore_delFragment(res.readIID);
      else
        gkpStore->gkStore_setFragment(&gkf);

      res.print(logFile);
    }

    AS_UTL_closeFile(resultFile);
    fgets(resultFileName, FILENAME_MAX, listFile);
  }

  AS_UTL_closeFile(listFile);
  AS_UTL_closeFile(logFile);

  gkpStore->gkStore_close();
}




void
merTrimShow(char *gkpStoreName,
            char *resultName) {

  errno = 0;
  FILE *resultFile = fopen(resultName, "r");
  if (errno)
    fprintf(stderr, "Failed to open result file '%s' for reading: %s\n", resultName, strerror(errno)), exit(1);

  mertrimResult   res;

  while (res.readResult(resultFile))
    res.print(stdout);
}





int
main(int argc, char **argv) {
  char     *gkpStoreName = NULL;
  char     *listName     = NULL;
  char     *logName      = NULL;
  char     *resultName   = NULL;

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

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

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

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

    } else {
      err++;
    }

    arg++;
  }
  if (((gkpStoreName == NULL) && (listName != NULL)) ||
      ((gkpStoreName != NULL) && (listName == NULL)))
    err++;
  if ((listName == NULL) && (resultName == NULL))
    err++;
  if ((listName != NULL) && (resultName != NULL))
    err++;
  if (err) {
    fprintf(stderr, "usage: %s -L merTrimOutputList -g gkpStore [-l output.log]\n", argv[0]);
    fprintf(stderr, "       %s -d merTrimOutput\n", argv[0]);
    fprintf(stderr, "\n");
    fprintf(stderr, "  The first form will read a list of merTrim output names from\n");
    fprintf(stderr, "  merTrimOuptutList, and apply the results to gkpStore.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  The second form will read a single merTrimOutput file and decode\n");
    fprintf(stderr, "  the results to stdout.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "\n");
    if (((gkpStoreName == NULL) && (listName != NULL)) ||
        ((gkpStoreName != NULL) && (listName == NULL)))
      fprintf(stderr, "ERROR:  First form needs both -L and -g.\n");
    if (gkpStoreName == NULL)
      fprintf(stderr, "ERROR:  No gatekeeper store supplied with -g.\n");
    if ((listName == NULL) && (resultName == NULL))
      fprintf(stderr, "ERROR:  Exactly one of -L and -d must be specified.\n");
    if ((listName != NULL) && (resultName != NULL))
      fprintf(stderr, "ERROR:  Only one of -L and -d can be specified.\n");
    fprintf(stderr, "\n");
    exit(1);
  }


  if (listName)
    merTrimApply(gkpStoreName, listName, logName);

  if (resultName)
    merTrimShow(gkpStoreName, resultName);

  return(0);
}