Mercurial > repos > peterjc > seq_rename
changeset 0:a4b9836f8f47
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
author | peterjc |
---|---|
date | Tue, 07 Jun 2011 17:43:26 -0400 |
parents | |
children | 9c8c5079c8af |
files | tools/filters/seq_rename.py tools/filters/seq_rename.txt tools/filters/seq_rename.xml |
diffstat | 3 files changed, 275 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/filters/seq_rename.py Tue Jun 07 17:43:26 2011 -0400 @@ -0,0 +1,141 @@ +#!/usr/bin/env python +"""Rename FASTA, QUAL, FASTQ or SSF sequences with ID mapping from tabular file. + +Takes six command line options, tabular filename, current (old) ID column +number (using one based counting), new ID column number (also using one based +counting), input sequence filename, input type (e.g. FASTA or SFF) and the +output filename (same format as input sequence file). + +When selecting from an SFF file, any Roche XML manifest in the input file is +preserved in both output files. + +This tool is a short Python script which requires Biopython 1.54 or later +for SFF file support. If you use this tool in scientific work leading to a +publication, please cite the Biopython application note: + +Cock et al 2009. Biopython: freely available Python tools for computational +molecular biology and bioinformatics. Bioinformatics 25(11) 1422-3. +http://dx.doi.org/10.1093/bioinformatics/btp163 pmid:19304878. + +This script is copyright 2011 by Peter Cock, The James Hutton Institute UK. +All rights reserved. See accompanying text file for licence details (MIT/BSD +style). + +This is version 0.0.1 of the script. +""" +import sys + +def stop_err(msg, err=1): + sys.stderr.write(msg.rstrip() + "\n") + sys.exit(err) + +#Parse Command Line +try: + tabular_file, old_col_arg, new_col_arg, in_file, seq_format, out_file = sys.argv[1:] +except ValueError: + stop_err("Expected six arguments, got %i:\n%s" % (len(sys.argv)-1, " ".join(sys.argv))) + +try: + if old_col_arg.startswith("c"): + old_column = int(old_col_arg[1:])-1 + else: + old_column = int(old_col_arg)-1 +except ValueError: + stop_err("Expected column number, got %s" % old_col_arg) +try: + if old_col_arg.startswith("c"): + new_column = int(new_col_arg[1:])-1 + else: + new_column = int(new_col_arg)-1 +except ValueError: + stop_err("Expected column number, got %s" % new_col_arg) +if old_column == new_column: + stop_err("Old and new column arguments are the same!") + +def parse_ids(tabular_file, old_col, new_col): + """Read tabular file and record all specified ID mappings.""" + handle = open(tabular_file, "rU") + for line in handle: + if not line.startswith("#"): + parts = line.rstrip("\n").split("\t") + yield parts[old_col].strip(), parts[new_col].strip() + handle.close() + +#Load the rename mappings +rename = dict(parse_ids(tabular_file, old_column, new_column)) +print "Loaded %i ID mappings" % len(rename) + +#Rewrite the sequence file +if seq_format.lower()=="sff": + #Use Biopython for this format + renamed = 0 + def rename_seqrecords(records, mapping): + global renamed #nasty, but practical! + for record in records: + try: + record.id = mapping[record.id] + renamed += 1 + except KeyError: + pass + yield record + + try: + from Bio.SeqIO.SffIO import SffIterator, SffWriter + except ImportError: + stop_err("Requires Biopython 1.54 or later") + + try: + from Bio.SeqIO.SffIO import ReadRocheXmlManifest + except ImportError: + #Prior to Biopython 1.56 this was a private function + from Bio.SeqIO.SffIO import _sff_read_roche_index_xml as ReadRocheXmlManifest + + in_handle = open(in_file, "rb") #must be binary mode! + try: + manifest = ReadRocheXmlManifest(in_handle) + except ValueError: + manifest = None + out_handle = open(out_file, "wb") + writer = SffWriter(out_handle, xml=manifest) + in_handle.seek(0) #start again after getting manifest + count = writer.write_file(rename_seqrecords(SffIterator(in_handle), rename)) + out_handle.close() + in_handle.close() +else: + #Use Galaxy for FASTA, QUAL or FASTQ + if seq_format.lower() in ["fasta", "csfasta"] \ + or seq_format.lower().startswith("qual"): + from galaxy_utils.sequence.fasta import fastaReader, fastaWriter + reader = fastaReader(open(in_file, "rU")) + writer = fastaWriter(open(out_file, "w")) + marker = ">" + elif seq_format.lower().startswith("fastq"): + from galaxy_utils.sequence.fastq import fastqReader, fastqWriter + reader = fastqReader(open(in_file, "rU")) + writer = fastqWriter(open(out_file, "w")) + marker = "@" + else: + stop_err("Unsupported file type %r" % seq_format) + #Now do the renaming + count = 0 + renamed = 0 + for record in reader: + #The [1:] is because the fastaReader leaves the > on the identifier, + #likewise the fastqReader leaves the @ on the identifier + try: + idn, descr = record.identifier[1:].split(None, 1) + except ValueError: + idn = record.identifier[1:] + descr = None + if idn in rename: + if descr: + record.identifier = "%s%s %s" % (marker, rename[idn], descr) + else: + record.identifier = "%s%s" % (marker, rename[idn]) + renamed += 1 + writer.write(record) + count += 1 + writer.close() + reader.close() + +print "Renamed %i out of %i records" % (renamed, count)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/filters/seq_rename.txt Tue Jun 07 17:43:26 2011 -0400 @@ -0,0 +1,78 @@ +Galaxy tool to renamed FASTA, QUAL, FASTQ or SFF sequences +========================================================== + +This tool is copyright 2011 by Peter Cock, The James Hutton Institute +(formerly SCRI, Scottish Crop Research Institute), UK. All rights reserved. +See the licence text below. + +This tool is a short Python script (using Biopython library functions) to rename +sequences from a FASTA, QUAL, FASTQ, or SFF file based on an ID mapping gives as +two columns of a tabular file. The output order follows that of the sequence file, +and if there are duplicates in the input sequence file, there will be duplicates +in the output sequence file. + +See also the sister tools to filter or select sequence files according to IDs +from column(s) of tabular file. + + +There are just two files to install: + +* seq_rename.py (the Python script) +* seq_rename.xml (the Galaxy tool definition) + +The suggested location is in the Galaxy folder tools/filters next to the tool +for calling sff_extract.py for converting SFF to FASTQ or FASTA + QUAL. + +You will also need to modify the tools_conf.xml file to tell Galaxy to offer the +tool. One suggested location is in the filters section. Simply add the line: + +<tool file="filters/seq_rename.xml" /> + +You will also need to install Biopython 1.54 or later. That's it. + + +History +======= + +v0.0.1 - Initial version. + + +Developers +========== + +This script and related tools are being developed on the following hg branch: +http://bitbucket.org/peterjc/galaxy-central/src/tools + +For making the "Galaxy Tool Shed" http://community.g2.bx.psu.edu/ tarball use +the following command from the Galaxy root folder: + +tar -czf seq_rename.tar.gz tools/filters/seq_rename.* + +Check this worked: + +$ tar -tzf seq_rename.tar.gz +filter/seq_rename.py +filter/seq_rename.txt +filter/seq_rename.xml + + +Licence (MIT/BSD style) +======================= + +Permission to use, copy, modify, and distribute this software and its +documentation with or without modifications and for any purpose and +without fee is hereby granted, provided that any copyright notices +appear in all copies and that both those copyright notices and this +permission notice appear in supporting documentation, and that the +names of the contributors or copyright holders not be used in +advertising or publicity pertaining to distribution of the software +without specific prior permission. + +THE CONTRIBUTORS AND COPYRIGHT HOLDERS OF THIS SOFTWARE DISCLAIM ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT +OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE +OR PERFORMANCE OF THIS SOFTWARE.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/filters/seq_rename.xml Tue Jun 07 17:43:26 2011 -0400 @@ -0,0 +1,56 @@ +<tool id="seq_rename" name="Rename sequences" version="0.0.1"> + <description>with ID mapping from a tabular file</description> + <command interpreter="python"> +seq_rename.py $input_tabular $old_column $new_column $input_file $input_file.ext $output_file + </command> + <inputs> + <param name="input_file" type="data" format="fasta,qual,fastq,sff" label="Sequence file" help="FASTA, QUAL, FASTQ, or SFF format." /> + <param name="input_tabular" type="data" format="tabular" label="Tabular file containing sequence identifiers"/> + <param name="old_column" type="data_column" data_ref="input_tabular" multiple="False" numerical="False" label="Column containing current (old) sequence identifiers"/> + <param name="new_column" type="data_column" data_ref="input_tabular" multiple="False" numerical="False" label="Column contai +ning new sequence identifiers"/> + </inputs> + <outputs> + <data name="output_file" format="fasta" label="Renamed ${on_string}"> + <!-- TODO - Replace this with format="input:input_fastq" if/when that works --> + <change_format> + <when input_dataset="input_file" attribute="extension" value="sff" format="sff" /> + <when input_dataset="input_file" attribute="extension" value="fastq" format="fastq" /> + <when input_dataset="input_file" attribute="extension" value="fastqsanger" format="fastqsanger" /> + <when input_dataset="input_file" attribute="extension" value="fastqsolexa" format="fastqsolexa" /> + <when input_dataset="input_file" attribute="extension" value="fastqillumina" format="fastqillumina" /> + <when input_dataset="input_file" attribute="extension" value="fastqcssanger" format="fastqcssanger" /> + </change_format> + </data> + </outputs> + <tests> + </tests> + <requirements> + <requirement type="python-module">Bio</requirement> + </requirements> + <help> + +**What it does** + +Takes a FASTA, QUAL, FASTQ or Standard Flowgram Format (SFF) file and produces a +new sequence file (of the same format) where the sequence identifiers have been +renamed according two the specified columns a the tabular file. + +WARNING: If you have any duplicates in the intput sequence file, you will still +have duplicate sequences in the output. + +WARNING: If the tabular file has more than one new name for any old ID, the +last one is used. + +**Citation** + +This tool uses Biopython to read and write SFF files. If you use this tool in +scientific work leading to a publication, please cite the Biopython application +note (and Galaxy too of course): + +Cock et al 2009. Biopython: freely available Python tools for computational +molecular biology and bioinformatics. Bioinformatics 25(11) 1422-3. +http://dx.doi.org/10.1093/bioinformatics/btp163 pmid:19304878. + + </help> +</tool>