forked from akahanaton/SpliceGrapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_as_in_tracking.py
executable file
·204 lines (191 loc) · 10.1 KB
/
get_as_in_tracking.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/home/gmswenm/python/bin/python3
from __future__ import print_function
from Bio import SeqIO
from Bio.Seq import Seq
from BCBio import GFF
from collections import defaultdict
import argparse
import sys
import pprint
import re
import copy
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def nested_dict(n, type):
if n == 1:
return defaultdict(type)
else:
return defaultdict(lambda: nested_dict(n-1, type))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Concat sequence from UCSC aligment')
parser.add_argument('trackingFile1', type=str, help='tracking file generated by cuffcompare, using species 1 as reference')
parser.add_argument('trackingFile2', type=str, help='tracking file generated by cuffcompare, using species 2 as reference')
parser.add_argument('mapping1', type=str, help='reference gffcompare file')
parser.add_argument('mapping2', type=str, help='query gffcompare file')
#--------------------------------------------------
# parser.add_argument('-c', dest='cleanCodon', type=str, default='Y', choices=['Y','N'], help='clean stop codon: Y/N')
# parser.add_argument('-r', dest='cleanByRef', type=str, default=None, help='aligment format')
#--------------------------------------------------
args = parser.parse_args()
tag1=args.mapping1.split('.')[0]
tag2=args.mapping2.split('.')[0]
trackingInfo = nested_dict(3,str)
tracking1Ref2Query = nested_dict(2,str)
tracking1Query2Ref = nested_dict(2,str)
tracking1RefCom = {}
tracking1QueryCom = {}
with open(args.trackingFile1) as f:
for line in f:
arr = line.rstrip().split("\t")
if arr[2] != '-' :
refInfo = arr[2].split('|')
queryInfo = re.split(':|\|',arr[4])
trackingInfo[tag1][refInfo[1]][queryInfo[2]] = arr[3]
#--------------------------------------------------
# if arr[3] == '=':
# tracking1RefCom[refInfo[1]] = 1
# tracking1QueryCom[queryInfo[2]] = 1
# if refInfo[1] not in tracking1RefCom:
# tracking1Ref2Query[refInfo[1]][arr[3]] = queryInfo[2]
# tracking1Query2Ref[queryInfo[2]][arr[3]] = refInfo[1]
# else:
# if refInfo[1] in tracking1Ref2Query:
# tracking1Ref2Query.pop[refInfo[1],None]
# tracking1Query2Ref.pop[queryInfo[1],None]
#--------------------------------------------------
f.close()
tracking2Ref2Query = nested_dict(2,str)
tracking2Query2Ref = nested_dict(2,str)
tracking2RefCom = {}
tracking2QueryCom = {}
with open(args.trackingFile2) as f:
for line in f:
arr = line.rstrip().split("\t")
refInfo = arr[2].split('|')
queryInfo = re.split(':|\|',arr[4])
if arr[2] != '-' :
trackingInfo[tag2][refInfo[1]][queryInfo[2]] = arr[3]
#--------------------------------------------------
# if arr[3] == '=':
# tracking2RefCom[refInfo[1]] = 1
# tracking2QueryCom[queryInfo[2]] = 1
# if refInfo[1] not in tracking2RefCom:
# tracking2Ref2Query[refInfo[1]][arr[3]] = queryInfo[2]
# tracking2Query2Ref[queryInfo[2]][arr[3]] = refInfo[1]
# else:
# if refInfo[1] in tracking2Ref2Query:
# tracking2Ref2Query.pop[refInfo[1],None]
# tracking2Query2Ref.pop[queryInfo[1],None]
#--------------------------------------------------
f.close()
gene2trans = nested_dict(2,str)
with open(args.mapping1) as f:
for line in f:
arr = line.rstrip().split("\t")
if arr[2] == 'transcript' :
refInfo = arr[8].split(' ')
refInfo[1] = refInfo[1].rstrip(";").replace(r'"',"")
refInfo[5] = refInfo[5].rstrip(";").replace(r'"',"")
if refInfo[5] in gene2trans:
gene2trans[refInfo[5]][tag1].append(refInfo[1])
else:
gene2trans[refInfo[5]][tag1] = []
gene2trans[refInfo[5]][tag2] = []
gene2trans[refInfo[5]][tag1].append(refInfo[1])
f.close()
with open(args.mapping2) as f:
for line in f:
arr = line.rstrip().split("\t")
if arr[2] == 'transcript' :
queryInfo = arr[8].split(' ')
queryInfo[1] = queryInfo[1].rstrip(";").replace(r'"',"")
queryInfo[5] = queryInfo[5].rstrip(";").replace(r'"',"")
if queryInfo[5] in gene2trans:
gene2trans[queryInfo[5]][tag2].append(queryInfo[1])
else:
gene2trans[queryInfo[5]][tag2] = []
gene2trans[queryInfo[5]][tag2].append(queryInfo[1])
f.close()
for geneID in gene2trans.keys():
print("%s" % geneID)
print(" %s" % tag1)
for transID in gene2trans[geneID][tag1]:
if transID in trackingInfo[tag1]:
print(" %s\t%s" % (transID,' '.join([b + "-" + a for a,b in zip(trackingInfo[tag1][transID].keys(), trackingInfo[tag1][transID].values())])))
#--------------------------------------------------
# trackingInfo[tag1].pop(transID,None)
#--------------------------------------------------
else:
print(" %s" % transID)
print(" %s" % tag2)
for transID in gene2trans[geneID][tag2]:
if transID in trackingInfo[tag2]:
print(" %s\t%s" % (transID,' '.join([b + "-" + a for a,b in zip(trackingInfo[tag2][transID].keys(), trackingInfo[tag2][transID].values())])))
#--------------------------------------------------
# trackingInfo[tag2].pop(transID,None)
#--------------------------------------------------
else:
print(" %s" % transID)
#--------------------------------------------------
# print(len(trackingInfo[tag2].keys()))
#--------------------------------------------------
#--------------------------------------------------
# sys.exit()
#--------------------------------------------------
#--------------------------------------------------
# inHandle = open(args.refGff)
# refGffInfo = {}
# for chr in GFF.parse(inHandle):
# for i in range(0,len(chr.features)): # transcripts
# transID=chr.features[i].id
# refGffInfo[chr.features[i].id]=chr.id
#--------------------------------------------------
#--------------------------------------------------
# if transID not in tracking1Ref2Query and transID not in tracking2Query2Ref :
# if transID in refMapping:
# print("%s\tU\t%s\t%s\t%s\t%s" % (args.trackingFile1,transID, chr.id, refMapping[transID]['id'], refMapping[transID]['idFull']))
# else:
# print("%s\tU\t%s\t%s" % (args.trackingFile1,transID, chr.id))
# elif transID not in tracking1RefCom and transID not in tracking2QueryCom :
# if transID in refMapping:
# print("%s\t%s\t%s\t%s\t%s\t%s" % (args.trackingFile1, '.'.join(tracking1Ref2Query[transID].keys()), transID, chr.id, refMapping[transID]['id'], refMapping[transID]['idFull'])),
# else:
# print("%s\t%s\t%s\t%s" % (args.trackingFile1,'.'.join(tracking1Ref2Query[transID].keys()),transID, chr.id))
#--------------------------------------------------
#--------------------------------------------------
# elif transID not in tracking1RefCom:
#--------------------------------------------------
#--------------------------------------------------
# for j in range(0,len(chr.features[i].sub_features)): # exon
# print(chr.features[i].sub_features[j])
#--------------------------------------------------
#--------------------------------------------------
# inHandle.close()
#--------------------------------------------------
#--------------------------------------------------
# inHandle = open(args.queryGff)
# queryGffInfo = {}
# for chr in GFF.parse(inHandle):
# for i in range(0,len(chr.features)): # transcripts
# transID=chr.features[i].id
# queryGffInfo[chr.features[i].id]=chr.id
#--------------------------------------------------
#--------------------------------------------------
# if transID not in tracking2Ref2Query and transID not in tracking1Query2Ref :
# if transID in queryMapping:
# print("%s\tU\t%s\t%s\t%s\t%s" % (args.trackingFile2, transID, chr.id, queryMapping[transID]['id'], queryMapping[transID]['idFull']))
# else:
# print("%s\tU\t%s\t%s" % (args.trackingFile2,transID, chr.id))
# elif transID not in tracking2RefCom and transID not in tracking1QueryCom :
# if transID in queryMapping:
# print("%s\t%s\t%s\t%s\t%s\t%s" % (args.trackingFile1, '.'.join(tracking2Ref2Query[transID].keys()), transID, chr.id, refMapping[transID]['id'], refMapping[transID]['idFull'])),
# else:
# print("%s\t%s\t%s\t%s" % (args.trackingFile1,'.'.join(tracking2Ref2Query[transID].keys()),transID, chr.id))
#--------------------------------------------------
#--------------------------------------------------
# for j in range(0,len(chr.features[i].sub_features)): # exon
# print(chr.features[i].sub_features[j])
#--------------------------------------------------
#--------------------------------------------------
# inHandle.close()
#--------------------------------------------------