forked from Oshlack/Lace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTViewer.py
executable file
·145 lines (114 loc) · 4.03 KB
/
STViewer.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
#Author: Anthony Hawkins
#Visualise a given gene in your super transcript
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
import os
from matplotlib.pyplot import cm
#import seaborn as sns
from matplotlib import gridspec
font = {'style' : 'oblique',
'size' : 16}
##################################################
###### Visualise blocks in SuperTranscript #######
##################################################
def Visualise(gene_file):
print("Producing Super Files\n")
gene= ""
if("/" in gene_file):
gene = (gene_file.split("/")[-1]).rstrip(".fasta")
else:
gene= gene_file.split(".fasta")
gene_string=""
#Find gene in genome
f= open("SuperDuper.fasta","r")
for line in f:
if(gene in line):
gene_string=next(f)
break
f.close()
fs= open("Super.fasta","w")
fs.write(">" + gene + "\n")
fs.write(gene_string)
fs.close()
#Match transcripts to super transcript
print("Producing match to super transcript")
BLAT_command = "blat Super.fasta %s supercomp.psl" %(gene_file)
os.system(BLAT_command)
#First read in BLAT output:
Header_names = ['match','mismatch','rep.','N\'s','Q gap count','Q gap bases','T gap count','T gap bases','strand','Q name','Q size','Q start','Q end','T name','T size','T start','T end','block count','blocksizes','qStarts','tStarts']
vData = pd.read_table('supercomp.psl',sep='\t',header = None,names=Header_names,skiprows=5)
#Read in GFF file
gff_data = pd.read_table('SuperDuper.gff',sep='\t',header=None)
#Subset GFF just for gene
gff_data = gff_data.loc[gff_data.ix[:,0] == gene,:]
#Make list of transcripts
transcripts = np.unique(list(vData['Q name']))
#First pass attempt, make a stacked bar chart of all the nodes in C in order, with size dependent on length of string, iterate through coloirs
#Get Super Transcript Length
ST_length = int(vData.iloc[0,14])
gs = gridspec.GridSpec(2,1,height_ratios=[4,1])
ax1=plt.subplot(gs[0])
accum = 0
plt.barh(len(transcripts),ST_length,color='#ffc024',left=0)
plot_dict = {}
col_dict = {}
labs = []
col2=iter(cm.terrain(np.linspace(0,1,len(transcripts))))
for i,key in enumerate(transcripts):
plot_dict[key] = i
col_dict[key] = next(col2)
lab =""
labs.append(lab)
#Empty vector to store coverage
coverage = np.zeros(ST_length)
for irow in range(0,len(vData)):
#Get blocks
block_sizes = (vData.iloc[irow,18]).rstrip(',').split(',')
tStarts = (vData.iloc[irow,20]).rstrip(',').split(',')
qName = vData.iloc[irow,9]
#Print transcripts blocks
for block in range(0,len(block_sizes)):
si = int(block_sizes[block])
left = int(tStarts[block])
plt.barh(plot_dict[qName],si,color=col_dict[qName],left=left,alpha=0.7)
#Sum up coverage
for i in range(left,left+si):
coverage[i] = coverage[i] + 1
plt.setp(ax1.get_xticklabels(), visible=False)
ind=np.arange(len(transcripts)+1)
width=0.8
labs.append('Super')
plt.yticks(ind + width/2.,labs,fontsize="medium",fontweight="semibold")
plt.ylabel('Transcripts',fontdict=font)
#################################
# Coverage Histogram Underneath #
#################################
#For a super block, how many transcripts cover that area....
ax2=plt.subplot(gs[1],sharex=ax1)
x = np.arange(ST_length)
plt.bar(x,coverage,color='slategray',alpha=0.7)
plt.xlim([0,ST_length+1])
#Fix x-axes
ax2.set_yticklabels([])
plt.xlabel('Bases',fontdict=font)
plt.ylabel('Coverage',fontdict=font)
plt.savefig('GeneView.pdf')
plt.show()
if __name__=='__main__':
if(len(sys.argv) != 2):
print("Visualisation function requires one argument\n")
print("The gene whose super transcripts you wish to visualise\n")
else:
#Check all the super files are there
if(not os.path.isfile(sys.argv[1])):
print("No fasta file for gene/cluster of interest\n")
sys.exit()
if(not os.path.isfile("SuperDuper.fasta")):
print("No fasta file for SuperTranscript\n")
sys.exit()
if(not os.path.isfile("SuperDuper.gff")):
print("No annotation file for SuperTranscript\n")
sys.exit()
Visualise(sys.argv[1])