summaryrefslogtreecommitdiff
path: root/dict.c
blob: 7bb0e63a8f01a259a69eef7ce5ebe8f3763647be (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright © 2016 by Justin Bedő
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
// SOFTWARE.
//
#include<fcntl.h>
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<zlib.h>
#include"dict.h"

//#define BITMIX

typedef enum {
  TINSEQ,
  TOUTSEQ
} TState;

typedef struct {
  gzFile f;
  TState s;
} tokeniser;

tokeniser*
new_tokeniser(char *path)
{
  tokeniser *tok = malloc(sizeof(tokeniser));
  if(tok == NULL)
    error("new_tokeniser");
  tok->f = gzopen(path, "rb");
  if(tok->f == NULL)
    error("gzopen");
  tok->s = TOUTSEQ;
  return tok;
}

int
getTok(tokeniser *tok)
{
  int c;
  switch(tok->s){
    case TOUTSEQ:
      c = gzgetc(tok->f);
      while(c != -1 && c != '@') c = gzgetc(tok->f);
      while(c != -1 && c != '\n') c = gzgetc(tok->f);
      tok->s = TINSEQ;
    case TINSEQ:
      c = gzgetc(tok->f);
      while(c != -1 && c == '\n') c = gzgetc(tok->f);
      switch(c){
        case -1:
          return -1;
        case 'A':
        case 'a':
          return 0;
        case 'C':
        case 'c':
          return 1;
        case 'G':
        case 'g':
          return 2;
        case 'T':
        case 't':
          return 3;
        case '+':
          tok->s = TOUTSEQ;
        default:
          return -2;
      }
  }
}

void
usage(char *name)
{
  fprintf(stderr, "usage: %s <input fastq> <bloom output>\n", name);
  exit(-1);
}

inline
uint64_t bitmix(uint64_t x)
{
  x ^= x >> 31;
  x *= 0x7fb5d329728ea185;
  x ^= x >> 27;
  x *= 0x81dadef4bc2dd44d;
  x ^= x >> 33;
  return x;
}

int
main(int argc, char **argv)
{
  if(argc != 3)
    usage(argv[0]);

  tokeniser *t = new_tokeniser(argv[1]);

  // map bloom filter
  uint64_t *bloom;
  if(access(argv[2], F_OK) == -1){
    int bfd = open(argv[2], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if(bfd == -1)
      error("open");
    if(ftruncate(bfd, BLOOMSIZE * sizeof(uint64_t)) == -1)
      error("ftruncate");
    bloom = (uint64_t *)mmap(NULL, BLOOMSIZE * sizeof(uint64_t), PROT_WRITE, MAP_SHARED, bfd, 0);
    if(bloom == MAP_FAILED)
      error("mmap");
    bzero(bloom, BLOOMSIZE * sizeof(uint64_t));
  }else{
    struct stat sb;
    int bfd = open(argv[2], O_RDWR);
    if(bfd == -1)
      error("open");
    if(fstat(bfd, &sb) == -1)
      error("fstat");
    if(sb.st_size != BLOOMSIZE * sizeof(uint64_t)){
      fprintf(stderr, "incompatible bloom table\n");
      return -1;
    }
    bloom = (uint64_t *)mmap(NULL, BLOOMSIZE * sizeof(uint64_t), PROT_WRITE, MAP_SHARED, bfd, 0);
    if(bloom == MAP_FAILED)
      error("mmap");
  }

  int tok;
  uint64_t term = 0;
  int bits;

  while((tok = getTok(t)) != -1){
    if(tok < 0){
      term = 0;
      continue;
    }

    term ^= term << 3;
    term += tok;
    //term = (term * 16777619) ^ tok;

#ifdef BITMIX
    bits = bitmix(term);
#else
    bits = term;
#endif
    if(!bit(bloom,bits)){
      term = 0;
      set(bloom,bits);
    }
  }

  int cnt = 0;
  for(int i = 0; i < BLOOMSIZE; i++)
    cnt += __builtin_popcountll(bloom[i]);
  printf("%d bits out of %d set\n", cnt, BLOOMSIZE * 64);
}