changeset 0:a8218b11216f draft

Uploaded
author jjohnson
date Wed, 29 Nov 2017 15:55:59 -0500
parents
children 0f70b1b92161
files ._ensembl_cdna_translate.py ._ensembl_cdna_translate.xml digest.py ensembl_cdna_translate.py ensembl_cdna_translate.xml test-data/human_transcripts.bed
diffstat 6 files changed, 851 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
Binary file ._ensembl_cdna_translate.py has changed
Binary file ._ensembl_cdna_translate.xml has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/digest.py	Wed Nov 29 15:55:59 2017 -0500
@@ -0,0 +1,158 @@
+#   Copyright 2012 Anton Goloborodko, Lev Levitsky
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+import re
+from collections import deque
+import itertools as it
+
+def cleave(sequence, rule, missed_cleavages=0, min_length=None):
+    """Cleaves a polypeptide sequence using a given rule.
+
+    Parameters
+    ----------
+    sequence : str
+        The sequence of a polypeptide.
+
+        .. note::
+            The sequence is expected to be in one-letter uppercase notation.
+            Otherwise, some of the cleavage rules in :py:data:`expasy_rules`
+            will not work as expected.
+
+    rule : str or compiled regex
+        A regular expression describing the site of cleavage. It is recommended
+        to design the regex so that it matches only the residue whose C-terminal
+        bond is to be cleaved. All additional requirements should be specified
+        using `lookaround assertions
+        <http://www.regular-expressions.info/lookaround.html>`_.
+        :py:data:`expasy_rules` contains cleavage rules for popular cleavage agents.
+    missed_cleavages : int, optional
+        Maximum number of allowed missed cleavages. Defaults to 0.
+    min_length : int or None, optional
+        Minimum peptide length. Defaults to :py:const:`None`.
+
+        ..note ::
+            This checks for string length, which is only correct for one-letter
+            notation and not for full *modX*. Use :py:func:`length` manually if
+            you know what you are doing and apply :py:func:`cleave` to *modX*
+            sequences.
+
+    Returns
+    -------
+    out : set
+        A set of unique (!) peptides.
+
+    Examples
+    --------
+    >>> cleave('AKAKBK', expasy_rules['trypsin'], 0) == {'AK', 'BK'}
+    True
+    >>> cleave('GKGKYKCK', expasy_rules['trypsin'], 2) == \
+    {'CK', 'GKYK', 'YKCK', 'GKGK', 'GKYKCK', 'GK', 'GKGKYK', 'YK'}
+    True
+
+    """
+    return set(_cleave(sequence, rule, missed_cleavages, min_length))
+
+def _cleave(sequence, rule, missed_cleavages=0, min_length=None):
+    """Like :py:func:`cleave`, but the result is a list. Refer to
+    :py:func:`cleave` for explanation of parameters.
+    """
+    peptides = []
+    ml = missed_cleavages+2
+    trange = range(ml)
+    cleavage_sites = deque([0], maxlen=ml)
+    cl = 1
+    for i in it.chain([x.end() for x in re.finditer(rule, sequence)],
+                   [None]):
+        cleavage_sites.append(i)
+        if cl < ml:
+            cl += 1
+        for j in trange[:cl-1]:
+            seq = sequence[cleavage_sites[j]:cleavage_sites[-1]]
+            if seq:
+                if min_length is None or len(seq) >= min_length:
+                    peptides.append(seq)
+    return peptides
+
+def num_sites(sequence, rule, **kwargs):
+    """Count the number of sites where `sequence` can be cleaved using
+    the given `rule` (e.g. number of miscleavages for a peptide).
+
+    Parameters
+    ----------
+    sequence : str
+        The sequence of a polypeptide.
+    rule : str or compiled regex
+        A regular expression describing the site of cleavage. It is recommended
+        to design the regex so that it matches only the residue whose C-terminal
+        bond is to be cleaved. All additional requirements should be specified
+        using `lookaround assertions
+        <http://www.regular-expressions.info/lookaround.html>`_.
+    labels : list, optional
+        A list of allowed labels for amino acids and terminal modifications.
+
+    Returns
+    -------
+    out : int
+        Number of cleavage sites.
+    """
+    return len(_cleave(sequence, rule, **kwargs)) - 1
+
+expasy_rules = {
+    'arg-c':         r'R',
+    'asp-n':         r'\w(?=D)',
+    'bnps-skatole' : r'W',
+    'caspase 1':     r'(?<=[FWYL]\w[HAT])D(?=[^PEDQKR])',
+    'caspase 2':     r'(?<=DVA)D(?=[^PEDQKR])',
+    'caspase 3':     r'(?<=DMQ)D(?=[^PEDQKR])',
+    'caspase 4':     r'(?<=LEV)D(?=[^PEDQKR])',
+    'caspase 5':     r'(?<=[LW]EH)D',
+    'caspase 6':     r'(?<=VE[HI])D(?=[^PEDQKR])',
+    'caspase 7':     r'(?<=DEV)D(?=[^PEDQKR])',
+    'caspase 8':     r'(?<=[IL]ET)D(?=[^PEDQKR])',
+    'caspase 9':     r'(?<=LEH)D',
+    'caspase 10':    r'(?<=IEA)D',
+    'chymotrypsin high specificity' : r'([FY](?=[^P]))|(W(?=[^MP]))',
+    'chymotrypsin low specificity':
+        r'([FLY](?=[^P]))|(W(?=[^MP]))|(M(?=[^PY]))|(H(?=[^DMPW]))',
+    'clostripain':   r'R',
+    'cnbr':          r'M',
+    'enterokinase':  r'(?<=[DE]{3})K',
+    'factor xa':     r'(?<=[AFGILTVM][DE]G)R',
+    'formic acid':   r'D',
+    'glutamyl endopeptidase': r'E',
+    'granzyme b':    r'(?<=IEP)D',
+    'hydroxylamine': r'N(?=G)',
+    'iodosobenzoic acid': r'W',
+    'lysc':          r'K',
+    'ntcb':          r'\w(?=C)',
+    'pepsin ph1.3':  r'((?<=[^HKR][^P])[^R](?=[FLWY][^P]))|'
+                     r'((?<=[^HKR][^P])[FLWY](?=\w[^P]))',
+    'pepsin ph2.0':  r'((?<=[^HKR][^P])[^R](?=[FL][^P]))|'
+                     r'((?<=[^HKR][^P])[FL](?=\w[^P]))',
+    'proline endopeptidase': r'(?<=[HKR])P(?=[^P])',
+    'proteinase k':  r'[AEFILTVWY]',
+    'staphylococcal peptidase i': r'(?<=[^E])E',
+    'thermolysin':   r'[^DE](?=[AFILMV])',
+    'thrombin':      r'((?<=G)R(?=G))|'
+                     r'((?<=[AFGILTVM][AFGILTVWA]P)R(?=[^DE][^DE]))',
+    'trypsin':       r'([KR](?=[^P]))|((?<=W)K(?=P))|((?<=M)R(?=P))'
+    }
+"""
+This dict contains regular expressions for cleavage rules of the most
+popular proteolytic enzymes. The rules were taken from the
+`PeptideCutter tool
+<http://ca.expasy.org/tools/peptidecutter/peptidecutter_enzymes.html>`_
+at Expasy.
+"""
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ensembl_cdna_translate.py	Wed Nov 29 15:55:59 2017 -0500
@@ -0,0 +1,426 @@
+#!/usr/bin/env python
+"""
+#
+#------------------------------------------------------------------------------
+#                         University of Minnesota
+#         Copyright 2017, Regents of the University of Minnesota
+#------------------------------------------------------------------------------
+# Author:
+#
+#  James E Johnson
+#
+#------------------------------------------------------------------------------
+"""
+
+import argparse
+import sys
+from time import sleep
+
+from Bio.Seq import translate
+
+import requests
+
+import digest
+
+
+server = "https://rest.ensembl.org"
+ext = "/info/assembly/homo_sapiens?"
+max_region = 5000000
+
+
+def ensembl_rest(ext, headers):
+    if True: print >> sys.stderr, "%s" % ext
+    r = requests.get(server+ext, headers=headers)
+    if r.status_code == 429:
+        print >> sys.stderr, "response headers: %s\n" % r.headers
+        if 'Retry-After' in r.headers:
+            sleep(r.headers['Retry-After'])
+            r = requests.get(server+ext, headers=headers)
+    if not r.ok:
+        r.raise_for_status()
+    return r
+
+
+def get_species():
+    results = dict()
+    ext = "/info/species"
+    req_header = {"Content-Type": "application/json"}
+    r = ensembl_rest(ext, req_header)
+    for species in r.json()['species']:
+        results[species['name']] = species
+        print >> sys.stdout,\
+            "%s\t%s\t%s\t%s\t%s"\
+            % (species['name'], species['common_name'], 
+               species['display_name'],
+               species['strain'],
+               species['taxon_id'])
+    return results
+
+
+def get_biotypes(species):
+    biotypes = []
+    ext = "/info/biotypes/%s?" % species
+    req_header = {"Content-Type": "application/json"}
+    r = ensembl_rest(ext, req_header)
+    for entry in r.json():
+        if 'biotype' in entry:
+            biotypes.append(entry['biotype'])
+    return biotypes
+        
+
+def get_toplevel(species):
+    coord_systems = dict()
+    ext = "/info/assembly/%s?" % species
+    req_header = {"Content-Type": "application/json"}
+    r = ensembl_rest(ext, req_header)
+    toplevel = r.json()
+    for seq in toplevel['top_level_region']:
+        if seq['coord_system'] not in coord_systems:
+            coord_systems[seq['coord_system']] = dict()
+        coord_system = coord_systems[seq['coord_system']]
+        coord_system[seq['name']] = int(seq['length'])
+    return coord_systems
+
+
+def get_transcripts_bed(species, refseq, start, length,params=None):
+    bed = []
+    param = params if params else ''
+    req_header = {"Content-Type": "text/x-bed"}
+    regions = range(start, length, max_region)
+    if not regions or regions[-1] < length:
+        regions.append(length)
+    for end in regions[1:]:
+        ext = "/overlap/region/%s/%s:%d-%d?feature=transcript;%s"\
+            % (species, refseq, start, end, param)
+        start = end + 1
+        r = ensembl_rest(ext, req_header)
+        if r.text:
+            bed += r.text.splitlines()
+    return bed
+
+
+def get_seq(id, seqtype,params=None):
+    param = params if params else ''
+    ext = "/sequence/id/%s?type=%s;%s" % (id, seqtype,param)
+    req_header = {"Content-Type": "text/plain"}
+    r = ensembl_rest(ext, req_header)
+    return r.text
+
+
+def get_cdna(id,params=None):
+    return get_seq(id, 'cdna',params=params)
+
+
+def get_cds(id,params=None):
+    return get_seq(id, 'cds',params=params)
+
+
+def bed_from_line(line):
+    fields = line.rstrip('\r\n').split('\t')
+    (chrom, chromStart, chromEnd, name, score, strand,
+     thickStart, thickEnd, itemRgb,
+     blockCount, blockSizes, blockStarts) = fields[0:12]
+    bed_entry = BedEntry(chrom=chrom, chromStart=chromStart, chromEnd=chromEnd,
+                         name=name, score=score, strand=strand,
+                         thickStart=thickStart, thickEnd=thickEnd,
+                         itemRgb=itemRgb,
+                         blockCount=blockCount,
+                         blockSizes=blockSizes.rstrip(','),
+                         blockStarts=blockStarts.rstrip(','))
+    return bed_entry
+
+
+class BedEntry(object):
+    def __init__(self, chrom=None, chromStart=None, chromEnd=None,
+                 name=None, score=None, strand=None,
+                 thickStart=None, thickEnd=None, itemRgb=None,
+                 blockCount=None, blockSizes=None, blockStarts=None):
+        self.chrom = chrom
+        self.chromStart = int(chromStart)
+        self.chromEnd = int(chromEnd)
+        self.name = name
+        self.score = int(score) if score is not None else 0
+        self.strand = '-' if str(strand).startswith('-') else '+'
+        self.thickStart = int(thickStart) if thickStart else self.chromStart
+        self.thickEnd = int(thickEnd) if thickEnd else self.chromEnd
+        self.itemRgb = str(itemRgb) if itemRgb is not None else r'100,100,100'
+        self.blockCount = int(blockCount)
+        if isinstance(blockSizes, str) or isinstance(blockSizes, unicode):
+            self.blockSizes = [int(x) for x in blockSizes.split(',')]
+        elif isinstance(blockSizes, list):
+            self.blockSizes = [int(x) for x in blockSizes]
+        else:
+            self.blockSizes = blockSizes
+        if isinstance(blockStarts, str) or isinstance(blockSizes, unicode):
+            self.blockStarts = [int(x) for x in blockStarts.split(',')]
+        elif isinstance(blockStarts, list):
+            self.blockStarts = [int(x) for x in blockStarts]
+        else:
+            self.blockStarts = blockStarts
+        self.seq = None
+        self.pep = None
+
+    def __str__(self):
+        return '%s\t%d\t%d\t%s\t%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s' % (
+            self.chrom, self.chromStart, self.chromEnd,
+            self.name, self.score, self.strand,
+            self.thickStart, self.thickEnd, str(self.itemRgb), self.blockCount,
+            ','.join([str(x) for x in self.blockSizes]),
+            ','.join([str(x) for x in self.blockStarts]))
+
+    # (start, end)
+    def get_subrange(self, tstart, tstop, debug=False):
+        chromStart = self.chromStart
+        chromEnd = self.chromEnd
+        if debug:
+            print >> sys.stderr, "%s" % (str(self))
+        r = range(self.blockCount)
+        if self.strand == '-':
+            r.reverse()
+        bStart = 0
+        bEnd = 0
+        for x in r:
+            bEnd = bStart + self.blockSizes[x]
+            if bStart <= tstart < bEnd:
+                if self.strand == '+':
+                    chromStart = self.chromStart + self.blockStarts[x] +\
+                        (tstart - bStart)
+                else:
+                    chromEnd = self.chromStart + self.blockStarts[x] +\
+                        self.blockSizes[x] - (tstart - bStart)
+            if bStart <= tstop < bEnd:
+                if self.strand == '+':
+                    chromEnd = self.chromStart + self.blockStarts[x] +\
+                        (tstop - bStart)
+                else:
+                    chromStart = self.chromStart + self.blockStarts[x] +\
+                        self.blockSizes[x] - (tstop - bStart)
+            if debug:
+                print >> sys.stderr,\
+                    "%3d %s\t%d\t%d\t%d\t%d\t%d\t%d"\
+                    % (x, self.strand, bStart, bEnd,
+                       tstart, tstop, chromStart, chromEnd)
+            bStart += self.blockSizes[x]
+        return(chromStart, chromEnd)
+
+    # get the blocks for sub range
+    def get_blocks(self, chromStart, chromEnd):
+        tblockCount = 0
+        tblockSizes = []
+        tblockStarts = []
+        for x in range(self.blockCount):
+            bStart = self.chromStart + self.blockStarts[x]
+            bEnd = bStart + self.blockSizes[x]
+            if bStart > chromEnd:
+                break
+            if bEnd < chromStart:
+                continue
+            cStart = max(chromStart, bStart)
+            tblockStarts.append(cStart - chromStart)
+            tblockSizes.append(min(chromEnd, bEnd) - cStart)
+            tblockCount += 1
+        return (tblockCount, tblockSizes, tblockStarts)
+
+    def trim(self, tstart, tstop, debug=False):
+        (tchromStart, tchromEnd) =\
+            self.get_subrange(tstart, tstop, debug=debug)
+        (tblockCount, tblockSizes, tblockStarts) =\
+            self.get_blocks(tchromStart, tchromEnd)
+        tbed = BedEntry(
+            chrom=self.chrom, chromStart=tchromStart, chromEnd=tchromEnd,
+            name=self.name, score=self.score, strand=self.strand,
+            thickStart=tchromStart, thickEnd=tchromEnd, itemRgb=self.itemRgb,
+            blockCount=tblockCount,
+            blockSizes=tblockSizes, blockStarts=tblockStarts)
+        if self.seq:
+            ts = tchromStart-self.chromStart
+            te = tchromEnd - tchromStart + ts
+            tbed.seq = self.seq[ts:te]
+        return tbed
+
+
+def __main__():
+    parser = argparse.ArgumentParser(
+        description='Retrieve Ensembl cDNAs and three frame translate')
+    parser.add_argument(
+        '-s', '--species', default='human',
+        help='Ensembl Species to retrieve')
+    parser.add_argument(
+        '-B', '--biotypes', action='append', default=[],
+        help='Restrict Ensembl biotypes to retrieve')
+    parser.add_argument(
+        '-i', '--input', default=None,
+        help='Use BED instead of retrieving cDNA from ensembl (-) for stdin')
+    parser.add_argument(
+        '-t', '--transcripts', default=None,
+        help='Path to output cDNA transcripts.bed (-) for stdout')
+    parser.add_argument(
+        '-r', '--raw', action='store_true',
+        help='Report transcript exacty as returned from Ensembl')
+    parser.add_argument(
+        '-f', '--fasta', default=None,
+        help='Path to output translations.fasta')
+    parser.add_argument(
+        '-b', '--bed', default=None,
+        help='Path to output translations.bed')
+    parser.add_argument(
+        '-m', '--min_length', type=int, default=7,
+        help='Minimum length of protein translation to report')
+    parser.add_argument(
+        '-e', '--enzyme', default=None,
+        help='Digest translation with enzyme')
+    parser.add_argument(
+        '-a', '--all', action='store_true',
+        help='Include reference protein translations')
+    parser.add_argument('-v', '--verbose', action='store_true', help='Verbose')
+    parser.add_argument('-d', '--debug', action='store_true', help='Debug')
+    args = parser.parse_args()
+    # print >> sys.stderr, "args: %s" % args
+    species = args.species
+    input_rdr = None
+    if args.input is not None:
+        input_rdr = open(args.input, 'r') if args.input != '-' else sys.stdin
+    tx_wtr = None
+    if args.transcripts is not None:
+        tx_wtr = open(args.transcripts, 'w')\
+            if args.transcripts != '-' else sys.stdout
+    fa_wtr = open(args.fasta, 'w') if args.fasta is not None else None
+    bed_wtr = open(args.bed, 'w') if args.bed is not None else None
+
+    enzyme = digest.expasy_rules.get(args.enzyme,args.enzyme)
+
+    # print >> sys.stderr, "args biotypes: %s" % args.biotypes
+    biotypea = ['biotype=%s' % bt.strip() for biotype in args.biotypes for bt in biotype.split(',')] 
+    # print >> sys.stderr, "args biotypes: %s" % biotypea
+    biotypes = ';'.join(['biotype=%s' % bt.strip() for biotype in args.biotypes for bt in biotype.split(',') if  bt.strip()]) 
+    # print >> sys.stderr, "biotypes: %s" % biotypes
+
+    translations = dict()  # start : end : seq
+
+    def unique_prot(tbed, seq):
+        if tbed.chromStart not in translations:
+            translations[tbed.chromStart] = dict()
+            translations[tbed.chromStart][tbed.chromEnd] = []
+            translations[tbed.chromStart][tbed.chromEnd].append(seq)
+        elif tbed.chromEnd not in translations[tbed.chromStart]:
+            translations[tbed.chromStart][tbed.chromEnd] = []
+            translations[tbed.chromStart][tbed.chromEnd].append(seq)
+        elif seq not in translations[tbed.chromStart][tbed.chromEnd]:
+            translations[tbed.chromStart][tbed.chromEnd].append(seq)
+        else:
+            return False
+        return True
+
+    def translate_bed(bed):
+        translate_count = 0
+        if any([fa_wtr, bed_wtr]):
+            transcript_id = bed.name
+            refprot = None
+            if not args.all:
+                try:
+                    cds = get_cds(transcript_id)
+                    if len(cds) % 3 != 0:
+                        cds = cds[:-(len(cds) % 3)]
+                    refprot = translate(cds) if cds else None
+                except:
+                    refprot = None
+            cdna = get_cdna(transcript_id)
+            cdna_len = len(cdna)
+            for offset in range(3):
+                seqend = cdna_len - (cdna_len - offset) % 3
+                aaseq = translate(cdna[offset:seqend])
+                aa_start = 0
+                while aa_start < len(aaseq):
+                    aa_end = aaseq.find('*', aa_start)
+                    if aa_end < 0:
+                        aa_end = len(aaseq)
+                    prot = aaseq[aa_start:aa_end]
+                    if enzyme and refprot:
+                        frags = digest._cleave(prot,enzyme)
+                        for frag in reversed(frags):
+                            if frag in refprot:
+                                prot = prot[:prot.rfind(frag)]
+                            else:
+                                break
+                    if len(prot) < args.min_length:
+                        pass
+                    elif refprot and prot in refprot:
+                        pass
+                    else:
+                        tstart = aa_start*3+offset
+                        tend = aa_end*3+offset
+                        prot_acc = "%s_%d_%d" % (transcript_id, tstart, tend)
+                        tbed = bed.trim(tstart, tend)
+                        if args.all or unique_prot(tbed, prot):
+                            translate_count += 1
+                            tbed.name = prot_acc
+                            bed_wtr.write("%s\t%s\n" % (str(tbed), prot))
+                            bed_wtr.flush()
+                            fa_id = ">%s\n" % (prot_acc)
+                            fa_wtr.write(fa_id)
+                            fa_wtr.write(prot)
+                            fa_wtr.write("\n")
+                            fa_wtr.flush()
+                    aa_start = aa_end + 1
+        return translate_count
+
+    if input_rdr:
+        translation_count = 0
+        for i, bedline in enumerate(input_rdr):
+            try:
+                bed = bed_from_line(bedline)
+                translation_count += translate_bed(bed)
+            except:
+                print >> sys.stderr, "BED format error: %s\n" % bedline
+        if args.debug or (args.verbose and any([fa_wtr, bed_wtr])):
+            print >> sys.stderr,\
+                "%s\tcDNA translations:%d" % (species, translation_count)
+    else:
+        coord_systems = get_toplevel(species)
+        if 'chromosome' in coord_systems:
+            for ref in sorted(coord_systems['chromosome'].keys()):
+                length = coord_systems['chromosome'][ref]
+                if not any([tx_wtr, fa_wtr, bed_wtr]):
+                    print >> sys.stderr,\
+                        "%s\t%s\tlength: %d" % (species, ref, length)
+                    continue
+                if args.debug:
+                    print >> sys.stderr,\
+                        "Retrieving transcripts: %s\t%s\tlength: %d"\
+                        % (species, ref, length)
+                translation_count = 0
+                start = 0
+                regions = range(start, length, max_region)
+                if not regions or regions[-1] < length:
+                    regions.append(length)
+                for end in regions[1:]:
+                    bedlines = get_transcripts_bed(species, ref, start, end, params=biotypes)
+                    if args.verbose or args.debug:
+                        print >> sys.stderr,\
+                            "%s\t%s\tstart: %d\tend: %d\tcDNA transcripts:%d"\
+                            % (species, ref, start, end, len(bedlines))
+                    # start, end, seq
+                    for i, bedline in enumerate(bedlines):
+                        try:
+                            bed = bed_from_line(bedline)\
+                                if any([not args.raw, fa_wtr, bed_wtr])\
+                                else None
+                            if tx_wtr:
+                                tx_wtr.write(bedline if args.raw else str(bed))
+                                tx_wtr.write("\n")
+                                tx_wtr.flush()
+                            if bed:
+                                translation_count += translate_bed(bed)
+                        except Exception as e:
+                            print >> sys.stderr,\
+                                "BED error (%s) : %s\n" % (e, bedline)
+                    start = end + 1
+
+                if args.debug or (args.verbose and any([fa_wtr, bed_wtr])):
+                    print >> sys.stderr,\
+                        "%s\t%s\tlength: %d\tcDNA translations:%d"\
+                        % (species, ref, length, translation_count)
+
+
+if __name__ == "__main__":
+    __main__()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ensembl_cdna_translate.xml	Wed Nov 29 15:55:59 2017 -0500
@@ -0,0 +1,248 @@
+<tool id="ensembl_cdna_translate" name="Ensembl cDNA Translations" version="0.1.0">
+    <description>using Ensembl REST API</description>
+    <requirements>
+        <requirement type="package" version="0.4.10">requests-cache</requirement>
+        <requirement type="package" version="1.62">biopython</requirement>
+    </requirements>
+    <stdio>
+        <exit_code range="1:" />
+    </stdio>
+    <command><![CDATA[
+        #if $input:
+            cat '$input' 
+        #else
+            python '$__tool_directory__/ensembl_cdna_translate.py'  
+            #if $species:
+                --species '$species'
+            #end if
+            $transcript_raw
+            #if $biotypes:
+                --biotypes '$biotypes'
+            #end if
+            #if str($output_choice).find('transcript_bed') >= 0:
+                --transcripts 
+                #if str($output_choice).find('translation') >= 0:
+                    '-' | tee '$transcript_bed'
+                #else
+                    '$transcript_bed'
+                #end if
+            #elif str($output_choice).find('translation') >= 0:
+                --transcripts '-'
+            #end if
+        #end if
+        #if str($output_choice).find('translation') >= 0:
+          | python '$__tool_directory__/ensembl_cdna_translate.py' -i '-' 
+            #if $input and str($output_choice).find('transcript_bed') >= 0:
+                --transcripts '$transcript_bed
+            #end if
+            #if str($output_choice).find('translation_bed') >= 0: 
+                --bed '$translation_bed'
+            #end if
+            #if str($output_choice).find('translation_fasta') >= 0: 
+                --fasta '$translation_fasta'
+            #end if
+            #if $enzyme:
+                --enzyme '$enzyme'
+            #end if
+        #end if
+       
+    ]]></command>
+    <inputs>
+        <param name="species" type="text" value="" label="Ensembl species" >
+            <help>
+            </help>
+            <option value="homo_sapiens">homo_sapiens  (Human) taxon_id: 9606</option>
+            <option value="mus_musculus">mus_musculus  (Mouse) taxon_id: 10090</option>
+            <option value="ailuropoda_melanoleuca">ailuropoda_melanoleuca  (Panda) taxon_id: 9646</option>
+            <option value="anas_platyrhynchos">anas_platyrhynchos  (Duck) taxon_id: 8839</option>
+            <option value="anolis_carolinensis">anolis_carolinensis  (Anole lizard) taxon_id: 28377</option>
+            <option value="astyanax_mexicanus">astyanax_mexicanus  (Cave fish) taxon_id: 7994</option>
+            <option value="bos_taurus">bos_taurus  (Cow) taxon_id: 9913</option>
+            <option value="caenorhabditis_elegans">caenorhabditis_elegans  (Caenorhabditis elegans) taxon_id: 6239</option>
+            <option value="callithrix_jacchus">callithrix_jacchus  (Marmoset) taxon_id: 9483</option>
+            <option value="canis_familiaris">canis_familiaris  (Dog) taxon_id: 9615</option>
+            <option value="carlito_syrichta">carlito_syrichta  (Tarsier) taxon_id: 1868482</option>
+            <option value="cavia_aperea">cavia_aperea  (Brazilian guinea pig) taxon_id: 37548</option>
+            <option value="cavia_porcellus">cavia_porcellus  (Guinea Pig) taxon_id: 10141</option>
+            <option value="chinchilla_lanigera">chinchilla_lanigera  (Long-tailed chinchilla) taxon_id: 34839</option>
+            <option value="chlorocebus_sabaeus">chlorocebus_sabaeus  (Vervet-AGM) taxon_id: 60711</option>
+            <option value="choloepus_hoffmanni">choloepus_hoffmanni  (Sloth) taxon_id: 9358</option>
+            <option value="ciona_intestinalis">ciona_intestinalis  (C.intestinalis) taxon_id: 7719</option>
+            <option value="ciona_savignyi">ciona_savignyi  (C.savignyi) taxon_id: 51511</option>
+            <option value="cricetulus_griseus_chok1gshd">cricetulus_griseus_chok1gshd  (Chinese hamster CHOK1GS) taxon_id: 10029</option>
+            <option value="cricetulus_griseus_crigri">cricetulus_griseus_crigri  (Chinese hamster CriGri) taxon_id: 10029</option>
+            <option value="danio_rerio">danio_rerio  (Zebrafish) taxon_id: 7955</option>
+            <option value="dasypus_novemcinctus">dasypus_novemcinctus  (Armadillo) taxon_id: 9361</option>
+            <option value="dipodomys_ordii">dipodomys_ordii  (Kangaroo rat) taxon_id: 10020</option>
+            <option value="drosophila_melanogaster">drosophila_melanogaster  (Fruitfly) taxon_id: 7227</option>
+            <option value="echinops_telfairi">echinops_telfairi  (Lesser hedgehog tenrec) taxon_id: 9371</option>
+            <option value="equus_caballus">equus_caballus  (Horse) taxon_id: 9796</option>
+            <option value="erinaceus_europaeus">erinaceus_europaeus  (Hedgehog) taxon_id: 9365</option>
+            <option value="felis_catus">felis_catus  (Cat) taxon_id: 9685</option>
+            <option value="ficedula_albicollis">ficedula_albicollis  (Flycatcher) taxon_id: 59894</option>
+            <option value="fukomys_damarensis">fukomys_damarensis  (Damara mole rat) taxon_id: 885580</option>
+            <option value="gadus_morhua">gadus_morhua  (Cod) taxon_id: 8049</option>
+            <option value="gallus_gallus">gallus_gallus  (Chicken) taxon_id: 9031</option>
+            <option value="gasterosteus_aculeatus">gasterosteus_aculeatus  (Stickleback) taxon_id: 69293</option>
+            <option value="gorilla_gorilla">gorilla_gorilla  (Gorilla) taxon_id: 9595</option>
+            <option value="heterocephalus_glaber_female">heterocephalus_glaber_female  (Naked mole-rat female) taxon_id: 10181</option>
+            <option value="heterocephalus_glaber_male">heterocephalus_glaber_male  (Naked mole-rat male) taxon_id: 10181</option>
+            <option value="ictidomys_tridecemlineatus">ictidomys_tridecemlineatus  (Squirrel) taxon_id: 43179</option>
+            <option value="jaculus_jaculus">jaculus_jaculus  (Lesser Egyptian jerboa) taxon_id: 51337</option>
+            <option value="latimeria_chalumnae">latimeria_chalumnae  (Coelacanth) taxon_id: 7897</option>
+            <option value="lepisosteus_oculatus">lepisosteus_oculatus  (Spotted gar) taxon_id: 7918</option>
+            <option value="loxodonta_africana">loxodonta_africana  (Elephant) taxon_id: 9785</option>
+            <option value="macaca_mulatta">macaca_mulatta  (Macaque) taxon_id: 9544</option>
+            <option value="meleagris_gallopavo">meleagris_gallopavo  (Turkey) taxon_id: 9103</option>
+            <option value="mesocricetus_auratus">mesocricetus_auratus  (Golden Hamster) taxon_id: 10036</option>
+            <option value="microcebus_murinus">microcebus_murinus  (Mouse Lemur) taxon_id: 30608</option>
+            <option value="microtus_ochrogaster">microtus_ochrogaster  (Prairie vole) taxon_id: 79684</option>
+            <option value="monodelphis_domestica">monodelphis_domestica  (Opossum) taxon_id: 13616</option>
+            <option value="mus_caroli">mus_caroli  (Ryukyu mouse) taxon_id: 10089</option>
+            <option value="mus_musculus_129s1svimj">mus_musculus_129s1svimj  (Mouse 129S1/SvImJ) taxon_id: 10090</option>
+            <option value="mus_musculus_aj">mus_musculus_aj  (Mouse A/J) taxon_id: 10090</option>
+            <option value="mus_musculus_akrj">mus_musculus_akrj  (Mouse AKR/J) taxon_id: 10090</option>
+            <option value="mus_musculus_balbcj">mus_musculus_balbcj  (Mouse BALB/cJ) taxon_id: 10090</option>
+            <option value="mus_musculus_c3hhej">mus_musculus_c3hhej  (Mouse C3H/HeJ) taxon_id: 10090</option>
+            <option value="mus_musculus_c57bl6nj">mus_musculus_c57bl6nj  (Mouse C57BL/6NJ) taxon_id: 10090</option>
+            <option value="mus_musculus_casteij">mus_musculus_casteij  (Mouse CAST/EiJ) taxon_id: 10091</option>
+            <option value="mus_musculus_cbaj">mus_musculus_cbaj  (Mouse CBA/J) taxon_id: 10090</option>
+            <option value="mus_musculus_dba2j">mus_musculus_dba2j  (Mouse DBA/2J) taxon_id: 10090</option>
+            <option value="mus_musculus_fvbnj">mus_musculus_fvbnj  (Mouse FVB/NJ) taxon_id: 10090</option>
+            <option value="mus_musculus_lpj">mus_musculus_lpj  (Mouse LP/J) taxon_id: 10090</option>
+            <option value="mus_musculus_nodshiltj">mus_musculus_nodshiltj  (Mouse NOD/ShiLtJ) taxon_id: 10090</option>
+            <option value="mus_musculus_nzohlltj">mus_musculus_nzohlltj  (Mouse NZO/HlLtJ) taxon_id: 10090</option>
+            <option value="mus_musculus_pwkphj">mus_musculus_pwkphj  (Mouse PWK/PhJ) taxon_id: 39442</option>
+            <option value="mus_musculus_wsbeij">mus_musculus_wsbeij  (Mouse WSB/EiJ) taxon_id: 10092</option>
+            <option value="mus_pahari">mus_pahari  (Shrew mouse) taxon_id: 10093</option>
+            <option value="mus_spretus_spreteij">mus_spretus_spreteij  (Algerian mouse) taxon_id: 10096</option>
+            <option value="mustela_putorius_furo">mustela_putorius_furo  (Ferret) taxon_id: 9669</option>
+            <option value="myotis_lucifugus">myotis_lucifugus  (Microbat) taxon_id: 59463</option>
+            <option value="nannospalax_galili">nannospalax_galili  (Upper Galilee mountains blind mole rat) taxon_id: 1026970</option>
+            <option value="nomascus_leucogenys">nomascus_leucogenys  (Gibbon) taxon_id: 61853</option>
+            <option value="notamacropus_eugenii">notamacropus_eugenii  (Wallaby) taxon_id: 9315</option>
+            <option value="ochotona_princeps">ochotona_princeps  (Pika) taxon_id: 9978</option>
+            <option value="octodon_degus">octodon_degus  (Degu) taxon_id: 10160</option>
+            <option value="oreochromis_niloticus">oreochromis_niloticus  (Tilapia) taxon_id: 8128</option>
+            <option value="ornithorhynchus_anatinus">ornithorhynchus_anatinus  (Platypus) taxon_id: 9258</option>
+            <option value="oryctolagus_cuniculus">oryctolagus_cuniculus  (Rabbit) taxon_id: 9986</option>
+            <option value="oryzias_latipes">oryzias_latipes  (Medaka) taxon_id: 8090</option>
+            <option value="otolemur_garnettii">otolemur_garnettii  (Bushbaby) taxon_id: 30611</option>
+            <option value="ovis_aries">ovis_aries  (Sheep) taxon_id: 9940</option>
+            <option value="pan_troglodytes">pan_troglodytes  (Chimpanzee) taxon_id: 9598</option>
+            <option value="papio_anubis">papio_anubis  (Olive baboon) taxon_id: 9555</option>
+            <option value="pelodiscus_sinensis">pelodiscus_sinensis  (Chinese softshell turtle) taxon_id: 13735</option>
+            <option value="peromyscus_maniculatus_bairdii">peromyscus_maniculatus_bairdii  (Northern American deer mouse) taxon_id: 230844</option>
+            <option value="petromyzon_marinus">petromyzon_marinus  (Lamprey) taxon_id: 7757</option>
+            <option value="poecilia_formosa">poecilia_formosa  (Amazon molly) taxon_id: 48698</option>
+            <option value="pongo_abelii">pongo_abelii  (Orangutan) taxon_id: 9601</option>
+            <option value="procavia_capensis">procavia_capensis  (Hyrax) taxon_id: 9813</option>
+            <option value="pteropus_vampyrus">pteropus_vampyrus  (Megabat) taxon_id: 132908</option>
+            <option value="rattus_norvegicus">rattus_norvegicus  (Rat) taxon_id: 10116</option>
+            <option value="saccharomyces_cerevisiae">saccharomyces_cerevisiae  (Saccharomyces cerevisiae) taxon_id: 4932</option>
+            <option value="sarcophilus_harrisii">sarcophilus_harrisii  (Tasmanian devil) taxon_id: 9305</option>
+            <option value="sorex_araneus">sorex_araneus  (Shrew) taxon_id: 42254</option>
+            <option value="sus_scrofa">sus_scrofa  (Pig) taxon_id: 9823</option>
+            <option value="taeniopygia_guttata">taeniopygia_guttata  (Zebra Finch) taxon_id: 59729</option>
+            <option value="takifugu_rubripes">takifugu_rubripes  (Fugu) taxon_id: 31033</option>
+            <option value="tetraodon_nigroviridis">tetraodon_nigroviridis  (Tetraodon) taxon_id: 99883</option>
+            <option value="tupaia_belangeri">tupaia_belangeri  (Tree Shrew) taxon_id: 37347</option>
+            <option value="tursiops_truncatus">tursiops_truncatus  (Dolphin) taxon_id: 9739</option>
+            <option value="vicugna_pacos">vicugna_pacos  (Alpaca) taxon_id: 30538</option>
+            <option value="xenopus_tropicalis">xenopus_tropicalis  (Xenopus) taxon_id: 8364</option>
+            <option value="xiphophorus_maculatus">xiphophorus_maculatus  (Platyfish) taxon_id: 8083</option>
+
+        </param>
+        <param name="biotypes" type="text" value="" optional="true" label="Restrict to these biotypes" >
+            <help><![CDATA[
+Example biotypes: 
+protein_coding, non_coding, pseudogene, nonsense_mediated_decay, non_stop_decay, 
+translated_processed_pseudogene, transcribed_processed_pseudogene, transcribed_unitary_pseudogene, transcribed_unprocessed_pseudogene, 
+polymorphic_pseudogene, processed_pseudogene, unprocessed_pseudogene, unitary_pseudogene, processed_transcript, 
+retained_intron, ccds_gene, sense_overlapping, sense_intronic, cdna_update, antisense, 
+LRG_gene, IG_C_gene, IG_D_gene, IG_J_gene, IG_LV_gene IG_V_gene, TR_C_gene, TR_D_gene, TR_J_gene, TR_V_gene, 
+IG_pseudogene, IG_C_pseudogene, IG_D_pseudogene, IG_J_pseudogene, IG_V_pseudogene, TR_J_pseudogene, TR_V_pseudogene, TEC, 
+ribozyme, RNase_P_RNA, guide_RNA, macro_lncRNA, bidirectional_promoter_lncRNA, 3prime_overlapping_ncRNA, antisense_RNA, vaultRNA, Y_RNA, SRP_RNA, RNase_MRP_RNA, IG_C_pseudogene, lncRNA, lincRNA, miRNA, snRNA, sRNA, telomerase_RNA, Mt_tRNA, Mt_rRNA, scaRNA, misc_RNA, rRNA, tRNA, scRNA, snoRNA, other
+            ]]></help>
+        </param>
+        <param name="input" type="data" format="bed" optional="true" label="A BED file with 12 columns, thickStart and thickEnd define protein coding region"/>
+        <param name="translate_all" type="boolean" truevalue="--all" falsevalue="" checked="false" 
+            label="Report all translations (Default is non reference protein sequences)"/>
+        <param name="transcript_raw" type="boolean" truevalue="--raw" falsevalue="" checked="true" 
+            label="Keep extra columns from ensembl BED"/>
+        <param name="output_choice" type="select" multiple="true" display="checkboxes" label="Outputs">
+            <option value="transcript_bed">transcripts.bed</option>
+            <option value="translation_bed">translation.bed</option>
+            <option value="translation_fasta">translation.fasta</option>
+        </param>
+        <param name="min_length" type="integer" value="7" min="1" label="Minimum length of protein translation to report"/>
+        <param name="enzyme" type="select" optional="true" label="Digest enzyme" 
+             help="Remove frags that are in a reference protein">
+            <option value="trypsin">trypsin:       ([KR](?=[^P]))|((?&lt;=W)K(?=P))|((?&lt;=M)R(?=P))</option>
+        </param>
+    </inputs>
+    <outputs>
+        <data name="transcript_bed" format="bed" label="Ensembl $species transcripts.bed">
+            <filter>'transcript_bed' in output_choice</filter>
+        </data>
+        <data name="translation_bed" format="bed" label="Ensembl $species translation.bed">
+            <filter>'translation_bed' in output_choice</filter>
+        </data>
+        <data name="translation_fasta" format="fasta" label="Ensembl $species translation.fasta">
+            <filter>'translation_fasta' in output_choice</filter>
+        </data>
+    </outputs>
+    <tests>
+        <test>
+            <param name="species" value="human"/>
+            <param name="input" value="human_transcripts.bed" ftype="bed"/>
+            <param name="output_choice" value="translation_bed,translation_fasta"/>
+            <output name="translation_bed">
+                <assert_contents>
+                    <has_text text="ENST00000641515" />
+                </assert_contents>
+            </output>
+            <output name="translation_fasta">
+                <assert_contents>
+                    <has_text text=">ENST00000641515" />
+                </assert_contents>
+            </output>
+        </test>
+    </tests>
+    <help><![CDATA[
+        usage: ensembl_cdna_translate.py [-h] [-s SPECIES] [-i INPUT] [-t TRANSCRIPTS]
+                                 [-r] [-f FASTA] [-b BED] [-m MIN_LENGTH] [-a]
+                                 [-v] [-d]
+
+Retrieve Ensembl cDNAs and three frame translate
+
+optional arguments:
+  -h, --help            show this help message and exit
+  -s SPECIES, --species SPECIES
+                        Ensembl Species to retrieve
+  -i INPUT, --input INPUT
+                        Use this bed instead of retrieving cDNA from ensembl
+                        (-) for stdin
+  -t TRANSCRIPTS, --transcripts TRANSCRIPTS
+                        Path to output cDNA transcripts.bed (-) for stdout
+  -r, --raw             Report transcript exacty as returned from Ensembl
+  -f FASTA, --fasta FASTA
+                        Path to output translations.fasta
+  -b BED, --bed BED     Path to output translations.bed
+  -m MIN_LENGTH, --min_length MIN_LENGTH
+                        Minimum length of protein translation to report
+  -a, --all             Report all translations (Default is non reference
+                        protein sequences)
+  -v, --verbose         Verbose
+  -d, --debug           Debug
+
+Esmebl REST API returns a 20 BED format with these additional columns::
+
+  second_name, cds_start_status, cds_end_status, exon_frames, type, gene_name, second_gene_name, gene_type
+
+    ]]></help>
+    <citations>
+        <citation type="doi">10.1093/bioinformatics/btu613</citation>
+        <citation type="doi">10.1093/nar/gku1010</citation>
+    </citations>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/human_transcripts.bed	Wed Nov 29 15:55:59 2017 -0500
@@ -0,0 +1,19 @@
+chr1	14403	29570	ENST00000488147	1000	-	14402	14402	0,0,0	11	98,34,152,159,198,136,137,147,99,154,37,	0,601,1392,2203,2454,2829,3202,3511,3864,10334,15130,	WASH7P-201	none	none	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,	unprocessed_pseudogene	ENSG00000227232	WASH7P	unprocessed_pseudogene
+chr1	29553	31097	ENST00000473358	1000	+	29552	29552	0,0,0	3	486,104,122,	0,1010,1422,	MIR1302-2HG-202	none	none	-1,-1,-1,	lincRNA	ENSG00000243485	MIR1302-2HG	lincRNA
+chr1	30266	31109	ENST00000469289	1000	+	30265	30265	0,0,0	2	401,134,	0,709,	MIR1302-2HG-201	none	none	-1,-1,	lincRNA	ENSG00000243485	MIR1302-2HG	lincRNA
+chr1	30365	30503	ENST00000607096	1000	+	30364	30364	0,0,0	1	138,	0,	MIR1302-2-201	none	none	-1,	miRNA	ENSG00000284332	MIR1302-2	miRNA
+chr1	34553	36081	ENST00000417324	1000	-	34552	34552	0,0,0	3	621,205,361,	0,723,1167,	FAM138A-201	none	none	-1,-1,-1,	lincRNA	ENSG00000237613	FAM138A	lincRNA
+chr1	35244	36073	ENST00000461467	1000	-	35243	35243	0,0,0	2	237,353,	0,476,	FAM138A-202	none	none	-1,-1,	lincRNA	ENSG00000237613	FAM138A	lincRNA
+chr1	52472	53312	ENST00000606857	1000	+	52471	52471	0,0,0	1	840,	0,	AL627309.6-201	none	none	-1,	unprocessed_pseudogene	ENSG00000268020	AL627309.6	unprocessed_pseudogene
+chr1	57597	64116	ENST00000642116	1000	+	57596	57596	0,0,0	3	56,157,1201,	0,1102,5318,	OR4G11P-202	none	none	-1,-1,-1,	processed_transcript	ENSG00000240361	OR4G11P	transcribed_unprocessed_pseudogene
+chr1	62948	63887	ENST00000492842	1000	+	62947	62947	0,0,0	1	939,	0,	OR4G11P-201	none	none	-1,	transcribed_unprocessed_pseudogene	ENSG00000240361	OR4G11P	transcribed_unprocessed_pseudogene
+chr1	65418	71585	ENST00000641515	1000	+	69090	70008	0,0,0	3	15,54,2549,	0,101,3618,	OR4F5-202	cmpl	cmpl	-1,-1,0,	protein_coding	ENSG00000186092	OR4F5	protein_coding
+chr1	69054	70108	ENST00000335137	1000	+	69090	70008	0,0,0	1	1054,	0,	OR4F5-201	cmpl	cmpl	0,	protein_coding	ENSG00000186092	OR4F5	protein_coding
+chr1	131024	134836	ENST00000442987	1000	+	131023	131023	0,0,0	1	3812,	0,	CICP27-201	none	none	-1,	processed_pseudogene	ENSG00000233750	CICP27	processed_pseudogene
+chr1	139789	140339	ENST00000493797	1000	-	139788	139788	0,0,0	2	58,265,	0,285,	AL627309.2-201	none	none	-1,-1,	antisense_RNA	ENSG00000239906	AL627309.2	antisense_RNA
+chr1	157783	157887	ENST00000410691	1000	-	157782	157782	0,0,0	1	104,	0,	RNU6-1100P-201	none	none	-1,	snRNA	ENSG00000222623	RNU6-1100P	snRNA
+chr1	187890	187958	ENST00000612080	1000	-	187889	187889	0,0,0	1	68,	0,	MIR6859-2-201	none	none	-1,	miRNA	ENSG00000273874	MIR6859-2	miRNA
+chr1	263014	297502	ENST00000424587	1000	-	263013	263013	0,0,0	4	5190,150,105,158,	0,5652,26251,34330,	AP006222.1-206	none	none	-1,-1,-1,-1,	processed_transcript	ENSG00000228463	AP006222.1	transcribed_processed_pseudogene
+chr1	347981	348366	ENST00000458203	1000	-	347980	347980	0,0,0	1	385,	0,	RPL23AP24-201	none	none	-1,	processed_pseudogene	ENSG00000236679	RPL23AP24	processed_pseudogene
+chr1	439869	440232	ENST00000437905	1000	+	439868	439868	0,0,0	1	363,	0,	WBP1LP7-201	none	none	-1,	processed_pseudogene	ENSG00000269732	WBP1LP7	processed_pseudogene
+chr1	450702	451697	ENST00000426406	1000	-	450739	451678	0,0,0	1	995,	0,	OR4F29-201	cmpl	cmpl	0,	protein_coding	ENSG00000284733	OR4F29	protein_coding