annotate ProteinInteractions_v2.py @ 10:00fbbb20ffbe draft

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