Previous changeset 9:7d67331368f3 (2015-04-23) Next changeset 11:5c6f33e20fcc (2015-04-24) |
Commit message:
fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo |
added:
GFFParser.py README.md bed_to_gff.py bed_to_gff.xml gbk_to_gff.py gbk_to_gff.xml gff_to_bed.py gff_to_bed.xml gff_to_gtf.py gff_to_gtf.xml gtf_to_gff.py gtf_to_gff.xml helper.py tool_conf.xml.sample tool_dependencies.xml |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 GFFParser.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GFFParser.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
b'@@ -0,0 +1,496 @@\n+#!/usr/bin/env python\n+"""\n+Extract genome annotation from a GFF (a tab delimited format for storing sequence features and annotations) file.\n+\n+Requirements: \n+ Numpy :- http://numpy.org/ \n+\n+Copyright (C)\t\n+\n+2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany. \n+2012-2015 Memorial Sloan Kettering Cancer Center, New York City, USA.\n+"""\n+\n+import re\n+import os\n+import sys\n+import urllib\n+import numpy as np\n+import helper as utils \n+from collections import defaultdict\n+\n+def attribute_tags(col9):\n+ """ \n+ Split the key-value tags from the attribute column, it takes column number 9 from GTF/GFF file \n+\n+ @args col9: attribute column from GFF file \n+ @type col9: str\n+ """\n+ info = defaultdict(list)\n+ is_gff = False\n+ \n+ if not col9:\n+ return is_gff, info\n+ \n+ # trim the line ending semi-colon ucsc may have some white-space \n+ col9 = col9.rstrip(\';| \')\n+ # attributes from 9th column \n+ atbs = col9.split(" ; ")\n+ if len(atbs) == 1:\n+ atbs = col9.split("; ")\n+ if len(atbs) == 1:\n+ atbs = col9.split(";")\n+ # check the GFF3 pattern which has key value pairs like:\n+ gff3_pat = re.compile("\\w+=")\n+ # sometime GTF have: gene_id uc002zkg.1;\n+ gtf_pat = re.compile("\\s?\\w+\\s")\n+\n+ key_vals = []\n+\n+ if gff3_pat.match(atbs[0]): # gff3 pattern \n+ is_gff = True\n+ key_vals = [at.split(\'=\') for at in atbs]\n+ elif gtf_pat.match(atbs[0]): # gtf pattern\n+ for at in atbs:\n+ key_vals.append(at.strip().split(" ",1))\n+ else:\n+ # to handle attribute column has only single value \n+ key_vals.append([\'ID\', atbs[0]])\n+ # get key, val items \n+ for item in key_vals:\n+ key, val = item\n+ # replace the double qoutes from feature identifier \n+ val = re.sub(\'"\', \'\', val)\n+ # replace the web formating place holders to plain text format \n+ info[key].extend([urllib.unquote(v) for v in val.split(\',\') if v])\n+\n+ return is_gff, info\n+ \n+def spec_features_keywd(gff_parts):\n+ """\n+ Specify the feature key word according to the GFF specifications\n+\n+ @args gff_parts: attribute field key \n+ @type gff_parts: str \n+ """\n+ for t_id in ["transcript_id", "transcriptId", "proteinId"]:\n+ try:\n+ gff_parts["info"]["Parent"] = gff_parts["info"][t_id]\n+ break\n+ except KeyError:\n+ pass\n+ for g_id in ["gene_id", "geneid", "geneId", "name", "gene_name", "genename"]:\n+ try:\n+ gff_parts["info"]["GParent"] = gff_parts["info"][g_id]\n+ break\n+ except KeyError:\n+ pass\n+ ## TODO key words\n+ for flat_name in ["Transcript", "CDS"]:\n+ if gff_parts["info"].has_key(flat_name):\n+ # parents\n+ if gff_parts[\'type\'] in [flat_name] or re.search(r\'transcript\', gff_parts[\'type\'], re.IGNORECASE):\n+ if not gff_parts[\'id\']:\n+ gff_parts[\'id\'] = gff_parts[\'info\'][flat_name][0]\n+ #gff_parts["info"]["ID"] = [gff_parts["id"]]\n+ # children \n+ elif gff_parts["type"] in ["intron", "exon", "three_prime_UTR",\n+ "coding_exon", "five_prime_UTR", "CDS", "stop_codon",\n+ "start_codon"]:\n+ gff_parts["info"]["Parent"] = gff_parts["info"][flat_name]\n+ break\n+ return gff_parts\n+\n+def Parse(ga_file):\n+ """\n+ Parsing GFF/GTF file based on feature relationship, it takes the input file.\n+\n+ @args ga_file: input file name \n+ @type ga_file: str \n+ """\n+ child_map = defaultdict(list)\n+ parent_map = dict()\n+\n+ ga_handle = utils.open_file(ga_file)\n+\n+ for rec in ga_handle:\n+ rec = rec.strip(\'\\n\\r\')\n+ \n+ # skip empty line fasta identifier and commented line\n+ if not rec or rec[0] in [\'#\', \'>\']:\n+ continue\n+ '..b'lete\'] = []\n+ gene[g_cnt][\'is_complete\'] = []\n+ gene[g_cnt][\'is_correctly_gff3_referenced\'] = \'\'\n+ gene[g_cnt][\'splicegraph\'] = []\n+ g_cnt += 1 \n+\n+ ## deleting empty gene records from the main array\n+ XPFLG=0\n+ for XP, ens in enumerate(gene):\n+ if ens[0]==0:\n+ XPFLG=1\n+ break\n+ \n+ if XPFLG==1:\n+ XQC = range(XP, len(gene)+1)\n+ gene = np.delete(gene, XQC)\n+\n+ return gene \n+\n+def NonetoemptyList(XS):\n+ """\n+ Convert a None type to empty list \n+\n+ @args XS: None type \n+ @type XS: str \n+ """\n+ return [] if XS is None else XS \n+\n+def create_missing_feature_type(p_feat, c_feat):\n+ """\n+ GFF/GTF file defines only child features. This function tries to create \n+ the parent feature from the information provided in the attribute column. \n+\n+ example: \n+ chr21 hg19_knownGene exon 9690071 9690100 0.000000 + . gene_id "uc002zkg.1"; transcript_id "uc002zkg.1"; \n+ chr21 hg19_knownGene exon 9692178 9692207 0.000000 + . gene_id "uc021wgt.1"; transcript_id "uc021wgt.1"; \n+ chr21 hg19_knownGene exon 9711935 9712038 0.000000 + . gene_id "uc011abu.2"; transcript_id "uc011abu.2"; \n+\n+ This function gets the parsed feature annotations. \n+ \n+ @args p_feat: Parent feature map \n+ @type p_feat: collections defaultdict\n+ @args c_feat: Child feature map \n+ @type c_feat: collections defaultdict\n+ """\n+\n+ child_n_map = defaultdict(list)\n+ for fid, det in c_feat.items():\n+ # get the details from grand child \n+ GID = STRD = SCR = None\n+ SPOS, EPOS = [], [] \n+ TYP = dict()\n+ for gchild in det:\n+ GID = gchild.get(\'gene_id\', [\'\'])[0] \n+ SPOS.append(gchild.get(\'location\', [])[0]) \n+ EPOS.append(gchild.get(\'location\', [])[1]) \n+ STRD = gchild.get(\'strand\', \'\')\n+ SCR = gchild.get(\'score\', \'\')\n+ if gchild.get(\'type\', \'\') == "gene": ## gencode GTF file has this problem \n+ continue\n+ TYP[gchild.get(\'type\', \'\')] = 1\n+ SPOS.sort() \n+ EPOS.sort()\n+ \n+ # infer transcript type\n+ transcript_type = \'transcript\'\n+ transcript_type = \'mRNA\' if TYP.get(\'CDS\', \'\') or TYP.get(\'cds\', \'\') else transcript_type\n+ \n+ # gene id and transcript id are same\n+ transcript_id = fid[-1]\n+ if GID == transcript_id:\n+ transcript_id = \'Transcript:\' + str(GID)\n+ \n+ # level -1 feature type \n+ p_feat[(fid[0], fid[1], GID)] = dict( type = \'gene\',\n+ location = [], ## infer location based on multiple transcripts \n+ strand = STRD,\n+ name = GID )\n+ # level -2 feature type \n+ child_n_map[(fid[0], fid[1], GID)].append(\n+ dict( type = transcript_type,\n+ location = [SPOS[0], EPOS[-1]], \n+ strand = STRD, \n+ score = SCR, \n+ ID = transcript_id,\n+ gene_id = \'\' ))\n+ # reorganizing the grand child\n+ for gchild in det:\n+ child_n_map[(fid[0], fid[1], transcript_id)].append(\n+ dict( type = gchild.get(\'type\', \'\'),\n+ location = gchild.get(\'location\'),\n+ strand = gchild.get(\'strand\'), \n+ ID = gchild.get(\'ID\'),\n+ score = gchild.get(\'score\'),\n+ gene_id = \'\' ))\n+ return p_feat, child_n_map \n+\n' |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,71 @@ +GFFtools-GX +=========== + +A collection of tools for converting genome annotation between [GTF](https://genome.ucsc.edu/FAQ/FAQformat.html#format4), [BED](https://genome.ucsc.edu/FAQ/FAQformat.html#format1), [GenBank](http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html) and [GFF](https://genome.ucsc.edu/FAQ/FAQformat.html#format3). + +##### INTRODUCTION + +Several genome annotation centers provide their data in GTF, BED, GFF and GenBank format. I have few programs, they mainly deals with converting between GTF, BED GenBank and GFF formats. They are extensively tested with files from different centers like [ENSEMBL](http://www.ensembl.org), [UCSC](https://genome.ucsc.edu/), [JGI](http://genome.jgi.doe.gov/) and [NCBI AceView](http://www.ncbi.nlm.nih.gov/IEB/Research/Acembly/HelpJan.html). These programs can be easily integrated into your galaxy instance. + +##### CONTENTS + +Included utilities are: + + BED-to-GFF: convert data from a 12 column UCSC wiggle BED format to GFF + GBK-to-GFF: convert data from genbank format to GFF + GFF-to-BED: convert data from GFF to 12 column BED format + GFF-to-GTF: convert data from GFF to GTF + GTF-to-GFF: convert data from GTF to valid GFF + +test-data: Test data set. (move to your galaxy-root-folder/test-data/) + + You may need to move the test files into your test-data directory so galaxy can find them. + If you want to run the functional tests eg as: + + exmaple: + sh run_functional_tests.sh -id fml_gtf2gff + +##### REQUIREMENTS + + python2.6 or 2.7 and biopython + + Galaxy should be able to automatically install biopython via Galaxy toolshed. + +##### COMMENTS/QUESTIONS + +I can be reached at vipin [at] cbio.mskcc.org + +##### LICENSE + +Copyright (c) 2009-2012, Friedrich Miescher Laboratory of the Max Planck Society + + 2013-2015, Memorial Sloan Kettering Cancer Center + Vipin T Sreedharan <vipin@cbio.mskcc.org> +All rights reserved. + +Licensed under the BSD 2-Clause License: <http://opensource.org/licenses/BSD-2-Clause> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +##### COURTESY + +To the Galaxy Team. |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 bed_to_gff.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bed_to_gff.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,71 @@ +#!/usr/bin/env python +""" +Convert genome annotation data in a 12 column BED format to GFF3. + +Usage: + python bed_to_gff.py in.bed > out.gff + +Requirement: + helper.py : https://github.com/vipints/GFFtools-GX/blob/master/helper.py + +Copyright (C) + 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany. + 2012-2015 Memorial Sloan Kettering Cancer Center New York City, USA. +""" + +import re +import sys +import helper + +def __main__(): + """ + main function + """ + + try: + bed_fname = sys.argv[1] + except: + print __doc__ + sys.exit(-1) + + bed_fh = helper.open_file(bed_fname) + + for line in bed_fh: + line = line.strip( '\n\r' ) + + if not line or line[0] in ['#']: + continue + + parts = line.split('\t') + assert len(parts) >= 12, line + + rstarts = parts[-1].split(',') + rstarts.pop() if rstarts[-1] == '' else rstarts + + exon_lens = parts[-2].split(',') + exon_lens.pop() if exon_lens[-1] == '' else exon_lens + + if len(rstarts) != len(exon_lens): + continue # checking the consistency col 11 and col 12 + + if len(rstarts) != int(parts[-3]): + continue # checking the number of exons and block count are same + + if not parts[5] in ['+', '-']: + parts[5] = '.' # replace the unknown strand with '.' + + # bed2gff result line + sys.stdout.write('%s\tbed2gff\tgene\t%d\t%s\t%s\t%s\t.\tID=Gene:%s;Name=Gene:%s\n' % (parts[0], int(parts[1])+1, parts[2], parts[4], parts[5], parts[3], parts[3])) + sys.stdout.write('%s\tbed2gff\ttranscript\t%d\t%s\t%s\t%s\t.\tID=%s;Name=%s;Parent=Gene:%s\n' % (parts[0], int(parts[1])+1, parts[2], parts[4], parts[5], parts[3], parts[3], parts[3])) + + st = int(parts[1]) + for ex_cnt in range(int(parts[-3])): + start = st + int(rstarts[ex_cnt]) + 1 + stop = start + int(exon_lens[ex_cnt]) - 1 + sys.stdout.write('%s\tbed2gff\texon\t%d\t%d\t%s\t%s\t.\tParent=%s\n' % (parts[0], start, stop, parts[4], parts[5], parts[3])) + + bed_fh.close() + + +if __name__ == "__main__": + __main__() |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 bed_to_gff.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bed_to_gff.xml Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,95 @@ +<tool id="fml_bed2gff" name="BED-to-GFF" version="2.1.0"> + <description>converter</description> + <command interpreter="python">bed_to_gff.py $inf_bed > $gff_format + </command> + <inputs> + <param format="bed" name="inf_bed" type="data" label="Convert this query" help="Provide genome annotation in 12 column BED format."/> + </inputs> + <outputs> + <data format="gff" name="gff_format" label="${tool.name} on ${on_string}: Converted" /> + </outputs> + <tests> + <test> + <param name="inf_bed" value="CCDS30770.bed" /> + <output name="gff_format" file="CCDS30770.gff" /> + </test> + </tests> + <help> + +**What it does** + +This tool converts data from a 12 column UCSC wiggle BED format to GFF3 (scroll down for format description). + +-------- + +**Example** + +- The following data in UCSC Wiggle BED format:: + + chr1 11873 14409 uc001aaa.3 0 + 11873 11873 0 3 354,109,1189, 0,739,1347, + +- Will be converted to GFF3:: + + ##gff-version 3 + chr1 bed2gff gene 11874 14409 0 + . ID=Gene:uc001aaa.3;Name=Gene:uc001aaa.3 + chr1 bed2gff transcript 11874 14409 0 + . ID=uc001aaa.3;Name=uc001aaa.3;Parent=Gene:uc001aaa.3 + chr1 bed2gff exon 11874 12227 0 + . Parent=uc001aaa.3 + chr1 bed2gff exon 12613 12721 0 + . Parent=uc001aaa.3 + chr1 bed2gff exon 13221 14409 0 + . Parent=uc001aaa.3 + +-------- + +**Reference** + +**BED-to-GFF** is part of oqtans package and cited as [1]_. + +.. [1] Sreedharan VT, Schultheiss SJ, Jean G et.al., Oqtans: the RNA-seq workbench in the cloud for complete and reproducible quantitative transcriptome analysis. Bioinformatics (2014). `10.1093/bioinformatics/btt731`_ + +.. _10.1093/bioinformatics/btt731: http://goo.gl/I75poH + +-------- + +**About file formats** + +**BED format** Browser Extensible Data format was designed at UCSC for displaying data tracks in the Genome Browser. It has three required fields and several additional optional ones: + +The first three BED fields (required) are:: + + 1. chrom - The name of the chromosome (e.g. chr1, chrY_random). + 2. chromStart - The starting position in the chromosome. (The first base in a chromosome is numbered 0.) + 3. chromEnd - The ending position in the chromosome, plus 1 (i.e., a half-open interval). + +The additional BED fields (optional) are:: + + 4. name - The name of the BED line. + 5. score - A score between 0 and 1000. + 6. strand - Defines the strand - either '+' or '-'. + 7. thickStart - The starting position where the feature is drawn thickly at the Genome Browser. + 8. thickEnd - The ending position where the feature is drawn thickly at the Genome Browser. + 9. reserved - This should always be set to zero. + 10. blockCount - The number of blocks (exons) in the BED line. + 11. blockSizes - A comma-separated list of the block sizes. The number of items in this list should correspond to blockCount. + 12. blockStarts - A comma-separated list of block starts. All of the blockStart positions should be calculated relative to chromStart. The number of items in this list should correspond to blockCount. + +**GFF format** General Feature Format is a format for describing genes and other features associated with DNA, RNA and Protein sequences. GFF lines have nine tab-separated fields:: + + 1. seqid - Must be a chromosome or scaffold or contig. + 2. source - The program that generated this feature. + 3. type - The name of this type of feature. Some examples of standard feature types are "gene", "CDS", "protein", "mRNA", and "exon". + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. stop - The ending position of the feature (inclusive). + 6. score - A score between 0 and 1000. If there is no score value, enter ".". + 7. strand - Valid entries include '+', '-', or '.' (for don't know/care). + 8. phase - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'. + 9. attributes - All lines with the same group are linked together into a single item. + +-------- + +**Copyright** + +BED-to-GFF Wrapper Version 0.6 (Apr 2015) + +2009-2015 Max Planck Society, University of Tübingen & Memorial Sloan Kettering Cancer Center + + </help> +</tool> |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gbk_to_gff.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gbk_to_gff.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,216 @@ +#!/usr/bin/env python +""" +Convert data from Genbank format to GFF. + +Usage: +python gbk_to_gff.py in.gbk > out.gff + +Requirements: + BioPython:- http://biopython.org/ + helper.py:- https://github.com/vipints/GFFtools-GX/blob/master/helper.py + +Copyright (C) + 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany. + 2012-2015 Memorial Sloan Kettering Cancer Center New York City, USA. +""" + +import os +import re +import sys +import helper +import collections +from Bio import SeqIO + +def feature_table(chr_id, source, orient, genes, transcripts, cds, exons, unk): + """ + Write the feature information + """ + for gname, ginfo in genes.items(): + line = [str(chr_id), + 'gbk2gff', + ginfo[3], + str(ginfo[0]), + str(ginfo[1]), + '.', + ginfo[2], + '.', + 'ID=%s;Name=%s' % (str(gname), str(gname))] + sys.stdout.write('\t'.join(line)+"\n") + ## construct the transcript line is not defined in the original file + t_line = [str(chr_id), 'gbk2gff', source, 0, 1, '.', ginfo[2], '.'] + + if not transcripts: + t_line.append('ID=Transcript:%s;Parent=%s' % (str(gname), str(gname))) + + if exons: ## get the entire transcript region from the defined feature + t_line[3] = str(exons[gname][0][0]) + t_line[4] = str(exons[gname][0][-1]) + elif cds: + t_line[3] = str(cds[gname][0][0]) + t_line[4] = str(cds[gname][0][-1]) + + if not cds: + t_line[2] = 'transcript' + else: + t_line[2] = 'mRNA' + sys.stdout.write('\t'.join(t_line)+"\n") + + if exons: + exon_line_print(t_line, exons[gname], 'Transcript:'+str(gname), 'exon') + + if cds: + exon_line_print(t_line, cds[gname], 'Transcript:'+str(gname), 'CDS') + if not exons: + exon_line_print(t_line, cds[gname], 'Transcript:'+str(gname), 'exon') + + else: ## transcript is defined + for idx in transcripts[gname]: + t_line[2] = idx[3] + t_line[3] = str(idx[0]) + t_line[4] = str(idx[1]) + t_line.append('ID='+str(idx[2])+';Parent='+str(gname)) + sys.stdout.write('\t'.join(t_line)+"\n") + + ## feature line print call + if exons: + exon_line_print(t_line, exons[gname], str(idx[2]), 'exon') + if cds: + exon_line_print(t_line, cds[gname], str(idx[2]), 'CDS') + if not exons: + exon_line_print(t_line, cds[gname], str(idx[2]), 'exon') + + if len(genes) == 0: ## feature entry with fragment information + + line = [str(chr_id), 'gbk2gff', source, 0, 1, '.', orient, '.'] + fStart = fStop = None + + for eid, ex in cds.items(): + fStart = ex[0][0] + fStop = ex[0][-1] + + for eid, ex in exons.items(): + fStart = ex[0][0] + fStop = ex[0][-1] + + if fStart or fStart: + + line[2] = 'gene' + line[3] = str(fStart) + line[4] = str(fStop) + line.append('ID=Unknown_Gene_' + str(unk) + ';Name=Unknown_Gene_' + str(unk)) + sys.stdout.write('\t'.join(line)+"\n") + + if not cds: + line[2] = 'transcript' + else: + line[2] = 'mRNA' + + line[8] = 'ID=Unknown_Transcript_' + str(unk) + ';Parent=Unknown_Gene_' + str(unk) + sys.stdout.write('\t'.join(line)+"\n") + + if exons: + exon_line_print(line, cds[None], 'Unknown_Transcript_' + str(unk), 'exon') + + if cds: + exon_line_print(line, cds[None], 'Unknown_Transcript_' + str(unk), 'CDS') + if not exons: + exon_line_print(line, cds[None], 'Unknown_Transcript_' + str(unk), 'exon') + + unk +=1 + + return unk + + +def exon_line_print(temp_line, trx_exons, parent, ftype): + """ + Print the EXON feature line + """ + for ex in trx_exons: + temp_line[2] = ftype + temp_line[3] = str(ex[0]) + temp_line[4] = str(ex[1]) + temp_line[8] = 'Parent=%s' % parent + sys.stdout.write('\t'.join(temp_line)+"\n") + + +def gbk_parse(fname): + """ + Extract genome annotation recods from genbank format + + @args fname: gbk file name + @type fname: str + """ + fhand = helper.open_file(gbkfname) + unk = 1 + + for record in SeqIO.parse(fhand, "genbank"): + gene_tags = dict() + tx_tags = collections.defaultdict(list) + exon = collections.defaultdict(list) + cds = collections.defaultdict(list) + mol_type, chr_id = None, None + + for rec in record.features: + + if rec.type == 'source': + try: + mol_type = rec.qualifiers['mol_type'][0] + except: + mol_type = '.' + pass + try: + chr_id = rec.qualifiers['chromosome'][0] + except: + chr_id = record.name + continue + + strand='-' + strand='+' if rec.strand>0 else strand + + fid = None + try: + fid = rec.qualifiers['gene'][0] + except: + pass + + transcript_id = None + try: + transcript_id = rec.qualifiers['transcript_id'][0] + except: + pass + + if re.search(r'gene', rec.type): + gene_tags[fid] = (rec.location._start.position+1, + rec.location._end.position, + strand, + rec.type + ) + elif rec.type == 'exon': + exon[fid].append((rec.location._start.position+1, + rec.location._end.position)) + elif rec.type=='CDS': + cds[fid].append((rec.location._start.position+1, + rec.location._end.position)) + else: + # get all transcripts + if transcript_id: + tx_tags[fid].append((rec.location._start.position+1, + rec.location._end.position, + transcript_id, + rec.type)) + # record extracted, generate feature table + unk = feature_table(chr_id, mol_type, strand, gene_tags, tx_tags, cds, exon, unk) + + fhand.close() + + +if __name__=='__main__': + + try: + gbkfname = sys.argv[1] + except: + print __doc__ + sys.exit(-1) + + ## extract gbk records + gbk_parse(gbkfname) |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gbk_to_gff.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gbk_to_gff.xml Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,100 @@ +<tool id="fml_gbk2gff" name="GBK-to-GFF" version="2.1.0"> + <description>converter</description> + <command interpreter="python">gbk_to_gff.py $inf_gbk > $gff_format + </command> + <inputs> + <param format="gb,gbk,genbank" name="inf_gbk" type="data" label="Convert this query" help="GenBank flat file format consists of an annotation section and a sequence section."/> + </inputs> + <outputs> + <data format="gff" name="gff_format" label="${tool.name} on ${on_string}: Converted"/> + </outputs> + <tests> + <test> + <param name="inf_gbk" value="s_cerevisiae_SCU49845.gbk" /> + <output name="gff_format" file="s_cerevisiae_SCU49845.gff" /> + </test> + </tests> + <help> + +**What it does** + +This tool converts data from a GenBank_ flat file format to GFF (scroll down for format description). + +.. _GenBank: http://www.ncbi.nlm.nih.gov/genbank/ + +------ + +**Example** + +- The following data in GenBank format:: + + LOCUS NM_001202705 2406 bp mRNA linear PLN 28-MAY-2011 + DEFINITION Arabidopsis thaliana thiamine biosynthesis protein ThiC (THIC) + mRNA, complete cds. + ACCESSION NM_001202705 + VERSION NM_001202705.1 GI:334184566......... + FEATURES Location/Qualifiers + source 1..2406 + /organism="Arabidopsis thaliana" + /mol_type="mRNA" + /db_xref="taxon:3702"........ + gene 1..2406 + /gene="THIC" + /locus_tag="AT2G29630" + /gene_synonym="PY; PYRIMIDINE REQUIRING; T27A16.27;........ + ORIGIN + 1 aagcctttcg ctttaggctg cattgggccg tgacaatatt cagacgattc aggaggttcg + 61 ttcctttttt aaaggaccct aatcactctg agtaccactg actcactcag tgtgcgcgat + 121 tcatttcaaa aacgagccag cctcttcttc cttcgtctac tagatcagat ccaaagcttc + 181 ctcttccagc tatggctgct tcagtacact gtaccttgat gtccgtcgta tgcaacaaca + // + + +- Will be converted to GFF3:: + + NM_001202705 gbk2gff chromosome 1 2406 . + 1 ID=NM_001202705;Alias=2;Dbxref=taxon:3702;Name=NM_001202705 + NM_001202705 gbk2gff gene 1 2406 . + 1 ID=AT2G29630;Dbxref=GeneID:817513,TAIR:AT2G29630;Name=THIC + NM_001202705 gbk2gff mRNA 192 2126 . + 1 ID=AT2G29630.t01;Parent=AT2G29630 + NM_001202705 gbk2gff CDS 192 2126 . + 1 ID=AT2G29630.p01;Parent=AT2G29630.t01 + NM_001202705 gbk2gff exon 192 2126 . + 1 Parent=AT2G29630.t01 + +------ + +**Reference** + +**GBK-to-GFF** is part of oqtans package and cited as [1]_. + +.. [1] Sreedharan VT, Schultheiss SJ, Jean G et.al., Oqtans: the RNA-seq workbench in the cloud for complete and reproducible quantitative transcriptome analysis. Bioinformatics (2014). `10.1093/bioinformatics/btt731`_ + +.. _10.1093/bioinformatics/btt731: http://goo.gl/I75poH + +------ + +**About file formats** + +**GenBank format** An example of a GenBank record may be viewed here_ + +.. _here: http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html + +**GFF** Generic Feature Format is a format for describing genes and other features associated with DNA, RNA and Protein sequences. GFF lines have nine tab-separated fields:: + + 1. seqid - Must be a chromosome or scaffold or contig. + 2. source - The program that generated this feature. + 3. type - The name of this type of feature. Some examples of standard feature types are "gene", "CDS", "protein", "mRNA", and "exon". + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. stop - The ending position of the feature (inclusive). + 6. score - A score between 0 and 1000. If there is no score value, enter ".". + 7. strand - Valid entries include '+', '-', or '.' (for don't know/care). + 8. phase - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'. + 9. attributes - All lines with the same group are linked together into a single item. + +-------- + +**Copyright** + +GBK-to-GFF Wrapper Version 0.6 (Apr 2015) + +2009-2015 Max Planck Society, University of Tübingen & Memorial Sloan Kettering Cancer Center + + </help> +</tool> |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gff_to_bed.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gff_to_bed.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,116 @@ +#!/usr/bin/env python +""" +Convert genome annotation data in GFF/GTF to a 12 column BED format. +BED format typically represents the transcript models. + +Usage: python gff_to_bed.py in.gff > out.bed + +Requirement: + GFFParser.py: https://github.com/vipints/GFFtools-GX/blob/master/GFFParser.py + +Copyright (C) + 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany. + 2012-2015 Memorial Sloan Kettering Cancer Center New York City, USA. +""" + +import re +import sys +import GFFParser + +def limitBEDWrite(tinfo): + """ + Write a three column BED file + + @args tinfo: list of genes + @type tinfo: numpy object + """ + + for contig_id, feature in tinfo.items(): + uns_line = dict() + for tid, tloc in feature.items(): + uns_line[(int(tloc[0])-1, int(tloc[1]))]=1 + for ele in sorted(uns_line): + pline = [contig_id, + str(ele[0]-1), + str(ele[1])] + + sys.stdout.write('\t'.join(pline)+"\n") + + +def writeBED(tinfo): + """ + writing result files in bed format + + @args tinfo: list of genes + @type tinfo: numpy object + """ + + for ent1 in tinfo: + child_flag = False + + for idx, tid in enumerate(ent1['transcripts']): + child_flag = True + exon_cnt = len(ent1['exons'][idx]) + exon_len = '' + exon_cod = '' + rel_start = None + rel_stop = None + for idz, ex_cod in enumerate(ent1['exons'][idx]):#check for exons of corresponding transcript + exon_len += '%d,' % (ex_cod[1]-ex_cod[0]+1) + if idz == 0: #calculate the relative start position + exon_cod += '0,' + rel_start = int(ex_cod[0])-1 + rel_stop = int(ex_cod[1]) + else: + exon_cod += '%d,' % (ex_cod[0]-1-rel_start) ## shifting the coordinates to zero + rel_stop = int(ex_cod[1]) + + if exon_len: + score = 0 + score = ent1['transcript_score'][idx] if ent1['transcript_score'].any() else score ## getting the transcript score + out_print = [ent1['chr'], + str(rel_start), + str(rel_stop), + tid[0], + str(score), + ent1['strand'], + str(rel_start), + str(rel_stop), + '0', + str(exon_cnt), + exon_len, + exon_cod] + sys.stdout.write('\t'.join(out_print)+"\n") + + if not child_flag: # file just contains only a single parent type i.e, gff3 defines only one feature type + score = 0 + score = ent1['transcript_score'][0] if ent1['transcript_score'].any() else score + + out_print = [ent1['chr'], + '%d' % int(ent1['start'])-1, + '%d' % int(ent1['stop']), + ent1['name'], + str(score), + ent1['strand'], + '%d' % int(ent1['start']), + '%d' % int(ent1['stop']), + '0', + '1', + '%d,' % (int(ent1['stop'])-int(ent1['start'])+1), + '0,'] + + sys.stdout.write('\t'.join(out_print)+"\n") + + +def __main__(): + try: + query_file = sys.argv[1] + except: + print __doc__ + sys.exit(-1) + + Transcriptdb = GFFParser.Parse(query_file) + writeBED(Transcriptdb) + +if __name__ == "__main__": + __main__() |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gff_to_bed.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gff_to_bed.xml Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,96 @@ +<tool id="fml_gff2bed" name="GFF-to-BED" version="2.1.0"> + <description>converter</description> + <command interpreter="python">gff_to_bed.py $inf_gff > $bed_format + </command> + <inputs> + <param format="gtf,gff,gff3" name="inf_gff" type="data" label="Convert this query" help="Provide genome annotation file in GFF, GTF, GFF3."/> + </inputs> + <outputs> + <data format="bed" name="bed_format" label="${tool.name} on ${on_string}: Converted" /> + </outputs> + <tests> + <test> + <param name="inf_gff" value="MB7_3R.gff3" /> + <output name="bed_format" file="MB7_3R.bed" /> + </test> + </tests> + <help> + +**What it does** + +This tool converts gene transcript annotation from GTF or GFF or GFF3 to UCSC wiggle 12 column BED format. + +-------- + +**Example** + +- The following data in GFF3:: + + ##gff-version 3 + chr1 protein_coding gene 11874 14409 0 + . ID=Gene:uc001aaa.3;Name=Gene:uc001aaa.3 + chr1 protein_coding transcript 11874 14409 0 + . ID=uc001aaa.3;Name=uc001aaa.3;Parent=Gene:uc001aaa.3 + chr1 protein_coding exon 11874 12227 0 + . Parent=uc001aaa.3 + chr1 protein_coding exon 12613 12721 0 + . Parent=uc001aaa.3 + chr1 protein_coding exon 13221 14409 0 + . Parent=uc001aaa.3 + +- Will be converted to UCSC Wiggle BED format:: + + chr1 11874 14409 uc001aaa.3 0 + 11874 14409 0 3 354,109,1189, 0,739,1347, + +-------- + +**Reference** + +**GFF-to-BED** is part of oqtans package and cited as [1]_. + +.. [1] Sreedharan VT, Schultheiss SJ, Jean G et.al., Oqtans: the RNA-seq workbench in the cloud for complete and reproducible quantitative transcriptome analysis. Bioinformatics (2014). `10.1093/bioinformatics/btt731`_ + +.. _10.1093/bioinformatics/btt731: http://goo.gl/I75poH + +-------- + +**About file formats** + +**GFF format** General Feature Format is a format for describing genes and other features associated with DNA, RNA and Protein sequences. GFF lines have nine tab-separated fields:: + + + 1. seqid - Must be a chromosome or scaffold or contig. + 2. source - The program that generated this feature. + 3. type - The name of this type of feature. Some examples of standard feature types are "gene", "CDS", "protein", "mRNA", and "exon". + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. stop - The ending position of the feature (inclusive). + 6. score - A score between 0 and 1000. If there is no score value, enter ".". + 7. strand - Valid entries include '+', '-', or '.' (for don't know/care). + 8. phase - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'. + 9. attributes - All lines with the same group are linked together into a single item. + +**BED format** Browser Extensible Data format was designed at UCSC for displaying data tracks in the Genome Browser. It has three required fields and several additional optional ones: + +The first three BED fields (required) are:: + + 1. chrom - The name of the chromosome (e.g. chr1, chrY_random). + 2. chromStart - The starting position in the chromosome. (The first base in a chromosome is numbered 0.) + 3. chromEnd - The ending position in the chromosome, plus 1 (i.e., a half-open interval). + +The additional BED fields (optional) are:: + + 4. name - The name of the BED line. + 5. score - A score between 0 and 1000. + 6. strand - Defines the strand - either '+' or '-'. + 7. thickStart - The starting position where the feature is drawn thickly at the Genome Browser. + 8. thickEnd - The ending position where the feature is drawn thickly at the Genome Browser. + 9. reserved - This should always be set to zero. + 10. blockCount - The number of blocks (exons) in the BED line. + 11. blockSizes - A comma-separated list of the block sizes. The number of items in this list should correspond to blockCount. + 12. blockStarts - A comma-separated list of block starts. All of the blockStart positions should be calculated relative to chromStart. The number of items in this list should correspond to blockCount. + +-------- + +**Copyright** + +GFF-to-BED Wrapper Version 0.6 (Apr 2015) + +2009-2015 Max Planck Society, University of Tübingen & Memorial Sloan Kettering Cancer Center + + </help> +</tool> |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gff_to_gtf.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gff_to_gtf.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,76 @@ +#!/usr/bin/env python +""" +Program to convert data from GFF to GTF + +Usage: python gff_to_gtf.py in.gff > out.gtf + +Requirement: + GFFParser.py: https://github.com/vipints/GFFtools-GX/blob/master/GFFParser.py + +Copyright (C) + 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany. + 2012-2015 Memorial Sloan Kettering Cancer Center New York City, USA. +""" + +import re +import sys +import GFFParser + +def printGTF(tinfo): + """ + writing result file in GTF format + + @args tinfo: parsed object from gff file + @type tinfo: numpy array + """ + + for ent1 in tinfo: + for idx, tid in enumerate(ent1['transcripts']): + + exons = ent1['exons'][idx] + cds_exons = ent1['cds_exons'][idx] + + stop_codon = start_codon = () + + if ent1['strand'] == '+': + if cds_exons.any(): + start_codon = (cds_exons[0][0], cds_exons[0][0]+2) + stop_codon = (cds_exons[-1][1]-2, cds_exons[-1][1]) + elif ent1['strand'] == '-': + if cds_exons.any(): + start_codon = (cds_exons[-1][1]-2, cds_exons[-1][1]) + stop_codon = (cds_exons[0][0], cds_exons[0][0]+2) + else: + sys.stdout.write('STRAND information missing - %s, skip the transcript - %s\n' % (ent1['strand'], tid[0])) + pass + + last_cds_cod = 0 + for idz, ex_cod in enumerate(exons): + + sys.stdout.write('%s\t%s\texon\t%d\t%d\t.\t%s\t.\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; \n' % (ent1['chr'], ent1['source'], ex_cod[0], ex_cod[1], ent1['strand'], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])) + + if cds_exons.any(): + try: + sys.stdout.write('%s\t%s\tCDS\t%d\t%d\t.\t%s\t%d\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; \n' % (ent1['chr'], ent1['source'], cds_exons[idz][0], cds_exons[idz][1], ent1['strand'], cds_exons[idz][2], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])) + last_cds_cod = idz + except: + pass + + if idz == 0: + sys.stdout.write('%s\t%s\tstart_codon\t%d\t%d\t.\t%s\t%d\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; \n' % (ent1['chr'], ent1['source'], start_codon[0], start_codon[1], ent1['strand'], cds_exons[idz][2], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])) + + if stop_codon: + sys.stdout.write('%s\t%s\tstop_codon\t%d\t%d\t.\t%s\t%d\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; \n' % (ent1['chr'], ent1['source'], stop_codon[0], stop_codon[1], ent1['strand'], cds_exons[last_cds_cod][2], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])) + + +if __name__ == "__main__": + + try: + gff_fname = sys.argv[1] + except: + print __doc__ + sys.exit(-1) + + Transcriptdb = GFFParser.Parse(gff_fname) + + printGTF(Transcriptdb) |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gff_to_gtf.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gff_to_gtf.xml Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,92 @@ +<tool id="fml_gff2gtf" name="GFF-to-GTF" version="2.1.0"> + <description>converter</description> + <command interpreter="python">gff_to_gtf.py $inf_gff3 > $gtf_format + </command> + <inputs> + <param format="gff3,gff" name="inf_gff3" type="data" label="Convert this query" help="Provide genome annotation file in GFF or GFF3."/> + </inputs> + <outputs> + <data format="gtf" name="gtf_format" label="${tool.name} on ${on_string}: Converted" /> + </outputs> + <tests> + <test> + <param name="inf_gff3" value="ens_mm9_chr18.gff3" /> + <output name="gtf_format" file="ens_mm9_chr18.gtf" /> + </test> + </tests> + <help> + +**What it does** + +This tool converts data from GFF to GTF file format (scroll down for format description). + +-------- + +**Example** + +- The following data in GFF3:: + + ##gff-version 3 + 17 protein_coding gene 7255208 7258258 . + . ID=ENSG00000213859;Name=KCTD11 + 17 protein_coding mRNA 7255208 7258258 . + . ID=ENST00000333751;Name=KCTD11-001;Parent=ENSG00000213859 + 17 protein_coding protein 7256262 7256960 . + . ID=ENSP00000328352;Name=KCTD11-001;Parent=ENST00000333751 + 17 protein_coding five_prime_UTR 7255208 7256261 . + . Parent=ENST00000333751 + 17 protein_coding CDS 7256262 7256960 . + 0 Name=CDS:KCTD11;Parent=ENST00000333751,ENSP00000328352 + 17 protein_coding three_prime_UTR 7256961 7258258 . + . Parent=ENST00000333751 + 17 protein_coding exon 7255208 7258258 . + . Parent=ENST00000333751 + +- Will be converted to GTF:: + + 17 protein_coding exon 7255208 7258258 . + . gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; + 17 protein_coding CDS 7256262 7256957 . + 0 gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; protein_id "ENSP00000328352"; + 17 protein_coding start_codon 7256262 7256264 . + 0 gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; + 17 protein_coding stop_codon 7256958 7256960 . + 0 gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; + +-------- + +**Reference** + +**GFF-to-GTF** is part of oqtans package and cited as [1]_. + +.. [1] Sreedharan VT, Schultheiss SJ, Jean G et.al., Oqtans: the RNA-seq workbench in the cloud for complete and reproducible quantitative transcriptome analysis. Bioinformatics (2014). `10.1093/bioinformatics/btt731`_ + +.. _10.1093/bioinformatics/btt731: http://goo.gl/I75poH + +-------- + +**About formats** + +**GFF format** General Feature Format is a format for describing genes and other features associated with DNA, RNA and Protein sequences. GFF lines have nine tab-separated fields:: + + 1. seqid - Must be a chromosome or scaffold. + 2. source - The program that generated this feature. + 3. type - The name of this type of feature. Some examples of standard feature types are "gene", "CDS", "protein", "mRNA", and "exon". + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. stop - The ending position of the feature (inclusive). + 6. score - A score between 0 and 1000. If there is no score value, enter ".". + 7. strand - Valid entries include '+', '-', or '.' (for don't know/care). + 8. phase - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'. + 9. attributes - All lines with the same group are linked together into a single item. + +**GTF format** Gene Transfer Format, it borrows from GFF, but has additional structure that warrants a separate definition and format name. GTF lines have nine tab-seaparated fields:: + + 1. seqname - The name of the sequence. + 2. source - This indicating where the annotation came from. + 3. feature - The name of the feature types. The following feature types are required: 'CDS', 'start_codon' and 'stop_codon' + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. end - The ending position of the feature (inclusive). + 6. score - The score field indicates a degree of confidence in the feature's existence and coordinates. + 7. strand - Valid entries include '+', '-', or '.' + 8. frame - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. + 9. attributes - These attributes are designed for handling multiple transcripts from the same genomic region. + +-------- + +**Copyright** + +GFF-to-GTF Wrapper Version 0.6 (Apr 2015) + +2009-2015 Max Planck Society, University of Tübingen & Memorial Sloan Kettering Cancer Center + + </help> +</tool> |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gtf_to_gff.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gtf_to_gff.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,78 @@ +#!/usr/bin/env python +""" +Convert Gene Transfer Format [GTF] to Generic Feature Format Version 3 [GFF3]. + +Usage: python gtf_to_gff.py in.gtf > out.gff3 + +Requirement: + GFFParser.py: https://github.com/vipints/GFFtools-GX/blob/master/GFFParser.py + helper.py: https://github.com/vipints/GFFtools-GX/blob/master/helper.py + +Copyright (C) + 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany. + 2012-2015 Memorial Sloan Kettering Cancer Center New York City, USA. +""" + +import re +import sys +import helper +import GFFParser + +def GFFWriter(gtf_content): + """ + write the feature information to GFF format + + @args gtf_content: Parsed object from gtf file + @type gtf_content: numpy array + """ + + sys.stdout.write('##gff-version 3\n') + for ent1 in gtf_content: + chr_name = ent1['chr'] + strand = ent1['strand'] + start = ent1['start'] + stop = ent1['stop'] + source = ent1['source'] + ID = ent1['name'] + Name = ent1['gene_info']['Name'] + Name = ID if not Name else Name + + sys.stdout.write('%s\t%s\tgene\t%d\t%d\t.\t%s\t.\tID=%s;Name=%s\n' % (chr_name, source, start, stop, strand, ID, Name)) + for idx, tid in enumerate(ent1['transcripts']): + + t_start = ent1['exons'][idx][0][0] + t_stop = ent1['exons'][idx][-1][-1] + t_type = ent1['transcript_type'][idx] + + utr5_exons, utr3_exons = [], [] + if ent1['exons'][idx].any() and ent1['cds_exons'][idx].any(): + utr5_exons, utr3_exons = helper.buildUTR(ent1['cds_exons'][idx], ent1['exons'][idx], strand) + + sys.stdout.write('%s\t%s\t%s\t%d\t%d\t.\t%s\t.\tID=%s;Parent=%s\n' % (chr_name, source, t_type, t_start, t_stop, strand, tid[0], ID)) + for ex_cod in utr5_exons: + sys.stdout.write('%s\t%s\tfive_prime_UTR\t%d\t%d\t.\t%s\t.\tParent=%s\n' % (chr_name, source, ex_cod[0], ex_cod[1], strand, tid[0])) + + for ex_cod in ent1['cds_exons'][idx]: + sys.stdout.write('%s\t%s\tCDS\t%d\t%d\t.\t%s\t%d\tParent=%s\n' % (chr_name, source, ex_cod[0], ex_cod[1], strand, ex_cod[2], tid[0])) + + for ex_cod in utr3_exons: + sys.stdout.write('%s\t%s\tthree_prime_UTR\t%d\t%d\t.\t%s\t.\tParent=%s\n' % (chr_name, source, ex_cod[0], ex_cod[1], strand, tid[0])) + + for ex_cod in ent1['exons'][idx]: + sys.stdout.write('%s\t%s\texon\t%d\t%d\t.\t%s\t.\tParent=%s\n' % (chr_name, source, ex_cod[0], ex_cod[1], strand, tid[0])) + + +def __main__(): + + try: + gtf_fname = sys.argv[1] + except: + print __doc__ + sys.exit(-1) + + gtf_file_content = GFFParser.Parse(gtf_fname) + + GFFWriter(gtf_file_content) + +if __name__ == "__main__": + __main__() |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 gtf_to_gff.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gtf_to_gff.xml Thu Apr 23 18:01:45 2015 -0400 |
[ |
@@ -0,0 +1,92 @@ +<tool id="fml_gtf2gff" name="GTF-to-GFF" version="2.1.0"> + <description>converter</description> + <command interpreter="python">gtf_to_gff.py $inf_gtf > $gff3_format + </command> + <inputs> + <param format="gtf" name="inf_gtf" type="data" label="Convert this query" help="Provide genome annotation file in GTF."/> + </inputs> + <outputs> + <data format="gff" name="gff3_format" label="${tool.name} on ${on_string}: Converted" /> + </outputs> + <tests> + <test> + <param name="inf_gtf" value="aceview_hs_37.gtf" /> + <output name="gff3_format" file="aceview_hs_37.gff3" /> + </test> + </tests> + <help> + +**What it does** + +This tool converts data from GTF to a valid GFF file (scroll down for format description). + +-------- + +**Example** + +- The following data in GTF:: + + 17 protein_coding exon 7255208 7258258 . + . gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; + 17 protein_coding CDS 7256262 7256957 . + 0 gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; protein_id "ENSP00000328352"; + 17 protein_coding start_codon 7256262 7256264 . + 0 gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; + 17 protein_coding stop_codon 7256958 7256960 . + 0 gene_id "ENSG00000213859"; transcript_id "ENST00000333751"; exon_number "1"; gene_name "KCTD11"; transcript_name "KCTD11-001"; + +- Will be converted to GFF3:: + + ##gff-version 3 + 17 protein_coding gene 7255208 7258258 . + . ID=ENSG00000213859;Name=KCTD11 + 17 protein_coding mRNA 7255208 7258258 . + . ID=ENST00000333751;Name=KCTD11-001;Parent=ENSG00000213859 + 17 protein_coding protein 7256262 7256960 . + . ID=ENSP00000328352;Name=KCTD11-001;Parent=ENST00000333751 + 17 protein_coding five_prime_UTR 7255208 7256261 . + . Parent=ENST00000333751 + 17 protein_coding CDS 7256262 7256960 . + 0 Name=CDS:KCTD11;Parent=ENST00000333751,ENSP00000328352 + 17 protein_coding three_prime_UTR 7256961 7258258 . + . Parent=ENST00000333751 + 17 protein_coding exon 7255208 7258258 . + . Parent=ENST00000333751 + +-------- + +**Reference** + +**GTF-to-GFF** is part of oqtans package and cited as [1]_. + +.. [1] Sreedharan VT, Schultheiss SJ, Jean G et.al., Oqtans: the RNA-seq workbench in the cloud for complete and reproducible quantitative transcriptome analysis. Bioinformatics (2014). `10.1093/bioinformatics/btt731`_ + +.. _10.1093/bioinformatics/btt731: http://goo.gl/I75poH + +------ + +**About formats** + +**GTF format** Gene Transfer Format, it borrows from GFF, but has additional structure that warrants a separate definition and format name. GTF lines have nine tab-seaparated fields:: + + 1. seqname - The name of the sequence. + 2. source - This indicating where the annotation came from. + 3. feature - The name of the feature types. The following feature types are required: 'CDS', 'start_codon' and 'stop_codon' + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. end - The ending position of the feature (inclusive). + 6. score - The score field indicates a degree of confidence in the feature's existence and coordinates. + 7. strand - Valid entries include '+', '-', or '.' + 8. frame - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. + 9. attributes - These attributes are designed for handling multiple transcripts from the same genomic region. + +**GFF format** General Feature Format is a format for describing genes and other features associated with DNA, RNA and Protein sequences. GFF lines have nine tab-separated fields:: + + 1. seqid - Must be a chromosome or scaffold. + 2. source - The program that generated this feature. + 3. type - The name of this type of feature. Some examples of standard feature types are "gene", "CDS", "protein", "mRNA", and "exon". + 4. start - The starting position of the feature in the sequence. The first base is numbered 1. + 5. stop - The ending position of the feature (inclusive). + 6. score - A score between 0 and 1000. If there is no score value, enter ".". + 7. strand - Valid entries include '+', '-', or '.' (for don't know/care). + 8. phase - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'. + 9. attributes - All lines with the same group are linked together into a single item. + +-------- + +**Copyright** + +GTF-to-GFF Wrapper Version 0.6 (Apr 2015) + +2009-2015 Max Planck Society, University of Tübingen & Memorial Sloan Kettering Cancer Center + + </help> +</tool> |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 helper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/helper.py Thu Apr 23 18:01:45 2015 -0400 |
[ |
b'@@ -0,0 +1,333 @@\n+#!/usr/bin/env python\n+"""\n+Common utility functions\n+"""\n+\n+import os \n+import re\n+import sys \n+import gzip \n+import bz2\n+import numpy \n+\n+def init_gene():\n+ """\n+ Initializing the gene structure \n+ """\n+\n+ gene_det = [(\'id\', \'f8\'), \n+ (\'anno_id\', numpy.dtype), \n+ (\'confgenes_id\', numpy.dtype),\n+ (\'name\', \'S25\'),\n+ (\'source\', \'S25\'),\n+ (\'gene_info\', numpy.dtype),\n+ (\'alias\', \'S15\'),\n+ (\'name2\', numpy.dtype),\n+ (\'strand\', \'S2\'), \n+ (\'score\', \'S15\'), \n+ (\'chr\', \'S15\'), \n+ (\'chr_num\', numpy.dtype),\n+ (\'paralogs\', numpy.dtype),\n+ (\'start\', \'f8\'),\n+ (\'stop\', \'f8\'), \n+ (\'transcripts\', numpy.dtype),\n+ (\'transcript_type\', numpy.dtype),\n+ (\'transcript_info\', numpy.dtype),\n+ (\'transcript_score\', numpy.dtype),\n+ (\'transcript_status\', numpy.dtype),\n+ (\'transcript_valid\', numpy.dtype),\n+ (\'exons\', numpy.dtype),\n+ (\'exons_confirmed\', numpy.dtype),\n+ (\'cds_exons\', numpy.dtype),\n+ (\'utr5_exons\', numpy.dtype),\n+ (\'utr3_exons\', numpy.dtype),\n+ (\'tis\', numpy.dtype),\n+ (\'tis_conf\', numpy.dtype),\n+ (\'tis_info\', numpy.dtype),\n+ (\'cdsStop\', numpy.dtype),\n+ (\'cdsStop_conf\', numpy.dtype),\n+ (\'cdsStop_info\', numpy.dtype),\n+ (\'tss\', numpy.dtype),\n+ (\'tss_info\', numpy.dtype),\n+ (\'tss_conf\', numpy.dtype),\n+ (\'cleave\', numpy.dtype),\n+ (\'cleave_info\', numpy.dtype),\n+ (\'cleave_conf\', numpy.dtype),\n+ (\'polya\', numpy.dtype),\n+ (\'polya_info\', numpy.dtype),\n+ (\'polya_conf\', numpy.dtype),\n+ (\'is_alt\', \'f8\'), \n+ (\'is_alt_spliced\', \'f8\'), \n+ (\'is_valid\', numpy.dtype),\n+ (\'transcript_complete\', numpy.dtype),\n+ (\'is_complete\', numpy.dtype),\n+ (\'is_correctly_gff3_referenced\', \'S5\'),\n+ (\'splicegraph\', numpy.dtype) ]\n+\n+ return gene_det\n+\n+def open_file(fname):\n+ """\n+ Open the file (supports .gz .bz2) and returns the handler\n+\n+ @args fname: input file name for reading \n+ @type fname: str\n+ """\n+\n+ try:\n+ if os.path.splitext(fname)[1] == ".gz":\n+ FH = gzip.open(fname, \'rb\')\n+ elif os.path.splitext(fname)[1] == ".bz2":\n+ FH = bz2.BZ2File(fname, \'rb\')\n+ else:\n+ FH = open(fname, \'rU\')\n+ except Exception as error:\n+ sys.exit(error)\n+\n+ return FH\n+\n+def add_CDS_phase(strand, cds):\n+ """\n+ Calculate CDS phase and add to the CDS exons\n+\n+ @args strand: feature strand information \n+ @type strand: +/- \n+ @args cds: coding exon coordinates \n+ @type cds: numpy array [[int, int, int]]\n+ """\n+\n+ cds_region, cds_flag = [], 0 \n+ if strand == \'+\':\n+ for cdspos in cds:\n+ if cds_flag == 0:\n+ cdspos = (cdspos[0], cdspos[1], 0)\n+ diff = (cdspos[1]-(cdspos[0]-1))%3\n+ else:\n+ xy = 0\n+ if diff == 0: \n+ cdspos = (cdspos[0], cdspos[1], 0)\n+ elif diff == 1: \n+ cdspos = (cdspos[0], cdspos[1], 2)\n+ xy = 2\n+ elif diff == 2: \n+ cdspos = (cdspos[0], cdspos[1], 1)\n+ xy = 1\n+ diff = ((cdspos[1]-(cdspos[0]-1))-xy)%3\n+ cds_region.append(cdspos)\n+ cds_flag = 1 \n+ elif strand == \'-\':\n+ cds.reverse()\n+ for cdspos in cds: \n+ if cds_flag == 0:\n+ cdspos = (cdspos[0], cdspos[1], 0)\n+ diff = (cdspos[1]-(cdspos[0]-1))%3\n+ else: \n+ xy = 0 \n+ if diff == 0: \n+ cdspos = (cdspos[0], cdspos[1], 0)\n+ '..b" exon_pos.append([cds_5start, utr3_end])\n+ for cds in cds_cod:\n+ exon_pos.append(cds)\n+ for utr3 in three_p_utr:\n+ exon_pos.append(utr3)\n+ else: \n+ if jun_exon != []:\n+ five_p_utr = five_p_utr[:-1]\n+ cds_cod = cds_cod[1:]\n+ for utr5 in five_p_utr:\n+ exon_pos.append(utr5)\n+ exon_pos.append(jun_exon) if jun_exon != [] else ''\n+ jun_exon = []\n+ utr3_start, utr3_end = 0, 0\n+ if three_p_utr != []:\n+ utr3_start = three_p_utr[0][0]\n+ utr3_end = three_p_utr[0][1]\n+ cds_3start = cds_cod[-1][0]\n+ cds_3end = cds_cod[-1][1]\n+ if utr3_start-cds_3end == 0 or utr3_start-cds_3end == 1: \n+ jun_exon = [cds_3start, utr3_end]\n+ if jun_exon != []:\n+ cds_cod = cds_cod[:-1]\n+ three_p_utr = three_p_utr[1:]\n+ for cds in cds_cod:\n+ exon_pos.append(cds)\n+ exon_pos.append(jun_exon) if jun_exon != [] else ''\n+ for utr3 in three_p_utr:\n+ exon_pos.append(utr3)\n+ elif strand_p == '-':\n+ utr3_start, utr3_end = 0, 0 \n+ if three_p_utr != []:\n+ utr3_start = three_p_utr[-1][0]\n+ utr3_end = three_p_utr[-1][1]\n+ cds_3start = cds_cod[0][0]\n+ cds_3end = cds_cod[0][1]\n+ jun_exon = []\n+ if cds_3start-utr3_end == 0 or cds_3start-utr3_end == 1:\n+ jun_exon = [utr3_start, cds_3end] \n+ if len(cds_cod) == 1: \n+ three_prime_flag = 0\n+ if jun_exon != []:\n+ three_p_utr = three_p_utr[:-1]\n+ three_prime_flag = 1\n+ for utr3 in three_p_utr:\n+ exon_pos.append(utr3)\n+ jun_exon = []\n+ (utr5_start, utr5_end) = (0, 0)\n+ if five_p_utr != []:\n+ utr5_start = five_p_utr[0][0]\n+ utr5_end = five_p_utr[0][1]\n+ if utr5_start-cds_3end == 0 or utr5_start-cds_3end == 1:\n+ jun_exon = [cds_3start, utr5_end]\n+ five_prime_flag = 0\n+ if jun_exon != []:\n+ cds_cod = cds_cod[:-1]\n+ five_p_utr = five_p_utr[1:]\n+ five_prime_flag = 1\n+ if three_prime_flag == 1 and five_prime_flag == 1:\n+ exon_pos.append([utr3_start, utr5_end])\n+ if three_prime_flag == 1 and five_prime_flag == 0:\n+ exon_pos.append([utr3_start, cds_3end])\n+ cds_cod = cds_cod[:-1]\n+ if three_prime_flag == 0 and five_prime_flag == 1:\n+ exon_pos.append([cds_3start, utr5_end]) \n+ for cds in cds_cod:\n+ exon_pos.append(cds)\n+ for utr5 in five_p_utr:\n+ exon_pos.append(utr5)\n+ else:\n+ if jun_exon != []:\n+ three_p_utr = three_p_utr[:-1]\n+ cds_cod = cds_cod[1:]\n+ for utr3 in three_p_utr:\n+ exon_pos.append(utr3) \n+ if jun_exon != []:\n+ exon_pos.append(jun_exon)\n+ jun_exon = []\n+ (utr5_start, utr5_end) = (0, 0)\n+ if five_p_utr != []:\n+ utr5_start = five_p_utr[0][0]\n+ utr5_end = five_p_utr[0][1] \n+ cds_5start = cds_cod[-1][0]\n+ cds_5end = cds_cod[-1][1]\n+ if utr5_start-cds_5end == 0 or utr5_start-cds_5end == 1:\n+ jun_exon = [cds_5start, utr5_end]\n+ if jun_exon != []:\n+ cds_cod = cds_cod[:-1]\n+ five_p_utr = five_p_utr[1:]\n+ for cds in cds_cod:\n+ exon_pos.append(cds)\n+ if jun_exon != []:\n+ exon_pos.append(jun_exon) \n+ for utr5 in five_p_utr:\n+ exon_pos.append(utr5)\n+ return exon_pos\n" |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 tool_conf.xml.sample --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_conf.xml.sample Thu Apr 23 18:01:45 2015 -0400 |
b |
@@ -0,0 +1,7 @@ +<section name="GFFtools" id="gfftools.web"> + <tool file="GFFtools-GX/gff_to_bed.xml"/> + <tool file="GFFtools-GX/bed_to_gff.xml"/> + <tool file="GFFtools-GX/gbk_to_gff.xml"/> + <tool file="GFFtools-GX/gff_to_gtf.xml"/> + <tool file="GFFtools-GX/gtf_to_gff.xml"/> +</section> |
b |
diff -r 7d67331368f3 -r c42c69aa81f8 tool_dependencies.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Thu Apr 23 18:01:45 2015 -0400 |
b |
@@ -0,0 +1,5 @@ +<tool_dependency> + <package name="biopython" version="1.65"> + <repository name="package_biopython_1_65" owner="biopython" /> + </package> +</tool_dependency> |