annotate unipept.py @ 0:6430407e5869 draft

Uploaded
author galaxyp
date Fri, 03 Apr 2015 14:55:49 -0400
parents
children 0c1ee95282fa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
1 #!/usr/bin/env python
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
2 """
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
3 #
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
4 #------------------------------------------------------------------------------
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
5 # University of Minnesota
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
6 # Copyright 2015, Regents of the University of Minnesota
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
7 #------------------------------------------------------------------------------
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
8 # Author:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
9 #
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
10 # James E Johnson
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
11 #
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
12 #------------------------------------------------------------------------------
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
13 """
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
14
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
15 import json
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
16 import logging
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
17 import optparse
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
18 from optparse import OptionParser
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
19 import os
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
20 import sys
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
21 import re
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
22 import urllib
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
23 import urllib2
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
24 try:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
25 import xml.etree.cElementTree as ET
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
26 except ImportError:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
27 import xml.etree.ElementTree as ET
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
28
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
29 def warn_err(msg,exit_code=1):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
30 sys.stderr.write(msg)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
31 if exit_code:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
32 sys.exit(exit_code)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
33
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
34 def read_fasta(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
35 name, seq = None, []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
36 for line in fp:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
37 line = line.rstrip()
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
38 if line.startswith(">"):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
39 if name: yield (name, ''.join(seq))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
40 name, seq = line, []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
41 else:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
42 seq.append(line)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
43 if name: yield (name, ''.join(seq))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
44
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
45 def read_mzid(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
46 peptides = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
47 for event, elem in ET.iterparse(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
48 if event == 'end':
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
49 if re.search('PeptideSequence',elem.tag):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
50 peptides.append(elem.text)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
51 return peptides
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
52
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
53 def read_pepxml(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
54 peptides = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
55 for event, elem in ET.iterparse(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
56 if event == 'end':
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
57 if re.search('search_hit',elem.tag):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
58 peptides.append(elem.get('peptide'))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
59 return peptides
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
60
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
61 def __main__():
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
62 #Parse Command Line
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
63 parser = optparse.OptionParser()
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
64 # unipept API
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
65 parser.add_option( '-A', '--api', dest='unipept', default='pept2lca', choices=['pept2lca','pept2taxa','pept2prot'], help='The unipept application: pept2lca, pept2taxa, or pept2prot' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
66 # files
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
67 parser.add_option( '-t', '--tabular', dest='tabular', default=None, help='A tabular file that contains a peptide column' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
68 parser.add_option( '-c', '--column', dest='column', type='int', default=0, help='The column (zero-based) in the tabular file that contains peptide sequences' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
69 parser.add_option( '-f', '--fasta', dest='fasta', default=None, help='A fasta file containing peptide sequences' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
70 parser.add_option( '-m', '--mzid', dest='mzid', default=None, help='A mxIdentML file containing peptide sequences' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
71 parser.add_option( '-p', '--pepxml', dest='pepxml', default=None, help='A pepxml file containing peptide sequences' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
72 # Unipept Flags
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
73 parser.add_option( '-e', '--equate_il', dest='equate_il', action='store_true', default=False, help='isoleucine (I) and leucine (L) are equated when matching tryptic peptides to UniProt records' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
74 parser.add_option( '-x', '--extra', dest='extra', action='store_true', default=False, help='return the complete lineage of the taxonomic lowest common ancestor' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
75 parser.add_option( '-n', '--names', dest='names', action='store_true', default=False, help='return the names of all ranks in the lineage of the taxonomic lowest common ancestor' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
76 # Warn vs Error Flag
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
77 parser.add_option( '-S', '--strict', dest='strict', action='store_true', default=False, help='Print exit on invalid peptide' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
78 # outputs
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
79 parser.add_option( '-J', '--json', dest='json', default=None, help='Output file path for json formatted results')
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
80 parser.add_option( '-T', '--tsv', dest='tsv', default=None, help='Output file path for TAB-separated-values (.tsv) formatted results')
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
81 parser.add_option( '-C', '--csv', dest='csv', default=None, help='Output file path for Comma-separated-values (.csv) formatted results')
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
82 parser.add_option( '-M', '--mismatch', dest='mismatch', default=None, help='Output file path for peptide with no matches' )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
83 (options, args) = parser.parse_args()
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
84 invalid_ec = 2 if options.strict else None
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
85 peptides = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
86 pep_pat = '^([ABCDEFGHIKLMNPQRSTVWXYZ]+)$'
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
87 ## Get peptide sequences
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
88 if options.mzid:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
89 peptides += read_mzid(options.mzid)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
90 if options.pepxml:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
91 peptides += read_pepxml(options.pepxml)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
92 if options.tabular:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
93 with open(options.tabular) as fp:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
94 for i,line in enumerate(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
95 if line.strip() == '' or line.startswith('#'):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
96 continue
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
97 fields = line.rstrip('\n').split('\t')
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
98 peptide = fields[options.column]
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
99 if not re.match(pep_pat,peptide):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
100 warn_err('"%s" is not a peptide (line %d column %d of tabular file: %s)\n' % (peptide,i,options.column,options.tabular),exit_code=invalid_ec)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
101 peptides.append(peptide)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
102 if options.fasta:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
103 with open(options.fasta) as fp:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
104 for id, peptide in read_fasta(fp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
105 if not re.match(pep_pat,peptide):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
106 warn_err('"%s" is not a peptide (id %s of fasta file: %s)\n' % (peptide,id,options.fasta),exit_code=invalid_ec)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
107 peptides.append(peptide)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
108 if args and len(args) > 0:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
109 for i,peptide in enumerate(args):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
110 if not re.match(pep_pat,peptide):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
111 warn_err('"%s" is not a peptide (arg %d)\n' % (peptide,i),exit_code=invalid_ec)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
112 peptides.append(peptide)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
113 if len(peptides) < 1:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
114 warn_err("No peptides input!",exit_code=1)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
115 ## unipept
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
116 post_data = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
117 if options.equate_il:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
118 post_data.append(("equate_il","true"))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
119 if options.names:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
120 post_data.append(("extra","true"))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
121 post_data.append(("names","true"))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
122 elif options.extra:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
123 post_data.append(("extra","true"))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
124 post_data += [('input[]', x) for x in peptides]
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
125 headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
126 url = 'http://api.unipept.ugent.be/api/v1/%s' % options.unipept
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
127 req = urllib2.Request( url, headers = headers, data = urllib.urlencode(post_data) )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
128 resp = json.loads( urllib2.urlopen( req ).read() )
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
129 ## output results
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
130 if not (options.mismatch or options.json or options.tsv or options.csv):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
131 print >> sys.stdout, str(resp)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
132 if options.mismatch:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
133 peptides_matched = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
134 for i,pdict in enumerate(resp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
135 peptides_matched.append(pdict['peptide'])
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
136 with open(options.mismatch,'w') as outputFile:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
137 for peptide in peptides:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
138 if not peptide in peptides_matched:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
139 outputFile.write("%s\n" % peptide)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
140 if options.json:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
141 with open(options.json,'w') as outputFile:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
142 outputFile.write(str(resp))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
143 if options.tsv or options.csv:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
144 # 'pept2lca','pept2taxa','pept2prot'
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
145 pept2lca_column_order = [ 'peptide','superkingdom','kingdom','subkingdom','superphylum','phylum','subphylum','superclass','class_','subclass','infraclass','superorder','order','suborder','infraorder','parvorder','superfamily','family','subfamily','tribe','subtribe','genus','subgenus','species_group','species_subgroup','species','subspecies','varietas','forma' ]
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
146 pept2prot_column_order = [ 'peptide','uniprot_id','taxon_id','taxon_name','ec_references','go_references','refseq_ids','refseq_protein_ids','insdc_ids','insdc_protein_ids']
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
147 column_order = pept2prot_column_order if options.unipept == 'pept2prot' else pept2lca_column_order
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
148 found_keys = set()
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
149 results = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
150 for i,pdict in enumerate(resp):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
151 results.append(pdict)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
152 found_keys |= set(pdict.keys())
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
153 # print >> sys.stderr, "%s\n%s" % (pdict.keys(),found_keys)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
154 column_names = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
155 column_keys = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
156 for col in column_order:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
157 if col in found_keys:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
158 column_names.append(col)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
159 column_keys.append(col)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
160 elif options.extra or options.names:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
161 col_id = col+'_id'
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
162 col_name = col+'_name'
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
163 if options.extra:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
164 if col_id in found_keys:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
165 column_names.append(col_id)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
166 column_keys.append(col_id)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
167 if options.names:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
168 if col_name in found_keys:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
169 column_names.append(col)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
170 column_keys.append(col_name)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
171 else:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
172 if col+'_name' in found_keys:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
173 column_names.append(col)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
174 column_keys.append(col+'_name')
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
175 elif col+'_id' in found_keys:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
176 column_names.append(col)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
177 column_keys.append(col+'_id')
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
178 # print >> sys.stderr, "%s\n%s" % (column_names,column_keys)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
179 taxa = []
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
180 for i,pdict in enumerate(results):
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
181 vals = [str(pdict[x]) if x in pdict and pdict[x] else '' for x in column_keys]
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
182 taxa.append(vals)
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
183 if options.tsv:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
184 with open(options.tsv,'w') as outputFile:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
185 outputFile.write("#%s\n"% '\t'.join(column_names))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
186 for vals in taxa:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
187 outputFile.write("%s\n"% '\t'.join(vals))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
188 if options.csv:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
189 with open(options.csv,'w') as outputFile:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
190 outputFile.write("%s\n"% ','.join(column_names))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
191 for vals in taxa:
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
192 outputFile.write("%s\n"% ','.join(['"%s"' % (v if v else '') for v in vals]))
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
193
6430407e5869 Uploaded
galaxyp
parents:
diff changeset
194 if __name__ == "__main__" : __main__()