1 |
gpertea |
29 |
/* |
2 |
|
|
* GFaIdx.h |
3 |
|
|
* |
4 |
|
|
* Created on: Aug 25, 2010 |
5 |
|
|
* Author: gpertea |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#ifndef GFAIDX_H_ |
9 |
|
|
#define GFAIDX_H_ |
10 |
|
|
|
11 |
|
|
#include "GHash.hh" |
12 |
|
|
#include "GList.hh" |
13 |
|
|
|
14 |
|
|
class GFastaRec { |
15 |
|
|
public: |
16 |
|
|
char* seqname; |
17 |
|
|
uint seqlen; |
18 |
|
|
off_t fpos; |
19 |
|
|
int line_len; //effective line length (without EoL) |
20 |
|
|
int line_blen; //length of line including EoL characters |
21 |
|
|
GFastaRec(uint slen=0, off_t fp=0, int llen=0, int llenb=0) { |
22 |
|
|
seqname=NULL; //only a pointer copy |
23 |
|
|
seqlen=slen; |
24 |
|
|
fpos=fp; |
25 |
|
|
line_len=llen; |
26 |
|
|
line_blen=llenb; |
27 |
|
|
} |
28 |
|
|
bool operator==(GFastaRec& d){ |
29 |
|
|
return (fpos==d.fpos); |
30 |
|
|
} |
31 |
|
|
bool operator>(GFastaRec& d){ |
32 |
|
|
return (fpos>d.fpos); |
33 |
|
|
} |
34 |
|
|
bool operator<(GFastaRec& d){ |
35 |
|
|
return (fpos<d.fpos); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
class GFastaIndex { |
41 |
|
|
char* fa_name; |
42 |
|
|
char* fai_name; |
43 |
|
|
bool haveFai; |
44 |
|
|
public: |
45 |
|
|
GHash<GFastaRec> records; |
46 |
|
|
void addRecord(const char* seqname, uint seqlen, |
47 |
|
|
off_t foffs, int llen, int llen_full); |
48 |
|
|
|
49 |
|
|
GFastaRec* getRecord(const char* seqname) { |
50 |
|
|
return records.Find(seqname); |
51 |
|
|
} |
52 |
|
|
bool hasIndex() { return haveFai; } |
53 |
|
|
int loadIndex(const char* finame); |
54 |
|
|
int buildIndex(); //build index in memory by parsing the whole fasta file |
55 |
|
|
int storeIndex(const char* finame); |
56 |
|
|
int storeIndex(FILE* fai); |
57 |
|
|
int getCount() { return records.Count(); } |
58 |
|
|
GFastaIndex(const char* fname, const char* finame=NULL):records() { |
59 |
|
|
if (fileExists(fname)!=2) GError("Error: fasta file %s not found!\n",fname); |
60 |
|
|
if (fileSize(fname)<=0) GError("Error: invalid fasta file %s !\n",fname); |
61 |
|
|
fa_name=Gstrdup(fname); |
62 |
|
|
fai_name=finame!=NULL ? Gstrdup(finame) : NULL; |
63 |
|
|
if (fileSize(fa_name)==0) { |
64 |
|
|
GError("Error creating GFastaIndex(%s): invalid fasta file!\n",fa_name); |
65 |
|
|
} |
66 |
|
|
haveFai=false; |
67 |
|
|
if (fai_name!=NULL && fileSize(fai_name)>0) { |
68 |
|
|
//try to load the index file if it exists |
69 |
|
|
loadIndex(fai_name); |
70 |
|
|
haveFai=(records.Count()>0); |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
~GFastaIndex() { |
74 |
|
|
GFREE(fa_name); |
75 |
|
|
GFREE(fai_name); |
76 |
|
|
} |
77 |
|
|
}; |
78 |
|
|
|
79 |
|
|
#endif /* GFAIDX_H_ */ |