-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetable.py
66 lines (59 loc) · 1.28 KB
/
genetable.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
#genetable.py
import sys
import string
import numpy as np
import collections as col
class Vars(object):
def __init__(self,fields):
self.chr=fields[0]
self.start=fields[1]
self.end=fields[2]
self.autoreg=fields[6]
self.length=float(fields[10])
self.gene=fields[11]
self.maf=float(fields[12])
self.type=fields[14]
f1=open(sys.argv[1],"r")
genes=set()
auto=set()
sct={}
nct={}
ct={}
maf=col.defaultdict(list)
length={}
glen={}
for line in f1:
v=Vars(line.rstrip().split("\t"))
genes.add(v.gene)
auto.add(v.autoreg)
if v.type=="ds":
sct[v.gene]=sct.get(v.gene,float(0))+float(1)
ct[v.gene]=ct.get(v.gene,float(0))+float(1)
if v.type=="dn":
nct[v.gene]=nct.get(v.gene,float(0))+float(1)
ct[v.gene]=ct.get(v.gene,float(0))+float(1)
maf[v.gene].append({v.chr+v.start+v.end+v.type:v.maf})
length[(v.gene,v.autoreg)]=v.length
f1.close()
for i in genes:
for j in auto:
try:
glen[i]+=float(length[(i,j)])
except KeyError:
glen[i]=glen.get(i,float(0))
try:
if sct[i]==0:
s=1
else:
s=sct[i]
except KeyError:
sct[i]=0; s=float(1)
try:
assert nct[i]
except KeyError:
nct[i]=0
if glen[i]==0:
g=float(1)
else:
g=glen[i]
print i,int(glen[i]),int(nct[i]),int(sct[i]),int(nct[i]+sct[i]),nct[i]/s,(nct[i]+sct[i])/g,np.median([k.values()[0] for k in maf[i]])