| 1 |
#ifndef FILTER_H |
| 2 |
#define FILTER_H |
| 3 |
|
| 4 |
#include <iostream> |
| 5 |
#include <fstream> |
| 6 |
#include <boost/unordered_map.hpp> |
| 7 |
#include <boost/graph/adjacency_list.hpp> |
| 8 |
#include <boost/graph/connected_components.hpp> |
| 9 |
#include <boost/program_options/cmdline.hpp> |
| 10 |
#include <boost/program_options/options_description.hpp> |
| 11 |
#include <boost/program_options/parsers.hpp> |
| 12 |
#include <boost/program_options/variables_map.hpp> |
| 13 |
#include <boost/date_time/posix_time/posix_time.hpp> |
| 14 |
#include <boost/date_time/gregorian/gregorian.hpp> |
| 15 |
#include <boost/iostreams/stream.hpp> |
| 16 |
#include <boost/iostreams/stream_buffer.hpp> |
| 17 |
#include <boost/iostreams/device/file.hpp> |
| 18 |
#include <boost/iostreams/filtering_stream.hpp> |
| 19 |
#include <boost/iostreams/filter/zlib.hpp> |
| 20 |
#include <boost/iostreams/filter/gzip.hpp> |
| 21 |
#include <boost/icl/interval_map.hpp> |
| 22 |
#include <boost/filesystem.hpp> |
| 23 |
#include <boost/tokenizer.hpp> |
| 24 |
|
| 25 |
#include <htslib/sam.h> |
| 26 |
#include <htslib/vcf.h> |
| 27 |
#include <htslib/tbx.h> |
| 28 |
|
| 29 |
|
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <zlib.h> |
| 34 |
#include <stdio.h> |
| 35 |
|
| 36 |
#include "tags.h" |
| 37 |
#include "version.h" |
| 38 |
#include "util.h" |
| 39 |
#include "modvcf.h" |
| 40 |
|
| 41 |
namespace torali |
| 42 |
{ |
| 43 |
|
| 44 |
|
| 45 |
struct FilterConfig { |
| 46 |
bool filterForPass; |
| 47 |
bool hasSampleFile; |
| 48 |
bool softFilter; |
| 49 |
int32_t minsize; |
| 50 |
int32_t maxsize; |
| 51 |
int32_t coverage; |
| 52 |
int32_t qualthres; |
| 53 |
float ratiogeno; |
| 54 |
float altaf; |
| 55 |
float controlcont; |
| 56 |
float gq; |
| 57 |
float rddel; |
| 58 |
float rddup; |
| 59 |
std::string filter; |
| 60 |
std::set<std::string> tumorSet; |
| 61 |
std::set<std::string> controlSet; |
| 62 |
boost::filesystem::path outfile; |
| 63 |
boost::filesystem::path samplefile; |
| 64 |
boost::filesystem::path vcffile; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
template<typename TFilterConfig> |
| 69 |
inline int |
| 70 |
filterRun(TFilterConfig const& c) { |
| 71 |
|
| 72 |
|
| 73 |
htsFile* ifile = hts_open(c.vcffile.string().c_str(), "r"); |
| 74 |
bcf_hdr_t* hdr = bcf_hdr_read(ifile); |
| 75 |
|
| 76 |
|
| 77 |
std::string fmtout = "wb"; |
| 78 |
if (c.outfile.string() == "-") fmtout = "w"; |
| 79 |
htsFile *ofile = hts_open(c.outfile.string().c_str(), fmtout.c_str()); |
| 80 |
bcf_hdr_t *hdr_out = bcf_hdr_dup(hdr); |
| 81 |
if (c.filter == "somatic") { |
| 82 |
bcf_hdr_remove(hdr_out, BCF_HL_INFO, "RDRATIO"); |
| 83 |
bcf_hdr_append(hdr_out, "##INFO=<ID=RDRATIO,Number=1,Type=Float,Description=\"Read-depth ratio of tumor vs. normal.\">"); |
| 84 |
bcf_hdr_remove(hdr_out, BCF_HL_INFO, "SOMATIC"); |
| 85 |
bcf_hdr_append(hdr_out, "##INFO=<ID=SOMATIC,Number=0,Type=Flag,Description=\"Somatic structural variant.\">"); |
| 86 |
if (c.softFilter) { |
| 87 |
bcf_hdr_append(hdr_out, "##FILTER=<ID=FailDellyFilter,Description=\"Failed delly filter.\">"); |
| 88 |
bcf_hdr_append(hdr_out, "##FILTER=<ID=FailSomatic,Description=\"Failed somatic filter (likely germline variant).\">"); |
| 89 |
} |
| 90 |
} else if (c.filter == "germline") { |
| 91 |
bcf_hdr_remove(hdr_out, BCF_HL_INFO, "RDRATIO"); |
| 92 |
bcf_hdr_append(hdr_out, "##INFO=<ID=RDRATIO,Number=1,Type=Float,Description=\"Read-depth ratio of SV carrier vs. non-carrier.\">"); |
| 93 |
if (c.softFilter) { |
| 94 |
bcf_hdr_append(hdr_out, "##FILTER=<ID=FailDellyFilter,Description=\"Failed delly filter.\">"); |
| 95 |
bcf_hdr_append(hdr_out, "##FILTER=<ID=FailGermline,Description=\"Failed germline filter.\">"); |
| 96 |
} |
| 97 |
} |
| 98 |
if (bcf_hdr_write(ofile, hdr_out) != 0) std::cerr << "Error: Failed to write BCF header!" << std::endl; |
| 99 |
|
| 100 |
|
| 101 |
int32_t nsvend = 0; |
| 102 |
int32_t* svend = NULL; |
| 103 |
int32_t nsvt = 0; |
| 104 |
char* svt = NULL; |
| 105 |
int32_t ninslen = 0; |
| 106 |
int32_t* inslen = NULL; |
| 107 |
int ngt = 0; |
| 108 |
int32_t* gt = NULL; |
| 109 |
int ngq = 0; |
| 110 |
int32_t* gq = NULL; |
| 111 |
float* gqf = NULL; |
| 112 |
int nrc = 0; |
| 113 |
int32_t* rc = NULL; |
| 114 |
int nrcl = 0; |
| 115 |
int32_t* rcl = NULL; |
| 116 |
int nrcr = 0; |
| 117 |
int32_t* rcr = NULL; |
| 118 |
int ndv = 0; |
| 119 |
int32_t* dv = NULL; |
| 120 |
int ndr = 0; |
| 121 |
int32_t* dr = NULL; |
| 122 |
int nrv = 0; |
| 123 |
int32_t* rv = NULL; |
| 124 |
int nrr = 0; |
| 125 |
int32_t* rr = NULL; |
| 126 |
bool germline = false; |
| 127 |
if (c.filter == "germline") germline = true; |
| 128 |
|
| 129 |
|
| 130 |
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
| 131 |
std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "Filtering VCF/BCF file" << std::endl; |
| 132 |
bcf1_t* rec = bcf_init1(); |
| 133 |
while (bcf_read(ifile, hdr, rec) == 0) { |
| 134 |
bcf_unpack(rec, BCF_UN_INFO); |
| 135 |
|
| 136 |
|
| 137 |
bcf_get_info_string(hdr, rec, "SVTYPE", &svt, &nsvt); |
| 138 |
|
| 139 |
|
| 140 |
bcf_get_info_int32(hdr, rec, "END", &svend, &nsvend); |
| 141 |
bool pass = true; |
| 142 |
if (c.filterForPass) pass = (bcf_has_filter(hdr, rec, const_cast<char*>("PASS"))==1); |
| 143 |
int32_t svlen = 1; |
| 144 |
if (svend != NULL) svlen = *svend - rec->pos; |
| 145 |
int32_t inslenVal = 0; |
| 146 |
if (bcf_get_info_int32(hdr, rec, "INSLEN", &inslen, &ninslen) > 0) inslenVal = *inslen; |
| 147 |
if ((rec->qual >= c.qualthres) && (pass) && ((std::string(svt) == "BND") || ((std::string(svt) == "INS") && (inslenVal >= c.minsize) && (inslenVal <= c.maxsize)) || ((std::string(svt) != "BND") && (std::string(svt) != "INS") && (svlen >= c.minsize) && (svlen <= c.maxsize)))) { |
| 148 |
|
| 149 |
bcf_unpack(rec, BCF_UN_ALL); |
| 150 |
bool precise = false; |
| 151 |
if (bcf_get_info_flag(hdr, rec, "PRECISE", 0, 0) > 0) precise = true; |
| 152 |
bcf_get_format_int32(hdr, rec, "GT", >, &ngt); |
| 153 |
if (_getFormatType(hdr, "GQ") == BCF_HT_INT) bcf_get_format_int32(hdr, rec, "GQ", &gq, &ngq); |
| 154 |
else if (_getFormatType(hdr, "GQ") == BCF_HT_REAL) bcf_get_format_float(hdr, rec, "GQ", &gqf, &ngq); |
| 155 |
bcf_get_format_int32(hdr, rec, "RC", &rc, &nrc); |
| 156 |
if (_isKeyPresent(hdr, "RCL")) bcf_get_format_int32(hdr, rec, "RCL", &rcl, &nrcl); |
| 157 |
if (_isKeyPresent(hdr, "RCR")) bcf_get_format_int32(hdr, rec, "RCR", &rcr, &nrcr); |
| 158 |
bcf_get_format_int32(hdr, rec, "DV", &dv, &ndv); |
| 159 |
bcf_get_format_int32(hdr, rec, "DR", &dr, &ndr); |
| 160 |
bcf_get_format_int32(hdr, rec, "RV", &rv, &nrv); |
| 161 |
bcf_get_format_int32(hdr, rec, "RR", &rr, &nrr); |
| 162 |
std::vector<float> rcraw; |
| 163 |
std::vector<float> rcControl; |
| 164 |
std::vector<float> rcTumor; |
| 165 |
std::vector<float> rcAlt; |
| 166 |
std::vector<float> rRefVar; |
| 167 |
std::vector<float> rAltVar; |
| 168 |
std::vector<float> gqRef; |
| 169 |
std::vector<float> gqAlt; |
| 170 |
uint32_t nCount = 0; |
| 171 |
uint32_t tCount = 0; |
| 172 |
uint32_t controlpass = 0; |
| 173 |
uint32_t tumorpass = 0; |
| 174 |
int32_t ac[2]; |
| 175 |
ac[0] = 0; |
| 176 |
ac[1] = 0; |
| 177 |
for (int i = 0; i < bcf_hdr_nsamples(hdr); ++i) { |
| 178 |
if ((bcf_gt_allele(gt[i*2]) != -1) && (bcf_gt_allele(gt[i*2 + 1]) != -1)) { |
| 179 |
int gt_type = bcf_gt_allele(gt[i*2]) + bcf_gt_allele(gt[i*2 + 1]); |
| 180 |
++ac[bcf_gt_allele(gt[i*2])]; |
| 181 |
++ac[bcf_gt_allele(gt[i*2 + 1])]; |
| 182 |
if ((germline) || (c.controlSet.find(hdr->samples[i]) != c.controlSet.end())) { |
| 183 |
|
| 184 |
++nCount; |
| 185 |
if (gt_type == 0) { |
| 186 |
rcraw.push_back(rc[i]); |
| 187 |
if (_getFormatType(hdr, "GQ") == BCF_HT_INT) gqRef.push_back(gq[i]); |
| 188 |
else if (_getFormatType(hdr, "GQ") == BCF_HT_REAL) gqRef.push_back(gqf[i]); |
| 189 |
if ((rcl != NULL) && (rcr != NULL) && (rcl[i] + rcr[i] != 0)) rcControl.push_back((float) rc[i] / ((float) (rcl[i] + rcr[i]))); |
| 190 |
else rcControl.push_back(rc[i]); |
| 191 |
float rVar = 0; |
| 192 |
if (!precise) rVar = (float) dv[i] / (float) (dr[i] + dv[i]); |
| 193 |
else rVar = (float) rv[i] / (float) (rr[i] + rv[i]); |
| 194 |
rRefVar.push_back(rVar); |
| 195 |
if (rVar <= c.controlcont) ++controlpass; |
| 196 |
} else if ((germline) && (gt_type >= 1)) { |
| 197 |
if (_getFormatType(hdr, "GQ") == BCF_HT_INT) gqAlt.push_back(gq[i]); |
| 198 |
else if (_getFormatType(hdr, "GQ") == BCF_HT_REAL) gqAlt.push_back(gqf[i]); |
| 199 |
if ((rcl != NULL) && (rcr != NULL) && (rcl[i] + rcr[i] != 0)) rcAlt.push_back((float) rc[i] / ((float) (rcl[i] + rcr[i]))); |
| 200 |
else rcAlt.push_back(rc[i]); |
| 201 |
float rVar = 0; |
| 202 |
if (!precise) rVar = (float) dv[i] / (float) (dr[i] + dv[i]); |
| 203 |
else rVar = (float) rv[i] / (float) (rr[i] + rv[i]); |
| 204 |
rAltVar.push_back(rVar); |
| 205 |
} |
| 206 |
} else if ((!germline) && (c.tumorSet.find(hdr->samples[i]) != c.tumorSet.end())) { |
| 207 |
|
| 208 |
++tCount; |
| 209 |
if ((rcl != NULL) && (rcr != NULL) && (rcl[i] + rcr[i] != 0)) rcTumor.push_back((float) rc[i] / ((float) (rcl[i] + rcr[i]))); |
| 210 |
else rcTumor.push_back(rc[i]); |
| 211 |
if (!precise) { |
| 212 |
if ((((float) dv[i] / (float) (dr[i] + dv[i])) >= c.altaf) && (dr[i] + dv[i] >= c.coverage)) ++tumorpass; |
| 213 |
} else { |
| 214 |
if ((((float) rv[i] / (float) (rr[i] + rv[i])) >= c.altaf) && (rr[i] + rv[i] >= c.coverage)) ++tumorpass; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
if (c.filter == "somatic") { |
| 220 |
float genotypeRatio = (float) (nCount + tCount) / (float) (c.controlSet.size() + c.tumorSet.size()); |
| 221 |
if ((controlpass) && (tumorpass) && (controlpass == nCount) && (genotypeRatio >= c.ratiogeno)) { |
| 222 |
float rccontrolmed = 0; |
| 223 |
getMedian(rcControl.begin(), rcControl.end(), rccontrolmed); |
| 224 |
float rctumormed = 0; |
| 225 |
getMedian(rcTumor.begin(), rcTumor.end(), rctumormed); |
| 226 |
float rdRatio = 1; |
| 227 |
if (rccontrolmed != 0) rdRatio = rctumormed/rccontrolmed; |
| 228 |
_remove_info_tag(hdr_out, rec, "RDRATIO"); |
| 229 |
bcf_update_info_float(hdr_out, rec, "RDRATIO", &rdRatio, 1); |
| 230 |
_remove_info_tag(hdr_out, rec, "SOMATIC"); |
| 231 |
bcf_update_info_flag(hdr_out, rec, "SOMATIC", NULL, 1); |
| 232 |
bcf_write1(ofile, hdr_out, rec); |
| 233 |
} else if (c.softFilter) { |
| 234 |
int32_t tmpi = bcf_hdr_id2int(hdr_out, BCF_DT_ID, "FailSomatic"); |
| 235 |
bcf_update_filter(hdr_out, rec, &tmpi, 1); |
| 236 |
bcf_write1(ofile, hdr_out, rec); |
| 237 |
} |
| 238 |
} else if (c.filter == "germline") { |
| 239 |
float genotypeRatio = (float) (nCount + tCount) / (float) (bcf_hdr_nsamples(hdr)); |
| 240 |
float rrefvarpercentile = 0; |
| 241 |
if (!rRefVar.empty()) getPercentile(rRefVar, 0.9, rrefvarpercentile); |
| 242 |
float raltvarmed = 0; |
| 243 |
if (!rAltVar.empty()) getMedian(rAltVar.begin(), rAltVar.end(), raltvarmed); |
| 244 |
float rccontrolmed = 0; |
| 245 |
if (!rcControl.empty()) getMedian(rcControl.begin(), rcControl.end(), rccontrolmed); |
| 246 |
float rcaltmed = 0; |
| 247 |
if (!rcAlt.empty()) getMedian(rcAlt.begin(), rcAlt.end(), rcaltmed); |
| 248 |
float rdRatio = 1; |
| 249 |
if (rccontrolmed != 0) rdRatio = rcaltmed/rccontrolmed; |
| 250 |
float gqaltmed = 0; |
| 251 |
if (!gqAlt.empty()) getMedian(gqAlt.begin(), gqAlt.end(), gqaltmed); |
| 252 |
float gqrefmed = 0; |
| 253 |
if (!gqRef.empty()) getMedian(gqRef.begin(), gqRef.end(), gqrefmed); |
| 254 |
float af = (float) ac[1] / (float) (ac[0] + ac[1]); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
bool failgerm = false; |
| 259 |
if (!((af>0) && (gqaltmed >= c.gq) && (gqrefmed >= c.gq) && (raltvarmed >= c.altaf) && (genotypeRatio >= c.ratiogeno))) failgerm = true; |
| 260 |
if ((std::string(svt)=="DEL") && (rdRatio > c.rddel)) failgerm = true; |
| 261 |
if ((std::string(svt)=="DUP") && (rdRatio < c.rddup)) failgerm = true; |
| 262 |
if ((std::string(svt)!="DEL") && (std::string(svt)!="DUP") && (rrefvarpercentile > 0)) failgerm = true; |
| 263 |
if (!failgerm) { |
| 264 |
_remove_info_tag(hdr_out, rec, "RDRATIO"); |
| 265 |
bcf_update_info_float(hdr_out, rec, "RDRATIO", &rdRatio, 1); |
| 266 |
bcf_write1(ofile, hdr_out, rec); |
| 267 |
} else if (c.softFilter) { |
| 268 |
int32_t tmpi = bcf_hdr_id2int(hdr_out, BCF_DT_ID, "FailGermline"); |
| 269 |
bcf_update_filter(hdr_out, rec, &tmpi, 1); |
| 270 |
bcf_write1(ofile, hdr_out, rec); |
| 271 |
} |
| 272 |
} |
| 273 |
} else if (c.softFilter) { |
| 274 |
int32_t tmpi = bcf_hdr_id2int(hdr_out, BCF_DT_ID, "FailDellyFilter"); |
| 275 |
bcf_update_filter(hdr_out, rec, &tmpi, 1); |
| 276 |
bcf_write1(ofile, hdr_out, rec); |
| 277 |
} |
| 278 |
} |
| 279 |
bcf_destroy(rec); |
| 280 |
|
| 281 |
|
| 282 |
if (svend != NULL) free(svend); |
| 283 |
if (svt != NULL) free(svt); |
| 284 |
if (inslen != NULL) free(inslen); |
| 285 |
if (gt != NULL) free(gt); |
| 286 |
if (gq != NULL) free(gq); |
| 287 |
if (gqf != NULL) free(gqf); |
| 288 |
if (rc != NULL) free(rc); |
| 289 |
if (rcl != NULL) free(rcl); |
| 290 |
if (rcr != NULL) free(rcr); |
| 291 |
if (dv != NULL) free(dv); |
| 292 |
if (dr != NULL) free(dr); |
| 293 |
if (rv != NULL) free(rv); |
| 294 |
if (rr != NULL) free(rr); |
| 295 |
|
| 296 |
|
| 297 |
bcf_hdr_destroy(hdr_out); |
| 298 |
hts_close(ofile); |
| 299 |
|
| 300 |
|
| 301 |
if (c.outfile.string() != "-") bcf_index_build(c.outfile.string().c_str(), 14); |
| 302 |
|
| 303 |
|
| 304 |
bcf_hdr_destroy(hdr); |
| 305 |
bcf_close(ifile); |
| 306 |
|
| 307 |
|
| 308 |
now = boost::posix_time::second_clock::local_time(); |
| 309 |
std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] Done." << std::endl; |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
int filter(int argc, char **argv) { |
| 316 |
FilterConfig c; |
| 317 |
|
| 318 |
|
| 319 |
boost::program_options::options_description generic("Generic options"); |
| 320 |
generic.add_options() |
| 321 |
("help,?", "show help message") |
| 322 |
("filter,f", boost::program_options::value<std::string>(&c.filter)->default_value("somatic"), "Filter mode (somatic, germline)") |
| 323 |
("outfile,o", boost::program_options::value<boost::filesystem::path>(&c.outfile), "Filtered SV BCF output file") |
| 324 |
("quality,y", boost::program_options::value<int32_t>(&c.qualthres)->default_value(300), "min. SV site quality") |
| 325 |
("altaf,a", boost::program_options::value<float>(&c.altaf)->default_value(0.03), "min. fractional ALT support") |
| 326 |
("minsize,m", boost::program_options::value<int32_t>(&c.minsize)->default_value(0), "min. SV size") |
| 327 |
("maxsize,n", boost::program_options::value<int32_t>(&c.maxsize)->default_value(500000000), "max. SV size") |
| 328 |
("ratiogeno,r", boost::program_options::value<float>(&c.ratiogeno)->default_value(0.75), "min. fraction of genotyped samples") |
| 329 |
("pass,p", "Filter sites for PASS") |
| 330 |
("tag,t", "Tag filtered sites in the FILTER column instead of removing them") |
| 331 |
; |
| 332 |
|
| 333 |
|
| 334 |
boost::program_options::options_description somatic("Somatic options"); |
| 335 |
somatic.add_options() |
| 336 |
("samples,s", boost::program_options::value<boost::filesystem::path>(&c.samplefile), "Two-column sample file listing sample name and tumor or control") |
| 337 |
("coverage,v", boost::program_options::value<int32_t>(&c.coverage)->default_value(10), "min. coverage in tumor") |
| 338 |
("controlcontamination,c", boost::program_options::value<float>(&c.controlcont)->default_value(0.0), "max. fractional ALT support in control") |
| 339 |
; |
| 340 |
|
| 341 |
|
| 342 |
boost::program_options::options_description germline("Germline options"); |
| 343 |
germline.add_options() |
| 344 |
("gq,q", boost::program_options::value<float>(&c.gq)->default_value(15), "min. median GQ for carriers and non-carriers") |
| 345 |
("rddel,e", boost::program_options::value<float>(&c.rddel)->default_value(0.8), "max. read-depth ratio of carrier vs. non-carrier for a deletion") |
| 346 |
("rddup,u", boost::program_options::value<float>(&c.rddup)->default_value(1.2), "min. read-depth ratio of carrier vs. non-carrier for a duplication") |
| 347 |
; |
| 348 |
|
| 349 |
|
| 350 |
boost::program_options::options_description hidden("Hidden options"); |
| 351 |
hidden.add_options() |
| 352 |
("input-file", boost::program_options::value<boost::filesystem::path>(&c.vcffile), "input file") |
| 353 |
; |
| 354 |
boost::program_options::positional_options_description pos_args; |
| 355 |
pos_args.add("input-file", -1); |
| 356 |
|
| 357 |
|
| 358 |
boost::program_options::options_description cmdline_options; |
| 359 |
cmdline_options.add(generic).add(somatic).add(germline).add(hidden); |
| 360 |
boost::program_options::options_description visible_options; |
| 361 |
visible_options.add(generic).add(somatic).add(germline); |
| 362 |
boost::program_options::variables_map vm; |
| 363 |
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(pos_args).run(), vm); |
| 364 |
boost::program_options::notify(vm); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
if ((vm.count("help")) || (!vm.count("input-file"))) { |
| 369 |
std::cerr << std::endl; |
| 370 |
std::cerr << "Usage: delly " << argv[0] << " [OPTIONS] <input.bcf>" << std::endl; |
| 371 |
std::cerr << visible_options << "\n"; |
| 372 |
return 0; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
if (vm.count("pass")) c.filterForPass = true; |
| 377 |
else c.filterForPass = false; |
| 378 |
|
| 379 |
|
| 380 |
if (vm.count("tag")) c.softFilter = true; |
| 381 |
else c.softFilter = false; |
| 382 |
|
| 383 |
|
| 384 |
if (c.filter == "germline") c.controlcont = 1.0; |
| 385 |
|
| 386 |
|
| 387 |
std::set<std::string> tSet; |
| 388 |
std::set<std::string> cSet; |
| 389 |
if (c.filter == "somatic") { |
| 390 |
c.hasSampleFile = true; |
| 391 |
if (!(boost::filesystem::exists(c.samplefile) && boost::filesystem::is_regular_file(c.samplefile) && boost::filesystem::file_size(c.samplefile))) { |
| 392 |
std::cerr << "Sample file is missing " << c.samplefile.string() << std::endl; |
| 393 |
return 1; |
| 394 |
} else { |
| 395 |
|
| 396 |
std::ifstream sampleFile(c.samplefile.string().c_str(), std::ifstream::in); |
| 397 |
if (sampleFile.is_open()) { |
| 398 |
while (sampleFile.good()) { |
| 399 |
std::string sampleFromFile; |
| 400 |
getline(sampleFile, sampleFromFile); |
| 401 |
typedef boost::tokenizer< boost::char_separator<char> > Tokenizer; |
| 402 |
boost::char_separator<char> sep(",\t "); |
| 403 |
Tokenizer tokens(sampleFromFile, sep); |
| 404 |
Tokenizer::iterator tokIter = tokens.begin(); |
| 405 |
if (tokIter != tokens.end()) { |
| 406 |
std::string sample = *tokIter++; |
| 407 |
if (tokIter != tokens.end()) { |
| 408 |
std::string type = *tokIter; |
| 409 |
if (type == "control") cSet.insert(sample); |
| 410 |
else if (type == "tumor") tSet.insert(sample); |
| 411 |
else { |
| 412 |
std::cerr << "Sample type for " << sample << " is neither tumor nor control" << std::endl; |
| 413 |
return 1; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
sampleFile.close(); |
| 419 |
} |
| 420 |
if (tSet.empty()) { |
| 421 |
std::cerr << "No tumor samples specified." << std::endl; |
| 422 |
return 1; |
| 423 |
} |
| 424 |
if (cSet.empty()) { |
| 425 |
std::cerr << "No control samples specified." << std::endl; |
| 426 |
return 1; |
| 427 |
} |
| 428 |
std::vector<std::string> intersection; |
| 429 |
std::set_intersection(cSet.begin(), cSet.end(), tSet.begin(), tSet.end(), std::back_inserter(intersection)); |
| 430 |
if (!intersection.empty()) { |
| 431 |
std::cerr << "Sample " << intersection[0] << " is both a tumor and control sample." << std::endl; |
| 432 |
return 1; |
| 433 |
} |
| 434 |
} |
| 435 |
} else c.hasSampleFile = false; |
| 436 |
|
| 437 |
|
| 438 |
if (vm.count("input-file")) { |
| 439 |
if (!(boost::filesystem::exists(c.vcffile) && boost::filesystem::is_regular_file(c.vcffile) && boost::filesystem::file_size(c.vcffile))) { |
| 440 |
std::cerr << "Input VCF/BCF file is missing: " << c.vcffile.string() << std::endl; |
| 441 |
return 1; |
| 442 |
} |
| 443 |
htsFile* ifile = bcf_open(c.vcffile.string().c_str(), "r"); |
| 444 |
if (ifile == NULL) { |
| 445 |
std::cerr << "Fail to open file " << c.vcffile.string() << std::endl; |
| 446 |
return 1; |
| 447 |
} |
| 448 |
hts_idx_t* bcfidx = NULL; |
| 449 |
tbx_t* tbx = NULL; |
| 450 |
if (hts_get_format(ifile)->format==vcf) tbx = tbx_index_load(c.vcffile.string().c_str()); |
| 451 |
else bcfidx = bcf_index_load(c.vcffile.string().c_str()); |
| 452 |
if ((bcfidx == NULL) && (tbx == NULL)) { |
| 453 |
std::cerr << "Fail to open index file for " << c.vcffile.string() << std::endl; |
| 454 |
return 1; |
| 455 |
} |
| 456 |
bcf_hdr_t* hdr = bcf_hdr_read(ifile); |
| 457 |
if (hdr == NULL) { |
| 458 |
std::cerr << "Fail to header for " << c.vcffile.string() << std::endl; |
| 459 |
return 1; |
| 460 |
} |
| 461 |
if (!(bcf_hdr_nsamples(hdr)>0)) { |
| 462 |
std::cerr << "BCF/VCF file has no sample genotypes!" << std::endl; |
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
if (c.filter == "somatic") { |
| 467 |
for (int i = 0; i < bcf_hdr_nsamples(hdr); ++i) { |
| 468 |
if (tSet.find(hdr->samples[i]) != tSet.end()) c.tumorSet.insert(hdr->samples[i]); |
| 469 |
else if (cSet.find(hdr->samples[i]) != cSet.end()) c.controlSet.insert(hdr->samples[i]); |
| 470 |
else std::cerr << "Warning: Sample " << hdr->samples[i] << " is missing in sample file." << std::endl; |
| 471 |
} |
| 472 |
if (c.tumorSet.empty()) { |
| 473 |
std::cerr << "No tumor samples specified." << std::endl; |
| 474 |
return 1; |
| 475 |
} |
| 476 |
if (c.controlSet.empty()) { |
| 477 |
std::cerr << "No control samples specified." << std::endl; |
| 478 |
return 1; |
| 479 |
} |
| 480 |
} |
| 481 |
bcf_hdr_destroy(hdr); |
| 482 |
if (bcfidx) hts_idx_destroy(bcfidx); |
| 483 |
if (tbx) tbx_destroy(tbx); |
| 484 |
bcf_close(ifile); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
if (!vm.count("outfile")) c.outfile = "-"; |
| 489 |
else { |
| 490 |
if (c.outfile.string() != "-") { |
| 491 |
if (!_outfileValid(c.outfile)) return 1; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
| 497 |
std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] "; |
| 498 |
std::cerr << "delly "; |
| 499 |
for(int i=0; i<argc; ++i) { std::cerr << argv[i] << ' '; } |
| 500 |
std::cerr << std::endl; |
| 501 |
|
| 502 |
return filterRun(c); |
| 503 |
} |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
#endif |