-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathensg2hugo.py
More file actions
executable file
·91 lines (81 loc) · 4.53 KB
/
ensg2hugo.py
File metadata and controls
executable file
·91 lines (81 loc) · 4.53 KB
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
#!/usr/bin/python
import argparse
import sys
import re
import csv
parser = argparse.ArgumentParser()
#parser.add_argument('-i', metavar='in-file', type=argparse.FileType('rt'))
parser.add_argument("input", nargs='?',type=argparse.FileType('r'), default=sys.stdin, help= 'your csv file with 9 columns')
#parser.add_argument("output", nargs='?',type=argparse.FileType('w'),default=sys.stdout, help='your output file name')
parser.add_argument("-f","--columns",nargs='?', type=int, default=1,help='the column which will be gene_name')
args = parser.parse_args()
file = open('/Users/bocheng/PycharmProjects/pythonProject1/Homo_sapiens.GRCh37.75.gtf', "r")
GRCh3775_dic = {}
for line in file:
match_obj = re.search(r'(.*)\t(.*)\t(.*)\t(\d+)\t(\d+)\t(.*)\tgene_id\s\"(.*?)\"(.*)gene_name\s"(.*?)"\;', line)
#if match_obj.group(3) == 'gene':
gene_name = match_obj.group(9)
gene_id = match_obj.group(7)
GRCh3775_dic[gene_id] = gene_name
#else:
# continue
#print(GRCh3775_dic)
dic_list = []
#file1 = open('/Users/bocheng/PycharmProjects/pythonProject1/qqq.csv', 'r')
file1 = args.input
for line in file1:
match_obj = re.search(r'"(.*?)","(.*?)","(.*?)",(.*?)\,(.*)', line)
if match_obj.group(2) != 'gene_id': #skip headline of csv files
match_obj_geneid = re.search(r'(.*)\.(\d+)', match_obj.group(2))
if match_obj_geneid.group(1) in GRCh3775_dic:
dic={}
if args.columns == 1:
dic = {'':GRCh3775_dic[match_obj_geneid.group(1)] , 'gene_id': match_obj.group(2),
'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr': match_obj.group(5),'t':'','P.Value':'','adj.P.Val':''}
elif args.columns ==2:
dic = {'': match_obj.group(1), 'gene_id': GRCh3775_dic[match_obj_geneid.group(1)],'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr': match_obj.group(5),'t':'','P.Value':'','adj.P.Val':''}
elif args.columns ==3:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(2),
'gene_type': GRCh3775_dic[match_obj_geneid.group(1)],
'logFC': match_obj.group(4), 'AveExpr': match_obj.group(5),'t':'','P.Value':'','adj.P.Val':''}
elif args.columns ==4:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(2),
'gene_type': match_obj.group(3),
'logFC': GRCh3775_dic[match_obj_geneid.group(1)], 'AveExpr': match_obj.group(5),'t':'','P.Value':'','adj.P.Val':''}
elif args.columns == 5:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(2),
'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr': GRCh3775_dic[match_obj_geneid.group(1)],'t':'','P.Value':'','adj.P.Val':''}
elif args.columns == 6:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(2),
'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr': match_obj.group(5), 't': GRCh3775_dic[match_obj_geneid.group(1)],
'P.Value': '', 'adj.P.Val': ''}
elif args.columns ==7:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(1),
'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr':match_obj.group(5), 't': '',
'P.Value': GRCh3775_dic[match_obj_geneid.group(1)], 'adj.P.Val': ''}
elif args.columns ==8:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(1),
'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr':match_obj.group(5), 't': '',
'P.Value': '', 'adj.P.Val': GRCh3775_dic[match_obj_geneid.group(1)]}
else:
dic = {'': match_obj.group(1), 'gene_id': match_obj.group(2)+ 'NA', 'gene_type': match_obj.group(3),
'logFC': match_obj.group(4), 'AveExpr': match_obj.group(5),'t':'','P.Value':'','adj.P.Val':''}
dic_list.append(dic)
else:
continue
#print(dic_list)
#import csv
with open('output.hugo.csv', mode='w') as csv_file:
fieldnames = ['', 'gene_id', 'gene_type','logFC','AveExpr','t','P.Value','adj.P.Val']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
n = 0
writer.writeheader()
for n in range(0,len(dic_list)):
writer.writerow(dic_list[n])
n +=1