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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/* The MIT License
Copyright (c) 2014 Adrian Tan <atks@umich.edu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "annotate_vntrs.h"
namespace
{
class Igor : Program
{
public:
std::string version;
///////////
//options//
///////////
std::string input_vcf_file;
std::string ref_fasta_file;
std::string output_vcf_file;
std::vector<GenomeInterval> intervals;
std::string interval_list;
bool debug;
std::string method; //methods of detection
std::string vntr_annotation_mode; //modes of annotation
int32_t vntr_classification; //vntr classification schemas
bool override_tag;
bool add_vntr_record;
bool add_flank_annotation; //add flank annotation
//motif related
std::string END;
std::string MOTIF;
std::string MLEN;
std::string RU;
std::string BASIS;
std::string BLEN;
//exact alignment related statistics
std::string REPEAT_TRACT;
std::string COMP;
std::string ENTROPY;
std::string ENTROPY2;
std::string RL;
std::string RU_COUNTS;
std::string SCORE;
std::string TRF_SCORE;
std::string FLANKSEQ;
//////////
//filter//
//////////
std::string fexp;
Filter filter;
bool filter_exists;
///////
//i/o//
///////
BCFOrderedReader *odr;
BCFOrderedWriter *odw;
/////////
//stats//
/////////
int32_t no_vntrs_annotated;
////////////////
//common tools//
////////////////
VariantManip* vm;
VNTRAnnotator* va;
ReferenceSequence* rs;
Igor(int argc, char **argv)
{
version = "0.5";
//////////////////////////
//options initialization//
//////////////////////////
try
{
std::string desc = "annotates indels with VNTR information and adds a VNTR record.";
TCLAP::CmdLine cmd(desc, ' ', version);
VTOutput my; cmd.setOutput(&my);
TCLAP::ValueArg<std::string> arg_intervals("i", "i", "intervals", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_interval_list("I", "I", "file containing list of intervals []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_output_vcf_file("o", "o", "output VCF file [-]", false, "-", "str", cmd);
TCLAP::ValueArg<std::string> arg_ref_fasta_file("r", "r", "reference sequence fasta file []", true, "", "str", cmd);
TCLAP::SwitchArg arg_debug("d", "d", "debug [false]", cmd, false);
TCLAP::UnlabeledValueArg<std::string> arg_input_vcf_file("<in.vcf>", "input VCF file", true, "","file", cmd);
TCLAP::ValueArg<std::string> arg_fexp("f", "f", "filter expression []", false, "", "str", cmd);
TCLAP::SwitchArg arg_override_tag("x", "x", "override tags [false]", cmd, false);
cmd.parse(argc, argv);
input_vcf_file = arg_input_vcf_file.getValue();
output_vcf_file = arg_output_vcf_file.getValue();
parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
fexp = arg_fexp.getValue();
debug = arg_debug.getValue();
ref_fasta_file = arg_ref_fasta_file.getValue();
}
catch (TCLAP::ArgException &e)
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
abort();
}
};
~Igor()
{
};
void initialize()
{
/////////////////////////
//filter initialization//
/////////////////////////
filter.parse(fexp.c_str(), false);
filter_exists = fexp=="" ? false : true;
//////////////////////
//i/o initialization//
//////////////////////
odr = new BCFOrderedReader(input_vcf_file, intervals);
odw = new BCFOrderedWriter(output_vcf_file, 10000);
odw->link_hdr(odr->hdr);
//////////////////////////////
//INFO header adding for VCF//
//////////////////////////////
bool rename = false;
//motif related
END = bcf_hdr_append_info_with_backup_naming(odw->hdr, "END", "1", "Integer", "End position of the variant.", rename);
MOTIF = bcf_hdr_append_info_with_backup_naming(odw->hdr, "MOTIF", "1", "String", "Canonical motif in a VNTR.", rename);
RU = bcf_hdr_append_info_with_backup_naming(odw->hdr, "RU", "1", "String", "Repeat unit in the reference sequence.", rename);
BASIS = bcf_hdr_append_info_with_backup_naming(odw->hdr, "BASIS", "1", "String", "Basis nucleotides in the motif.", rename);
MLEN = bcf_hdr_append_info_with_backup_naming(odw->hdr, "MLEN", "1", "Integer", "Motif length.", rename);
BLEN = bcf_hdr_append_info_with_backup_naming(odw->hdr, "BLEN", "1", "Integer", "Basis length.", rename);
//exact alignment related statisitcs
REPEAT_TRACT = bcf_hdr_append_info_with_backup_naming(odw->hdr, "REPEAT_TRACT", "2", "Integer", "Boundary of the repeat tract detected by exact alignment.", rename);
COMP = bcf_hdr_append_info_with_backup_naming(odw->hdr, "COMP", "4", "Integer", "Composition(%) of bases in an exact repeat tract.", rename);
ENTROPY = bcf_hdr_append_info_with_backup_naming(odw->hdr, "ENTROPY", "1", "Float", "Entropy measure of an exact repeat tract (0-2).", rename);
ENTROPY2 = bcf_hdr_append_info_with_backup_naming(odw->hdr, "ENTROPY2", "1", "Float", "Dinucloetide Entropy measure of an exact repeat tract (0-2).", rename);
RL = bcf_hdr_append_info_with_backup_naming(odw->hdr, "RL", "1", "Integer", "Reference exact repeat tract length in bases.", rename);
RU_COUNTS = bcf_hdr_append_info_with_backup_naming(odw->hdr, "RU_COUNTS", "2", "Integer", "Number of exact repeat units and total number of repeat units in exact repeat tract.", rename);
SCORE = bcf_hdr_append_info_with_backup_naming(odw->hdr, "SCORE", "1", "Float", "Score of repeat unit in exact repeat tract.", rename);
TRF_SCORE = bcf_hdr_append_info_with_backup_naming(odw->hdr, "TRF_SCORE", "1", "Integer", "TRF Score for M/I/D as 2/-7/-7 in exact repeat tract.", rename);
FLANKSEQ = bcf_hdr_append_info_with_backup_naming(odw->hdr, "FLANKSEQ", "1", "String", "Flanking sequence 10bp on either side of REF.", rename);
////////////////////////
//stats initialization//
////////////////////////
no_vntrs_annotated = 0;
////////////////////////
//tools initialization//
////////////////////////
vm = new VariantManip(ref_fasta_file);
va = new VNTRAnnotator(ref_fasta_file, debug);
rs = new ReferenceSequence(ref_fasta_file);
}
void print_options()
{
std::clog << "annotate_vntrs v" << version << "\n";
std::clog << "\n";
std::clog << "options: input VCF file(s) " << input_vcf_file << "\n";
std::clog << " [o] output VCF file " << output_vcf_file << "\n";
std::clog << " [v] add VNTR records " << (add_vntr_record ? "true" : "false") << "\n";
std::clog << " [k] add_flank_annotation " << (add_flank_annotation ? "true" : "false") << "\n";
print_boo_op(" [d] debug ", debug);
print_ref_op(" [r] ref FASTA file ", ref_fasta_file);
print_boo_op(" [x] override tag ", override_tag);
print_int_op(" [i] intervals ", intervals);
std::clog << "\n";
}
void print_stats()
{
std::clog << "\n";
std::cerr << "stats: no. of VNTRs annotated " << no_vntrs_annotated << "\n";
std::clog << "\n";
}
/**
* Updates the FLANKSEQ INFO field.
*/
void update_flankseq(bcf_hdr_t* h, bcf1_t *v, const char* chrom, int32_t lflank_beg1, int32_t lflank_end1, int32_t rflank_beg1, int32_t rflank_end1)
{
std::string flanks;
char* seq = rs->fetch_seq(chrom, lflank_beg1, lflank_end1);
flanks.assign(seq);
if (seq) free(seq);
flanks.append(1, '[');
seq = rs->fetch_seq(chrom, lflank_end1+1, rflank_beg1-1);
flanks.append(seq);
if (seq) free(seq);
flanks.append(1, ']');
seq = rs->fetch_seq(chrom, rflank_beg1, rflank_end1);
flanks.append(seq);
if (seq) free(seq);
bcf_update_info_string(h, v, "FLANKSEQ", flanks.c_str());
}
void annotate_vntrs()
{
odw->write_hdr();
bcf1_t *v = odw->get_bcf1_from_pool();
bcf_hdr_t *h = odw->hdr;
Variant variant;
while (odr->read(v))
{
int32_t vtype = vm->classify_variant(odr->hdr, v, variant);
if (filter_exists)
{
if (!filter.apply(h, v, &variant, false))
{
continue;
}
}
//variants with N crashes the alignment models!!!!!! :(
if (vm->contains_N(v))
{
std::string var = variant.get_variant_string();
fprintf(stderr, "[%s:%d %s] Variant contains N bases, skipping annotation: %s\n", __FILE__, __LINE__, __FUNCTION__, var.c_str());
odw->write(v);
v = odw->get_bcf1_from_pool();
continue;
}
if (debug)
{
bcf_print_liten(h,v);
}
if (vtype==VT_VNTR)
{
VNTR& vntr = variant.vntr;
int32_t beg1 = bcf_get_pos1(v);
int32_t end1 = bcf_get_end1(v);
va->annotate(variant, FINAL);
//assumes MOTIF is present
bcf_update_info_int32(h, v, MLEN.c_str(), &vntr.mlen, 1);
bcf_update_info_int32(h, v, END.c_str(), &variant.end1, 1);
bcf_update_info_string(h, v, RU.c_str(), vntr.ru.c_str());
bcf_update_info_string(h, v, BASIS.c_str(), vntr.basis.c_str());
bcf_update_info_int32(h, v, BLEN.c_str(), &vntr.blen, 1);
int32_t repeat_tract[2] = {beg1, end1};
bcf_update_info_int32(h, v, REPEAT_TRACT.c_str(), &repeat_tract, 2);
bcf_update_info_int32(h, v, COMP.c_str(), &vntr.comp[0], 4);
bcf_update_info_float(h, v, ENTROPY.c_str(), &vntr.entropy, 1);
bcf_update_info_float(h, v, ENTROPY2.c_str(), &vntr.entropy2, 1);
bcf_update_info_int32(h, v, RL.c_str(), &vntr.rl, 1);
int32_t ru_count[2] = {vntr.no_perfect_ru, vntr.no_ru};
bcf_update_info_int32(h, v, RU_COUNTS.c_str(), &ru_count, 2);
bcf_update_info_float(h, v, SCORE.c_str(), &vntr.score, 1);
bcf_update_info_int32(h, v, TRF_SCORE.c_str(), &vntr.trf_score, 1);
update_flankseq(h, v, variant.chrom.c_str(),
variant.beg1-10, variant.beg1-1,
variant.end1+1, variant.end1+10);
++no_vntrs_annotated;
}
odw->write(v);
v = odw->get_bcf1_from_pool();
}
odw->close();
odr->close();
};
private:
};
}
void annotate_vntrs(int argc, char ** argv)
{
Igor igor(argc, argv);
igor.print_options();
igor.initialize();
igor.annotate_vntrs();
igor.print_stats();
};
|