-
Notifications
You must be signed in to change notification settings - Fork 9
/
Haplotag.cpp
223 lines (198 loc) · 8.09 KB
/
Haplotag.cpp
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
#include "Haplotag.h"
#include "HaplotagProcess.h"
#include "Util.h"
#include <getopt.h>
#define SUBPROGRAM "haplotag"
static const char *CORRECT_USAGE_MESSAGE =
"Usage: " " " SUBPROGRAM " [OPTION] ... READSFILE\n"
" --help display this help and exit.\n\n"
"require arguments:\n"
" -s, --snp-file=NAME input SNP vcf file.\n"
" -b, --bam-file=NAME input bam file.\n"
" -r, --reference=NAME reference fasta.\n"
"optional arguments:\n"
" --tagSupplementary tag supplementary alignment. default:false\n"
" --sv-file=NAME input phased SV vcf file.\n"
" --mod-file=NAME input a modified VCF file (produced by longphase modcall and processed by longphase phase).\n"
" -q, --qualityThreshold=Num not tag alignment if the mapping quality less than threshold. default:1\n"
" -p, --percentageThreshold=Num the alignment will be tagged according to the haplotype corresponding to most alleles.\n"
" if the alignment has no obvious corresponding haplotype, it will not be tagged. default:0.6\n"
" -t, --threads=Num number of thread. default:1\n"
" -o, --out-prefix=NAME prefix of phasing result. default:result\n"
" --cram the output file will be in the cram format. default:bam\n"
" --region=REGION tagging include only reads/variants overlapping those regions. default:\"\"(all regions)\n"
" input format:chrom (consider entire chromosome)\n"
" chrom:start (consider region from this start to end of chromosome)\n"
" chrom:start-end\n"
" --log an additional log file records the result of each read. default:false\n";
static const char* shortopts = "s:b:o:t:q:p:r:";
enum { OPT_HELP = 1, TAG_SUP, SV_FILE, REGION, LOG, MOD_FILE, CRAM};
static const struct option longopts[] = {
{ "help", no_argument, NULL, OPT_HELP },
{ "snp-file", required_argument, NULL, 's' },
{ "bam-file", required_argument, NULL, 'b' },
{ "reference", required_argument, NULL, 'r' },
{ "tagSupplementary", no_argument, NULL, TAG_SUP },
{ "sv-file", required_argument, NULL, SV_FILE },
{ "mod-file", required_argument, NULL, MOD_FILE },
{ "out-prefix", required_argument, NULL, 'o' },
{ "cram", no_argument, NULL, CRAM },
{ "threads", required_argument, NULL, 't' },
{ "qualityThreshold", required_argument, NULL, 'q' },
{ "percentageThreshold", required_argument, NULL, 'p' },
{ "region", required_argument, NULL, REGION },
{ "log", no_argument, NULL, LOG },
{ NULL, 0, NULL, 0 }
};
namespace opt
{
static int numThreads = 1;
static int qualityThreshold = 1;
static double percentageThreshold = 0.6;
static std::string snpFile="";
static std::string svFile="";
static std::string modFile="";
static std::string bamFile="";
static std::string fastaFile="";
static std::string resultPrefix="result";
static std::string region="";
static std::string outputFormat="bam";
static bool tagSupplementary = false;
static bool writeReadLog = false;
static std::string command="longphase ";
}
void HaplotagOptions(int argc, char** argv)
{
optind=1; //reset getopt
bool die = false;
for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;)
{
std::istringstream arg(optarg != NULL ? optarg : "");
switch (c)
{
case 's': arg >> opt::snpFile; break;
case 't': arg >> opt::numThreads; break;
case 'b': arg >> opt::bamFile; break;
case 'r': arg >> opt::fastaFile; break;
case 'o': arg >> opt::resultPrefix; break;
case 'q': arg >> opt::qualityThreshold; break;
case 'p': arg >> opt::percentageThreshold; break;
case SV_FILE: arg >> opt::svFile; break;
case MOD_FILE: arg >> opt::modFile; break;
case REGION: arg >> opt::region; break;
case TAG_SUP: opt::tagSupplementary = true; break;
case CRAM: opt::outputFormat = "cram"; break;
case LOG: opt::writeReadLog = true; break;
case OPT_HELP:
std::cout << CORRECT_USAGE_MESSAGE;
exit(EXIT_SUCCESS);
default: die = true; break;
}
}
for(int i = 0; i < argc; ++i){
opt::command.append(argv[i]);
opt::command.append(" ");
}
if (argc - optind < 0 )
{
std::cerr << SUBPROGRAM ": missing arguments\n";
die = true;
}
if( opt::snpFile != "")
{
std::ifstream openFile( opt::snpFile.c_str() );
if( !openFile.is_open() )
{
std::cerr<< "File " << opt::snpFile << " not exist.\n\n";
die = true;
}
}
else{
std::cerr << SUBPROGRAM ": missing SNP file.\n";
die = true;
}
if( opt::svFile != "")
{
std::ifstream openFile( opt::svFile.c_str() );
if( !openFile.is_open() )
{
std::cerr<< "File " << opt::svFile << " not exist.\n\n";
die = true;
}
}
if( opt::modFile != "")
{
std::ifstream openFile( opt::modFile.c_str() );
if( !openFile.is_open() )
{
std::cerr<< "File " << opt::modFile << " not exist.\n\n";
die = true;
}
}
if( opt::bamFile != "")
{
std::ifstream openFile( opt::bamFile.c_str() );
if( !openFile.is_open() )
{
std::cerr<< "File " << opt::bamFile << " not exist.\n\n";
die = true;
}
}
else{
std::cerr << SUBPROGRAM ": missing bam file.\n";
die = true;
}
if( opt::fastaFile != "")
{
std::ifstream openFile( opt::snpFile.c_str() );
if( !openFile.is_open() )
{
std::cerr<< "File " << opt::fastaFile << " not exist.\n\n";
die = true;
}
}
else{
std::cerr << SUBPROGRAM ": missing reference.\n";
die = true;
}
if ( opt::numThreads < 1 ){
std::cerr << SUBPROGRAM " invalid threads. value: "
<< opt::numThreads
<< "\nplease check -t, --threads=Num\n";
die = true;
}
if ( opt::percentageThreshold > 1 || opt::percentageThreshold < 0 ){
std::cerr << SUBPROGRAM " invalid percentage threshold. value: "
<< opt::percentageThreshold
<< "\nthis value need: 0~1, please check -p, --percentageThreshold=Num\n";
die = true;
}
if (die)
{
std::cerr << "\n" << CORRECT_USAGE_MESSAGE;
exit(EXIT_FAILURE);
}
}
int HaplotagMain(int argc, char** argv, std::string in_version)
{
HaplotagParameters ecParams;
// set parameters
HaplotagOptions(argc, argv);
ecParams.numThreads=opt::numThreads;
ecParams.qualityThreshold=opt::qualityThreshold;
ecParams.snpFile=opt::snpFile;
ecParams.svFile=opt::svFile;
ecParams.modFile=opt::modFile;
ecParams.bamFile=opt::bamFile;
ecParams.fastaFile=opt::fastaFile;
ecParams.resultPrefix=opt::resultPrefix;
ecParams.tagSupplementary=opt::tagSupplementary;
ecParams.percentageThreshold=opt::percentageThreshold;
ecParams.region=opt::region;
ecParams.writeReadLog=opt::writeReadLog;
ecParams.version=in_version;
ecParams.command=opt::command;
ecParams.outputFormat=opt::outputFormat;
HaplotagProcess processor(ecParams);
return 0;
}