-
Notifications
You must be signed in to change notification settings - Fork 67
/
Chrom.h
67 lines (60 loc) · 1.87 KB
/
Chrom.h
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
#pragma once
#include "stdafx.h"
#include "Genome.h"
#include "Mutation.h"
//
// Chromosome utilities.
//
namespace sim {
//
// Ad hoc helper: return whether 'pieceName' denotes 'chrom'.
//
inline bool matchesChromName(const std::string &pieceName, const std::string &chrom)
{
std::string prefix = "chr";
_ASSERT(!chrom.find(prefix));
bool mitochondrial = chrom == "chrMT";
if (pieceName == chrom || mitochondrial && pieceName == "chrM")
return true; // Matches the format Jesse first worked with.
std::string longForm =
mitochondrial ? "mitochondrion" : "chromosome " + chrom.substr(prefix.size());
return pieceName.find(longForm) != std::string::npos; // Matches GRCh37.p5.
}
//
// Return the first piece corresponding to 'chrom' in 'genome'; NULL for failure.
//
inline const Genome::Piece *getChrom(const Genome &genome, const std::string &chrom)
{
for (int i = 0; i < genome.getNumPieces(); ++i) {
const Genome::Piece *piece = genome.getPieces() + i;
if (matchesChromName(piece->name, chrom))
return piece;
}
return NULL;
}
class ChromSexer {
public:
explicit ChromSexer(Sex i_child) : cache(4), child(i_child) {
FORSEX {
cache[sex].push_back(Sex(sex));
cache[2].push_back(Sex(sex));
}
}
//
// Does a child of given 'sex' inherit 'chrom' from mom, dad, both, or neither?
//
const std::vector<Sex> &sex(const std::string &chrom) const {
return cache[getIndex(chrom)];
}
private:
std::vector<std::vector<Sex> > cache;
Sex child;
int getIndex(const std::string &chrom) const {
if (matchesChromName(chrom, "chrX") && child || matchesChromName(chrom, "chrMT"))
return FEMALE;
if (matchesChromName(chrom, "chrY"))
return child ? MALE : 3;
return 2;
}
};
} // namespace sim