-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_dist.cpp
executable file
·201 lines (177 loc) · 7.12 KB
/
build_dist.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// build distribution file based on flow cells (reading group)
#define SEQAN_HAS_ZLIB 1
#include <seqan/bam_io.h>
#include "utils.h"
void write_counts(map <seqan::CharString, map<int, int> > &rg_lenCounts, string &rg_lenCnt_file){
map <seqan::CharString, map<int, int> >::iterator sii;
map<int, int>::iterator si;
cout << "output to: " << rg_lenCnt_file << endl;
for (sii = rg_lenCounts.begin(); sii != rg_lenCounts.end() ; sii++) {
ofstream fout((rg_lenCnt_file + toCString(sii->first)).c_str());
for (si = (sii->second).begin(); si != (sii->second).end(); si++)
fout << si->first << " " << si->second << endl;
fout.close();
}
}
void read_pn(map <seqan::CharString, map<int, int> > &rg_lenCnt, string &bamInput_file, int jump_first, int max_read_num){
seqan::BamAlignmentRecord record;
seqan::BamStream bamStreamIn(bamInput_file.c_str());
assert(isGood(bamStreamIn));
unsigned idx_RG;
int i = 0;
while (!atEnd(bamStreamIn)) {
i++;
readRecord(record, bamStreamIn);
if ( record.rID > 20 ) break; // only count from chr0 - chr21
if ( i < jump_first) continue;
if ( max_read_num and i > max_read_num ) break;
if ((not hasFlagQCNoPass(record) ) and hasFlagAllProper(record) and (not hasFlagDuplicate(record)) and hasFlagMultiple(record) ) {
if (hasFlagFirst(record)) continue; // look at only one end of the pair
seqan::BamTagsDict tags(record.tags);
if (!findTagKey(idx_RG, tags, "RG")) continue;
seqan::CharString rg = getTagValue(tags, idx_RG);
addKey(rg_lenCnt[rg], abs(record.tLen));
}
}
seqan::close(bamStreamIn);
}
void write_pdf(string &f_count, string &f_prob, int len_min, int len_max, int bin_width){
int insertlen, count, counts = 0;
map <int, int> bin_counts;
ifstream fin(f_count.c_str());
assert(fin);
while (fin >> insertlen >> count) {
if (insertlen < len_min) continue;
if (insertlen >= len_max) break;
int id_bin = (insertlen - len_min)/bin_width;
addKey(bin_counts, id_bin, count);
counts += count;
}
fin.close();
int half_bin_width = bin_width / 2;
ofstream fout(f_prob.c_str());
for (map<int, int>::iterator bc = bin_counts.begin(); bc != bin_counts.end(); bc++)
fout << len_min + (bc->first) * bin_width + half_bin_width << " " << (bc->second) / (float)counts << endl;
fout.close();
cout << "written into " << f_prob << endl;
}
int main( int argc, char* argv[] )
{
string config_file = argv[1];
string opt = argv[2];
int idx_pn = seqan::lexicalCast <int> (argv[3]);
ConfigFileHandler cf_fh = ConfigFileHandler(config_file);
if (argc != 4) exit(1);
map<int, string> ID_pn;
get_pn(cf_fh.get_conf("file_pn"), ID_pn);
string pn = ID_pn[idx_pn];
string bamInput_file = cf_fh.get_conf( "file_bam_prefix") + pn + ".bam";
if ( opt == "build_dist") {
string path0 = cf_fh.get_conf("file_insertlen");
check_folder_exists(path0);
string path_count = path0 + "count/";
check_folder_exists(path_count);
string path_prob = cf_fh.get_conf("file_dist_prefix");
check_folder_exists(path_prob);
string rg_lenCnt_file = path_count + pn + ".count."; /* + RG*/
map <seqan::CharString, map<int, int> > rg_lenCnt; // strata by reading group
//read_pn(rg_lenCnt, bamInput_file, 5e3, 5e7); // use only 5% reads to estimate
read_pn(rg_lenCnt, bamInput_file, 5e3, 0);
write_counts(rg_lenCnt, rg_lenCnt_file);
ofstream fout( (path_prob + "RG." + pn).c_str());
for (map <seqan::CharString, map<int, int> >::iterator ri = rg_lenCnt.begin(); ri != rg_lenCnt.end(); ri++ )
fout << seqan::toCString(ri->first) << endl; // need to use toCString, otherwise has ^@ in the ending (due to binary file)
fout.close();
cout << "counting: " << pn << " done\n";
stringstream ss;
string pdf_param = cf_fh.get_conf( "pdf_param");
ss.str(pdf_param);
int len_min, len_max, bin_width; ///int len_min = 100, len_max = 1000, bin_width = 5;
string token;
getline(ss, token, '_');
seqan::lexicalCast2(len_min, token);
getline(ss, token, '_');
seqan::lexicalCast2(len_max, token);
getline(ss, token, '_');
seqan::lexicalCast2(bin_width, token);
string rg;
ifstream fin( (path_prob + "RG." + pn).c_str());
assert(fin);
while (fin >> rg) {
string f_count = path_count + pn + ".count." + rg;
string f_prob = path_prob + pn + ".count." + rg + "." + pdf_param;
cout << "Reading " << f_count << "\nWriting " << f_prob << endl;
write_pdf(f_count, f_prob, len_min, len_max, bin_width);
}
fin.close();
} else if ( opt == "mason_db" ) { // write database for mason simulation
string path_input = "/home/qianyuxx/faststorage/AluDK/inputs/alu_hg19/";
string path_output = "/home/qianyuxx/faststorage/AluDK/inputs/alu_hg19_mason/";
AluRefPos *alurefpos;
vector<string> chrns;
for (int i = 1; i < 23; i++) chrns.push_back("chr" + int_to_string(i) );
chrns.push_back("chrX");
chrns.push_back("chrY");
for (vector<string>::iterator ci = chrns.begin(); ci!= chrns.end(); ci++) {
string chrn = *ci;
alurefpos = new AluRefPos(path_input + "alu_" + chrn, 200, 600); // no Alu within 600 bp
ofstream fout ( (path_output + "alu_" + chrn).c_str() );
while ( alurefpos->nextdb() ) {
string at;
int beginPos, endPos;
beginPos = alurefpos->get_beginP();
endPos = alurefpos->get_endP();
at = alurefpos->get_type();
fout << chrn << " " << beginPos << " " << endPos << " " << at << endl;
}
delete alurefpos;
fout.close();
}
} else if ( opt == "debug" ) {
string s1 = "apple is apple\n";
string s2 = replace_str0_str(s1, "banan", "apple");
cout << s1;
cout << s2;
return 0;
string chrn = "chr1";
ConfigFileHandler cf_fh = ConfigFileHandler(config_file);
string file_fa = cf_fh.get_conf("file_fa_prefix");
FastaFileHandler *fasta_fh = new FastaFileHandler(file_fa + chrn + ".fa", chrn);
vector<string> chrns;
chrns.push_back(chrn);
BamFileHandler *bam_fh = new BamFileHandler(chrns, bamInput_file, bamInput_file + ".bai");
bam_fh->print_mapping_rID2chrn();
return 0;
bam_fh->jump_to_region(chrn, 59768068, 59768098);
seqan::BamAlignmentRecord record;
int i = 0;
//// try RC flag
while (i < 4000) {
bam_fh->fetch_a_read(record);
if (!QC_delete_read(record)) continue;
////cout << i << " " << (hasFlagRC(record) != left_read(record)) << endl;
if ( !left_read(record) )
cout << record.beginPos - record.pNext + getAlignmentLengthInRef(record) << " " << - record.tLen << endl; // ALWAYS equal
i++;
}
/////////// NB !
//////// if left_read, then no RC flag !!!
// 10 0
// 3990 1
/*
seqan::CharString ref_fa;
while (i < 100) {
bam_fh->fetch_a_read(record);
if ( QC_delete_read(record) and length(record.cigar)==1 ) {
fasta_fh->fetch_fasta_upper( record.beginPos, record.beginPos + getAlignmentLengthInRef(record), ref_fa);
cout << hasFlagRC(record) << endl;
cout << record.seq << endl;
cout << ref_fa << endl;
i++;
}
}
*/
delete fasta_fh;
}
return 0;
}