-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGenomeScore.h
104 lines (97 loc) · 2.71 KB
/
GenomeScore.h
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
#ifndef _GENOMESCORE_H_
#define _GENOMESCORE_H_
#include <map>
#include <string>
#include <cstdio>
#include "IO.h"
#include "TypeConversion.h"
class GenomeScore {
std::string dir;
std::map<std::string,FILE*> fpmap;
std::string curChrom;
public:
static int convert(const char* chrom, const char* inFile, const char* outFile) {
FILE* fp = fopen(outFile, "wb");
LineReader lr (inFile);
std::vector <std::string> fd;
int lineNo = 0;
float value;
while (lr.readLineBySep(&fd, "\t ")) {
value = toFloat(fd[1]);
if (fwrite(&value, sizeof(float), 1, fp) == 0) {
fprintf(stderr, "Cannot write base position %s:%d in %s\n", chrom, lineNo, outFile);
return -1;
} else {
++lineNo;
}
if (lineNo % 1000000l == 0) {
fprintf(stderr, "\rFinished %s:%d ...", chrom, lineNo);
};
};
};
bool openChr(const char* chrom) {
if ( fpmap.find(chrom) != fpmap.end() ) {
// file pointer already exist. Do nothing
return false;
}
else {
std::string fname = dir + "/chr" + chrom + ".fbin";
FILE* fp = fopen(fname.c_str(),"rb");
if ( fp == NULL ) {
fprintf(stderr, "Cannot open genomeScore file %s\n",fname.c_str());
return false;
}
fpmap[chrom] = fp;
return true;
}
};
public:
GenomeScore(const char* _dir) : dir(_dir) {}
bool empty() { return dir.empty(); }
void setDir(const char* _dir) {
if ( !dir.empty() ) {
for(std::map<std::string,FILE*>::iterator it = fpmap.begin();
it != fpmap.end(); ++it) {
fclose(it->second);
}
fpmap.clear();
}
dir = _dir;
}
~GenomeScore() {
for(std::map<std::string,FILE*>::iterator it = fpmap.begin();
it != fpmap.end(); ++it) {
fclose(it->second);
}
fpmap.clear();
}
/**
* @param pos is 1-based index
*/
float baseScore(const char* chrom, int pos) {
openChr(chrom);
FILE* fp = fpmap[chrom];
if ( fseek(fp, (pos-1)*4, SEEK_SET) != 0 ) {
fprintf(stderr, "Cannot access base position %s:%d in %s\n",chrom,pos,dir.c_str());
return 0;
}
float ret;
if ( fread(&ret,sizeof(float),1,fp) == 0 ) {
fprintf(stderr, "Cannot read base position %s:%d in %s\n",chrom,pos,dir.c_str());
return 0;
}
//error("baseScore(%s,%d)=%f",chrom,pos,ret);
return ret;
}
float baseScore(const char* markerID) {
char* pcolon = strchr((char*)markerID,':');
if ( pcolon == NULL) {
fprintf(stderr, "Cannot parse marker %s in %s\n",markerID,dir.c_str());
return 0;
}
std::string chrom(markerID,pcolon-markerID);
int pos = atoi(pcolon+1);
return baseScore(chrom.c_str(),pos);
}
};
#endif /* _GENOMESCORE_H_ */