1 /* The MIT License
2 
3    Copyright (c) 2015 Adrian Tan <atks@umich.edu>
4 
5    Permission is hereby granted, free of charge, to any person obtaining a copy
6    of this software and associated documentation files (the "Software"), to deal
7    in the Software without restriction, including without limitation the rights
8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9    copies of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21    THE SOFTWARE.
22 */
23 
24 #include "consolidate_vntrs.h"
25 
26 namespace
27 {
28 
29 class Igor : Program
30 {
31     public:
32 
33     ///////////
34     //options//
35     ///////////
36     std::string input_vcf_file;
37     std::string output_vcf_file;
38     std::string ref_fasta_file;
39     std::vector<GenomeInterval> intervals;
40     bool debug;
41 
42     /////////
43     //tools//
44     /////////
45     VariantManip *vm;
46     VNTRConsolidator *vc;
47 
Igor(int argc,char ** argv)48     Igor(int argc, char **argv)
49     {
50         version = "0.57";
51 
52         //////////////////////////
53         //options initialization//
54         //////////////////////////
55         try
56         {
57             std::string desc = "Consolidates VNTRs by\n"
58                  "              1. removing overlapping VNTRs and leaving behind the most complete VNTR\n"
59                  "              2. resolves adjacent VNTRs\n"
60                  "              3. marking VNTRs that are \n"
61                  "              4. Adds INFO fields indicating the number of variants that overlap with this variant";
62 
63             TCLAP::CmdLine cmd(desc, ' ', version);
64             VTOutput my;
65             cmd.setOutput(&my);
66             TCLAP::ValueArg<std::string> arg_intervals("i", "i", "intervals []", false, "", "str", cmd);
67             TCLAP::ValueArg<std::string> arg_interval_list("I", "I", "file containing list of intervals []", false, "", "file", cmd);
68             TCLAP::ValueArg<std::string> arg_ref_fasta_file("r", "r", "reference sequence fasta file []", true, "", "str", cmd);
69             TCLAP::ValueArg<std::string> arg_output_vcf_file("o", "o", "output VCF file [-]", false, "-", "str", cmd);
70             TCLAP::SwitchArg arg_debug("d", "d", "debug [false]", cmd, false);
71             TCLAP::UnlabeledValueArg<std::string> arg_input_vcf_file("<in.vcf>", "input VCF file", true, "","file", cmd);
72 
73             cmd.parse(argc, argv);
74 
75             input_vcf_file = arg_input_vcf_file.getValue();
76             output_vcf_file = arg_output_vcf_file.getValue();
77             ref_fasta_file = arg_ref_fasta_file.getValue();
78             debug = arg_debug.getValue();
79             parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
80         }
81         catch (TCLAP::ArgException &e)
82         {
83             std::cerr << "error: " << e.error() << " for arg " << e.argId() << "\n";
84             abort();
85         }
86     };
87 
initialize()88     void initialize()
89     {
90         ////////////////////////
91         //tools initialization//
92         ////////////////////////
93         vc = new VNTRConsolidator(input_vcf_file, intervals, output_vcf_file, ref_fasta_file, debug);
94         vm = new VariantManip();
95     }
96 
consolidate_vntrs()97     void consolidate_vntrs()
98     {
99         bcf1_t *v = vc->odw->get_bcf1_from_pool();
100         bcf_hdr_t *h = vc->odr->hdr;
101 
102         Variant* variant;
103         while (vc->odr->read(v))
104         {
105             bcf_print_liten(h,v);
106 
107             variant = new Variant(vc->odw->hdr, v);
108             vc->flush_variant_buffer(variant);
109             vc->insert_variant_record_into_buffer(variant);
110             v = vc->odw->get_bcf1_from_pool();
111 
112             ++vc->no_total_variants;
113         }
114 
115         vc->flush_variant_buffer();
116         vc->close();
117     };
118 
print_options()119     void print_options()
120     {
121         std::clog << "consolidate_vntrs v" << version << "\n\n";
122 
123         std::clog << "options:     input VCF file        " << input_vcf_file << "\n";
124         std::clog << "         [o] output VCF file       " << output_vcf_file << "\n";
125         print_int_op("         [i] intervals              ", intervals);
126         std::clog << "\n";
127     }
128 
print_stats()129     void print_stats()
130     {
131         std::clog << "\n";
132         std::clog << "       Variants observed\n";
133         std::clog << "       No. of SNPs                 " << vc->no_snps << "\n";
134         std::clog << "       No. of Indels               " << vc->no_indels << "\n";
135         std::clog << "       No. of VNTRs                " << vc->no_vntrs << "\n";
136         std::clog << "       No. of other variant types  " << vc->no_other_variants << "\n";
137         std::clog << "\n";
138         std::clog << "       No. of isolated VNTRs           " << vc->no_isolated_vntrs << "\n";
139         std::clog << "       No. of clustered VNTRs\n";
140         std::clog << "            consistent RUs             " << vc->no_clustered_consistent_ru_vntrs << " (" << vc->no_merged_consistent_ru_vntrs << ")\n";
141         std::clog << "            consistent bases           " << vc->no_clustered_consistent_basis_vntrs << " (" << vc->no_merged_consistent_basis_vntrs << ")\n";
142         std::clog << "            inconsistent RUs and bases " << vc->no_clustered_inconsistent_ru_basis_vntrs << "\n";
143 
144         std::clog << "\n";
145     };
146 
~Igor()147     ~Igor() {};
148 
149     private:
150 };
151 
152 }
153 
consolidate_vntrs(int argc,char ** argv)154 void consolidate_vntrs(int argc, char ** argv)
155 {
156     Igor igor(argc, argv);
157     igor.print_options();
158     igor.initialize();
159     igor.consolidate_vntrs();
160     igor.print_stats();
161 };
162