-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathevo_diversity_subsampling.cpp
More file actions
194 lines (172 loc) · 7.98 KB
/
Copy pathevo_diversity_subsampling.cpp
File metadata and controls
194 lines (172 loc) · 7.98 KB
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_diversity_subsampling.cpp
// process_vcf
//
// Created by Milan Malinsky on 13/03/2017.
// Copyright © 2017 Milan Malinsky. All rights reserved.
//
#include "evo_diversity_subsampling.h"
#include <random>
#define SUBPROGRAM "RegionsSubsampledDxy"
#define DEBUG 1
static const char *SUBSAMPLED_DXY_USAGE_MESSAGE =
"Usage: " PROGRAM_BIN " " SUBPROGRAM " [OPTIONS] regions.bed variants.vcf\n"
"Calculate Dxy (and possibly other statistics) for regions defined in the .bed file\n"
"Subsample the regions from the .bed file into random sets of coordinates of a given length\n"
"\n"
" -h, --help display this help and exit\n"
" -s, --subsample=LENGTH (default = 100) length in bp of randomly selected bases to subsample\n"
" -e, --elements the bed file defines elements linked by a common name in the fourth column\n"
" --outFolder=FOLDER put the output in FOLDER (default .)\n"
"\n\n"
"\nReport bugs to " PACKAGE_BUGREPORT "\n\n";
static const char* shortopts = "hs:e";
enum { OPT_FOLDER };
static const struct option longopts[] = {
{ "subsample", required_argument, NULL, 's' },
{ "elements", no_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ "outFolder", required_argument, NULL, OPT_FOLDER },
{ NULL, 0, NULL, 0 }
};
namespace opt
{
static int subsampleLength = 100;
static string bedFile = "";
static string vcfFile = "";
static string outFolder = "";
static bool bElements = false;
}
int subsamplingDxy(int argc, char** argv) {
parseSubsamplingDxyOptions(argc, argv);
// Load up the intervals file
std::ifstream* bedFile = new std::ifstream(opt::bedFile.c_str());
std::map<int, string> linearToGenomeMap;
std::cerr << "Loading coordinates from the bed file " << opt::bedFile << std::endl;
LinkedCoordsBed coords;
if (!opt::bElements) {
SimpleCoordsBed simpleCoords(bedFile, linearToGenomeMap);
} else {
coords = LinkedCoordsBed(bedFile);
}
std::cerr << "Done" << std::endl;
// Load up the VCF file:
std::istream* vcfFile = createReader(opt::vcfFile.c_str());
string line; std::unordered_map<string, Counts> vcfCountsMap;
std::unordered_map<string, double> vcfDxyMap;
std::unordered_map<string, double> vcfInbreedingPvalMap;
std::cerr << "Loading variants from the VCF file " << opt::vcfFile << std::endl;
double totalDxySum = 0;
while (getline(*vcfFile, line)) {
if (line[0] == '#' && line[1] == '#') {
} else if (line[0] == '#' && line[1] == 'C') {
} else {
// totalVariantNumber++;
//std::cerr << "Variant N:" << totalVariantNumber << std::endl;
std::vector<std::string> fields = split(line, '\t');
string scaffold = fields[0]; string pos = fields[1];
Counts* counts = new Counts();
getThisVariantCountsSimple(fields, counts);
counts->inbreedingCoefficient = calculateInbreedingCoefficient(counts->individualsWithVariant);
counts->chiSqPvalForInbreeding = calculateChiSqPvalForInbreeding(counts->individualsWithVariant);
if (counts->inbreedingCoefficient < 0 && counts->chiSqPvalForInbreeding < 0.05) {
std::cerr << "filtering out:" << std::endl;
std::cerr << scaffold+"\t"+pos << std::endl;
std::cerr << "counts.chiSqPvalForInbreeding: " << counts->chiSqPvalForInbreeding << std::endl;
std::cerr << "counts.inbreedingCoefficient: " << counts->inbreedingCoefficient << std::endl;
std::cerr << std::endl;
} else {
vcfCountsMap[scaffold+"\t"+pos] = *counts;
double thisVariantDxy = calculateOverallDxy(*counts);
totalDxySum = totalDxySum + thisVariantDxy;
vcfDxyMap[scaffold+"\t"+pos] = thisVariantDxy;
}
delete counts;
}
}
std::cerr << "Done" << std::endl;
if (!opt::bElements) {
int totalIntervalsLength = (int)linearToGenomeMap.size();
std::cout << "Average Dxy = " << totalDxySum/totalIntervalsLength << std::endl;
// Prepare the output file for subsamples of Dxy values
size_t suffixPos = opt::bedFile.find_last_of('.');
string outDxyFileName;
if (suffixPos != std::string::npos) {
outDxyFileName = opt::outFolder + stripExtension(opt::bedFile) + "_DxyVals_l" + numToString(opt::subsampleLength) + ".txt";
} else {
outDxyFileName = opt::outFolder + opt::bedFile + "_DxyVals_l" + numToString(opt::subsampleLength) + ".txt";
}
//std::cerr << "into: " << AAFileName << std::endl;
std::ofstream* outDxyFile = new std::ofstream(outDxyFileName.c_str());
// Subsampling:
int numSubsamplesToDo = totalIntervalsLength/opt::subsampleLength;
std::mt19937_64 rng; // random number generator
rng.seed();
std::uniform_int_distribution<int> distribution(0,totalIntervalsLength);
std::cerr << "Now running subsamples to be written into: " << outDxyFileName << std::endl;
for (int i = 0; i < numSubsamplesToDo; i++) {
if (i % 10000 == 0) {
std::cerr << "Subsample " << i << " out of " << numSubsamplesToDo << std::endl;
}
double subsampleDxyTotal = 0;
for (int j = 0; j < opt::subsampleLength; j++) {
int linearIndex = distribution(rng);
string genomeLocation = linearToGenomeMap[linearIndex];
if (vcfDxyMap.count(genomeLocation) == 1) {
subsampleDxyTotal = subsampleDxyTotal + vcfDxyMap[genomeLocation];
}
}
*outDxyFile << subsampleDxyTotal/opt::subsampleLength << std::endl;
}
} else {
string outDxyFileName;
size_t suffixPos = opt::bedFile.find_last_of('.');
if (suffixPos != std::string::npos) {
outDxyFileName = opt::outFolder + stripExtension(opt::bedFile) + "_DxyVals_perElement.txt";
} else {
outDxyFileName = opt::outFolder + opt::bedFile + "_DxyVals_perElement.txt";
} std::ofstream* outDxyFile = new std::ofstream(outDxyFileName.c_str());
std::cerr << "Now calculating Dxy values per element to be written out into: " << outDxyFileName << std::endl;
std::vector<double> elementDxyValues = coords.getMeanPerElement(vcfDxyMap);
std::cerr << "Calculations done" << std::endl;
std::vector<string> elementNames = coords.getElementNames();
std::vector<std::vector<string> > elementOuterBounds = coords.getElementOuterBoundaries();
for (int i = 0; i < (int)elementDxyValues.size(); i++) {
*outDxyFile << elementOuterBounds[i][0] << "\t" << elementOuterBounds[i][1] << "\t" << elementOuterBounds[i][2] << "\t" << elementNames[i] << "\t" << elementDxyValues[i] << std::endl;
}
}
std::cerr << "Done" << std::endl;
return 0;
}
void parseSubsamplingDxyOptions(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 's': arg >> opt::subsampleLength; break;
case 'e': opt::bElements = true; break;
case OPT_FOLDER: arg >> opt::outFolder; break;
case 'h':
std::cout << SUBSAMPLED_DXY_USAGE_MESSAGE;
exit(EXIT_SUCCESS);
}
}
if (argc - optind < 2) {
std::cerr << "missing arguments\n";
die = true;
}
else if (argc - optind > 2)
{
std::cerr << "too many arguments\n";
die = true;
}
if (die) {
std::cout << "\n" << SUBSAMPLED_DXY_USAGE_MESSAGE;
exit(EXIT_FAILURE);
}
opt::bedFile = argv[optind++];
opt::vcfFile = argv[optind++];
}