annotate ProteinInteractions_v2.py @ 5:fd6f8df5a043 draft

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