-
Notifications
You must be signed in to change notification settings - Fork 1
/
contigs.cpp
178 lines (158 loc) · 4.15 KB
/
contigs.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
#include "piler2.h"
const int INSANE_LENGTH = 500000000;
// Arbitrarily chosen prime number
static ContigData *HashTable[HASH_TABLE_SIZE];
static ContigData **ContigMap;
static int SeqLength;
void SetSeqLength(int Length)
{
SeqLength = Length;
}
static ContigData *HashLookup(const char *Label)
{
unsigned h = Hash(Label);
assert(h < HASH_TABLE_SIZE);
ContigData *p = HashTable[h];
while (p != 0)
{
if (0 == strcmp(Label, p->Label))
return p;
p = p->Next;
}
return 0;
}
void LogContigs()
{
Log(" Label Hash Pos Length\n");
Log("-------------------- ------ ---------- ----------\n");
for (int h = 0; h < HASH_TABLE_SIZE; ++h)
{
for (const ContigData *p = HashTable[h]; p != 0; p = p->Next)
{
const ContigData &Contig = *p;
Log("%20.20s %6d %10d %10d",
Contig.Label,
h,
Contig.From,
Contig.Length);
if (Hash(Contig.Label) != h)
Log(" ** Hash=%d", Hash(Contig.Label));
Log("\n");
}
}
}
static void AppendContig(const char *Label, int Pos)
{
ContigData *Contig = all(ContigData, 1);
Contig->Label = strsave(Label);
char *Space = strchr(Contig->Label, ' ');
if (0 != Space)
*Space = 0;
unsigned h = Hash(Label);
assert(h < HASH_TABLE_SIZE);
ContigData *p = HashTable[h];
Contig->Length = Pos + 1;
Contig->Next = p;
HashTable[h] = Contig;
}
void AddContigPos(const char *Label, int Pos)
{
ContigData *Contig = HashLookup(Label);
if (0 == Contig)
AppendContig(Label, Pos);
else
{
if (Pos > Contig->Length)
Contig->Length = Pos;
}
}
void AddContig(const char *Label, int GlobalPos, int Length)
{
ContigData *Contig = all(ContigData, 1);
Contig->Label = strsave(Label);
char *Space = strchr(Contig->Label, ' ');
if (0 != Space)
*Space = 0;
unsigned h = Hash(Contig->Label);
assert(h < HASH_TABLE_SIZE);
ContigData *p = HashTable[h];
Contig->From = GlobalPos;
Contig->Length = Length;
Contig->Next = p;
HashTable[h] = Contig;
}
int GlobalizeContigs()
{
int GlobalPos = 0;
for (int h = 0; h < HASH_TABLE_SIZE; ++h)
{
for (ContigData *p = HashTable[h]; p != 0; p = p->Next)
{
ContigData &Contig = *p;
const int Length = p->Length;
if (Length < 1 || Length >= INSANE_LENGTH)
Quit("GlobalizeContigs, insane length %d", Length);
p->From = GlobalPos;
GlobalPos += Length;
// Pad up to start of next bin
// (required for contig map)
int BinRemainder = GlobalPos%CONTIG_MAP_BIN_SIZE;
if (BinRemainder > 0)
GlobalPos += CONTIG_MAP_BIN_SIZE - BinRemainder;
if (GlobalPos%CONTIG_MAP_BIN_SIZE)
Quit("Dumb mistake rounding contig");
}
}
SeqLength = GlobalPos;
return SeqLength;
}
void MakeContigMap()
{
if (SeqLength%CONTIG_MAP_BIN_SIZE)
Quit("MakeContigMap: expects rounded size");
const int BinCount = SeqLength/CONTIG_MAP_BIN_SIZE;
ContigMap = all(ContigData *, BinCount);
zero(ContigMap, ContigData *, BinCount);
for (int h = 0; h < HASH_TABLE_SIZE; ++h)
{
for (ContigData *p = HashTable[h]; p != 0; p = p->Next)
{
ContigData &Contig = *p;
int From = p->From;
int To = From + p->Length - 1;
if (From%CONTIG_MAP_BIN_SIZE)
Quit("MakeContigMap: expected rounded contig from");
int BinFrom = From/CONTIG_MAP_BIN_SIZE;
int BinTo = To/CONTIG_MAP_BIN_SIZE;
for (int Bin = BinFrom; Bin <= BinTo; ++Bin)
{
if (ContigMap[Bin] != 0)
Quit("MakeContigMap: overlap error");
ContigMap[Bin] = p;
}
}
}
}
const char *GlobalToContig(int GlobalPos, int *ptrContigPos)
{
if (GlobalPos < 0 || GlobalPos >= SeqLength)
Quit("GlobalToContig: invalid pos");
const int Bin = GlobalPos/CONTIG_MAP_BIN_SIZE;
ContigData *Contig = ContigMap[Bin];
if (0 == Contig)
Quit("GlobalToContig: doesn't map");
int ContigPos = GlobalPos - Contig->From;
if (ContigPos < 0 || ContigPos >= Contig->Length)
Quit("GlobalToContig: out of bounds");
*ptrContigPos = ContigPos;
return Contig->Label;
}
int ContigToGlobal(int ContigPos, const char *Label)
{
const ContigData *Contig = HashLookup(Label);
if (0 == Contig)
Quit("ContigToGlobal: contig not found (%s)", Label);
if (ContigPos < 0 || ContigPos >= Contig->Length)
Quit("ContigToGlobal: out of bounds");
return Contig->From + ContigPos;
}