-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerging_annotations.py
More file actions
144 lines (129 loc) · 4.02 KB
/
merging_annotations.py
File metadata and controls
144 lines (129 loc) · 4.02 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
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
'''
This script merges the individual .gff results from all tools into one
'''
#!/usr/bin/env python
import subprocess,os,sys
'''
Function to merge the .gffs
'''
def merge_gff(input_directory_path,output_directory_path):
files=os.listdir(input_directory_path)
#Getting a list od unique sample names
sample_list=[]
for item in files:
sample_name=item.split("_")
if sample_name[0] not in sample_list:
sample_list.append(sample_name[0])
#Merging the .gffs of each sample
for sample in sample_list:
sample_dict={}
#Reading crispr results
sample_crispr=input_directory_path+"/"+sample+"_crispr.gff"
if os.path.isfile(sample_crispr)==True:
with open(sample_crispr,"r") as crisp:
for line in crisp:
line=line.rstrip()
col=line.split("\t")
crisp_key=col[0]+","+col[3]+","+col[4]
crisp_def="CRISPR array:"+col[-1]
if crisp_key not in sample_dict.keys():
sample_dict[crisp_key]=[crisp_def]
else:
sample_dict[crisp_key].append(crisp_def)
#Reading TM results
sample_tm=input_directory_path+"/"+sample+"_tmhmm.gff"
if os.path.isfile(sample_tm)==True:
with open(sample_tm,"r") as tm:
for line in tm:
line=line.rstrip()
col=line.split("\t")
tm_key=col[0]+","+col[3]+","+col[4]
tm_def=col[-1]
if tm_key not in sample_dict.keys():
sample_dict[tm_key]=[tm_def]
else:
sample_dict[tm_key].append(tm_def)
#Reading SignalP results
sample_sp=input_directory_path+"/"+sample+"_union_signalp.gff"
if os.path.isfile(sample_sp)==True:
with open(sample_sp,"r") as sp:
for line in sp:
line=line.rstrip()
col=line.split("\t")
sp_key=col[0]+","+col[3]+","+col[4]
sp_def=col[-1]
if sp_key not in sample_dict.keys():
sample_dict[sp_key]=[sp_def]
else:
sample_dict[sp_key].append(sp_def)
#Reading VFDB results
sample_vf=input_directory_path+"/"+sample+"_union_vf.gff"
if os.path.isfile(sample_vf)==True:
with open(sample_vf,"r") as vf:
for line in vf:
line=line.rstrip()
col=line.split("\t")
vf_key=col[0]+","+col[3]+","+col[4]
vf_def="Virulence Factor:"+col[-1]
if vf_key not in sample_dict.keys():
sample_dict[vf_key]=[vf_def]
else:
sample_dict[vf_key].append(vf_def)
#Reading CARD results
sample_ca=input_directory_path+"/"+sample+"_union_card.gff"
if os.path.isfile(sample_ca)==True:
with open(sample_ca,"r") as ca:
for line in ca:
line=line.rstrip()
col=line.split("\t")
ca_key=col[0]+","+col[3]+","+col[4]
ca_def="AMR profile:"+col[-1]
if ca_key not in sample_dict.keys():
sample_dict[ca_key]=[ca_def]
else:
sample_dict[ca_key].append(ca_def)
#Reading Eggnog resulst
sample_eg=input_directory_path+"/"+sample+"_union_eg.gff"
if os.path.isfile(sample_eg)==True:
with open(sample_eg,"r") as eg:
for line in eg:
line=line.rstrip()
col=line.split("\t")
eg_key=col[0]+","+col[3]+","+col[4]
eg_def="Eggnog:"+col[-1]
if eg_key not in sample_dict.keys():
sample_dict[eg_key]=[eg_def]
else:
sample_dict[eg_key].append(eg_def)
'''
#Reading Operon results
sample_op=input_directory_path+"/"+sample+"_union_op.gff"
if os.path.isfile(sample_op)==True:
with open(sample_op,"r") as op:
for line in op:
line=line.rstrip()
col=line.split("\t")
op_key=col[0]+","+col[3]+","+col[4]
op_def=col[-1]
if op_key not in sample_dict.keys():
sample_dict[op_key]=[op_def]
else:
sample_dict[op_key].append(op_def)
'''
#Writing final merged .gff
output_file=output_directory_path+"/"+sample+".gff"
for keys in sample_dict.keys():
col=keys.split(",")
node=col[0]
start=col[1]
stop=col[2]
funct_list=sample_dict[keys]
function=";".join(funct_list)
with open(output_file,"a+") as out:
out.write(node+"\t"+"."+"\t"+"."+"\t"+start+"\t"+stop+"\t"+"."+"\t"+"."+"\t"+"."+"\t"+function+"\n")
def main():
input_path=sys.argv[1]
output_path=sys.argv[2]
merge_gff(input_path,output_path)
if __name__ == "__main__":
main()