This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
forked from thegenemyers/DALIGNER
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDBX.c
85 lines (77 loc) · 1.82 KB
/
DBX.c
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
#include "DBX.h"
#include "DB.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <assert.h>
// From Jason, with 1 change
static char* Load_Read_Data(DAZZ_DB *db) {
FILE *bases = (FILE*) db->bases;
struct stat sbuf;
char *data;
bases = fopen(Catenate(db->path,"","",".bps"),"r");
if (bases == NULL) EXIT(1);
stat(Catenate(db->path,"","",".bps"), &sbuf);
data = (char *) malloc(sbuf.st_size);
if (data == NULL) return NULL; // was EXIT(1), but we can proceed
fread(data, sbuf.st_size, 1, bases);
fclose(bases);
return(data);
}
// Wrapper
int Open_DBX(char *path, DAZZ_DBX *dbx, bool preload) {
dbx->data = NULL;
int rc = Open_DB(path, &dbx->db);
switch (rc) {
case -1:
return -1;
case 0:
break;
case 1:
assert(rc != 1);
abort();
default:
assert(rc < -1 || rc > 1);
abort();
}
if (preload) {
dbx->data = Load_Read_Data(&dbx->db);
}
return 0;
}
// From Jason
static int Load_Read_From_RAM(DAZZ_DB *db, char *data, int i, char *read, int ascii) {
int64 off;
int len, clen;
DAZZ_READ *r = db->reads;
if (i >= db->nreads) { EXIT(1); }
off = r[i].boff;
len = r[i].rlen;
clen = COMPRESSED_LEN(len);
if (clen > 0) { memcpy(read, data + off, clen); } //fread(read,clen,1,bases)
Uncompress_Read(len, read);
if (ascii == 1)
{ Lower_Read(read);
read[-1] = '\0';
}
else if (ascii == 2)
{ Upper_Read(read);
read[-1] = '\0';
}
else
read[-1] = 4;
return (0);
}
// Wrapper
int Load_ReadX(DAZZ_DBX *dbx, int i, char *read, int ascii) {
if (dbx->data) {
return Load_Read_From_RAM(&dbx->db, dbx->data, i, read, ascii);
} else {
return Load_Read(&dbx->db, i, read, ascii);
}
}
// Wrapper
void Close_DBX(DAZZ_DBX *dbx) {
Close_DB(&dbx->db);
if (dbx->data) free(dbx->data);
}