-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevo_getInformativeReadsFromSam.cpp
194 lines (158 loc) · 6.7 KB
/
evo_getInformativeReadsFromSam.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
//
// evo_getInformativeReadsFromSam.cpp
// process_vcf
//
// Created by Milan Malinsky on 05.04.22.
// Copyright © 2022 Milan Malinsky. All rights reserved.
//
// Sam flags:
// 65 - Paired, first in pair, + strand
// 81 - Paired, first in pair, - strand
// 97 - Paired, first in pair, + strand
// 113 - Paired, first in pair, - strand
// 129 - Paired, second in pair, + strand
// 145 - Paired, second in pair, - strand
// 161 - Paired, second in pair, + strand
// 177 - Paired, second in pair, - strand
// >2000 - secondary alignment
// 73 - Paired, mate unmapped, first in pair, + strand
// 137 - Paired, mate unmapped, second in pair, + strand
// 185 - Paired, mate unmapped, mate - strand, second in pair, - strand
// 121 - Paired, mate unmapped, mate - strand, first in pair, - strand
#include "evo_getInformativeReadsFromSam.h"
#include <unordered_set>
#define SUBPROGRAM "InfoReadsSam"
#define DEBUG 1
static const char *INFOREADS_USAGE_MESSAGE =
"Usage: " PROGRAM_BIN " " SUBPROGRAM " [OPTIONS] hetPosFile.txt\n"
"Select reads from SAMTOOLS_FILE which could be informative about recombination\n"
"Expects sam input on STDIN\n"
"\n"
" -h, --help display this help and exit\n"
" -n, --run-name run-name will be included in the output file name\n"
" -m, --min-MQ (default: 20) the minimum mapping quality for a read to be considered\n"
" --hapCut the het positions come from HapCut output"
"\n"
"\nReport bugs to " PACKAGE_BUGREPORT "\n\n";
static const char* shortopts = "hn:m:";
enum { OPT_HAPCUT };
static const struct option longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "min-MQ", required_argument, NULL, 'm' },
{ "run-name", required_argument, NULL, 'n' },
{ "hapCut", no_argument, NULL, OPT_HAPCUT },
{ NULL, 0, NULL, 0 }
};
namespace opt
{
static string hetFile;
static bool hapcutFormat = false;
static string runName = "";
static int minMQ = 20;
}
int InfoReadsMain(int argc, char** argv) {
parseInfoReadsOptions(argc, argv);
string line; // for reading the input files
std::istream* hetFile = createReader(opt::hetFile.c_str());
std::istream* samtoolsFile = &std::cin;
std::unordered_set<int> phasedHetsPos;
std::map<int,PhaseInfo*> positionToPhase;
std::map<string,std::vector<string> > readNameToSamRecords;
if (opt::hapcutFormat) {
// Parse the Hapcut blocks file
while (getline(*hetFile, line)) {
if (line[0] == '*') {
} else if (line[0] == 'B' && line[1] == 'L') { // New block - should in the future separate the hets by blocks
} else {
std::vector<string> phasedSNPdetails = split(line, '\t');
int snpPos = atoi(phasedSNPdetails[4].c_str());
int H1phase = atoi(phasedSNPdetails[1].c_str());
int H2phase = atoi(phasedSNPdetails[2].c_str());
//std::cout << "line: " << line << std::endl;
// std::cout << "phasedSNPdetails[5]: " << phasedSNPdetails[5] << std::endl;
assert(phasedSNPdetails[5].length() == 1); assert(phasedSNPdetails[6].length() == 1);
char refBase = phasedSNPdetails[5][0];
char altBase = phasedSNPdetails[6][0];
double phaseQual = stringToDouble(phasedSNPdetails[10].c_str());
int snpCoverage = atoi(phasedSNPdetails[11].c_str());
std::vector<char> phasedVars;
if (H1phase == 0 && H2phase == 1) {
phasedVars.push_back(refBase); phasedVars.push_back(altBase);
} else if (H1phase == 1 && H2phase == 0) {
phasedVars.push_back(altBase); phasedVars.push_back(refBase);
} else{
continue;
}
PhaseInfo* thisPhase = new PhaseInfo(snpPos,phaseQual,snpCoverage, phasedVars,1);
positionToPhase[snpPos] = thisPhase;
}
}
} else {
while (getline(*hetFile, line)) {
std::vector<string> phasedSNPdetails = split(line, '\t');
int snpPos = atoi(phasedSNPdetails[1].c_str());
phasedHetsPos.insert(snpPos);
}
}
std::cerr << "Finished reading het sites. There are " << positionToPhase.size() << " hets." << std::endl;
std::cerr << "Processing reads:" << std::endl;
int readsProcessed = 0;
// Now parse the pairtools file to find read pairs that can be informative about the phasing and recombination
while (getline(*samtoolsFile,line)) {
readsProcessed++;
if (readsProcessed % 100000 == 0) {
std::cerr << "Processed " << readsProcessed << " reads" << std::endl;
}
// std::cerr << line << std::endl;
std::vector<string> samRecVec = split(line, '\t'); //assert(pairVec.size() == 8);
int flag = atoi(samRecVec[1].c_str());
if (flag > 2000) continue;
int MQ = atoi(samRecVec[4].c_str());
if (MQ < opt::minMQ) continue;
RecombRead* thisRead = new RecombRead(samRecVec);
thisRead->findHetsInRead(positionToPhase);
if (thisRead->hetSites.size() > 0) {
readNameToSamRecords[thisRead->readName].push_back(line);
}
delete thisRead;
}
for (std::map<string,std::vector<string>>::iterator it = readNameToSamRecords.begin(); it != readNameToSamRecords.end(); it++) {
if (it->second.size() > 1) {
std::cout << it->second[0] << std::endl;
std::cout << it->second[1] << std::endl;
}
}
return 0;
}
void parseInfoReadsOptions(int argc, char** argv) {
bool die = false;
for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;)
{
std::istringstream arg(optarg != NULL ? optarg : "");
switch (c)
{
case '?': die = true; break;
case 'n': arg >> opt::runName; break;
case OPT_HAPCUT: opt::hapcutFormat = true; break;
case 'm': arg >> opt::minMQ; break;
case 'h':
std::cout << INFOREADS_USAGE_MESSAGE;
exit(EXIT_SUCCESS);
}
}
if (argc - optind < 1) {
std::cerr << "missing arguments\n";
die = true;
}
else if (argc - optind > 1)
{
std::cerr << "too many arguments\n";
die = true;
}
if (die) {
std::cout << "\n" << INFOREADS_USAGE_MESSAGE;
exit(EXIT_FAILURE);
}
// Parse the input filenames
opt::hetFile = argv[optind++];
}