-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
99 lines (91 loc) · 2.29 KB
/
main.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
/* File: main.cpp
* Author: CRE
* Last Edited: Mon Jan 16 13:09:54 2017
*/
#include "vcfmerger.h"
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include "crelib/crelib.h"
#include <omp.h>
using namespace std;
using namespace cre;
static inline void printHelp()
{
die("Usage:\n"\
"\tvcfmerger vcf1 vcf2 ... > merged.vcf\n"\
"or\tvcfmerger vcf1 vcf2 ... -o merged.vcf\n"\
"and\t-t n for multithreading\n"\
"Warning: This program may generate or overwrite several temporary files(less than input files) in this directory, they are %s* and %s*. If the program ends normal, they would be deleted as well. Although I don't think you have those files in your directory, just be aware of it.", TEMP_FILE_NAME1, TEMP_FILE_NAME2);
}
string getTempFileName(const char * Prefix, int n)
{
string Name(Prefix);
Name+=to_string(n);
return Name;
}
uint MostFileNumber=0;
void removeTempFiles()
{
for (uint i=0;i<MostFileNumber;++i)
{
remove(getTempFileName(TEMP_FILE_NAME1,i).c_str());
remove(getTempFileName(TEMP_FILE_NAME2,i).c_str());
}
}
uint Threads=1;
int main (int argc, char ** argv)
{
if (argc<=1) printHelp();
atexit(removeTempFiles);
vector<string> InFileNames;
char OutFile[NAME_BUFFER_SIZE];
OutFile[0]='\0';
for (int i=1;i<argc;++i)
{
if (argv[i][0]=='-')
{
if (argv[i][1]=='o')
{
if (i+1>=argc) printHelp();
strcpy(OutFile, argv[++i]);
}
else if (argv[i][1]=='t'&& argv[i][2]=='\0')
{
if (i+1>=argc) printHelp();
Threads=atoi(argv[++i]);
}
}
else
{
InFileNames.push_back(argv[i]);
}
}
if (InFileNames.size()<2) printHelp();
uint FilesToMerge=InFileNames.size();
MostFileNumber=FilesToMerge/2;
const char *Prefix=TEMP_FILE_NAME1, *LPrefix=TEMP_FILE_NAME2;
while(FilesToMerge>1)
{
if (FilesToMerge==2)
{
merge2(InFileNames[0].c_str(),InFileNames[1].c_str(),OutFile);
break;
}
omp_set_num_threads(Threads);
#pragma omp parallel for
for (int i=0;i<FilesToMerge/2;++i)
{
string TempFileName=getTempFileName(Prefix,i);
merge2(InFileNames[i].c_str(), InFileNames[FilesToMerge-1-i].c_str(), TempFileName.c_str());
InFileNames[i]=TempFileName;
}
if (FilesToMerge%2) FilesToMerge=FilesToMerge/2+1;
else FilesToMerge/=2;
const char * Temp=Prefix;
Prefix=LPrefix;
LPrefix=Temp;
}
return 0;
}