Skip to content

Commit 0376b1e

Browse files
committed
Add some additional check
1 parent 24f5ef7 commit 0376b1e

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,16 @@ int main(int argc, char *argv[])
520520
std::string chrLenFile = (std::string)cf.Value("general","chrLenFile","");
521521
cout << "..File with chromosome lengths:\t" << chrLenFile << "\n";
522522

523+
if (targetBed!="" & chrLenFile!="") {
524+
//make a check that in the chromosome length file there is no chromosomes absent in the captureRegions:
525+
bool chrLenFileIsOK = checkChrLen(chrLenFile,targetBed);
526+
if (!chrLenFileIsOK) {
527+
cerr <<"Will exit\n";
528+
exit (1);
529+
}
530+
}
531+
532+
523533
bool isMinMappabilitySet = cf.hasValue("general","minMappabilityPerWindow");
524534
minMappabilityPerWindow = double(cf.Value("general","minMappabilityPerWindow",0.85));
525535
if (isMinMappabilitySet && isUseGC) {

src/myFunc.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ std::vector<std::string> &split(const std::string &s, char delim, std::vector<st
7171
return elems;
7272
}
7373

74+
bool checkChrLen(const std::string &chrLenFile,const std::string &targetBed) {
75+
std::vector<std::string> names;
76+
std::vector<int> lengths;
77+
std::vector<std::string> names_bed;
78+
readFileWithGenomeInfo(chrLenFile, names, lengths);
79+
readChrNamesInBed(targetBed, names_bed);
80+
bool toReturn = true;
81+
82+
if(names.empty()){
83+
cerr << "Error:Cound not read "<< chrLenFile<<"\n";
84+
exit(1);
85+
}
86+
if(names_bed.empty()){
87+
cerr << "Error:Cound not read "<< targetBed<<"\n";
88+
exit(1);
89+
}
90+
91+
for (int i=0; i<names.size();i++) {
92+
if(std::find(names_bed.begin(), names_bed.end(), names[i]) != names_bed.end()) {
93+
/* names_bed contains names[i]; everything is OK */
94+
} else {
95+
/* names_bed does not contain names[i] */
96+
toReturn = false;
97+
cerr << "Error: chromosome "<< names[i]<< " present in your "<<chrLenFile << " file was not detected in your file with capture regions " <<targetBed<<"\n";
98+
cerr << "Please solve this issue and rerun Control-FREEC\n";
99+
cerr << "For example, you can remove chromosome "<< names[i]<<" from your "<<chrLenFile<<"\n";
100+
}
101+
}
102+
return toReturn;
103+
}
74104

75105
std::vector<std::string> split(const std::string &s, char delim) {
76106
std::vector<std::string> elems;
@@ -234,6 +264,38 @@ float get_iqr(const std::vector<float>& myvector) {
234264
return upper_quartile - lower_quartile;
235265
}
236266

267+
void readChrNamesInBed(const std::string &targetBed, std::vector<std::string>&names_bed){
268+
ifstream file(targetBed.c_str());
269+
if (!file.is_open()) {
270+
cerr << "Error: unable to open "+targetBed+"\n" ;
271+
exit(-1);
272+
}
273+
string line;
274+
string name;
275+
bool isFai=0;
276+
while (std::getline(file,line)) {
277+
if (! line.length()) continue;
278+
if (line[0] == '#') continue;
279+
280+
std::vector<std::string> strs = split(line, '\t');
281+
if (strs.size()<2) {
282+
continue;
283+
}
284+
name = strs[0];
285+
strs.clear();
286+
myReplace(name, " ", "");
287+
288+
//delete "Chr"
289+
string::size_type pos = 0;
290+
if ( (pos = name.find("chr", pos)) != string::npos )
291+
name.replace( pos, 3, "" );
292+
293+
names_bed.push_back(name);
294+
}
295+
file.close();
296+
}
297+
298+
237299
void readFileWithGenomeInfo(const std::string &chrLenFileName, std::vector<std::string>& names, std::vector<int>& lengths) {
238300
//reading the file with genome information
239301
ifstream file(chrLenFileName.c_str());

src/myFunc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ float get_weighted_mean(const std::vector<float>& data, const std::vector<float>
8383
float get_sum(const std::vector<float>& data) ;
8484
float get_iqr(const std::vector<float>& data);
8585
void readFileWithGenomeInfo(const std::string &chrLenFileName, std::vector<std::string>& names, std::vector<int>& lengths);
86+
void readChrNamesInBed(const std::string &targetBed, std::vector<std::string>&names_bed);
8687
unsigned long sum(const std::vector<int>& data);
8788
long getLineNumber(std::string const& file, const std::string& pathToSamtools, const std::string& pathToSambamba, const std::string& SambambaThreads);
8889
long getReadNumberFromPileup(std::string const& file);
@@ -150,6 +151,7 @@ void advance_to(const std::string& haystack, size_t& offset, char needle) ;
150151
std::vector<float> get_quartiles(std::vector<float> vect);
151152

152153
int calculateTotalLength(std::vector <int> lefts,std::vector <int> rights);
154+
bool checkChrLen(const std::string &chrLenFile,const std::string &targetBed) ;
153155

154156
#ifdef _WIN32
155157
double expm1(double x);

0 commit comments

Comments
 (0)