forked from twolinin/longphase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHaplotag.cpp
186 lines (163 loc) · 6.22 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
#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"
" -q, --qualityThreshold=Num not tag alignment if the mapping quality less than threshold. default:0\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"
" --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, LOG};
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 },
{ "out-prefix", required_argument, NULL, 'o' },
{ "threads", required_argument, NULL, 't' },
{ "qualityThreshold", required_argument, NULL, 'q' },
{ "percentageThreshold", required_argument, NULL, 'p' },
{ "log", no_argument, NULL, LOG },
{ NULL, 0, NULL, 0 }
};
namespace opt
{
static int numThreads = 1;
static int qualityThreshold = 20;
static double percentageThreshold = 0.6;
static std::string snpFile="";
static std::string svFile="";
static std::string bamFile="";
static std::string fastaFile="";
static std::string resultPrefix="result";
static bool tagSupplementary = false;
static bool writeReadLog = false;
}
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 TAG_SUP: opt::tagSupplementary = true; break;
case LOG: opt::writeReadLog = true; break;
case OPT_HELP:
std::cout << CORRECT_USAGE_MESSAGE;
exit(EXIT_SUCCESS);
}
}
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::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)
{
HaplotagParameters ecParams;
// set parameters
HaplotagOptions(argc, argv);
ecParams.numThreads=opt::numThreads;
ecParams.qualityThreshold=opt::qualityThreshold;
ecParams.snpFile=opt::snpFile;
ecParams.svFile=opt::svFile;
ecParams.bamFile=opt::bamFile;
ecParams.fastaFile=opt::fastaFile;
ecParams.resultPrefix=opt::resultPrefix;
ecParams.tagSupplementary=opt::tagSupplementary;
ecParams.percentageThreshold=opt::percentageThreshold;
ecParams.writeReadLog=opt::writeReadLog;
HaplotagProcess processor(ecParams);
return 0;
}