annotate CRAPomeQuery.py @ 19:f1459638e1bc draft default tip

Uploaded
author bornea
date Fri, 29 Apr 2016 15:49:34 -0400
parents 378fc3676c78
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
1 # -*- coding: utf-8 -*-
4d47d78b193a Uploaded
bornea
parents:
diff changeset
2 """
4d47d78b193a Uploaded
bornea
parents:
diff changeset
3 Created on Thu Apr 14 16:58:05 2016
4d47d78b193a Uploaded
bornea
parents:
diff changeset
4
4d47d78b193a Uploaded
bornea
parents:
diff changeset
5 @author: brentkuenzi
4d47d78b193a Uploaded
bornea
parents:
diff changeset
6 """
4d47d78b193a Uploaded
bornea
parents:
diff changeset
7 ################################################################################
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
8 ## Dependencies ##
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
9 import urllib2
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
10 import sys
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
11 import numpy
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
12 import os
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
13 ################################################################################
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
14 # This program will read in a SAINT formatted 'prey.txt' file or a file
4d47d78b193a Uploaded
bornea
parents:
diff changeset
15 # containing a single column list of uniprot accessions (e.g. "P00533" or
4d47d78b193a Uploaded
bornea
parents:
diff changeset
16 # "EGFR_HUMAN")query the CRAPome database (v1.1), and return a file specifying
4d47d78b193a Uploaded
bornea
parents:
diff changeset
17 # the prevalence of each protein in the CRAPome.
4d47d78b193a Uploaded
bornea
parents:
diff changeset
18 ################################################################################
4d47d78b193a Uploaded
bornea
parents:
diff changeset
19 # Copyright (C) Brent Kuenzi.
4d47d78b193a Uploaded
bornea
parents:
diff changeset
20 # Permission is granted to copy, distribute and/or modify this document
4d47d78b193a Uploaded
bornea
parents:
diff changeset
21 # under the terms of the GNU Free Documentation License, Version 1.3
4d47d78b193a Uploaded
bornea
parents:
diff changeset
22 # or any later version published by the Free Software Foundation;
4d47d78b193a Uploaded
bornea
parents:
diff changeset
23 # with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
4d47d78b193a Uploaded
bornea
parents:
diff changeset
24 # A copy of the license is included in the section entitled "GNU
4d47d78b193a Uploaded
bornea
parents:
diff changeset
25 # Free Documentation License".
4d47d78b193a Uploaded
bornea
parents:
diff changeset
26 ################################################################################
4d47d78b193a Uploaded
bornea
parents:
diff changeset
27 ## REQUIRED INPUT ##
4d47d78b193a Uploaded
bornea
parents:
diff changeset
28 # 1) crappyData: Prey.txt or single column list of Uniprot accessions
4d47d78b193a Uploaded
bornea
parents:
diff changeset
29 crappyData = sys.argv[1] # Prey file or File with single column of accessions
4d47d78b193a Uploaded
bornea
parents:
diff changeset
30 # 2) Species: HUMAN or YEAST
4d47d78b193a Uploaded
bornea
parents:
diff changeset
31 species = sys.argv[2] # HUMAN or YEAST
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
32 fasta_db = sys.argv[3]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
33 db_path = sys.argv[5]
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
34 ################################################################################
4d47d78b193a Uploaded
bornea
parents:
diff changeset
35 ## Global Variables ##
4d47d78b193a Uploaded
bornea
parents:
diff changeset
36 if species == "HUMAN":
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
37 database = str(db_path) + "Human_CRAPome_v1-1.txt"
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
38 if species == "YEAST":
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
39 database = str(db_path) + "Yeast_CRAPome_v1-1.txt"
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
40 ################################################################################
4d47d78b193a Uploaded
bornea
parents:
diff changeset
41 ## CRAPomeQuery ##
4d47d78b193a Uploaded
bornea
parents:
diff changeset
42 class ReturnValue1(object):
4d47d78b193a Uploaded
bornea
parents:
diff changeset
43 def __init__(self, uniprot_acc, gene, swissprot):
4d47d78b193a Uploaded
bornea
parents:
diff changeset
44 self.up = uniprot_acc
4d47d78b193a Uploaded
bornea
parents:
diff changeset
45 self.gn = gene
4d47d78b193a Uploaded
bornea
parents:
diff changeset
46 self.sp = swissprot
4d47d78b193a Uploaded
bornea
parents:
diff changeset
47 def get_info(uniprot_accession_in): #get aa lengths and gene name
4d47d78b193a Uploaded
bornea
parents:
diff changeset
48 error = open('error proteins.txt', 'a+')
4d47d78b193a Uploaded
bornea
parents:
diff changeset
49 i=0
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
50 if fasta_db == "None":
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
51 while i==0:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
52 try:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
53 data = urllib2.urlopen("http://www.uniprot.org/uniprot/" + uniprot_accession_in + ".fasta")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
54 break
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
55 except urllib2.HTTPError, err:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
56 i = i + 1
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
57 if i == 50:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
58 sys.exit("More than 50 errors. Check your file or try again later.")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
59 if err.code == 404:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
60 error.write(uniprot_accession_in + '\t' + "Invalid URL. Check protein" + '\n')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
61 seqlength = 'NA'
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
62 genename = 'NA'
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
63 return ReturnValue1(seqlength, genename)
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
64 elif err.code == 302:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
65 sys.exit("Request timed out. Check connection and try again.")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
66 else:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
67 sys.exit("Uniprot had some other error")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
68 lines = data.readlines()
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
69 header = lines[0]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
70 lst = header.split('|')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
71 lst2 = lst[2].split(' ')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
72 swissprot = lst2[0]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
73 uniprot_acc = lst[1]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
74 if lines == []:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
75 error.write(uniprot_accession_in + '\t' + "Blank Fasta" + '\n')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
76 error.close
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
77 uniprot_acc = 'NA'
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
78 genename = 'NA'
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
79 return ReturnValue1(uniprot_acc, genename, swissprot)
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
80 if lines != []:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
81 seqlength = 0
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
82 header = lines[0]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
83 if 'GN=' in header:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
84 lst = header.split('GN=')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
85 lst2 = lst[1].split(' ')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
86 genename = lst2[0]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
87 error.close
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
88 return ReturnValue1(uniprot_acc, genename, swissprot)
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
89 if 'GN=' not in header:
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
90 genename = 'NA'
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
91 error.close
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
92 return ReturnValue1(uniprot_acc, genename, swissprot)
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
93 else:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
94 data = open(fasta_db, 'r')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
95 data_lines = data.readlines()
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
96 db_len = len(data_lines)
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
97 for fasta_line in data_lines:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
98 if uniprot_accession_in in fasta_line:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
99 sp, uniprot_acc, name_info = fasta_line.split("|")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
100 swissprot = name_info.split(' ')[0]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
101 if 'GN=' in name_info:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
102 lst = name_info.split('GN=')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
103 lst2 = lst[1].split(' ')
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
104 genename = lst2[0]
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
105 return ReturnValue1(uniprot_acc, genename, swissprot)
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
106 if 'GN=' not in name_info:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
107 genename = 'NA'
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
108 return ReturnValue1(uniprot_acc, genename, swissprot)
19
f1459638e1bc Uploaded
bornea
parents: 17
diff changeset
109 if sp == ">sp":
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
110 error.close()
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
111 else:
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
112 error.write(uniprot_accession_in + '\t' + "Blank Fasta" + '\n')
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
113 error.close
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
114 uniprot_acc = 'NA'
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
115 genename = 'NA'
4d47d78b193a Uploaded
bornea
parents:
diff changeset
116 return ReturnValue1(uniprot_acc, genename, swissprot)
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
117
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
118
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
119 def readtab(infile): # read in tab-delim text
4d47d78b193a Uploaded
bornea
parents:
diff changeset
120 with open(infile,'r') as x:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
121 output = []
4d47d78b193a Uploaded
bornea
parents:
diff changeset
122 for line in x:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
123 line = line.strip()
4d47d78b193a Uploaded
bornea
parents:
diff changeset
124 temp = line.split('\t')
4d47d78b193a Uploaded
bornea
parents:
diff changeset
125 output.append(temp)
4d47d78b193a Uploaded
bornea
parents:
diff changeset
126 return output
4d47d78b193a Uploaded
bornea
parents:
diff changeset
127 def crapome(infile): # Query CRAPome
4d47d78b193a Uploaded
bornea
parents:
diff changeset
128 data = readtab(infile)
4d47d78b193a Uploaded
bornea
parents:
diff changeset
129 crapome = readtab(database)
4d47d78b193a Uploaded
bornea
parents:
diff changeset
130 filt = []
4d47d78b193a Uploaded
bornea
parents:
diff changeset
131 for i in data: # Filter CRAPome database on our data
4d47d78b193a Uploaded
bornea
parents:
diff changeset
132 flag = 0 # is protein in CRAPome?
4d47d78b193a Uploaded
bornea
parents:
diff changeset
133 ac_flag = 0 # is it _SPECIES or not
4d47d78b193a Uploaded
bornea
parents:
diff changeset
134 unique = 0 # only take first ID in CRAPome
4d47d78b193a Uploaded
bornea
parents:
diff changeset
135 if "_"+species in i[0]:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
136 ac = i[0]
4d47d78b193a Uploaded
bornea
parents:
diff changeset
137 else:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
138 ac = get_info(i[0]).sp # query swissprot if not _SPECIES
4d47d78b193a Uploaded
bornea
parents:
diff changeset
139 ac_flag +=1
4d47d78b193a Uploaded
bornea
parents:
diff changeset
140 for j in crapome:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
141 if ac == j[2]:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
142 if ac_flag == 0: # if _SPECIES
4d47d78b193a Uploaded
bornea
parents:
diff changeset
143 if unique == 0:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
144 filt.append(j)
4d47d78b193a Uploaded
bornea
parents:
diff changeset
145 flag+=1
4d47d78b193a Uploaded
bornea
parents:
diff changeset
146 unique+=1
4d47d78b193a Uploaded
bornea
parents:
diff changeset
147 if ac_flag != 0: # if not _SPECIES
4d47d78b193a Uploaded
bornea
parents:
diff changeset
148 if unique == 0:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
149 unique+=1
4d47d78b193a Uploaded
bornea
parents:
diff changeset
150 j[2] = i[0] # change to user input
4d47d78b193a Uploaded
bornea
parents:
diff changeset
151 filt.append(j)
4d47d78b193a Uploaded
bornea
parents:
diff changeset
152 flag +=1
4d47d78b193a Uploaded
bornea
parents:
diff changeset
153 if flag == 0: # if protein is not present in CRAPome database then add it
14
b451afff1aad Uploaded
bornea
parents: 13
diff changeset
154 filt.append(["\t", "\t", i[0], "Invalid identifier / gene not available"])
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
155 total = 0 # Experiment counter
4d47d78b193a Uploaded
bornea
parents:
diff changeset
156 query = []
4d47d78b193a Uploaded
bornea
parents:
diff changeset
157 for i in filt: # Create CRAPome file as list
4d47d78b193a Uploaded
bornea
parents:
diff changeset
158 temp=[]
4d47d78b193a Uploaded
bornea
parents:
diff changeset
159 if len(i) > 5:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
160 cnt=0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
161 temp.append(i[2]) # append accession
4d47d78b193a Uploaded
bornea
parents:
diff changeset
162 temp.append(i[0]) # append gene name
4d47d78b193a Uploaded
bornea
parents:
diff changeset
163 ave = []
4d47d78b193a Uploaded
bornea
parents:
diff changeset
164 total = len(i[3:]) # calculate total experiments
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
165 for j in i[3:]:
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
166 if j != '0':
4d47d78b193a Uploaded
bornea
parents:
diff changeset
167 ave.append(int(j)) # calculate Ave.SC on only experiments with ID
4d47d78b193a Uploaded
bornea
parents:
diff changeset
168 cnt+=1
4d47d78b193a Uploaded
bornea
parents:
diff changeset
169 temp.append(str(cnt) + " / "+str(total)) # format ratio
4d47d78b193a Uploaded
bornea
parents:
diff changeset
170 if ave != []:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
171 temp.append(str(round(numpy.mean(ave),1))) # calculate Ave.SC
4d47d78b193a Uploaded
bornea
parents:
diff changeset
172 temp.append(str(max(ave))) # calculate Max.SC
4d47d78b193a Uploaded
bornea
parents:
diff changeset
173 else:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
174 temp.append(0) # add 0 if has not been ID'd in CRAPome
4d47d78b193a Uploaded
bornea
parents:
diff changeset
175 temp.append(0) # add 0 if has not been ID'd in CRAPome
4d47d78b193a Uploaded
bornea
parents:
diff changeset
176 else:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
177 temp.append(i[2]) # append accession
4d47d78b193a Uploaded
bornea
parents:
diff changeset
178 temp.append(i[3])
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
179 temp.append("NA")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
180 temp.append("NA")
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
181 temp.append("NA")
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
182 query.append(temp) # final query results
4d47d78b193a Uploaded
bornea
parents:
diff changeset
183
4d47d78b193a Uploaded
bornea
parents:
diff changeset
184 header = ["User Input","Mapped Gene Symbol","Num of Expt. (found/total)","Ave SC","Max SC"]
4d47d78b193a Uploaded
bornea
parents:
diff changeset
185 with open("Crappy Data.txt","wt") as x: # write file
4d47d78b193a Uploaded
bornea
parents:
diff changeset
186 x.write("\t".join(header) + "\n")
4d47d78b193a Uploaded
bornea
parents:
diff changeset
187 for i in query:
4d47d78b193a Uploaded
bornea
parents:
diff changeset
188 x.write("\t".join(i) + "\n")
4d47d78b193a Uploaded
bornea
parents:
diff changeset
189 if __name__ == '__main__':
4d47d78b193a Uploaded
bornea
parents:
diff changeset
190 crapome(crappyData)
4d47d78b193a Uploaded
bornea
parents:
diff changeset
191
17
378fc3676c78 Uploaded
bornea
parents: 16
diff changeset
192 os.rename("Crappy Data.txt", sys.argv[4])
0
4d47d78b193a Uploaded
bornea
parents:
diff changeset
193 ## END ##