Mercurial > repos > jjohnson > unipept
changeset 0:c643e1fe2faa draft default tip
Imported from capsule None
author | jjohnson |
---|---|
date | Mon, 30 Mar 2015 18:00:54 -0400 |
parents | |
children | |
files | repository_dependencies.xml test-data/input.fasta test-data/input.tsv test-data/input_bad.fasta unipept.py unipept.xml |
diffstat | 6 files changed, 517 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/repository_dependencies.xml Mon Mar 30 18:00:54 2015 -0400 @@ -0,0 +1,4 @@ +<?xml version="1.0"?> +<repositories description="Required proteomics dependencies."> + <repository changeset_revision="9cfabf0b942d" name="proteomics_datatypes" owner="iracooke" toolshed="https://testtoolshed.g2.bx.psu.edu" /> +</repositories>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/input.fasta Mon Mar 30 18:00:54 2015 -0400 @@ -0,0 +1,10 @@ +>1 +AIPQLEVARPADAYETAEAYR +>2 +AAEGGLSR +>3 +APVLSDSSCK +>4 +DQIAHEGK +>5 +ATLTSGAAR
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/input.tsv Mon Mar 30 18:00:54 2015 -0400 @@ -0,0 +1,5 @@ +1 AIPQLEVARPADAYETAEAYR AIPQLEVARPADAYETAEAYR +2 AAEGGLSR AAEGQLSR +3 APVLSDSSCK APVLJDSSCK +4 DQIAHEGK DQUAHEGK +5 ATLTSGAAR ATLTSGAAR
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/input_bad.fasta Mon Mar 30 18:00:54 2015 -0400 @@ -0,0 +1,10 @@ +>1 +AIPQLEVARPADAYETAEAYR +>2 +AAEGQLSR +>3 +APVLJDSSCK +>4 +DQUAHEGK +>5 +ATLTSGAAR
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unipept.py Mon Mar 30 18:00:54 2015 -0400 @@ -0,0 +1,182 @@ +#!/usr/bin/env python +""" +# +#------------------------------------------------------------------------------ +# University of Minnesota +# Copyright 2015, Regents of the University of Minnesota +#------------------------------------------------------------------------------ +# Author: +# +# James E Johnson +# +#------------------------------------------------------------------------------ +""" + +import json +import logging +import optparse +from optparse import OptionParser +import os +import sys +import re +import urllib +import urllib2 +try: + import xml.etree.cElementTree as ET +except ImportError: + import xml.etree.ElementTree as ET + +def warn_err(msg,exit_code=1): + sys.stderr.write(msg) + if exit_code: + sys.exit(exit_code) + +def read_fasta(fp): + name, seq = None, [] + for line in fp: + line = line.rstrip() + if line.startswith(">"): + if name: yield (name, ''.join(seq)) + name, seq = line, [] + else: + seq.append(line) + if name: yield (name, ''.join(seq)) + +def read_mzid(fp): + peptides = [] + for event, elem in ET.iterparse(fp): + if event == 'end': + if re.search('PeptideSequence',elem.tag): + peptides.append(elem.text) + return peptides + +def read_pepxml(fp): + peptides = [] + for event, elem in ET.iterparse(fp): + if event == 'end': + if re.search('search_hit',elem.tag): + peptides.append(elem.get('peptide')) + return peptides + +def __main__(): + #Parse Command Line + parser = optparse.OptionParser() + # unipept API + parser.add_option( '-A', '--api', dest='unipept', default='pept2lca', choices=['pept2lca','pept2taxa','pept2prot'], help='The unipept application: pept2lca, pept2taxa, or pept2prot' ) + # files + parser.add_option( '-t', '--tabular', dest='tabular', default=None, help='A tabular file that contains a peptide column' ) + parser.add_option( '-c', '--column', dest='column', type='int', default=0, help='The column (zero-based) in the tabular file that contains peptide sequences' ) + parser.add_option( '-f', '--fasta', dest='fasta', default=None, help='A fasta file containing peptide sequences' ) + parser.add_option( '-m', '--mzid', dest='mzid', default=None, help='A mxIdentML file containing peptide sequences' ) + parser.add_option( '-p', '--pepxml', dest='pepxml', default=None, help='A pepxml file containing peptide sequences' ) + # Unipept Flags + 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' ) + parser.add_option( '-x', '--extra', dest='extra', action='store_true', default=False, help='return the complete lineage of the taxonomic lowest common ancestor' ) + 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' ) + # Warn vs Error Flag + parser.add_option( '-S', '--strict', dest='strict', action='store_true', default=False, help='Print exit on invalid peptide' ) + # outputs + parser.add_option( '-J', '--json', dest='json', default=None, help='Output file path for json formatted results') + parser.add_option( '-T', '--tsv', dest='tsv', default=None, help='Output file path for TAB-separated-values (.tsv) formatted results') + parser.add_option( '-C', '--csv', dest='csv', default=None, help='Output file path for Comma-separated-values (.csv) formatted results') + parser.add_option( '-M', '--mismatch', dest='mismatch', default=None, help='Output file path for peptide with no matches' ) + (options, args) = parser.parse_args() + invalid_ec = 2 if options.strict else None + peptides = [] + pep_pat = '^([ABCDEFGHIKLMNPQRSTVWXYZ]+)$' + ## Get peptide sequences + if options.mzid: + peptides += read_mzid(options.mzid) + if options.pepxml: + peptides += read_pepxml(options.pepxml) + if options.tabular: + with open(options.tabular) as fp: + for i,line in enumerate(fp): + if line.strip() == '' or line.startswith('#'): + continue + fields = line.rstrip('\n').split('\t') + peptide = fields[options.column] + if not re.match(pep_pat,peptide): + 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) + peptides.append(peptide) + if options.fasta: + with open(options.fasta) as fp: + for id, peptide in read_fasta(fp): + if not re.match(pep_pat,peptide): + warn_err('"%s" is not a peptide (id %s of fasta file: %s)\n' % (peptide,id,options.fasta),exit_code=invalid_ec) + peptides.append(peptide) + if args and len(args) > 0: + for i,peptide in enumerate(args): + if not re.match(pep_pat,peptide): + warn_err('"%s" is not a peptide (arg %d)\n' % (peptide,i),exit_code=invalid_ec) + peptides.append(peptide) + if len(peptides) < 1: + warn_err("No peptides input!",exit_code=1) + ## unipept + post_data = [] + if options.equate_il: + post_data.append(("equate_il","true")) + if options.names: + post_data.append(("extra","true")) + post_data.append(("names","true")) + elif options.extra: + post_data.append(("extra","true")) + post_data += [('input[]', x) for x in peptides] + headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'} + url = 'http://api.unipept.ugent.be/api/v1/%s' % options.unipept + req = urllib2.Request( url, headers = headers, data = urllib.urlencode(post_data) ) + resp = json.loads( urllib2.urlopen( req ).read() ) + ## output results + if not (options.mismatch or options.json or options.tsv or options.csv): + print >> sys.stdout, str(resp) + if options.mismatch: + peptides_matched = [] + for i,pdict in enumerate(resp): + peptides_matched.append(pdict['peptide']) + with open(options.mismatch,'w') as outputFile: + for peptide in peptides: + if not peptide in peptides_matched: + outputFile.write("%s\n" % peptide) + if options.json: + with open(options.json,'w') as outputFile: + outputFile.write(str(resp)) + if options.tsv or options.csv: + # 'pept2lca','pept2taxa','pept2prot' + 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' ] + pept2prot_column_order = [ 'peptide','uniprot_id','taxon_id','taxon_name','ec_references','go_references','refseq_ids','refseq_protein_ids','insdc_ids','insdc_protein_ids'] + column_order = pept2prot_column_order if options.unipept == 'pept2prot' else pept2lca_column_order + found_keys = set() + results = [] + for i,pdict in enumerate(resp): + results.append(pdict) + found_keys |= set(pdict.keys()) + # print >> sys.stderr, "%s\n%s" % (pdict.keys(),found_keys) + column_names = [] + column_keys = [] + for col in column_order: + if col in found_keys: + column_names.append(col) + column_keys.append(col) + elif col+'_name' in found_keys: + column_names.append(col) + column_keys.append(col+'_name') + elif col+'_id' in found_keys: + column_names.append(col) + column_keys.append(col+'_id') + # print >> sys.stderr, "%s\n%s" % (column_names,column_keys) + taxa = [] + for i,pdict in enumerate(results): + vals = [str(pdict[x]) if x in pdict and pdict[x] else '' for x in column_keys] + taxa.append(vals) + if options.tsv: + with open(options.tsv,'w') as outputFile: + outputFile.write("#%s\n"% '\t'.join(column_names)) + for vals in taxa: + outputFile.write("%s\n"% '\t'.join(vals)) + if options.csv: + with open(options.csv,'w') as outputFile: + outputFile.write("%s\n"% ','.join(column_names)) + for vals in taxa: + outputFile.write("%s\n"% ','.join([v if v else "" for v in vals])) + +if __name__ == "__main__" : __main__()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unipept.xml Mon Mar 30 18:00:54 2015 -0400 @@ -0,0 +1,306 @@ +<tool id="unipept" name="Unipept" version="0.1.0"> + <description>retrieve taxonomy for peptides</description> + <macros> + <xml name="equate_il"> + <param name="equate_il" type="boolean" truevalue="-e" falsevalue="" checked="true" label="Equate isoleucine and leucine"> + <help>isoleucine (I) and leucine (L) are equated when matching tryptic peptides to UniProt records</help> + </param > + </xml> + <xml name="extra"> + <param name="extra" type="boolean" truevalue="-x" falsevalue="" checked="false" label="retrieve extra information"> + <yield/> + </param > + </xml> + <xml name="names"> + <param name="names" type="boolean" truevalue="-n" falsevalue="" checked="true" label="names" > + <help>return the names of taxons</help> + </param > + </xml> + </macros> + <requirements> + </requirements> + <stdio> + <exit_code range="1:" /> + </stdio> + <command interpreter="python"><![CDATA[ + unipept.py + --api=$unipept.api + $unipept.equate_il $unipept.extra + #if $unipept.api != 'pept2prot': + $unipept.names + #end if + $strict + #if str($peptide_src.fmt) == 'proteomic': + #if $peptide_src.input.datatype.file_ext == 'fasta': + --fasta="$peptide_src.input" + #elif $peptide_src.input.datatype.file_ext == 'mzid': + --mzid="$peptide_src.input" + #elif $peptide_src.input.datatype.file_ext == 'pepxml': + --pepxml="$peptide_src.input" + #end if + #elif str($peptide_src.fmt) == 'tabular': + --tabular="$peptide_src.input_tsv" + #set $col = int(str($peptide_src.column)) - 1 + --column=$col + #elif str($peptide_src.fmt) == 'fasta': + --fasta="$peptide_src.input_fasta" + #elif str($peptide_src.fmt) == 'mzid': + --mzid="$peptide_src.input_mzid" + #elif str($peptide_src.fmt) == 'pepxml': + --pepxml="$peptide_src.input_pepxml" + #end if + #if 'json' in str($outputs).split(','): + --json $output_json + #end if + #if 'tsv' in str($outputs).split(','): + --tsv $output_tsv + #end if + #if 'csv' in str($outputs).split(','): + --csv $output_csv + #end if + #if 'mismatch' in str($outputs).split(','): + --mismatch $output_mismatch + #end if + ]]></command> + <inputs> + <conditional name="unipept"> + <param name="api" type="select" label="Unipept application" > + <option value="pept2taxa" selected="true">pept2taxa: organisms associated with the UniProt entries containing a given tryptic peptide</option> + <option value="pept2lca">pept2lca: lowest common ancestor</option> + <option value="pept2prot">pept2prot: UniProt entries containing a given tryptic peptide</option> + </param> + <when value="pept2taxa"> + <expand macro="equate_il" /> + <expand macro="extra"> + <checked>true</checked> + <help>Return the complete lineage of each organism.</help> + </expand> + <expand macro="names" /> + </when> + <when value="pept2lca"> + <expand macro="equate_il" /> + <expand macro="extra"> + <help>Return the complete lineage of the taxonomic lowest common ancestor.</help> + </expand> + <expand macro="names" /> + </when> + <when value="pept2prot"> + <expand macro="equate_il" /> + <expand macro="extra"> + <help>Return additional information fields: taxon_name, ec_references, go_references, refseq_ids, refseq_protein_ids, insdc_ids, insdc_protein_ids + WARNING: Huge perfomance penalty! Only use for small number of peptides when the extra infomation is required. + </help> + </expand> + </when> + </conditional> + <conditional name="peptide_src"> + <param name="fmt" type="select" label="Peptides input format" > + <option value="proteomic">proteomics formats: mzid, pepxml, fasta</option> + <option value="tabular">tabular</option> + <option value="fasta">fasta</option> + <option value="mzid">mzid</option> + <option value="pepxml">pepxml</option> + </param> + <when value="proteomic"> + <param name="input" type="data" format="mzid,pepxml,fasta" label="Peptide Input" /> + </when> + <when value="tabular"> + <param name="input_tsv" type="data" format="tabular" label="Tabular Input Containing Peptide column" /> + <param name="column" label="Select column with peptides" type="data_column" numerical="false" data_ref="input_tsv" /> + </when> + <when value="fasta"> + <param name="input_fasta" type="data" format="fasta" label="Peptide Fasta Input" /> + </when> + <when value="mzid"> + <param name="input_mzid" type="data" format="mzid" label="mzIndetML Input" /> + </when> + <when value="pepxml"> + <param name="input_pepxml" type="data" format="pepxml" label="mzIndetML Input" /> + </when> + </conditional> + <param name="outputs" type="select" multiple="true" display="checkboxes" label="Choose outputs"> + <option value="tsv" selected="true">tabular</option> + <option value="csv">Comma Separated Values (.csv)</option> + <option value="json">JSON</option> + <option value="mismatch">Mismatches</option> + </param> + <param name="strict" type="boolean" truevalue="--strict" falsevalue="" checked="false" label="Exit with error on invalid peptides, otherwise ignore them"/> + </inputs> + <outputs> + <data name="output_json" format="json" label="${tool.name} ${unipept.api} on ${on_string} json"> + <filter>'json' in outputs</filter> + </data> + <data name="output_tsv" format="tabular" label="${tool.name} ${unipept.api} on ${on_string} tsv"> + <filter>'tsv' in outputs</filter> + </data> + <data name="output_csv" format="csv" label="${tool.name} ${unipept.api} on ${on_string} csv"> + <filter>'csv' in outputs</filter> + </data> + <data name="output_mismatch" format="tabular" label="${tool.name} ${unipept.api} on ${on_string} mismatch"> + <filter>'mismatch' in outputs</filter> + </data> + </outputs> + <tests> + <test> + <param name="fmt" value="tabular"/> + <param name="input_tsv" value="input.tsv"/> + <param name="column" value="2"/> + <param name="extra" value="True"/> + <param name="names" value="True"/> + <param name="outputs" value="tsv,mismatch"/> + <output name="output_tsv"> + <assert_contents> + <has_text text="AIPQLEVARPADAYETAEAYR" /> + </assert_contents> + </output> + <output name="output_mismatch"> + <assert_contents> + <has_text text="DQIAHEGK" /> + </assert_contents> + </output> + </test> + <test> + <param name="fmt" value="fasta"/> + <param name="input_tsv" value="input.fasta"/> + <param name="equate_il" value="True"/> + <param name="extra" value="True"/> + <param name="names" value="True"/> + <param name="outputs" value="json,mismatch"/> + <output name="output_json"> + <assert_contents> + <has_text text="AIPQLEVARPADAYETAEAYR" /> + </assert_contents> + </output> + <output name="output_mismatch"> + <assert_contents> + <has_text text="DQIAHEGK" /> + </assert_contents> + </output> + </test> + </tests> + <help><![CDATA[ + **Unipept** + + Retrieve Uniprot and taxanomic information for trypic peptides. + + **pept2prot** + Returns the list of UniProt entries containing a given tryptic peptide. This is the same information as provided on the Protein matches tab when performing a search with the Tryptic Peptide Analysis in the web interface. + + By default, each object contains the following information fields extracted from the UniProt record:: + + peptide: the peptide that matched this record + uniprot_id: the UniProt accession number of the matching record + taxon_id: the NCBI taxon id of the organism associated with the matching record + + When the extra parameter is set to true, objects contain the following additional fields extracted from the UniProt record:: + + taxon_name: the name of the organism associated with the matching UniProt record + ec_references: a space separated list of associated EC numbers + go_references: a space separated list of associated GO terms + refseq_ids: a space separated list of associated RefSeq accession numbers + refseq_protein_ids: a space separated list of associated RefSeq protein accession numbers + insdc_ids: a space separated list of associated insdc accession numbers + insdc_protein_ids: a space separated list of associated insdc protein accession numbers + + http://unipept.ugent.be/apidocs/pept2prot + + **pept2taxa** + Returns the set of organisms associated with the UniProt entries containing a given tryptic peptide. This is the same information as provided on the Lineage table tab when performing a search with the Tryptic Peptide Analysis in the web interface. + + By default, each object contains the following information fields extracted from the UniProt record and NCBI taxonomy:: + + peptide: the peptide that matched this record + taxon_id: the NCBI taxon id of the organism associated with the matching record + taxon_name: the name of the organism associated with the matching record + taxon_rank: the taxonomic rank of the organism associated with the matching record + + When the extra parameter is set to true, objects contain additional information about the lineages of the organism extracted from the NCBI taxonomy. The taxon id of each rank in the lineage is specified using the following information fields:: + + superkingdom_id + kingdom_id + subkingdom_id + superphylum_id + phylum_id + subphylum_id + superclass_id + class_id + subclass_id + infraclass_id + superorder_id + order_id + suborder_id + infraorder_id + parvorder_id + superfamily_id + family_id + subfamily_id + tribe_id + subtribe_id + genus_id + subgenus_id + species_group_id + species_subgroup_id + species_id + subspecies_id + varietas_id + forma_id + + http://unipept.ugent.be/apidocs/pept2taxa + + **pept2lca** + Returns the taxonomic lowest common ancestor for a given tryptic peptide. This is the same information as provided when performing a search with the Tryptic Peptide Analysis in the web interface. + + By default, each object contains the following information fields extracted from the UniProt record and NCBI taxonomy:: + + peptide: the peptide that matched this record + taxon_id: the NCBI taxon id of the organism associated with the matching record + taxon_name: the name of the organism associated with the matching record + taxon_rank: the taxonomic rank of the organism associated with the matching record + + When the extra parameter is set to true, objects contain additional information about the lineage of the taxonomic lowest common ancestor extracted from the NCBI taxonomy. The taxon id of each rank in the lineage is specified using the following information fields:: + + superkingdom_id + kingdom_id + subkingdom_id + superphylum_id + phylum_id + subphylum_id + superclass_id + class_id + subclass_id + infraclass_id + superorder_id + order_id + suborder_id + infraorder_id + parvorder_id + superfamily_id + family_id + subfamily_id + tribe_id + subtribe_id + genus_id + subgenus_id + species_group_id + species_subgroup_id + species_id + subspecies_id + varietas_id + forma_id + + http://unipept.ugent.be/apidocs/pept2lca + + **Attributions** + + The Unipept metaproteomics analysis pipeline + Bart Mesuere1,*, Griet Debyser2, Maarten Aerts3, Bart Devreese2, Peter Vandamme3 andPeter Dawyndt1 + Article first published online: 11 FEB 2015 + DOI: 10.1002/pmic.201400361 + http://onlinelibrary.wiley.com/doi/10.1002/pmic.201400361/abstract;jsessionid=BFF1994E4C14DA73D7C907EB208AD710.f04t04 + + ]]></help> + <citations> + <citation type="doi">doi:10.1002/pmic.201400361</citation> + </citations> + +</tool>