-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsummarise_vgw.py
executable file
·181 lines (161 loc) · 7.46 KB
/
summarise_vgw.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
#! /usr/bin/env python
import subprocess
import os
import argparse
from Bio import SeqIO
import re
parser = argparse.ArgumentParser(description='Summarise vgw results using gmap')
parser.add_argument('genome', help='vgw output file')
parser.add_argument('transcripts', help='transcript sequences')
parser.add_argument('-o','--overwrite', action='store_true', help='overwrite gmap database')
parser.add_argument('-g','--gff3', nargs='?', default='vgw.gff', help='output gff3 filename, default (%(default)s)')
parser.add_argument('-s','--summary', nargs='?', default='vgw_summary.txt', help='output file name, default (%(default)s)')
args = parser.parse_args()
if not os.path.exists("gmapdb/"):
subprocess.call("mkdir gmapdb", shell=True)
args.overwrite = True
if args.overwrite or not os.path.exists("gmapdb/"+args.genome):
subprocess.call("gmap_build -d "+args.genome+" -D ./gmapdb "+args.genome, shell=True)
genomehash = dict()
genomeinfo = dict()
for record in SeqIO.parse(args.genome, 'fasta'):
genomehash[record.name] = record.seq
count = 0
for m in re.finditer("N"*500, str(record.seq)):
count += 1
genomeinfo[record.name] = count
transhash = dict()
for record in SeqIO.parse(args.transcripts, 'fasta'):
transhash[record.name] = record.seq
subprocess.call("gmap -d "+args.genome+" -D ./gmapdb -f 2 -t 10 "+args.transcripts+" > "+args.gff3, shell=True)
gene = ""
path = ""
mrna = ""
newid = ""
last = []
resdict = dict()
with open(args.gff3, 'r') as ins:
for l in ins:
if l.startswith('#'):
continue
data = l.rstrip().split("\t")
#print data
if data[2] == 'mRNA':
if len(last) > 1:
#print "Last exon!"
info = last[8].split(";")
coords = info[3].split(" ")
#print "Genome len: "+str(len(genomehash[gene]))
if last[6] == "-":
resdict[newid]['5prime'] = int(last[3]) - 1
else:
resdict[newid]['3prime'] = len(genomehash[last[0]]) - int(last[4])
#print "Trans len: "+str(len(transhash[gene]))
if coords[3] == "+":
extra = len(transhash[gene]) - int(coords[2])
#print extra
resdict[newid]['bp_notmapped_ext'] += extra
else:
resdict[newid]['bp_notmapped_ext'] += (int(coords[1])-1)
last = []
info = data[8].split(";")
gene = info[1].split("=")[1]
path = info[2].split(".")[-1]
mrna = info[0].split(".")[-1]
#print "Gene: %s \t Path: %s \t mRNA: %s" % (gene, path, mrna)
newid = gene+"@"+path+"@"+mrna
if newid not in resdict:
resdict[newid] = dict()
resdict[newid]['exons'] = 0
resdict[newid]['bp_notmapped_int'] = 0
resdict[newid]['bp_notmapped_ext'] = 0
resdict[newid]['introns_bridged'] = 0
resdict[newid]['introns'] = []
resdict[newid]['bridged'] = []
resdict[newid]['5prime'] = 0
resdict[newid]['3prime'] = 0
resdict[newid]['genome'] = data[0]
if data[2] == 'exon':
resdict[newid]['exons'] += 1
if 'exon1;' in data[8]:
#print "First exon!"
if data[6] == "+":
resdict[newid]['5prime'] = int(data[3]) - 1
else:
resdict[newid]['3prime'] = len(genomehash[data[0]]) - int(data[4])
info = data[8].split(";")
coords = info[3].split(" ")
if coords[3] == "+":
resdict[newid]['bp_notmapped_ext'] += (int(coords[1])-1)
else:
resdict[newid]['bp_notmapped_ext'] += (len(transhash[gene]) - int(coords[2]))
else:
previnfo = last[8].split(";")
prevcoords = previnfo[3].split(" ")
info = data[8].split(";")
coords = info[3].split(" ")
#print "Trans: %s - %s" % (prevcoords[2], coords[1])
if coords[3] == "+":
if int(prevcoords[2])+1 < int(coords[1]):
extra = int(coords[1]) - int(prevcoords[2]) - 1;
resdict[newid]['bp_notmapped_int'] += extra
else:
if int(prevcoords[1])-1 > int(coords[2]):
extra = int(prevcoords[1]) - int(coords[2]) - 1;
resdict[newid]['bp_notmapped_int'] += extra
#print "Genome: %s - %s" % (last[4], data[3])
length = 0
intronseq = ""
if data[6] == "+":
length = int(data[3]) - int(last[4])
intronseq = genomehash[data[0]][int(last[4]):int(data[3])]
else:
length = int(last[3]) - int(data[4])
intronseq = genomehash[data[0]][int(data[4]):int(last[3])]
resdict[newid]['introns'].append(length)
#print intronseq
if not "N"*500 in intronseq:
#print "BRIDGED"
resdict[newid]['introns_bridged'] += 1
resdict[newid]['bridged'].append(length)
last = data
if data[2] == 'CDS' and len(last) > 1:
#print "Last exon!"
info = last[8].split(";")
coords = info[3].split(" ")
#print "Genome len: "+str(len(genomehash[gene]))
if last[6] == "-":
resdict[newid]['5prime'] = int(last[3]) - 1
else:
resdict[newid]['3prime'] = len(genomehash[last[0]]) - int(last[4])
#print "Trans len: "+str(len(transhash[gene]))
if coords[3] == "+":
extra = len(transhash[gene]) - int(coords[2])
#print extra
resdict[newid]['bp_notmapped_ext'] += extra
else:
resdict[newid]['bp_notmapped_ext'] += (int(coords[1])-1)
last = []
ins.close()
with open(args.summary, 'w') as ins:
ins.write("Genome\tGene\tPath\tmRNA\tTranscript.length\tvgw.length\tvgw.minus500.length\tNo.Exons\t5prime.bp\t3prime.bp\tunmapped.internal.bp\tunmapped.external.bp\tmax.intron\tmin.intron\tmean.intron\tno.bridged\tmax.bridged\tmin.bridged\tmean.bridged\n")
for i in resdict:
names = i.split("@")
ins.write(resdict[i]['genome']+"\t")
ins.write("\t".join(names)+"\t")
ins.write("%s\t%s\t%s\t" % (len(transhash[names[0]]), len(genomehash[resdict[i]['genome']]), len(genomehash[resdict[i]['genome']]) - (500 * genomeinfo[resdict[i]['genome']])))
ins.write("%s\t%s\t%s\t%s\t%s\t" % (resdict[i]['exons'], resdict[i]['5prime'], resdict[i]['3prime'], resdict[i]['bp_notmapped_int'], resdict[i]['bp_notmapped_ext']))
if resdict[i]['exons'] > 1:
introns = resdict[i]['introns']
introns = sorted(introns)
ins.write("%s\t%s\t%s\t" % (max(introns), min(introns), sum(introns)/len(introns)))
else:
ins.write("0\t0\t0\t")
ins.write(str(resdict[i]['introns_bridged'])+"\t")
if resdict[i]['introns_bridged'] > 0:
bridged = resdict[i]['bridged']
bridged = sorted(bridged)
ins.write("%s\t%s\t%s\n" % (max(bridged), min(bridged), sum(bridged)/len(bridged)))
else:
ins.write("0\t0\t0\n")
ins.close()