This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetRNASeqData.py
146 lines (116 loc) · 4.3 KB
/
getRNASeqData.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
def RNASeqwithGene(fileName, review=True):
geneList = dict()
organList = list()
RNASeqFile = open(fileName, "r")
for _ in range(2): assert RNASeqFile.readline()
for organ in RNASeqFile.readline().split("\t"):
assert organ not in geneList
organList.append(organ)
while True:
line = RNASeqFile.readline()
if not line: break
line = line.split("\t")
gene = line[0]
assert gene not in geneList
geneList[gene] = dict()
for i, val_i in enumerate(line):
if i == 0: continue
if i == 1:
geneList[gene][organList[i]] = val_i
continue
if review and float(val_i) == 0: continue
geneList[gene][organList[i]] = float(val_i)
RNASeqFile.close()
if review:
geneName = list(geneList.keys())
for gene in geneName:
if len(geneList[gene]) == 1:
del(geneList[gene])
return geneList
def RNASeqwithOrgan(fileName, review=True):
organList = dict()
organName = list()
RNASeqFile = open(fileName, "r")
for _ in range(2): assert RNASeqFile.readline()
for organ in RNASeqFile.readline().split("\t")[2:]:
assert organ not in organList
organList[organ] = dict()
organName.append(organ)
while True:
line = RNASeqFile.readline()
if not line: break
line = line.split("\t")
gene = line[0]
for i, val_i in enumerate(line[2:]):
assert gene not in organList[organName[i]]
if review and float(val_i) == 0: continue
organList[organName[i]][gene] = float(val_i)
RNASeqFile.close()
if review:
for organ in organName:
if len(organList[organ]) == 0: del(organList[organ])
return organList
def RNASeqOrganName(fileName):
organList = dict()
RNASeqFile = open(fileName, "r")
for _ in range(2): assert RNASeqFile.readline()
for i, ch_i in enumerate(RNASeqFile.readline().split("\t")[2:]):
assert ch_i not in organList
organList[ch_i] = i
RNASeqFile.close()
return organList
def RNASeqGeneName(fileName):
geneList = dict()
RNASeqFile = open(fileName, "r")
for _ in range(2): assert RNASeqFile.readline()
seq = 0
while True:
line = RNASeqFile.readline()
if not line: break
gene = line.split("\t")[:2]
gene = str(gene[0]) + "+" + str(gene[1])
try: assert gene not in geneList
except:
print(gene, geneList[gene], seq)
seq += 1
geneList[gene] = seq
seq += 1
RNASeqFile.close()
return geneList
def selectedGene(fileName, geneNum):
geneList = dict()
organList = list()
RNASeqFile = open(fileName, "r")
for _ in range(2): assert RNASeqFile.readline()
for organ in RNASeqFile.readline().split("\t"):
organList.append(organ)
for _ in range(geneNum, 0, -1): assert RNASeqFile.readline()
for i, val_i in enumerate(RNASeqFile.readline().split("\t")):
if i<2: geneList[organList[i]] = val_i
else: geneList[organList[i]] = float(val_i)
RNASeqFile.close()
return geneList
def selectedOrgan(fileName, organNum):
organList = dict()
RNASeqFile = open(fileName, "r")
for _ in range(2+1): assert RNASeqFile.readline()
while True:
line = RNASeqFile.readline()
if not line: break
line = line.split("\t")
organList[line[0]] = float(line[organNum+2])
RNASeqFile.close()
return organList
def selectedGeneOrgan(fileName, geneNum, organNum):
with open(fileName, "r") as RNASeqFile:
for _ in range(2+1): assert RNASeqFile.readline()
for _ in range(geneNum, 0, -1): assert RNASeqFile.readline()
return float(RNASeqFile.readline().split("\t")[2+organNum])
if __name__ == "__main__":
#name = "../GTEx/RNA-Seq/GTEx_Analysis_2016-01-15_v7_RNASeQCv1.1.8_gene_tpm.gct"
name = "../GTEx/RNA-Seq/GTEx_Analysis_2016-01-15_v7_STARv2.4.2a_junctions.gct"
organList = RNASeqOrganName(name); print(len(organList));
geneList = RNASeqGeneName(name); print(len(geneList));
selGene = selectedGene(name, 1234); print(len(selGene));
selOrgan = selectedOrgan(name, 33); print(len(selOrgan));
selGeneOrgan = selectedGeneOrgan(name, 1234, 33); print(selGeneOrgan);