-
Notifications
You must be signed in to change notification settings - Fork 1
/
vcfmerger.cpp
354 lines (333 loc) · 9.55 KB
/
vcfmerger.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* File: vcfmerger.cpp
* Author: CRE
* Last Edited: Thu Oct 20 14:52:19 2016
*/
#include "crelib/crelib.h"
#include "vcfmerger.h"
#include <iostream>
#include <cstring>
#include <string>
#include <list>
#include <set>
#include <sstream>
#include <vector>
using namespace cre;
using namespace std;
static inline void getRootName(const char * FileName, char * RootName);
static inline void mergeHead(FILE* LFile, FILE* RFile, FILE* OutFile, char* LBuffer, char* RBuffer, const char * RRootName, uint & LSampleNum, uint & RSampleNum);
static inline void mergeSites(FILE* LFile, FILE* RFile, FILE* OutFile, char* LBuffer, char* RBuffer, const uint &LSampleNum, const uint &RSampleNum);
static inline void removeTailingN(char * str, uint Length=0)//to remove a tailing \n(if there is one).You can provide the Length of Buffer to avoid recalculating.
{
if(Length==0) Length=strlen(str);
if (Length>0&&str[--Length]=='\n')
{
str[Length]='\0';
}
}
static inline int chromCompare(const char * A, const char * B)//return -1 if A<B, 0 if A=B, 1 if A>B
{
if (strcmp(A,"MT")==0)
{
if (strcmp(A,B)==0) return 0;
else return 1;
}
if (strcmp(B,"MT")==0)
{
if (strcmp(A,B)==0) return 0;
else return -1;
}
return strcmp(A,B);
}
void merge2(const char * LFileName, const char * RFileName, const char * OutFileName)
{
char LRootName[NAME_BUFFER_SIZE], RRootName[NAME_BUFFER_SIZE];
getRootName(LFileName,LRootName);
getRootName(RFileName,RRootName);
FILE * LFile=fopen(LFileName, "r");
if (LFile==NULL) die("Open left file failed!");
FILE * RFile=fopen(RFileName, "r");
FILE * OutFile=NULL;
if (RFile==NULL) die("Open right file failed!");
if (OutFileName[0]=='\0') OutFile=stdout;
else
{
OutFile=fopen(OutFileName, "w");
if (OutFile==NULL) die("Open out file failed!");
}
char *LBuffer=myalloc(BUFFER_SIZE,char);
char *RBuffer=myalloc(BUFFER_SIZE,char);
uint LSampleNum, RSampleNum;
mergeHead(LFile,RFile,OutFile, LBuffer, RBuffer, RRootName, LSampleNum, RSampleNum);
mergeSites(LFile,RFile,OutFile, LBuffer, RBuffer, LSampleNum, RSampleNum);
free(LBuffer);
free(RBuffer);
fclose(LFile);
fclose(RFile);
fclose(OutFile);
}
static inline void getRootName(const char * FileName, char * RootName)
{
strcpy(RootName,FileName);
if (strlen(FileName)>4)
{
if (strcmp(FileName+strlen(FileName)-4, ".vcf")==0)
{
RootName[strlen(FileName)-4]='\0';
}
}
}
static inline void mergeHeader(char* LBuffer, char* RBuffer, const char * RRootName, FILE* OutFile, uint &LSampleNum, uint &RSampleNum);
static inline void mergeHead(FILE* LFile, FILE* RFile, FILE* OutFile, char* LBuffer, char* RBuffer, const char * RRootName, uint &LSampleNum, uint &RSampleNum)
{
list<string> LHeads, RHeads;
//get L heads
while(fgets(LBuffer, BUFFER_SIZE, LFile))
{
uint Length=strlen(LBuffer);
if (Length==0) die("Read heads error");
if (Length>=2&&LBuffer[0]=='#'&&LBuffer[1]=='#')
{
if (Length==BUFFER_SIZE-1) die("Buffer may not be enough!");
if (Length>0&&LBuffer[Length-1]=='\n')
{
LBuffer[--Length]='\0';
}
LHeads.push_back(LBuffer);
}
else break;
}
//get R heads
while(fgets(RBuffer, BUFFER_SIZE, RFile))
{
uint Length=strlen(RBuffer);
if (Length==0) die("Read heads error");
if (Length>=2&&RBuffer[0]=='#'&&RBuffer[1]=='#')
{
if (Length==BUFFER_SIZE-1) die("Buffer may not be enough!");
if (Length>0&&RBuffer[Length-1]=='\n')
{
RBuffer[--Length]='\0';
}
RHeads.push_back(RBuffer);
}
else break;
}
//remove duplicated heads and output LHeads
for (list<string>::iterator i=LHeads.begin();i!=LHeads.end();++i)
{
fprintf(OutFile,"%s\n",i->c_str());
for (list<string>::iterator j=RHeads.begin();j!=RHeads.end();++j)
{
if (*i==*j)
{
j=RHeads.erase(j);
if (j!=RHeads.begin()) --j;
}
}
}
//output RHeads
for (list<string>::iterator j=RHeads.begin();j!=RHeads.end();++j)
{
fprintf(OutFile,"%s\n",j->c_str());
}
mergeHeader(LBuffer, RBuffer, RRootName, OutFile, LSampleNum, RSampleNum);
}
static inline void mergeHeader(char* LBuffer, char* RBuffer, const char* RRootName, FILE* OutFile, uint & LSampleNum, uint & RSampleNum)
{
if (LBuffer[0]!='#') die("Can't read left's header!");
if (RBuffer[0]!='#') die("Can't read right's header!");
char * Buffer = myalloc(SMALL_BUFFER_SIZE, char);
stringstream SS;
set<string> LSamplesSet, RSamplesSet;
vector<string> LSamples, RSamples;
SS.str(LBuffer);
SS>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer;//They are #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT
//get L samples
while(SS>>Buffer)
{
if(SS.eof()) break;
if (LSamplesSet.count(Buffer)!=0) die("Left file has duplicate samplenames!");
LSamplesSet.insert(Buffer);
LSamples.push_back(Buffer);
}
SS.clear();
SS.str(RBuffer);
SS>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer>>Buffer;//They are #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT
//get R samples
while(SS>>Buffer)
{
if(SS.eof()) break;
if (LSamplesSet.count(Buffer)!=0)//to add a root name and a _ as prefix
{
uint RRootLength=strlen(RRootName);
uint BufferLength=strlen(Buffer);
if (RRootLength+BufferLength+1>=SMALL_BUFFER_SIZE) die("Small buffer is too small!");
memmove(Buffer+RRootLength+1, Buffer, BufferLength+1);
memcpy(Buffer, RRootName, RRootLength);
Buffer[RRootLength]='_';
}
if (RSamplesSet.count(Buffer)!=0) die("Left file has duplicate samplenames!");
RSamplesSet.insert(Buffer);
RSamples.push_back(Buffer);
}
uint Length=strlen(LBuffer);
if (Length>0&&LBuffer[--Length]=='\n')
{
LBuffer[Length]='\0';
}
fprintf(OutFile, "%s", LBuffer);
for (uint i=0;i<RSamples.size();++i)
{
fprintf(OutFile, "\t%s", RSamples[i].c_str());
}
LSampleNum=LSamples.size();
RSampleNum=RSamples.size();
free(Buffer);
}
class Site
{
Site& operator=(const Site&) {return *this;}
Site(const Site&) {};
public:
uint Pos;
char *Chrom;
Site()
:Pos(0)
{
Chrom=myalloc(NAME_BUFFER_SIZE, char);
}
~Site()
{
if (Chrom!=NULL) free(Chrom);
}
bool operator<(const Site&B) const
{
if (chromCompare(Chrom, B.Chrom)>0) return false;
if (chromCompare(Chrom,B.Chrom)<0) return true;
return Pos<B.Pos;
}
bool operator>(const Site&B) const
{
if (chromCompare(Chrom,B.Chrom)<0) return false;
if (chromCompare(Chrom,B.Chrom)>0) return true;
return Pos>B.Pos;
}
bool operator==(const Site&B) const
{
if (Pos==B.Pos&&chromCompare(Chrom,B.Chrom)==0) return true;
return false;
}
};
static inline void getSite(const char * Buffer, Site& TheSite)
{
sscanf(Buffer,"%s %u",TheSite.Chrom, &(TheSite.Pos));
}
static inline void getRefNAlt(const char * Buffer, char * Ref, char * Alt)
{
sscanf (Buffer, "%s %s %s %s %s", Ref,Ref,Ref, Ref, Alt);
}
static inline uint findTheDataOutset(const char * Buffer, uint Length=0)//return the index of the first varriant call, you can give the Length of Buffer to avoid recalculating.
{
if (Length==0) Length=strlen(Buffer);
uint TableCount=0;//there are 9 tabs before data
for (uint i=0;i<Length;++i)
{
if (Buffer[i]=='\t') ++TableCount;
if (TableCount==9)
{
return i+1;
}
}
die("Something wrong with the data(not enough \\t before data!).");
return 0;
}
static inline void mergeSites(FILE* LFile, FILE* RFile, FILE* OutFile, char* LBuffer, char* RBuffer, const uint &LSampleNum, const uint &RSampleNum)
{
uint LLength, RLength, RDIndex;
Site LSite, RSite;
char* LRef=myalloc(ALT_BUFFER_SIZE, char);
char* RRef=myalloc(ALT_BUFFER_SIZE, char);
char* LAlt=myalloc(ALT_BUFFER_SIZE, char);
char* RAlt=myalloc(ALT_BUFFER_SIZE, char);
bool LReside=false, RReside=false;
do
{
if (!LReside) LBuffer[0]='\0';
if (!RReside) RBuffer[0]='\0';
if (!feof(LFile)&&!LReside) fgets(LBuffer, BUFFER_SIZE, LFile);
if (!feof(RFile)&&!RReside) fgets(RBuffer, BUFFER_SIZE, RFile);
LReside=false;
RReside=false;
LLength=strlen(LBuffer);
RLength=strlen(RBuffer);
if (LLength==0&&RLength==0) break;
if (LLength==0)
{
removeTailingN(RBuffer,RLength);
RDIndex=findTheDataOutset(RBuffer,RLength);
fprintf(OutFile,"\n");
fwrite(RBuffer, 1, RDIndex-1, OutFile);//doesn't include the last \t
for (uint i=0;i<LSampleNum;++i)
{
fprintf(OutFile,"\t.|.");
}
if (RSampleNum!=0) fprintf(OutFile,"\t%s",RBuffer+RDIndex);
}
else if (RLength==0)
{
removeTailingN(LBuffer,LLength);
fprintf(OutFile,"\n%s", LBuffer);
for (uint i=0;i<RSampleNum;++i) fprintf(OutFile,"\t.|.");
}
else
{
getSite(LBuffer,LSite);
getSite(RBuffer,RSite);
if (LSite<RSite)
{
RReside=true;
removeTailingN(LBuffer,LLength);
fprintf(OutFile,"\n%s", LBuffer);
for (uint i=0;i<RSampleNum;++i) fprintf(OutFile,"\t.|.");
}
else if (LSite>RSite)
{
LReside=true;
RDIndex=findTheDataOutset(RBuffer,RLength);
fprintf(OutFile,"\n");
fwrite(RBuffer, 1, RDIndex-1, OutFile);//doesn't include the last \t
for (uint i=0;i<LSampleNum;++i)
{
fprintf(OutFile,"\t.|.");
}
removeTailingN(RBuffer,RLength);
if (RSampleNum!=0) fprintf(OutFile,"\t%s",RBuffer+RDIndex);
}
else
{
getRefNAlt(LBuffer,LRef, LAlt);
getRefNAlt(RBuffer,RRef, RAlt);
if (strcmp(LRef, RRef)==0&&strcmp(LAlt,RAlt)==0)
{
removeTailingN(LBuffer,LLength);
removeTailingN(RBuffer,RLength);
RDIndex=findTheDataOutset(RBuffer,RLength);
fprintf(OutFile,"\n%s", LBuffer);
if (RSampleNum!=0) fprintf(OutFile,"\t%s",RBuffer+RDIndex);
}
else
{
RReside=true;
removeTailingN(LBuffer,LLength);
fprintf(OutFile,"\n%s", LBuffer);
for (uint i=0;i<RSampleNum;++i) fprintf(OutFile,"\t.|.");
}
}
}
}
while(!(feof(LFile)&&feof(RFile)));
free(LRef);
free(RRef);
free(LAlt);
free(RAlt);
}