-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileHandler.cpp
More file actions
83 lines (74 loc) · 1.76 KB
/
fileHandler.cpp
File metadata and controls
83 lines (74 loc) · 1.76 KB
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
#include "fileHandler.h"
// for BAM file
string getNext(int& pos, char* rec_seq, int rec_len) {
string nextStr = "";
// skip the spaces
while (pos < rec_len && rec_seq[pos] == '\t')
pos++;
// get the sequence
while (pos < rec_len && rec_seq[pos] != '\t') {
nextStr.append(1, rec_seq[pos]);
pos++;
}
return nextStr;
}
void SamBamFileHander::openFile(char* fileName) {
// first assume the file is in BAM format
bamQueryFile = bam_open ( fileName, "r" );
bamHeader = bam_header_init();
int i;
string aline;
i = bgzf_check_EOF(bamQueryFile);
if (i == 0) {
// cout << "The format of inut file: SAM" << endl;
type = 2; // SAM format
bam_close(bamQueryFile);
fin.open(fileName);
} else {
// cout << "The format of inut file: BAM" << endl;
type = 1; // BAM format
bamHeader = bam_header_read ( bamQueryFile );
bam = bam_init1();
}
}
bool SamBamFileHander::getNextSeq(string& line) {
char* rec_seq;
size_t rec_len;
int i;
if (type==1) {
// BAM format
if (bam_read1 (bamQueryFile, bam) > 0) {
rec_seq = bam_format1 (bamHeader, bam);
rec_len = strlen(rec_seq);
if (rec_len == 0)
return false;
line.resize(rec_len);
for (i=0; i<rec_len; i++)
line[i] = rec_seq[i];
free(rec_seq);
} else
return false;
} else {
// SAM format
// skip all the sentences starting with char "@"
if (getline(fin, line)) {
while ((line.length()==0 || (line.length() > 0 && line[0]=='@'))) {
if (!getline(fin, line))
return false;
}
} else {
return false;
}
}
return true;
}
void SamBamFileHander::closeFile() {
if (type==1) {
// BAM format
bam_close(bamQueryFile);
delete bamHeader;
} else {
// SAM format
fin.close();
}
}