forked from hakyimlab/QTL_to_PredictDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateGeneInformation.py.bak
136 lines (104 loc) · 3.25 KB
/
GenerateGeneInformation.py.bak
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
#/usr/bin/env python
import pickle
import gzip
GENE_ID = "gene_id"
GENE_NAME = "gene_name"
GENE_TYPE = "gene_type"
CHROMOSOME = "chromosome"
FEATURE = "feature"
START = "start"
END = "end"
STRAND = "strand"
CHROMOSOME_idx = 0
FEATURE_idx = 2
START_idx = 3
END_idx = 4
STRAND_idx = 6
KEY_VALUE_PAIRS = 8
def extract_gene_info(dictionary, key_value_pairs):
for x in key_value_pairs:
if GENE_ID in x:
idx = x.index(GENE_ID)
gene_id = x[idx+1]
gene_id = gene_id.replace('"', '')
gene_id = gene_id[0:15]
if GENE_NAME in x:
idx = x.index(GENE_NAME)
gene_name = x[idx+1]
gene_name = gene_name.replace('"', '')
if GENE_TYPE in x:
idx = x.index(GENE_TYPE)
gene_type = x[idx+1]
gene_type = gene_type.replace('"', '')
dictionary[GENE_ID] = gene_id
dictionary[GENE_NAME] = gene_name
dictionary[GENE_TYPE] = gene_type
return dictionary
def extract_geneID_and_genename(dictionary, key_value_pairs):
entry = None
for x in key_value_pairs:
if GENE_ID in x:
idx = x.index(GENE_ID)
key = x[idx+1]
key = key.replace('"', '')
key = key[0:15]
if GENE_NAME in x:
idx = x.index(GENE_NAME)
value = x[idx+1]
value = value.replace('"', '')
if key is not None and value is not None:
dictionary[key] = value
def generate_dictionary(gencode_file, key):
d = {}
_open = gzip.open if gencode_file.endswith(".gz") else open
with _open(gencode_file) as gencode:
for i, line in enumerate(gencode):
if "##" in line:
continue
comps = line.strip().split("\t")
key_value_pairs = comps[KEY_VALUE_PAIRS].split(";")
key_value_pairs = [x.split(" ") for x in key_value_pairs]
feature = comps[FEATURE_idx]
if feature == "gene":
dd = {}
dd = extract_gene_info(dd, key_value_pairs)
dd[CHROMOSOME] = comps[CHROMOSOME_idx]
dd[START] = comps[START_idx]
dd[END] = comps[END_idx]
dd[STRAND] = comps[STRAND_idx]
gene_id = dd[GENE_ID]
d[gene_id] = dd
if i == 1000:
print "Processed %s lines" % str(i)
break
return d
def save_pkl(obj, pkl_file):
with open(pkl_file, "wb") as ff:
pickle.dump(obj, ff)
def load_pkl(pkl_file):
with open(pkl_file, "rb") as ff:
d = pickle.load(ff)
return d
def run(args):
d = generate_dictionary(args.gencode_file, args.dictionary_key)
if args.save_as_binary:
if not args.output_file.endswith(".pkl"):
args.output_file = args.output_file + ".pkl"
save_pkl(d, args.output_file)
else:
with open(args.output_file, "w") as of:
header = [GENE_ID, GENE_NAME, GENE_TYPE, CHROMOSOME, START, END, STRAND]
header = "%s\n" % "\t".join(header)
of.write(header)
for key in d:
dd = d[key]
of.write("%s\n" % "\t".join([dd[GENE_ID], dd[GENE_NAME], dd[GENE_TYPE], dd[CHROMOSOME], dd[START], dd[END], dd[STRAND]]))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--gencode_file")
parser.add_argument("--output_file")
parser.add_argument("--dictionary_key", default="gene_id")
parser.add_argument("--save_as_binary", action="store_true")
args = parser.parse_args()
run(args)