annotate tools/seq_select_by_id/seq_select_by_id.py @ 4:6842c0c7bc70 draft

Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
author peterjc
date Mon, 28 Oct 2013 05:21:45 -0400
parents
children 91f55ee8fea5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
1 #!/usr/bin/env python
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
2 """Select FASTA, QUAL, FASTQ or SSF sequences by IDs from a tabular file.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
3
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
4 Takes five command line options, tabular filename, ID column number (using
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
5 one based counting), input filename, input type (e.g. FASTA or SFF) and the
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
6 output filename (same format as input sequence file).
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
7
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
8 When selecting from an SFF file, any Roche XML manifest in the input file is
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
9 preserved in both output files.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
10
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
11 This tool is a short Python script which requires Biopython 1.54 or later
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
12 for SFF file support. If you use this tool in scientific work leading to a
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
13 publication, please cite the Biopython application note:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
14
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
15 Cock et al 2009. Biopython: freely available Python tools for computational
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
16 molecular biology and bioinformatics. Bioinformatics 25(11) 1422-3.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
17 http://dx.doi.org/10.1093/bioinformatics/btp163 pmid:19304878.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
18
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
19 This script is copyright 2011-2013 by Peter Cock, The James Hutton Institute UK.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
20 All rights reserved. See accompanying text file for licence details (MIT
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
21 license).
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
22
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
23 This is version 0.0.6 of the script.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
24 """
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
25 import sys
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
26
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
27 def stop_err(msg, err=1):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
28 sys.stderr.write(msg.rstrip() + "\n")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
29 sys.exit(err)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
30
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
31 if "-v" in sys.argv or "--version" in sys.argv:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
32 print "v0.0.6"
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
33 sys.exit(0)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
34
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
35 #Parse Command Line
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
36 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
37 tabular_file, col_arg, in_file, seq_format, out_file = sys.argv[1:]
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
38 except ValueError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
39 stop_err("Expected five arguments, got %i:\n%s" % (len(sys.argv)-1, " ".join(sys.argv)))
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
40 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
41 if col_arg.startswith("c"):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
42 column = int(col_arg[1:])-1
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
43 else:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
44 column = int(col_arg)-1
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
45 except ValueError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
46 stop_err("Expected column number, got %s" % col_arg)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
47
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
48 if seq_format == "fastqcssanger":
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
49 stop_err("Colorspace FASTQ not supported.")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
50 elif seq_format.lower() in ["sff", "fastq", "qual", "fasta"]:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
51 seq_format = seq_format.lower()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
52 elif seq_format.lower().startswith("fastq"):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
53 #We don't care how the qualities are encoded
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
54 seq_format = "fastq"
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
55 elif seq_format.lower().startswith("qual"):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
56 #We don't care what the scores are
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
57 seq_format = "qual"
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
58 else:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
59 stop_err("Unrecognised file format %r" % seq_format)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
60
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
61
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
62 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
63 from Bio import SeqIO
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
64 except ImportError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
65 stop_err("Biopython 1.54 or later is required")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
66
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
67
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
68 def parse_ids(tabular_file, col):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
69 """Read tabular file and record all specified identifiers."""
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
70 handle = open(tabular_file, "rU")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
71 for line in handle:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
72 if line.strip() and not line.startswith("#"):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
73 yield line.rstrip("\n").split("\t")[col].strip()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
74 handle.close()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
75
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
76 #Index the sequence file.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
77 #If very big, could use SeqIO.index_db() to avoid memory bottleneck...
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
78 records = SeqIO.index(in_file, seq_format)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
79 print "Indexed %i sequences" % len(records)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
80
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
81 if seq_format.lower()=="sff":
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
82 #Special case to try to preserve the XML manifest
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
83 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
84 from Bio.SeqIO.SffIO import SffIterator, SffWriter
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
85 except ImportError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
86 stop_err("Requires Biopython 1.54 or later")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
87
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
88 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
89 from Bio.SeqIO.SffIO import ReadRocheXmlManifest
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
90 except ImportError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
91 #Prior to Biopython 1.56 this was a private function
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
92 from Bio.SeqIO.SffIO import _sff_read_roche_index_xml as ReadRocheXmlManifest
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
93
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
94 in_handle = open(in_file, "rb") #must be binary mode!
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
95 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
96 manifest = ReadRocheXmlManifest(in_handle)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
97 except ValueError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
98 manifest = None
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
99 in_handle.close()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
100
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
101 out_handle = open(out_file, "wb")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
102 writer = SffWriter(out_handle, xml=manifest)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
103 count = 0
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
104 #This does have the overhead of parsing into SeqRecord objects,
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
105 #but doing the header and index at the low level is too fidly.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
106 iterator = (records[name] for name in parse_ids(tabular_file, column))
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
107 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
108 count = writer.write_file(iterator)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
109 except KeyError, err:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
110 out_handle.close()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
111 if name not in records:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
112 stop_err("Identifier %r not found in sequence file" % name)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
113 else:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
114 raise err
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
115 out_handle.close()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
116 else:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
117 #Avoid overhead of parsing into SeqRecord objects,
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
118 #just re-use the original formatting from the input file.
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
119 out_handle = open(out_file, "w")
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
120 count = 0
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
121 for name in parse_ids(tabular_file, column):
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
122 try:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
123 out_handle.write(records.get_raw(name))
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
124 except KeyError:
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
125 out_handle.close()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
126 stop_err("Identifier %r not found in sequence file" % name)
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
127 count += 1
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
128 out_handle.close()
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
129
6842c0c7bc70 Uploaded v0.0.7, depend on Biopython 1.62, tabs to spaces in XML
peterjc
parents:
diff changeset
130 print "Selected %i sequences by ID" % count