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
|
#!/usr/bin/python2
"""This script use the nearest marker to the transcript as control, increasing permutation rounds according to the p-value"""
########################################################################
# Last Updated Sep 27, 2011 by Xiaodong
########################################################################
import string
import sys
import MySQLdb
import getpass
import time
########################################################################
def translateAlias(str):
if str == "B6":
return "C57BL/6J"
elif str == "D2":
return "DBA/2J"
else:
return str
########################################################################
#
# Indicate Data Start Position, ProbeFreezeId, GeneChipId, DataFile
#
########################################################################
dataStart = 1
GeneChipId = int(input("Enter GeneChipId:"))
ProbeSetFreezeId = int(input("Enter ProbeSetFreezeId:"))
input_file_name = input("Enter file name with suffix:")
fp = open("%s" % input_file_name, 'rb')
try:
passwd = getpass.getpass('Please enter mysql password here : ')
con = MySQLdb.Connect(db='db_webqtl', host='localhost',
user='username', passwd=passwd)
db = con.cursor()
print("You have successfully connected to mysql.\n")
except:
print("You entered incorrect password.\n")
sys.exit(0)
time0 = time.time()
#########################################################################
#
# Check if each line have same number of members
# generate the gene list of expression data here
#
#########################################################################
print('Checking if each line have same number of members')
GeneList = []
isCont = 1
header = fp.readline()
header = header.strip().split('\t')
header = [x.strip() for x in header]
nfield = len(header)
line = fp.readline()
kj = 0
while line:
line2 = line.strip().split('\t')
line2 = [x.strip() for x in line2]
if len(line2) != nfield:
print(("Error : " + line))
isCont = 0
GeneList.append(line2[0])
line = fp.readline()
kj += 1
if kj % 100000 == 0:
print(('checked ', kj, ' lines'))
GeneList = sorted(map(string.lower, GeneList))
if isCont == 0:
sys.exit(0)
print(('used ', time.time()-time0, ' seconds'))
#########################################################################
#
# Check if each strain exist in database
# generate the string id list of expression data here
#
#########################################################################
print('Checking if each strain exist in database')
isCont = 1
fp.seek(0)
header = fp.readline()
header = header.strip().split('\t')
header = [x.strip() for x in header]
header = list(map(translateAlias, header))
header = header[dataStart:]
Ids = []
for item in header:
try:
db.execute('select Id from Strain where Name = "%s"' % item)
Ids.append(db.fetchall()[0][0])
except:
print((item, 'does not exist, check the if the strain name is correct'))
isCont = 0
if isCont == 0:
sys.exit(0)
print(('used ', time.time()-time0, ' seconds'))
########################################################################
#
# Check if each ProbeSet exist in database
#
########################################################################
print('Check if each ProbeSet exist in database')
##---- find PID is name or target ----##
line = fp.readline()
line = fp.readline()
line2 = line.strip().split('\t')
line2 = [x.strip() for x in line2]
PId = line2[0]
db.execute('select Id from ProbeSet where Name="%s" and ChipId=%d' %
(PId, GeneChipId))
results = db.fetchall()
IdStr = 'TargetId'
if len(results) > 0:
IdStr = 'Name'
##---- get Name/TargetId list from database ----##
db.execute('select distinct(%s) from ProbeSet where ChipId=%d order by %s' % (
IdStr, GeneChipId, IdStr))
results = db.fetchall()
Names = []
for item in results:
Names.append(item[0])
print(Names)
Names = sorted(map(string.lower, Names))
##---- compare genelist with names ----##
x = y = 0
x1 = -1
GeneList2 = []
while x < len(GeneList) and y < len(Names):
if GeneList[x] == Names[y]:
x += 1
y += 1
elif GeneList[x] < Names[y]:
if x != x1:
GeneList2.append(GeneList[x])
x1 = x
x += 1
elif GeneList[x] > Names[y]:
y += 1
if x % 100000 == 0:
print(('check Name, checked %d lines' % x))
while x < len(GeneList):
GeneList2.append(GeneList[x])
x += 1
isCont = 1
ferror = open("ProbeSetError.txt", "wb")
for item in GeneList2:
ferror.write(item + " doesn't exist \n")
print((item, " doesn't exist, check if the ProbeSet name is correct"))
isCont = 0
if isCont == 0:
sys.exit(0)
print(('used ', time.time()-time0, ' seconds'))
#########################################################################
#
# Insert data into database
#
#########################################################################
print('getting ProbeSet/Id')
#---- get Name/Id map ----#
db.execute('select %s, Id from ProbeSet where ChipId=%d order by %s' %
(IdStr, GeneChipId, IdStr))
results = db.fetchall()
NameIds = {}
for item in results:
NameIds[item[0]] = item[1]
print(('used ', time.time()-time0, ' seconds'))
print('inserting data')
##---- get old max dataId ----##
db.execute('select max(Id) from ProbeSetData')
maxDataId = int(db.fetchall()[0][0])
bmax = maxDataId
print(("old_max = %d\n" % bmax))
##---- insert data ----##
fp.seek(0)
line = fp.readline()
line = fp.readline()
kj = 0
values1 = []
values2 = []
while line:
line2 = line.strip().split('\t')
line2 = [x.strip() for x in line2]
PId = line2[0]
recordId = NameIds[PId]
maxDataId += 1
datasorig = line2[dataStart:]
###### Data Table items ######
i = 0
for item in datasorig:
try:
values1.append('(%d,%d,%s)' % (maxDataId, Ids[i], float(item)))
except:
pass
i += 1
values2.append("(%d,%d,%d)" % (ProbeSetFreezeId, recordId, maxDataId))
##---- insert into table ----##
kj += 1
if kj % 100 == 0:
cmd = ','.join(values1)
cmd = 'insert into ProbeSetData values %s' % cmd
db.execute(cmd)
cmd = ','.join(values2)
cmd = 'insert into ProbeSetXRef(ProbeSetFreezeId, ProbeSetId, DataId) values %s' % cmd
db.execute(cmd)
values1 = []
values2 = []
print(('Inserted ', kj, ' lines'))
print(('used ', time.time()-time0, ' seconds'))
line = fp.readline()
if len(values1) > 0:
cmd = ','.join(values1)
cmd = 'insert into ProbeSetData values %s' % cmd
db.execute(cmd)
cmd = ','.join(values2)
cmd = 'insert into ProbeSetXRef(ProbeSetFreezeId, ProbeSetId, DataId) values %s' % cmd
db.execute(cmd)
con.close()
|