Codebase list kmer / HEAD sim4db / sim4th.C
HEAD

Tree @HEAD (Download .tar.gz)

sim4th.C @HEADraw · history · blame

  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
// This file is part of sim4db.
// Copyright (c) 2005 Brian Walenz
// Author: Brian Walenz
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received (LICENSE.txt) a copy of the GNU General Public 
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <signal.h>
#include <math.h>

#include <pthread.h>
#include <semaphore.h>

#include "bio++.H"
#include "sim4.H"
#include "sweatShop.H"

//  XXX  Both loader and loaderAll leave the last gen sequence undeleted!

readBuffer       *scriptFile       = 0L;

seqCache         *GENs             = 0L;
seqCache         *ESTs             = 0L;

uint32            lastGENiid       = ~uint32ZERO;
uint32            lastESTiid       = ~uint32ZERO;
seqInCore        *lastGENseq       = 0L;

int               fOutput          = 0;
int               fYesNo           = 0;

char             *cdnaFileName     = 0L;
char             *scriptFileName   = 0L;
char             *databaseFileName = 0L;
char             *outputFileName   = 0L;
char             *yesnoFileName    = 0L;
char             *touchFileName    = 0L;

bool              pairwise         = false;

bool              beVerbose        = false;
bool              beYesNo          = false;

uint32            numThreads       = 2;
uint32            loaderCacheSize  = 1024;

sim4parameters    sim4params;

//  Parse the command line to create a sim4command object
//
//  [-f|-r] -e ESTid -D GENid GENlo GENhi
//
//    -f  Forward only
//    -r  Reverse only
//    -D  genSeqIID genLo genHi
//    -e  estSeqIID
//
//
char*
getNextScript(uint32     &ESTiid,
              uint32     &GENiid, uint32 &GENlo, uint32 &GENhi,
              bool       &doForward,
              bool       &doReverse) {

  char x = scriptFile->read();

  //  Skip any white space in the file
  //
  while ((scriptFile->eof() == false) && (whitespaceSymbol[x]))
    x = scriptFile->read();

  //  Exit if we're all done.
  //
  if (scriptFile->eof())
    return(0L);

  uint32  linePos = 0;
  uint32  lineMax = 128;
  char   *line    = new char [lineMax];

  //  Copy the line from the readBuffer into our storage
  //
  while ((scriptFile->eof() == false) && (x != '\n')) {
    line[linePos++] = x;
    x = scriptFile->read();
  }
  line[linePos] = 0;

  //  Decode the line
  //
  uint32         argWords = 0;
  splitToWords   words(line);

  while (words.getWord(argWords)) {
    switch (words.getWord(argWords)[1]) {
      case 'f':
        doForward = true;
        doReverse = false;
        break;
      case 'r':
        doForward = false;
        doReverse = true;
        break;
      case 'D':
        GENiid = strtouint32(words.getWord(++argWords), 0L);
        GENlo  = strtouint32(words.getWord(++argWords), 0L);
        GENhi  = strtouint32(words.getWord(++argWords), 0L);
        break;
      case 'e':
        ESTiid = strtouint32(words.getWord(++argWords), 0L);
        break;
      default:
        //fprintf(stderr, "Unknown option '%s'\n", words.getWord(argWords));
        break;
    }

    argWords++;
  }

  return(line);
}




class sim4thWork {
public:
  sim4command          *input;
  char                 *script;
  sim4polishList       *output;
  seqInCore            *gendelete;
  seqInCore            *estdelete;

  sim4thWork() {
    input = 0L;
    script = 0L;
    output = 0L;
    gendelete = 0L;
    estdelete = 0L;
  };
};


void*
loader(void *U) {
  bool                  doForward = true;
  bool                  doReverse = true;
  uint32                ESTiid = 0;
  uint32                GENiid = 0;
  uint32                GENlo  = 0;
  uint32                GENhi  = 0;

  sim4thWork *p = new sim4thWork();
  
  p->script = getNextScript(ESTiid, GENiid, GENlo, GENhi, doForward, doReverse);

  if (p->script) {
    seqInCore  *ESTseq = 0L;
    seqInCore  *GENseq = 0L;

    //  If we already have the GENseq, use that, otherwise, register it for deletion.
    //
    if (lastGENiid == GENiid) {
      GENseq = lastGENseq;
    } else {

      //  Register it for deletion.  Technically, we're deleting this
      //  on the state AFTER it's used, but we can't guarantee that
      //  that state is still around.  The writer is deleting this, so
      //  by the time it gets here, it already wrote everyone that
      //  used this, which kind of implies that everyone that needs
      //  this is already computed.
      //
      p->gendelete = lastGENseq;

      GENseq = GENs->getSequenceInCore(GENiid);

      lastGENiid = GENiid;
      lastGENseq = GENseq;
    }

    //  The cache can, and does, overwrite the EST sequence we care
    //  about.  For now, we just copy the EST from the cache.
    //
    ESTseq         = ESTs->getSequenceInCore(ESTiid)->copy();
    p->estdelete   = ESTseq;

    p->input       = new sim4command(ESTseq, GENseq, GENlo, GENhi, doForward, doReverse);
  } else {
    delete p;
    p = 0L;
  }

  return(p);
}


void*
loaderPairwise(void *) {

  //  Align cDNA i to genomic i.

  if (lastGENiid == ~uint32ZERO)  //  happens on the first time through
    lastGENiid = 0;
  if (lastESTiid == ~uint32ZERO)  //  happens on the first time through
    lastESTiid = 0;

  //  If we've run out of sequences, we're done!
  if ((lastGENiid >= GENs->getNumberOfSequences()) ||
      (lastESTiid >= ESTs->getNumberOfSequences()))
    return(0L);

  sim4thWork  *p = new sim4thWork();

  //  Grab the GEN sequence
  p->gendelete = GENs->getSequenceInCore(lastGENiid++);

  //  Grab the EST sequence
  p->estdelete = ESTs->getSequenceInCore(lastESTiid++)->copy();

  //  build the command
  p->input     = new sim4command(p->estdelete,
                                 p->gendelete, 0, p->gendelete->sequenceLength(),
                                 true, true);

  return(p);
}


void*
loaderAll(void *) {

  sim4thWork  *p = new sim4thWork();

  //  Previous implementations "Ping-pong'd" through the ESTs.  The
  //  idea being we would use the cache on the ends.  We can't easily
  //  do that here, so we always go forward.

  //  Flip around the end, if needed.
  if (lastESTiid >= ESTs->getNumberOfSequences()) {
    lastESTiid   = 0;
    p->gendelete = lastGENseq;
    lastGENseq   = 0L;

    if (lastGENiid == ~uint32ZERO)  //  happens on the first time through
      lastGENiid = 0;
    else
      lastGENiid++;
  }

  //  If we've run out of sequences, we're done!
  if (lastGENiid >= GENs->getNumberOfSequences()) {
    delete p;
    return(0L);
  }

  //  Update the genomic sequence?
  if (lastGENseq == 0L) {
    lastGENseq = GENs->getSequenceInCore(lastGENiid);
  }

  //  Grab the EST sequence
  p->estdelete = ESTs->getSequenceInCore(lastESTiid++)->copy();

  //  build the command
  p->input     = new sim4command(p->estdelete,
                                 lastGENseq, 0, lastGENseq->sequenceLength(),
                                 true, true);

  return(p);
}




void
worker(void *U, void *T, void *S) {
  sim4thWork  *p = (sim4thWork *)S;

  Sim4       *sim = new Sim4(&sim4params);
  p->output       = sim->run(p->input);
  delete sim;
}


void
writer(void *U, void *S) {
  sim4thWork  *p = (sim4thWork *)S;

  sim4polishList  &L4 = *(p->output);

  for (uint32 i=0; L4[i]; i++) {
    char *o = L4[i]->s4p_polishToString(sim4params.getOutputFormat());

    errno = 0;
    write(fOutput, o, strlen(o) * sizeof(char));
    if (errno)
      fprintf(stderr, "Couldn't write the output file '%s': %s\n", outputFileName, strerror(errno)), exit(1);

    delete [] o;
  }

  if (yesnoFileName) {
    char  str[128];

    if (L4[0])
      sprintf(str, "%s -Y "uint32FMT" "uint32FMT"\n",
              p->script, L4[0]->_percentIdentity, L4[0]->_querySeqIdentity);
    else
      sprintf(str, "%s -N 0 0\n", p->script);

    write(fYesNo, str, strlen(str) * sizeof(char));
  }

  //  Release this compute

  delete    p->input;
  delete [] p->script;
  delete    p->output;
  delete    p->gendelete;
  delete    p->estdelete;
  delete    p;
}



int
openOutputFile(char *name) {
  int  f = 0;

  if (name == 0L)
    return(0);

  if (strcmp(name, "-") == 0) {
    f = fileno(stdout);
  } else {
    errno = 0;
    f = open(name,
             O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC,
             S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    if (errno)
      fprintf(stderr, "Couldn't open the output file '%s': %s\n", name, strerror(errno)), exit(1);
  }
  return(f);
}



int
main(int argc, char **argv) {

  int arg = 1;
  int err = 0;
  while (arg < argc) {
    if        (strncmp(argv[arg], "-alignments", 4) == 0) {
      sim4params.setPrintAlignments(true);

    } else if (strncmp(argv[arg], "-alwaysprint", 4) == 0) {
      sim4params.setFindAllExons(true);
      sim4params.setAlwaysReport(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-cdna", 3) == 0) {
      cdnaFileName = argv[++arg];

    } else if (strncmp(argv[arg], "-cut", 3) == 0) {
      double x = atof(argv[++arg]);
      if (x < 0.0) {
        fprintf(stderr, "WARNING:  -cut adjusted to 0.0 (you gave %f)!\n", x);
        x = 0.0;
      }
      if (x > 1.0) {
        fprintf(stderr, "WARNING:  -cut adjusted to 1.0 (you gave %f)!\n", x);
        x = 1.0;
      }
      sim4params.setPolyTailPercent(x);

    } else if (strncmp(argv[arg], "-genomic", 4) == 0) {
      databaseFileName = argv[++arg];

    } else if (strncmp(argv[arg], "-minc", 5) == 0) {
      sim4params.setFindAllExons(true);
      sim4params.setMinCoverage(atoi(argv[++arg]) / 100.0);

    } else if (strncmp(argv[arg], "-mini", 5) == 0) {
      sim4params.setFindAllExons(true);
      sim4params.setMinPercentExonIdentity(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-minl", 5) == 0) {
      sim4params.setFindAllExons(true);
      sim4params.setMinCoverageLength(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-nod", 4) == 0) {
      sim4params.setIncludeDefLine(false);

    } else if (strncmp(argv[arg], "-non", 4) == 0) {
      sim4params.setDontForceCanonicalSplicing(true);

    } else if (strncmp(argv[arg], "-f", 2) == 0) {
      sim4params.setForceStrandPrediction(true);

    } else if (strncmp(argv[arg], "-o", 2) == 0) {
      outputFileName = argv[++arg];

    } else if (strncmp(argv[arg], "-po", 3) == 0) {
      sim4params.setIgnorePolyTails(false);

    } else if (strncmp(argv[arg], "-sc", 3) == 0) {
      scriptFileName = argv[++arg];

    } else if (strncmp(argv[arg], "-sp", 3) == 0) {
      sim4params.setSpliceModel(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-pa", 3) == 0) {
      pairwise = true;

    } else if (strncmp(argv[arg], "-to", 3) == 0) {
      touchFileName = argv[++arg];

    } else if (strncmp(argv[arg], "-verbose", 2) == 0) {
      beVerbose = true;

    } else if (strncmp(argv[arg], "-YN", 3) == 0) {
      yesnoFileName = argv[++arg];

    } else if (strncmp(argv[arg], "-threads", 3) == 0) {
      numThreads = strtouint32(argv[++arg], 0L);

    } else if (strncmp(argv[arg], "-H", 2) == 0) {
      sim4params.setRelinkWeight(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-K", 2) == 0) {
      sim4params.setMSPThreshold1(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-C", 2) == 0) {
      sim4params.setMSPThreshold2(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-Z", 2) == 0) {
      sim4params.setSpacedSeed(argv[++arg]);

    } else if (strncmp(argv[arg], "-Ma", 3) == 0) {
      sim4params.setMSPLimitAbsolute(atoi(argv[++arg]));

    } else if (strncmp(argv[arg], "-Mp", 3) == 0) {
      sim4params.setMSPLimitPercent(atof(argv[++arg]));

    } else if (strncmp(argv[arg], "-interspecies", 2) == 0) {
      sim4params.setInterspecies(true);

    } else if (strcmp(argv[arg], "-gff3") == 0) {
      sim4params.setOutputFormat(S4P_POLISH_GFF3);

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

    arg++;
  }

  if ((err) ||
      (cdnaFileName == 0L) ||
      (databaseFileName == 0L) ||
      (outputFileName == 0L)) {
    fprintf(stderr, "usage: %s -genomic g.fasta -cdna c.fasta -output o.sim4db [options]\n", argv[0]);
    fprintf(stderr, "\n");
    fprintf(stderr, "       -v            print status to stderr while running\n");
    fprintf(stderr, "       -V            print script lines (stderr) as they are processed\n");
    fprintf(stderr, "       -YN           print script lines (to given file) as they are processed, annotated with yes/no\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -cdna         use these cDNA sequences\n");
    fprintf(stderr, "       -genomic      use these genomic sequences\n");
    fprintf(stderr, "       -script       use this script file\n");
    fprintf(stderr, "       -pairwise     do pairs of sequences\n");
    fprintf(stderr, "       -output       write output to this file\n");
    fprintf(stderr, "       -touch        create this file when the program finishes execution\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -threads      Use n threads.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -mincoverage  iteratively find all exon models with the specified\n");
    fprintf(stderr, "                     minimum PERCENT COVERAGE\n");
    fprintf(stderr, "       -minidentity  iteratively find all exon models with the specified\n");
    fprintf(stderr, "                     minimum PERCENT EXON IDENTITY\n");
    fprintf(stderr, "       -minlength    iteratively find all exon models with the specified\n");
    fprintf(stderr, "                     minimum ABSOLUTE COVERAGE (number of bp matched)\n");
    fprintf(stderr, "       -alwaysreport always report <number> exon models, even if they\n");
    fprintf(stderr, "                     are below the quality thresholds\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "         If no mincoverage or minidentity or minlength is given, only\n");
    fprintf(stderr, "         the best exon model is returned.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "         You will probably want to specify ALL THREE of mincoverage,\n");
    fprintf(stderr, "         minidentity and minlength!  Don't assume the default values\n");
    fprintf(stderr, "         are what you want!\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "         You will DEFINITELY want to specify at least one of mincoverage,\n");
    fprintf(stderr, "         minidentity and minlength with alwaysreport!  If you don't, mincoverage\n");
    fprintf(stderr, "         will be set to 90 and minidentity to 95 -- to reduce the number of\n");
    fprintf(stderr, "         spurious matches when a good match is found.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -nodeflines   don't include the defline in the output\n");
    fprintf(stderr, "       -alignments   print alignments\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -polytails    DON'T mask poly-A and poly-T tails.\n");
    fprintf(stderr, "       -cut          Trim marginal exons if A/T %% > x (poly-AT tails)\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -noncanonical Don't force canonical splice sites\n");
    fprintf(stderr, "       -splicemodel  Use the following splice model: 0 - original sim4;\n");
    fprintf(stderr, "                     1 - GeneSplicer; 2 - Glimmer (default: 0)\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -forcestrand  Force the strand prediction to always be\n");
    fprintf(stderr, "                     'forward' or 'reverse'\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "       -interspecies Use sim4cc for inter-species alignments\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  The following are for use only by immortals.\n");
    fprintf(stderr, "       -Z            set the (spaced) seed pattern\n");
    fprintf(stderr, "       -H            set the relink weight factor\n");
    fprintf(stderr, "       -K            set the first MSP threshold\n");
    fprintf(stderr, "       -C            set the second MSP threshold\n");
    fprintf(stderr, "       -Ma           set the limit of the number of MSPs allowed\n");
    fprintf(stderr, "       -Mp           same, as percentage of bases in cDNA\n");
    fprintf(stderr, "                     NOTE:  If used, both -Ma and -Mp must be specified!\n");
    exit(1);
  }

  //  Open input files
  //
  GENs = new seqCache(databaseFileName);
  ESTs = new seqCache(cdnaFileName, loaderCacheSize, false);

  //  Open the output file
  fOutput = openOutputFile(outputFileName);
  fYesNo  = openOutputFile(yesnoFileName);

  sweatShop  *ss = 0L;

  err = sim4params.setSpliceMutex();
  if (err) {
    fprintf(stderr, "sim4th::main()--  Failed to initialize splice mutex: %s.\n", strerror(err));
    exit(1);
  }


  //  If we have a script, read work from there, otherwise,
  //  do an all-vs-all.
  //
  if (scriptFileName) {
    scriptFile = new readBuffer(scriptFileName);
    ss = new sweatShop(loader,
                       worker,
                       writer);
  } else if (pairwise) {
    ss = new sweatShop(loaderPairwise,
                       worker,
                       writer);
  } else {
    ss = new sweatShop(loaderAll,
                       worker,
                       writer);
  }

  ss->setNumberOfWorkers(numThreads);
  ss->run(0L, beVerbose);

  delete ss;

  //  Only close the file if it isn't stdout
  //
  if (strcmp(outputFileName, "-") != 0)
    close(fOutput);

  if (yesnoFileName)
    close(fYesNo);

  delete scriptFile;

  delete ESTs;
  delete GENs;

  exit(0);
}