-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
196 lines (189 loc) · 5.76 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <unordered_map>
#include "general_functions.h"
#include "index.h"
#define STRLENPAR 500
using namespace std;
struct Parameters {
string inputBgenFile;
string inputSampleFile;
string inputSNPList;
string inputIndexFile;
string inputOutFile;
string inputChr;
bool inputBgenFileB;
bool inputSampleFileB;
bool inputSNPListB;
bool inputIndexFileB;
bool inputOutFileB;
bool inputGenB;
bool inputChrB;
bool inputRsidB;
};
Parameters read_options(int &argc,char *argv[]);
void print_usage();
void print_options(Parameters result);
void process_dosages();
void print_header();
int main( int argc, char *argv[] ){
using namespace std;
// print_header();
Parameters inUse=read_options(argc,argv);
if(!(inUse.inputBgenFileB && inUse.inputIndexFileB)){
print_usage();
}
// If we're indexing the file
if(!inUse.inputOutFileB && !inUse.inputGenB && !inUse.inputSNPListB && !inUse.inputSampleFileB){
// index the file here
index_snps(inUse.inputBgenFile,inUse.inputIndexFile);
}else if(inUse.inputOutFileB && inUse.inputSNPListB && inUse.inputChrB && inUse.inputSampleFileB){
// pull out the SNPs
extract_snps(inUse.inputBgenFile,inUse.inputSampleFile,inUse.inputIndexFile,inUse.inputSNPList,inUse.inputOutFile,inUse.inputGenB,inUse.inputChr,inUse.inputRsidB);
}else{
print_usage();
}
puts( "\tDONE!\n\n" );
return 0;
}
Parameters read_options(int &argc,char *argv[]){
int m,n, /* Loop counters */
l, /* String length */
x; /* Exit code. */
char argcase[10]; /* List buffer */
Parameters result;
//initialise result
result.inputIndexFileB=false;
result.inputSNPListB=false;
result.inputSampleFileB=false;
result.inputBgenFileB=false;
result.inputOutFileB=false;
result.inputGenB=false;
result.inputChrB=false;
for( n=1; n < argc; n++ ) /* Scan through args */
{
switch( (int)argv[n][0] ) /* Check for option character */
{
case '-': x = 0; /* Bail out if 1. */
l = strlen( argv[n] );
for( m = 1; m < l; ++m ) /* scan through options */
{
if( strcmp(&argv[n][1],"bgen")==0 ){
if( (int)argv[n+1][0] == '-' ){
puts( "Illegal syntax -- no string following option -bgen\n" );
exit( 1 );
}else{
result.inputBgenFile=&argv[n+1][0];
result.inputBgenFileB=true;
}
x = 1;
}else if( strcmp(&argv[n][1],"sample")==0 ){
if( (int)argv[n+1][0] == '-' ){
puts( "Illegal syntax -- no string following option -sample\n" );
exit( 1 );
}else{
result.inputSampleFile=&argv[n+1][0];
result.inputSampleFileB=true;
/* printf( "String = %s\n", s );*/
}
x = 1;
}else if( strcmp(&argv[n][1],"snps")==0 ){
if( (int)argv[n+1][0] == '-' ){
puts( "Illegal syntax -- no string following option -snps\n" );
exit( 1 );
}else{
result.inputSNPList=&argv[n+1][0];
result.inputSNPListB=true;
}
}else if( strcmp(&argv[n][1],"index-file")==0 ){
if( (int)argv[n+1][0] == '-' ){
puts( "Illegal syntax -- no string following option -index-file\n" );
exit( 1 );
}else{
result.inputIndexFile=&argv[n+1][0];
result.inputIndexFileB=true;
}
}else if( strcmp(&argv[n][1],"out")==0 ){
if( (int)argv[n+1][0] == '-' ){
puts( "Illegal syntax -- no string following option -out\n" );
exit( 1 );
}else{
result.inputOutFile=&argv[n+1][0];
result.inputOutFileB=true;
}
}else if( strcmp(&argv[n][1],"gen")==0 ){
result.inputGenB=true;
}else if( strcmp(&argv[n][1],"chr")==0 ){
if( (int)argv[n+1][0] == '-' ){
puts( "Illegal syntax -- no string following option -chr\n" );
exit( 1 );
}else{
result.inputChr=&argv[n+1][0];
result.inputChrB=true;
}
}else if( strcmp(&argv[n][1],"rsid")==0 ){
result.inputRsidB=true;
}else{
strcpy( argcase, &argv[n][1] );
printf( "Unrecognised option : %s\n", &argv[n][0] ); /* THIS NEEDS FIXING TO OUTPUT THE WHOLE ARGUMENT!!!!!!! */
print_usage() ;
}
if( x == 1 ) {
break;
}
}
break;
default: /*printf( "Unrecognised option : %s\n", &argv[n][0] ); Not option -- text. */
break;
}
}
return result;
}
void print_usage( ){
printf( "\nUsage:\n" );
printf( "\t-bgen bgen file\n" );
printf( "\t-sample sample file\n" );
printf( "\t-index-file file containing SNP indexes\n" );
printf( "\t-snps SNPs to extract\n" );
printf( "\t-out output filename\n" );
printf( "\t-chr chr from which to extract SNPs\n" );
printf( "\t-gen (optional) specifies gen output rather than genotype dosages\n" );
printf( "\t-rsid (optional) include rsids in output file (where available) rather than chr:pos\n\n\n" );
exit(0);
return;
}
void print_options(Parameters result){
cout<<"\nOptions in use:"<<endl;
if(result.inputBgenFileB){
cout<<"-bgen "<<result.inputBgenFile<<endl;
}
if(result.inputSampleFileB){
cout<<"-sample "<<result.inputSampleFile<<endl;
}
if(result.inputSNPListB){
cout<<"-snps "<<result.inputSNPList<<endl;
}
if(result.inputIndexFileB){
cout<<"-index-file "<<result.inputIndexFile<<endl;
}
if(result.inputOutFileB){
cout<<"-out "<<result.inputOutFile<<endl;
}
if(result.inputGenB){
cout<<"-gen true"<<endl;
}
if(result.inputGenB){
cout<<"-chr "<<result.inputChr<<endl;
}
cout<<endl;
return;
}
void print_header(){
cout<<"\n\nindex_bgen\n"<<endl;
return;
}