File: gatekeeperPartition.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 (287 lines) | stat: -rw-r--r-- 10,147 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

/******************************************************************************
 *
 *  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.
 *
 *  Modifications by:
 *
 *    Brian P. Walenz from 2014-DEC-23 to 2015-MAR-17
 *      are Copyright 2014-2015 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 "gkStore.H"
#include "tgStore.H"

//#include "AS_UTL_fileIO.H"

#include <libgen.h>


uint32 *
buildPartition(char    *tigStoreName,
               uint32   tigStoreVers,
               uint32   readCountTarget,
               uint32   partCountTarget,
               uint32   numReads) {
  tgStore *tigStore   = new tgStore(tigStoreName, tigStoreVers);

  //  Decide on how many reads per partition.  We take two targets, the partCountTarget
  //  is used to decide how many partitions to make, but if there are too few reads in
  //  each partition, we'll reset to readCountTarget.

  if (readCountTarget < numReads / partCountTarget)
    readCountTarget = numReads / partCountTarget;

  //  Figure out how many partitions we'll make, then spread the reads equally through them.

  uint32  numParts = (uint32)ceil((double)numReads / readCountTarget);

  readCountTarget = 1 + numReads / numParts;

  fprintf(stderr, "For %u reads, will make %u partition%s with up to %u reads%s.\n",
          numReads,
          (numParts),
          (numParts == 1) ? "" : "s",
          readCountTarget,
          (numParts == 1) ? "" : " in each");
  fprintf(stderr, "\n");

  //  Allocate space for the partitioning.

  uint32  *readToPart = new uint32 [numReads + 1];

  for (uint32 i=0; i<=numReads; i++)   //  All reads are in invalid
    readToPart[i] = UINT32_MAX;        //  partitions, initially.

  //  Run through all tigs and partition!

  uint32   partCount  = 1;
  uint32   tigsCount  = 0;
  uint32   readCount  = 0;
  uint32   longest    = 0;

  uint32   totalTigs  = 0;
  uint32   totalReads = 0;
  uint32   longestG   = 0;   //  Globally longest

  fprintf(stderr, "Partition      Tigs     Reads   Longest\n");
  fprintf(stderr, "--------- --------- --------- ---------\n");

  for (uint32 ti=0; ti<tigStore->numTigs(); ti++) {
    if (tigStore->isDeleted(ti))
      continue;

    tgTig  *tig = tigStore->loadTig(ti);

    //  Move to the next partition if needed

    if ((readCount + tig->numberOfChildren() >= readCountTarget) &&
        (readCount                           >  0)) {
      fprintf(stderr, "%9u %9u %9u %9u\n", partCount, tigsCount, readCount, longest);

      partCount++;
      tigsCount = 0;
      readCount = 0;
      longest   = 0;
    }

    //  Assign all the reads in this tig to this partition.

    tigsCount  += 1;
    readCount  += tig->numberOfChildren();

    totalTigs  += 1;
    totalReads += tig->numberOfChildren();

    longest  = max(longest,  tig->length());
    longestG = max(longestG, tig->length());

    //if (longest < tig->length())
    //  longest = tig->length();

    for (uint32 ci=0; ci<tig->numberOfChildren(); ci++)
      readToPart[tig->getChild(ci)->ident()] = partCount;

    tigStore->unloadTig(ti);
  }

  if (readCount > 0)
    fprintf(stderr, "%9u %9u %9u %9u\n", partCount, tigsCount, readCount, longest);

  fprintf(stderr, "--------- --------- --------- ---------\n");
  fprintf(stderr, "          %9u %9u %9u (partitioned)\n", totalTigs, totalReads, longestG);
  fprintf(stderr, "                    %9u           (unpartitioned)\n", numReads - totalReads);
  fprintf(stderr, "\n");

  delete tigStore;

  return(readToPart);
}



int
main(int argc, char **argv) {
  char   *gkpStorePath      = NULL;
  char   *tigStorePath      = NULL;
  uint32  tigStoreVers      = 0;
  uint32  readCountTarget   = 2500;   //  No partition smaller than this
  uint32  partCountTarget   = 200;    //  No more than this many partitions
  bool    doDelete          = false;

  argc = AS_configure(argc, argv);

  vector<char *>  err;
  int             arg = 1;
  while (arg < argc) {
    if        (strcmp(argv[arg], "-G") == 0) {
      gkpStorePath = argv[++arg];

    } else if (strcmp(argv[arg], "-T") == 0) {
      tigStorePath = argv[++arg];
      tigStoreVers = atoi(argv[++arg]);

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

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

    } else if (strcmp(argv[arg], "-D") == 0) {
      tigStorePath = argv[++arg];
      tigStoreVers = 1;
      doDelete = true;

    } else {
      char *s = new char [1024];
      snprintf(s, 1024, "ERROR: unknown option '%s'\n", argv[arg]);
      err.push_back(s);
    }

    arg++;
  }

  if ((gkpStorePath == NULL) &&
      (doDelete == false))       err.push_back("ERROR: no gkpStore (-G) supplied.\n");
  if (tigStorePath == NULL)      err.push_back("ERROR: no tigStore (-T) supplied.\n");

  if (err.size() > 0) {
    fprintf(stderr, "usage: %s [-G <gkpStore> -T <tigStore> <v>] ...\n", argv[0]);
    fprintf(stderr, "       %s [-D <tigStore>]\n", argv[0]);
    fprintf(stderr, "  -G <gkpStore>       path to gatekeeper store\n");
    fprintf(stderr, "  -T <tigStore> <v>   path to tig store and version to be partitioned\n");
    fprintf(stderr, "  -D <tigStore>       remove a partitioned gkpStore\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -b <nReads>         minimum number of reads per partition (50000)\n");
    fprintf(stderr, "  -p <nPartitions>    number of partitions (200)\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Create a partitioned copy of <gkpStore> and place it in <tigStore>/partitionedReads.gkpStore\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "NOTE:  Path handling in this is probably quite brittle.  Due to an implementation\n");
    fprintf(stderr, "       detail, the new store must have symlinks back to the original store.  Canu\n");
    fprintf(stderr, "       wants to use relative paths, and this program tries to adjust <gkpStore> to\n");
    fprintf(stderr, "       be relative to <tigStore/partitionedReads.gkpStore.  If it fails to do this,\n");
    fprintf(stderr, "       correctly one of two (seen so far) errors will occur:\n");
    fprintf(stderr, "         Original file '.../partitionedReads.gkpStore/info' doesn't exist, won't make a link to nothing.\n");
    fprintf(stderr, "         Couldn't open '.../partitionedReads.gkpStore/libraries' for mmap: No such file or directory\n");
    fprintf(stderr, "       In both cases, try to simplify <tigStore> -- in particular, remove any '..'\n");
    fprintf(stderr, "       or '.' components -- or run this from a higher/lower directory.\n");

    for (uint32 ii=0; ii<err.size(); ii++)
      if (err[ii])
        fputs(err[ii], stderr);

    exit(1);
  }

  char    gkpSourcePath[FILENAME_MAX] = {0};
  char    gkpClonePath[FILENAME_MAX]  = {0};

  //  We're making a clone of the master gkpStore in the tigStore directory.

  snprintf(gkpClonePath, FILENAME_MAX, "%s/partitionedReads.gkpStore", tigStorePath);

  //  The path to the gkpStore that we want to use in the link is a wee-bit more complicated.
  //
  //  1.  Do nothing if there is no gkpStore.
  //  2.  If it's an absolute path, there's nothing we need to do.
  //  3.  But if it's a relative path, we need to add a bunch of dots.  One pair
  //      to account for the directory we added above, and then more dots for
  //      each component in tigStorePath.
  //

  if (gkpStorePath == NULL) {
  }

  else if (gkpStorePath[0] == '/') {
    strcpy(gkpSourcePath, gkpStorePath);
  }

  else {
    char    t[FILENAME_MAX];                  //  Copy command line tigStorePath to a
    char   *p = t;                            //  local, and modifiable, space.

    strcpy(p, tigStorePath);

    strcat(gkpSourcePath, "../");             //  One for the directory we created above

    while ((p[0] != '.') || (p[1] != 0)) {    //  Many for each component in the tigStorePath.
      strcat(gkpSourcePath, "../");
      p = dirname(p);
    }

    if ((gkpStorePath[0] == '.') && (gkpStorePath[1] == '/'))   //  Finally, append the supplied
      strcat(gkpSourcePath, gkpStorePath + 2);                  //  gkpStorePath, possibly
    else                                                        //  stripping off any ./ at the
      strcat(gkpSourcePath, gkpStorePath);                      //  start.
  }

  //  Now, just....do it.

  if (doDelete == true) {
    gkStore *gkpStore = gkStore::gkStore_open(gkpClonePath, gkStore_readOnly);
    gkpStore->gkStore_deletePartitions();
    gkpStore->gkStore_close();
  }


  if (doDelete == false) {
    gkStore::gkStore_clone(gkpSourcePath, gkpClonePath);                           //  Make the clone.

    gkStore *gkpStore = gkStore::gkStore_open(gkpClonePath, gkStore_readOnly);     //  Open the clone.

    uint32   *partition = buildPartition(tigStorePath, tigStoreVers,               //  Scan all the tigs
                                         readCountTarget,                          //  to build a map from
                                         partCountTarget,                          //  read to partition.
                                         gkpStore->gkStore_getNumReads());

    gkpStore->gkStore_buildPartitions(partition);                                  //  Build partitions.

    delete [] partition;

    gkpStore->gkStore_close();
  }


  exit(0);
}