-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractGTExDatas.py
311 lines (246 loc) · 8.73 KB
/
extractGTExDatas.py
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
from xlrd import *
from xlwt import *
from xlutils.copy import *
import time
import numpy as np
import csv
import xlsxwriter as xlwriter
import openpyxl
import paths
def extractGene(fileName):
'''Extract SAMPID and GeneID'''
geneNameList = []
geneDescriptionList = []
sampleList = []
i = 0
file = open(fileName,'r')
for line in file:
print i
element = ''
j = 0
for c in line:
element += c
if i == 2:
if c == ' ':
sampleList.append(element)
element = ''
else:
if c == ' ':
if j==0:
geneNameList.append(element)
else:
geneDescriptionList.append(element)
break
element = ''
j+=1
i += 1
return sampleList, geneNameList, geneDescriptionList
def extractTranscript(fileName):
'''extract TranscriptID and chromosome and coord of Gene'''
transcriptIDList = []
geneSymbolList = []
chromosomesList = []
coordList = []
i = 0
file = open(fileName, 'r')
for line in file:
print i
element = ''
j = 0
for c in line:
element+=c
if c == ' ':
if j == 0:
transcriptIDList.append(element)
if j == 1:
geneSymbolList.append (element)
elif j == 2:
chromosomesList.append (element)
elif j == 3:
coordList.append(element)
break
element =''
j+=1
i+=1
return transcriptIDList, geneSymbolList, chromosomesList, coordList
def extractRPKMGene():
return
def cleanList(list, listType):
'''delete elements of list which not correspond to the type of the list'''
comparison = False
for element in list:
if listType == 'gene':
comparison = element[0:4] != 'GTEX'
elif listType == 'sample':
comparison = element[0:4] != 'ENSG'
if comparison: #pourquoi ne connait pas l'element Description???? (element juste avant ceux a garder
list.remove(element)
return
def writeSampleTable():
'''write table (excel) containing sample informations'''
sampleTable = open_workbook(sampleTableFileName)
samplePhenotypeTable = open_workbook(samplePhenotypeTableFileName)
sampleTableCopy = copy(sampleTable)
subjIDPhenList =[]
phenTitleList = []
firstTime = True
for row in samplePhenotypeTable.sheet_by_index(0)._cell_values:
if firstTime:
phenTitleList = row
firstTime = False
subjIDPhen = row [0]
subjIDPhenList.append(subjIDPhen)
sampleTableCopy.get_sheet(0).write(0,61,phenTitleList[0])
sampleTableCopy.get_sheet(0).write(0,62,phenTitleList[1])
sampleTableCopy.get_sheet(0).write(0,63,phenTitleList[2])
sampleTableCopy.get_sheet(0).write(0,64,phenTitleList[3])
j=0
for row in sampleTable.sheet_by_index(0)._cell_values:
print j
i=0
subjIDGen = ''
for c in row[0]:
subjIDGen += c
if c == '-':
i+=1
if i == 2:
subjIDGen = subjIDGen[:-1]
try:
rowContent = samplePhenotypeTable.sheet_by_index(0)._cell_values[subjIDPhenList.index(subjIDGen)]
except ValueError:
print 'SUBJID : ', subjIDGen, ' not find in the phenotype table'
sampleTableCopy.get_sheet(0).write(j,61,rowContent[0])
sampleTableCopy.get_sheet(0).write(j,62,rowContent[1])
sampleTableCopy.get_sheet(0).write(j,63,rowContent[2])
sampleTableCopy.get_sheet(0).write(j,64,rowContent[3])
break
j+=1
sampleTableCopy.save(sampleTableFileName)
return
def writeGeneTable(nameList, descriptionList):
'''write table (excel) which contain Gene informations'''
geneTable = open_workbook(geneTableFileName)
geneTableCopy = copy(geneTable)
i=0
while (i < len(nameList)):
print i
geneTableCopy.get_sheet(0).write(i+1, 0, nameList[i])
geneTableCopy.get_sheet(0).write(i+1, 1, descriptionList[i])
i+=1
geneTableCopy.save(geneTableFileNameTest)
return
def updateGeneTable(geneList, chrList, coordList):
'''add information in the Gene table'''
geneTable = open_workbook(geneTableFileName)
geneTableCopy = copy(geneTable)
rowIndex = 0
for row in geneTable.sheet_by_index(0)._cell_values:
print rowIndex
try:
listIndex = geneList.index(row[0])
geneTableCopy.get_sheet(0).write(rowIndex, 2, chrList[listIndex])
geneTableCopy.get_sheet(0).write(rowIndex, 3, coordList[listIndex])
except ValueError:
print 'GeneSymbol: ', row[0], ' not find in the gene table'
rowIndex +=1
geneTableCopy.save(geneTableFileNameTest)
return
def writeTranscriptTable(transcriptList, geneList):
transcriptTable = openpyxl.Workbook()
transcriptTableSheet = transcriptTable.get_active_sheet()
rowIndex = 0
while(rowIndex < len(transcriptList)):
print 'rowIndex: ', rowIndex
transcriptTableSheet.cell(row = rowIndex, column = 0).value = transcriptList[rowIndex]
transcriptTableSheet.cell(row = rowIndex, column = 1).value = geneList[rowIndex]
rowIndex +=1
transcriptTable.save(transcriptTableFileName)
return
def writeRPKMGeneTable():
csvFileReader = open(geneRPKMFileNameTail, 'rb')
geneRKMTable = csv.reader(csvFileReader, delimiter = ' ')
csvFileWriter = open(geneRPKMTableFileName, 'wb')
outputWriter = csv.writer(csvFileWriter, quotechar = '"', quoting = csv.QUOTE_MINIMAL)
outputWriter.writerow(["RPKMID", "GeneID", "SAMPID", "RPKMValue"])
rowIndex = 0
newRowIndex = 0
sampList = []
for row in geneRKMTable:
print 'rowIndex: ', rowIndex
colIndex = 0
for element in row[2:]:
if (rowIndex==0):
sampList.append(element)
else:
outputWriter.writerow([newRowIndex, row[0], sampList[colIndex], element ])
newRowIndex +=1
colIndex +=1
rowIndex+=1
return
def writeRPKMTranscriptTable():
csvFileReader = open(transcriptRPKMFileName, 'rb')
geneRKMTable = csv.reader(csvFileReader, delimiter = ' ')
csvFileWriter = open(transcriptRPKMTableFileNameExterne, 'wb')
outputWriter = csv.writer(csvFileWriter, quotechar = '"', quoting = csv.QUOTE_MINIMAL)
outputWriter.writerow(["RPKMID", "TranscriptID", "SAMPID", "RPKMValue"])
rowIndex = 0
newRowIndex = 0
sampList = []
for row in geneRKMTable:
print 'rowIndex: ', rowIndex
colIndex = 0
for element in row[4:]:
if (rowIndex==0):
sampList.append(element)
else:
outputWriter.writerow([newRowIndex, row[0], sampList[colIndex], element ])
newRowIndex +=1
colIndex +=1
rowIndex+=1
return
def removeLast(string):
return string[:-1]
def main():
'''main function'''
startTime = time.time()
#writeSampleTable()
# writeRPKMTranscriptTable()
endTime = time.time()-startTime
print 'endTime : ', endTime
return
def processARNTable():
writeSampleTable()
return
def processGeneTable():
#make table
sampleList, geneNameList, geneDescriptionList = extractGene(geneRPKMFileName)
geneNameList = geneNameList[1:]
cleanList(sampleList, 'sample')
cleanList(sampleList, 'sample')
writeGeneTable(geneNameList,geneDescriptionList)
print 'sampleList: ', sampleList, ' length: ', len(sampleList)
print 'nameList: ', geneNameList, ' length: ', len(geneNameList)
print 'descriptionList', geneDescriptionList, 'length: ', len(geneDescriptionList)
#update table
transcript, gene, chr, coord = extractTranscript(transcriptRPKMFileNameTest)
gene = gene[1:]
chr = chr[1:]
coord = coord[1:]
gene = map(removeLast, gene)
chr = map(removeLast, chr) # a faire plus tard
coord = map(removeLast, coord)
# print 'gene : ', gene
# print 'chr : ', chr
# print 'coord : ', coord
updateGeneTable(gene, chr, coord)
return
def processTranscriptTable():
transcript, gene, chr, coord = extractTranscript(transcriptRPKMFileName)
gene = map(removeLast, gene)
transcript = map(removeLast, transcript)
writeTranscriptTable(transcript, gene)
return
def processRPKMGeneTable():
writeRPKMGeneTable()
return
main()