-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.cpp
More file actions
154 lines (127 loc) · 3.83 KB
/
utils.cpp
File metadata and controls
154 lines (127 loc) · 3.83 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
//
// utils.cpp
// RADTAGpainter
//
// Created by Milan Malinsky on 29/01/2016.
// Copyright (c) 2016 Milan Malinsky. All rights reserved.
//
#include "utils.h"
double stringToDouble(std::string s) {
double d;
std::stringstream ss(s); //turn the string into a stream
ss >> d; //convert
return d;
}
void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
// Initialize a matrix
void initialize_matrix_double(std::vector<std::vector<double> >& m, int m_size) {
for (int i = 0; i < m_size; i++) {
std::vector<double> v(m_size,0);
m.push_back(v);
}
}
// Initialize a matrix
void initialize_matrix_int(std::vector<std::vector<int> >& m, int m_size) {
for (int i = 0; i < m_size; i++) {
std::vector<int> v(m_size,0);
m.push_back(v);
}
}
// Remove a single file extension from the filename
std::string stripExtension(const std::string& filename)
{
size_t suffixPos = filename.find_last_of('.');
if(suffixPos == std::string::npos)
return filename; // no suffix
else
return filename.substr(0, suffixPos);
}
std::string suffix(const std::string& seq, size_t len)
{
assert(seq.length() >= len);
return seq.substr(seq.length() - len);
}
// Returns true if the filename has an extension indicating it is compressed
bool isGzip(const std::string& filename)
{
size_t suffix_length = sizeof(GZIP_EXT) - 1;
// Assume files without an extension are not compressed
if(filename.length() < suffix_length)
return false;
std::string extension = suffix(filename, suffix_length);
return extension == GZIP_EXT;
}
// Ensure a filehandle is open
void assertFileOpen(std::ifstream& fh, const std::string& fn)
{
if(!fh.is_open())
{
std::cerr << "Error: could not open " << fn << " for read\n";
exit(EXIT_FAILURE);
}
}
// Ensure a filehandle is open
void assertFileOpen(std::ofstream& fh, const std::string& fn)
{
if(!fh.is_open())
{
std::cerr << "Error: could not open " << fn << " for write\n";
exit(EXIT_FAILURE);
}
}
void assertGZOpen(gzstreambase& gh, const std::string& fn)
{
if(!gh.good())
{
std::cerr << "Error: could not open " << fn << std::endl;
exit(EXIT_FAILURE);
}
}
// Open a file that may or may not be gzipped for reading
// The caller is responsible for freeing the handle
std::istream* createReader(const std::string& filename, std::ios_base::openmode mode)
{
if(isGzip(filename))
{
igzstream* pGZ = new igzstream(filename.c_str(), mode);
assertGZOpen(*pGZ, filename);
return pGZ;
}
else
{
std::ifstream* pReader = new std::ifstream(filename.c_str(), mode);
assertFileOpen(*pReader, filename);
return pReader;
}
}
double calculateInbreedingCoefficient(std::vector<int>& numericGenotypes) {
int naa = 0; int nAa = 0; int nAA = 0;
int nSamples = (int)numericGenotypes.size();
for (std::vector<int>::size_type i = 0; i != nSamples; i++) {
if (numericGenotypes[i] == 0) naa++;
if (numericGenotypes[i] == 1) nAa++;
if (numericGenotypes[i] == 2) nAA++;
}
// Get the proportions of alt-hom and hets
double pAA = (double)nAA / nSamples;
double pAa = (double)nAa / nSamples;
// Allele frequencies
double p = pAA + (0.5 * pAa);
double q = 1 - p;
// Get the Hardy-Weinberg prediction for expected number of heterozygotes:
double HWAa = 2*p*q;
// Get the inbreeding coefficient
double F = (HWAa - pAa) / HWAa;
return F;
}