aboutsummaryrefslogtreecommitdiff
path: root/examples/bwt.c
blob: 842712d70a9e871a5b7a9faca500e490ac576a95 (plain)
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
/*
 * bwt.c for libdivsufsort
 * Copyright (c) 2003-2008 Yuta Mori All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <divsufsort.h>
#include <stdio.h>
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_STRING_H
# if !STDC_HEADERS && HAVE_MEMORY_H
#  include <memory.h>
# endif
# include <string.h>
#endif
#if HAVE_STRINGS_H
# include <strings.h>
#endif
#include <time.h>


static
saidx_t
_str2size(const char *str) {
  saidx_t s[3];
  saidx_t t;
  int i, c;
  for(i = 0, t = s[0] = s[1] = s[2] = 0; (c = str[i]) != '\0'; ++i) {
    if(('0' <= c) && (c <= '9')) {
      t = (t * 10) + (c - '0');
    } else {
      switch(c) {
      case 'm':
      case 'M':
        s[0] += t << 20;
        break;
      case 'k':
      case 'K':
        s[1] += t << 10;
        break;
      case 'b':
      case 'B':
        s[2] += t;
        break;
      }
      t = 0;
    }
  }
  return s[0] + s[1] + s[2];
}

int
main(int argc, const char *argv[]) {
  sauchar_t *T;
  saidx_t *SA;
  saidx_t m, n, blocksize, idx;
  clock_t start,finish;

  /* Check argument. */
  if(((argc != 1) && (argc != 2)) ||
     ((argc == 2) && (strcmp(argv[1], "-h") == 0)) ||
     ((argc == 2) && (strcmp(argv[1], "--help") == 0))) {
    fprintf(stderr,
      "bwt, a burrows-wheeler transform program, version %s.\n"
      , divsufsort_version());
    fprintf(stderr,
      "usage: %s [BLOCKSIZE] < STDIN > STDOUT\n\n"
      , argv[0]);
    exit(EXIT_FAILURE);
  }
  blocksize = (argc == 2) ? _str2size(argv[1]) : 0;
  if(blocksize <= 0) {
    fseek(stdin, 0, SEEK_END);
    blocksize = ftell(stdin);
    if(blocksize < 0) { blocksize = BUFSIZ; }
    rewind(stdin);
  }

  /* Allocate 5blocksize bytes of memory. */
  if(((T = malloc(blocksize * sizeof(sauchar_t))) == NULL) ||
     ((SA = malloc(blocksize * sizeof(saidx_t))) == NULL)) {
    fprintf(stderr, "%s: Cannot allocate memory.\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  /* Write the blocksize. */
  if(fwrite(&blocksize, sizeof(saidx_t), 1, stdout) != 1) {
    fprintf(stderr, "%s: Cannot write to `stdout': ", argv[0]);
    perror(NULL);
    exit(EXIT_FAILURE);
  }

  fprintf(stderr, "  BWT (blocksize %d) ... ", (int)blocksize);
  start=clock();
  for(n = 0; 0 < (m = fread(T, sizeof(sauchar_t), blocksize, stdin)); n += m) {
    /* Burrows-Wheeler Transform. */
    idx = divbwt(T, T, SA, m);
    if(idx < 0) {
      fprintf(stderr, "%s (bw_transform): %s.\n",
        argv[0],
        (idx == -1) ? "Invalid arguments" : "Cannot allocate memory");
      exit(EXIT_FAILURE);
    }

    /* Write the bwted data. */
    if((fwrite(&idx, sizeof(saidx_t), 1, stdout) != 1) ||
       (fwrite(T, sizeof(sauchar_t), m, stdout) != m)) {
      fprintf(stderr, "%s: Cannot write to `stdout': ", argv[0]);
      perror(NULL);
      exit(EXIT_FAILURE);
    }
  }
  if(ferror(stdin)) {
    fprintf(stderr, "%s: Cannot read from `stdin': ", argv[0]);
    perror(NULL);
    exit(EXIT_FAILURE);
  }
  finish = clock();
  fprintf(stderr, "%d bytes: %.4f sec\n",
    (int)n, (double)(finish - start) / (double)CLOCKS_PER_SEC);

  /* Deallocate memory. */
  free(T);
  free(SA);

  return 0;
}