| 184 | } |
| 185 | |
| 186 | void BAFpileup::createBedFileWithChromosomeLengths(std::string bedFileWithRegionsOfInterest, std::string chrLenFileName, bool doesNeedChrPrefix) { |
| 187 | |
| 188 | //reading the file with the chromosome length information |
| 189 | std::vector<std::string> chr_names; |
| 190 | std::vector<int> lengths; |
| 191 | ifstream file(chrLenFileName.c_str()); |
| 192 | if (!file.is_open()) { |
| 193 | cerr << "Error: unable to open "+chrLenFileName+"\n" ; |
| 194 | exit(-1); |
| 195 | } |
| 196 | string line; |
| 197 | string name; |
| 198 | int value = 0; |
| 199 | bool isFai=0; |
| 200 | if (chrLenFileName.substr(chrLenFileName.length()-3,3).compare("fai")==0) {isFai=1;} |
| 201 | while (std::getline(file,line)) { |
| 202 | |
| 203 | if (! line.length()) continue; |
| 204 | if (line[0] == '#') continue; |
| 205 | |
| 206 | std::vector<std::string> strs = split(line, '\t'); |
| 207 | if (strs.size()<2) { |
| 208 | cerr << "uncorrect file with chromosomes "<< chrLenFileName <<"\nUse tab-delimited format:\n1\tchr1\t249250621\nor\nchr1\t249250621\n"; |
| 209 | } |
| 210 | if (strs.size()==2 || strs[0].substr(0,3)=="chr" || isFai) { |
| 211 | name = strs[0]; |
| 212 | value = atoi(strs[1].c_str()); |
| 213 | } |
| 214 | if (strs.size()>=3 && strs[0].substr(0,3)!="chr" && !isFai) { |
| 215 | name = strs[1]; |
| 216 | value = atoi(strs[2].c_str()); |
| 217 | } |
| 218 | strs.clear(); |
| 219 | myReplace(name, " ", ""); |
| 220 | |
| 221 | //delete "Chr" |
| 222 | string::size_type pos = 0; |
| 223 | if ( (pos = name.find("chr", pos)) != string::npos ) |
| 224 | name.replace( pos, 3, "" ); |
| 225 | if (value>0) { |
| 226 | chr_names.push_back(name); |
| 227 | lengths.push_back(value); |
| 228 | //cout << name << "\t" << value << "\n"; |
| 229 | } |
| 230 | } |
| 231 | file.close(); |
| 232 | cout << "..File "<<chrLenFileName<<" was read to create a miniPileup\n"; |
| 233 | |
| 234 | ofstream myfile; |
| 235 | myfile.open(bedFileWithRegionsOfInterest.c_str()); |
| 236 | for (unsigned int i = 0; i < chr_names.size(); i++) { |
| 237 | if(doesNeedChrPrefix) |
| 238 | myfile << "chr"<< chr_names[i] << "\t" << "1" << "\t" << lengths[i] << "\n"; |
| 239 | else |
| 240 | myfile << chr_names[i] << "\t" << "1" << "\t" << lengths[i] << "\n"; |
| 241 | } |
| 242 | myfile.close(); |
| 243 | } |