annotate ProteinInteractions_v2.py @ 17:41286262976c draft

Uploaded
author bornea
date Fri, 29 Jan 2016 12:53:09 -0500
parents fd27366b9faf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
fd27366b9faf Uploaded
bornea
parents:
diff changeset
1 ################################################################################
fd27366b9faf Uploaded
bornea
parents:
diff changeset
2 # This program will read in a SAINT 'list.txt' file and the interactions from
fd27366b9faf Uploaded
bornea
parents:
diff changeset
3 # the consensus path db database and return all the interactions that we saw in
fd27366b9faf Uploaded
bornea
parents:
diff changeset
4 # our experiment in a format suitable for cytoscape. This allows us to filter
fd27366b9faf Uploaded
bornea
parents:
diff changeset
5 # before getting PPIs so that it doesn't affect our SAINT score or include
fd27366b9faf Uploaded
bornea
parents:
diff changeset
6 # interactions that don't score well
fd27366b9faf Uploaded
bornea
parents:
diff changeset
7 ################################################################################
fd27366b9faf Uploaded
bornea
parents:
diff changeset
8 # Copyright (C) Brent Kuenzi.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
9 # Permission is granted to copy, distribute and/or modify this document
fd27366b9faf Uploaded
bornea
parents:
diff changeset
10 # under the terms of the GNU Free Documentation License, Version 1.3
fd27366b9faf Uploaded
bornea
parents:
diff changeset
11 # or any later version published by the Free Software Foundation;
fd27366b9faf Uploaded
bornea
parents:
diff changeset
12 # with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
13 # A copy of the license is included in the section entitled "GNU
fd27366b9faf Uploaded
bornea
parents:
diff changeset
14 # Free Documentation License".
fd27366b9faf Uploaded
bornea
parents:
diff changeset
15 ################################################################################
fd27366b9faf Uploaded
bornea
parents:
diff changeset
16 ## REQUIRED INPUT ##
fd27366b9faf Uploaded
bornea
parents:
diff changeset
17
fd27366b9faf Uploaded
bornea
parents:
diff changeset
18 # 1) listfile: SAINTexpress output
fd27366b9faf Uploaded
bornea
parents:
diff changeset
19 # 2) SAINT_cutoff: Saint score cutoff for import (between 0 and 1)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
20 # 3) Int_conf: Confidence of PPI from CPDB to include
fd27366b9faf Uploaded
bornea
parents:
diff changeset
21 # - low: no filtering
fd27366b9faf Uploaded
bornea
parents:
diff changeset
22 # - medium: >0.5
fd27366b9faf Uploaded
bornea
parents:
diff changeset
23 # - high: >0.7
fd27366b9faf Uploaded
bornea
parents:
diff changeset
24 # - very high: >0.9
fd27366b9faf Uploaded
bornea
parents:
diff changeset
25 # 4) Species: Human, Yeast, or Mouse
fd27366b9faf Uploaded
bornea
parents:
diff changeset
26 ################################################################################
fd27366b9faf Uploaded
bornea
parents:
diff changeset
27
fd27366b9faf Uploaded
bornea
parents:
diff changeset
28
fd27366b9faf Uploaded
bornea
parents:
diff changeset
29 import urllib2
fd27366b9faf Uploaded
bornea
parents:
diff changeset
30 import itertools
fd27366b9faf Uploaded
bornea
parents:
diff changeset
31 import sys
fd27366b9faf Uploaded
bornea
parents:
diff changeset
32 import os
fd27366b9faf Uploaded
bornea
parents:
diff changeset
33
fd27366b9faf Uploaded
bornea
parents:
diff changeset
34
fd27366b9faf Uploaded
bornea
parents:
diff changeset
35 listfile = sys.argv[1]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
36 SAINT_cutoff = sys.argv[2]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
37 Int_conf = sys.argv[3]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
38 Species = sys.argv[4]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
39 cyto_file = sys.argv[5]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
40 db_path = sys.argv[6]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
41
fd27366b9faf Uploaded
bornea
parents:
diff changeset
42
fd27366b9faf Uploaded
bornea
parents:
diff changeset
43 class ReturnValue1(object):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
44 def __init__(self, uniprot_acc, gene, swissprot):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
45 self.up = uniprot_acc
fd27366b9faf Uploaded
bornea
parents:
diff changeset
46 self.gn = gene
fd27366b9faf Uploaded
bornea
parents:
diff changeset
47 self.sp = swissprot
fd27366b9faf Uploaded
bornea
parents:
diff changeset
48 class ReturnValue2(object):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
49 def __init__(self, getdata, getproteins, getheader):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
50 self.data = getdata
fd27366b9faf Uploaded
bornea
parents:
diff changeset
51 self.proteins = getproteins
fd27366b9faf Uploaded
bornea
parents:
diff changeset
52 self.header = getheader
fd27366b9faf Uploaded
bornea
parents:
diff changeset
53
fd27366b9faf Uploaded
bornea
parents:
diff changeset
54
fd27366b9faf Uploaded
bornea
parents:
diff changeset
55 def main(listfile, SAINT_cutoff, Int_conf, Species):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
56 cytoscape(dd_network(listfile, SAINT_cutoff, Int_conf), listfile, SAINT_cutoff)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
57
fd27366b9faf Uploaded
bornea
parents:
diff changeset
58
fd27366b9faf Uploaded
bornea
parents:
diff changeset
59 def readtab(infile):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
60 with open(infile, 'r') as file_to_read:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
61 # Read in tab-delim text.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
62 output = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
63 for line in file_to_read:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
64 line = line.strip()
fd27366b9faf Uploaded
bornea
parents:
diff changeset
65 temp = line.split('\t')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
66 output.append(temp)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
67 return output
fd27366b9faf Uploaded
bornea
parents:
diff changeset
68
fd27366b9faf Uploaded
bornea
parents:
diff changeset
69
fd27366b9faf Uploaded
bornea
parents:
diff changeset
70 def read_listfile(listfile):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
71 # Get data, proteins and header from scaffold output
fd27366b9faf Uploaded
bornea
parents:
diff changeset
72 dupes = readtab(listfile)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
73 header = dupes[0]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
74 prot_start = header.index("PreyGene")-1
fd27366b9faf Uploaded
bornea
parents:
diff changeset
75 data = dupes[1:]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
76 # Cut off blank line and END OF FILE.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
77 proteins = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
78 for protein in data:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
79 proteins.append(protein[prot_start])
fd27366b9faf Uploaded
bornea
parents:
diff changeset
80 return ReturnValue2(data, proteins, header)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
81
fd27366b9faf Uploaded
bornea
parents:
diff changeset
82
fd27366b9faf Uploaded
bornea
parents:
diff changeset
83 def get_info(uniprot_accession_in):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
84 # Get aa lengths and gene name.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
85 error = open('error proteins.txt', 'a+')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
86 i = 0
fd27366b9faf Uploaded
bornea
parents:
diff changeset
87 while i == 0:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
88 try:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
89 data = urllib2.urlopen("http://www.uniprot.org/uniprot/" + uniprot_accession_in
fd27366b9faf Uploaded
bornea
parents:
diff changeset
90 + ".fasta")
fd27366b9faf Uploaded
bornea
parents:
diff changeset
91 break
fd27366b9faf Uploaded
bornea
parents:
diff changeset
92 except urllib2.HTTPError, err:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
93 i = i + 1
fd27366b9faf Uploaded
bornea
parents:
diff changeset
94 if i == 50:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
95 sys.exit("More than 50 errors. Check your file or try again later.")
fd27366b9faf Uploaded
bornea
parents:
diff changeset
96 if err.code == 404:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
97 error.write(uniprot_accession_in + '\t' + "Invalid URL. Check protein" + '\n')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
98 seqlength = 'NA'
fd27366b9faf Uploaded
bornea
parents:
diff changeset
99 genename = 'NA'
fd27366b9faf Uploaded
bornea
parents:
diff changeset
100 return ReturnValue1(seqlength, genename)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
101 elif err.code == 302:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
102 sys.exit("Request timed out. Check connection and try again.")
fd27366b9faf Uploaded
bornea
parents:
diff changeset
103 else:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
104 sys.exit("Uniprot had some other error")
fd27366b9faf Uploaded
bornea
parents:
diff changeset
105 lines = data.readlines()
fd27366b9faf Uploaded
bornea
parents:
diff changeset
106 header = lines[0]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
107 lst = header.split('|')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
108 lst2 = lst[2].split(' ')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
109 swissprot = lst2[0]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
110 uniprot_acc = lst[1]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
111 if lines == []:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
112 error.write(uniprot_accession_in + '\t' + "Blank Fasta" + '\n')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
113 error.close
fd27366b9faf Uploaded
bornea
parents:
diff changeset
114 uniprot_acc = 'NA'
fd27366b9faf Uploaded
bornea
parents:
diff changeset
115 genename = 'NA'
fd27366b9faf Uploaded
bornea
parents:
diff changeset
116 return ReturnValue1(uniprot_acc, genename, swissprot)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
117 if lines != []:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
118 seqlength = 0
fd27366b9faf Uploaded
bornea
parents:
diff changeset
119 header = lines[0]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
120 if 'GN=' in header:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
121 lst = header.split('GN=')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
122 lst2 = lst[1].split(' ')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
123 genename = lst2[0]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
124 error.close
fd27366b9faf Uploaded
bornea
parents:
diff changeset
125 return ReturnValue1(uniprot_acc, genename, swissprot)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
126 if 'GN=' not in header:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
127 genename = 'NA'
fd27366b9faf Uploaded
bornea
parents:
diff changeset
128 error.close
fd27366b9faf Uploaded
bornea
parents:
diff changeset
129 return ReturnValue1(uniprot_acc, genename, swissprot)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
130
fd27366b9faf Uploaded
bornea
parents:
diff changeset
131
fd27366b9faf Uploaded
bornea
parents:
diff changeset
132 def dd_network(listfile, SAINTscore, CPDB_filter):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
133 # Filter by SS and CPDB.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
134 data = read_listfile(listfile).data
fd27366b9faf Uploaded
bornea
parents:
diff changeset
135 # Change to filtered list.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
136 SS = (read_listfile(listfile).header).index("SaintScore")
fd27366b9faf Uploaded
bornea
parents:
diff changeset
137 filt_data = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
138 for i in data:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
139 if i[SS] >= SAINTscore:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
140 filt_data.append(i)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
141 accessions = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
142 for i in filt_data:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
143 accessions.append(get_info(i[1]).sp)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
144 GO = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
145 for i in CPDB[2:]:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
146 if i[3] >= CPDB_filter:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
147 # Filter interaction confidence.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
148 GO.append(i[2])
fd27366b9faf Uploaded
bornea
parents:
diff changeset
149 # All known interactions.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
150 GO2 = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
151 for i in GO:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
152 GO2.append(i.split(','))
fd27366b9faf Uploaded
bornea
parents:
diff changeset
153 # Make interactions list friendly.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
154 unfiltered_network = {}
fd27366b9faf Uploaded
bornea
parents:
diff changeset
155 for i in accessions:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
156 interactions = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
157 for j in GO2:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
158 if i in j:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
159 # Find the interactions.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
160 if j not in interactions:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
161 # Dont add duplicate interactions.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
162 interactions.append(j)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
163 merged = list(itertools.chain(*interactions))
fd27366b9faf Uploaded
bornea
parents:
diff changeset
164 # Flatten list of lists.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
165 unfiltered_network[i] = merged
fd27366b9faf Uploaded
bornea
parents:
diff changeset
166 # Assign all possible interactions to protein in a dictionary.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
167 dd_network = {}
fd27366b9faf Uploaded
bornea
parents:
diff changeset
168 # Data dependent network.
fd27366b9faf Uploaded
bornea
parents:
diff changeset
169 for i in unfiltered_network:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
170 temp = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
171 for j in unfiltered_network[i]:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
172 if j in accessions:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
173 if j not in temp:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
174 if j != i:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
175 temp.append(j)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
176 dd_network[i] = temp
fd27366b9faf Uploaded
bornea
parents:
diff changeset
177 return dd_network
fd27366b9faf Uploaded
bornea
parents:
diff changeset
178
fd27366b9faf Uploaded
bornea
parents:
diff changeset
179
fd27366b9faf Uploaded
bornea
parents:
diff changeset
180 def cytoscape(dd_network, listfile, SAINTscore):
fd27366b9faf Uploaded
bornea
parents:
diff changeset
181 with open('network.sif', 'wt') as y:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
182 data = read_listfile(listfile).data
fd27366b9faf Uploaded
bornea
parents:
diff changeset
183 SS = (read_listfile(listfile).header).index("SaintScore")
fd27366b9faf Uploaded
bornea
parents:
diff changeset
184 filt_data = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
185 for i in data:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
186 if i[SS] >= SAINTscore:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
187 filt_data.append(i)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
188 header = ["Prey", "Interactions"]
fd27366b9faf Uploaded
bornea
parents:
diff changeset
189 header = '\t'.join(header)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
190 y.write(header + '\n')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
191 for i in filt_data:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
192 if dd_network[i[1]] != []:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
193 lst = []
fd27366b9faf Uploaded
bornea
parents:
diff changeset
194 for j in dd_network[i[1]]:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
195 lst.append(j)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
196 for j in lst:
fd27366b9faf Uploaded
bornea
parents:
diff changeset
197 y.write(i[1]+'\t'+'pp'+'\t' + j+'\n')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
198
fd27366b9faf Uploaded
bornea
parents:
diff changeset
199
fd27366b9faf Uploaded
bornea
parents:
diff changeset
200 if Species == "Human":
fd27366b9faf Uploaded
bornea
parents:
diff changeset
201 CPDB = readtab(str(db_path) + 'ConsensusPathDB_human_PPI.txt')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
202 if Species == "Yeast":
fd27366b9faf Uploaded
bornea
parents:
diff changeset
203 CPDB = readtab(str(db_path) + 'ConsensusPathDB_yeast_PPI.txt')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
204 if Species == "Mouse":
fd27366b9faf Uploaded
bornea
parents:
diff changeset
205 CPDB = readtab(str(db_path) +'ConsensusPathDB_mouse_PPI.txt')
fd27366b9faf Uploaded
bornea
parents:
diff changeset
206 if __name__ == '__main__':
fd27366b9faf Uploaded
bornea
parents:
diff changeset
207 main(listfile, SAINT_cutoff, Int_conf, Species)
fd27366b9faf Uploaded
bornea
parents:
diff changeset
208 os.rename('network.sif', str(cyto_file))