-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateeQTLGWAS.c
183 lines (166 loc) · 6.33 KB
/
generateeQTLGWAS.c
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
#include <cmath>
#include <map>
#include <vector>
#include <sstream>
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <string>
#include <stdlib.h> /* atof */
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
using namespace std;
int getDir (string dir, vector<string> &files) {
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
if(string(dirp->d_name).find(".peak")!=string::npos)
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
void generateeQTLGWAS(map <string, vector <string> > & gtexCisSig, vector <string> snpIDs, vector <double> zScoreGWAS, string outputFileName, bool signOrNoSign) {
ofstream outputStream;
for(int i = 0; i < snpIDs.size(); i++) {
string snpId = snpIDs[i];
vector <string> cisSig = gtexCisSig[snpId];
for(int j = 0; j < cisSig.size(); j++) {
string tmp = cisSig[j];
string tmp2;
string tmp3;
string geneId = tmp.substr(0, tmp.find("\t"));
outputStream.open( (outputFileName+ geneId + ".eqtl").c_str() , std::ofstream::out | std::ofstream::app);
//TODO not sure which one is the z-score
outputStream << snpId << "\t";
tmp2 = tmp.substr(tmp.find("\t")+1);
tmp3 = tmp2.substr(tmp2.find("\t")+1);
double zscoretmp = atof(tmp3.substr(0, tmp3.find("\t")).c_str());
if(signOrNoSign) outputStream << zscoretmp << endl;
else outputStream << abs(zscoretmp) << endl;
outputStream.close();
outputStream.open( (outputFileName + geneId + ".gwas").c_str(), std::ofstream::out | std::ofstream::app);
outputStream << snpId << "\t";
if(signOrNoSign) outputStream << zScoreGWAS[i] << endl;
else outputStream << abs(zScoreGWAS[i]) << endl;
outputStream.close();
}
}
}
void generateALLeQTLGWAS(map <string, vector <string> > & gtexCisSig, string dicName, string outputFileName, bool signOrNoSign) {
vector <string> files;
getDir(dicName, files);
string line;
ifstream test;
vector <string> snpIDs;
vector <double> zScoreGWAS;
for(int i = 0; i < files.size(); i++) {
cout << files[i] << endl;
test.open((dicName + "/" + files[i]).c_str());
while(std::getline(test, line)) {
int indexFirstTab = line.find("\t");
string snpID = line.substr(0, indexFirstTab);
double zScore = atof(line.substr(line.find("\t")+1, line.length()).c_str());
snpIDs.push_back(snpID);
zScoreGWAS.push_back(zScore);
}
string tmpfileName = files[i].substr(0, files[i].find(".peak"));
generateeQTLGWAS(gtexCisSig, snpIDs, zScoreGWAS, outputFileName + "/" + tmpfileName, signOrNoSign);
snpIDs.erase( snpIDs.begin(), snpIDs.end() );
zScoreGWAS.erase( zScoreGWAS.begin(), zScoreGWAS.end() );
test.close();
}
}
void obtainALLSNPsFromDirectory(string dicName, map <string, double> & hashMapSNP){
vector <string> files;
getDir(dicName, files);
string line;
ifstream snpStream;
for(int i = 0; i < files.size(); i++) {
snpStream.open( (dicName + files[i]).c_str() );
while(std::getline(snpStream, line)) {
string snpID = line.substr(0, line.find("\t"));
float zScore = atof(line.substr(line.find("\t")+1, line.length()).c_str());
hashMapSNP[snpID] = zScore;
}
snpStream.close();
}
}
void obtainGTExCisSignificant(string dosageFileName, map <string, vector <string> > & gtexCisSig, map <string, double> hashMapSNP2ZGWAS, vector <string> & geneGTExCisSig) {
string line;
map <string, int> geneMap;
ifstream dosageStream;
dosageStream.open(dosageFileName.c_str());
while(std::getline(dosageStream, line)) {
int indexFirstTab = line.find("\t");
string snpID = line.substr(0, indexFirstTab);
string geneId = line.substr(indexFirstTab+1, line.find("\t", indexFirstTab+1)-indexFirstTab );
if(hashMapSNP2ZGWAS.find(snpID) != hashMapSNP2ZGWAS.end()){
gtexCisSig[snpID].push_back(line.substr(line.find("\t")+1));
if(geneMap.find(geneId) == geneMap.end()) {
geneGTExCisSig.push_back(geneId);
geneMap[geneId] = 0;
}
}
}
}
int main(int argc, char *argv[]) {
int oc = 0;
int signValue = 0;
bool signOrNoSign = true;
string tissueName;
string gwasName;
string currentPath;
map <string, double> hashMapSNP2ZGWAS;
map <string, vector <string> > gtexCisSig;
vector <string> geneGTExCisSig;
vector <string> snpIDs;
vector <double> zScoreGWAS;
string cisGTEXFileName;
while ((oc = getopt(argc, argv, "vhl:f:o:t:g:s:")) != -1) {
switch (oc) {
case 'v':
cout << "version 0.0:" << endl;
case 'h':
cout << "Options: " << endl;
cout << " -h help" << endl;
cout << " -t TISSUENAME" << " specify the name of tissue" << endl;
cout << " -g GWASNAME" << " specify the name of GWAS" << endl;
cout << " -o TMPFILE" << " specify the tmp folder" << endl;
cout << " -f GTEXCisFILE" << " specify the GTEx Cis significant folder" << endl;
cout << " -s SIGN " << "specify if we need sign or not for Z-score and LD, 0 (everything is positive), 1 (sign is important)" << endl;
return(0);
case 't':
tissueName = string(optarg);
break;
case 'g':
gwasName = string(optarg);
break;
case 'o':
currentPath = string(optarg);
break;
case 'f':
cisGTEXFileName = string(optarg) + "/" + tissueName + "_Analysis.cis.eqtl";
break;
case 's':
signValue = atoi(optarg);
signOrNoSign = ((signValue==0) ? false : true);
break;
case ':':
case '?':
default:
cout << "Strange" << endl;
break;
}
}
cout << cisGTEXFileName << endl;
cout << currentPath + "/peak/" + gwasName + "/" + tissueName + "/" << endl;
cout << currentPath + "/in/" + gwasName + "/" + tissueName << endl;
obtainALLSNPsFromDirectory(currentPath + "/peak/" + gwasName + "/" + tissueName + "/" , hashMapSNP2ZGWAS);
obtainGTExCisSignificant(cisGTEXFileName, gtexCisSig, hashMapSNP2ZGWAS, geneGTExCisSig);
generateALLeQTLGWAS(gtexCisSig, currentPath + "/peak/" + gwasName + "/" + tissueName + "/" , currentPath + "/in/" + gwasName + "/" + tissueName + "/", signOrNoSign);
}