2
|
1 #!/usr/bin/env python
|
|
2
|
50
|
3 import argparse
|
|
4 import logging
|
|
5 from string import maketrans
|
|
6 from sys import stdout
|
|
7 from Bio import SeqIO
|
|
8 from Bio.Seq import Seq
|
|
9 from Bio.Alphabet import IUPAC
|
|
10
|
2
|
11 tool_description = """
|
|
12 Convert standard nucleotides to IUPAC nucleotide codes used for binary barcodes.
|
|
13
|
|
14 A and G are converted to nucleotide code R. T, U and C are converted to Y. By
|
|
15 default output is written to stdout.
|
|
16
|
|
17 Example usage:
|
|
18 - write converted sequences from file in.fa to file file out.fa:
|
|
19 convert_bc_to_binary_RY.py in.fa --outfile out.fa
|
|
20 """
|
|
21
|
|
22 epilog = """
|
|
23 Author: Daniel Maticzka
|
|
24 Copyright: 2015
|
|
25 License: Apache
|
|
26 Email: maticzkd@informatik.uni-freiburg.de
|
|
27 Status: Testing
|
|
28 """
|
|
29
|
|
30 # parse command line arguments
|
|
31 parser = argparse.ArgumentParser(description=tool_description,
|
|
32 epilog=epilog,
|
|
33 formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
34 # positional arguments
|
|
35 parser.add_argument(
|
|
36 "infile",
|
8
|
37 help="Path to fastq input file.")
|
2
|
38 # optional arguments
|
|
39 parser.add_argument(
|
|
40 "-o", "--outfile",
|
|
41 help="Write results to this file.")
|
|
42 parser.add_argument(
|
8
|
43 "--fasta-format",
|
|
44 dest="fasta_format",
|
|
45 help="Read and write fasta instead of fastq format.",
|
|
46 action="store_true")
|
|
47 parser.add_argument(
|
2
|
48 "-v", "--verbose",
|
|
49 help="Be verbose.",
|
|
50 action="store_true")
|
|
51 parser.add_argument(
|
|
52 "-d", "--debug",
|
|
53 help="Print lots of debugging information",
|
|
54 action="store_true")
|
|
55 parser.add_argument(
|
|
56 '--version',
|
|
57 action='version',
|
|
58 version='0.1.0')
|
|
59
|
|
60
|
|
61 def translate_nt_to_RY(seq):
|
|
62 """Translates nucleotides to RY (A,G -> R; C,U,T -> Y).
|
|
63
|
|
64 >>> translate_nt_to_RY("ACGUTACGUT")
|
|
65 RYRYYRYRYY
|
|
66 """
|
|
67 trans_table = maketrans("AGCUT", "RRYYY")
|
|
68 trans_seq = seq.translate(trans_table)
|
|
69 logging.debug(seq + " -> " + trans_seq)
|
|
70 return trans_seq
|
|
71
|
|
72
|
|
73 def translate_nt_to_RY_iterator(robj):
|
|
74 """Translate SeqRecords sequences to RY alphabet."""
|
|
75 for record in robj:
|
8
|
76 if not args.fasta_format:
|
|
77 saved_letter_annotations = record.letter_annotations
|
|
78 record.letter_annotations = {}
|
2
|
79 record.seq = Seq(translate_nt_to_RY(str(record.seq)),
|
|
80 IUPAC.unambiguous_dna)
|
8
|
81 if not args.fasta_format:
|
|
82 record.letter_annotations = saved_letter_annotations
|
2
|
83 yield record
|
|
84
|
|
85 # handle arguments
|
|
86 args = parser.parse_args()
|
|
87 if args.debug:
|
|
88 logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(filename)s - %(levelname)s - %(message)s")
|
|
89 elif args.verbose:
|
|
90 logging.basicConfig(level=logging.INFO, format="%(filename)s - %(levelname)s - %(message)s")
|
|
91 else:
|
|
92 logging.basicConfig(format="%(filename)s - %(levelname)s - %(message)s")
|
|
93 logging.info("Parsed arguments:")
|
|
94 if args.outfile:
|
|
95 logging.info(" outfile: enabled writing to file")
|
|
96 logging.info(" outfile: '{}'".format(args.outfile))
|
|
97 logging.info(" outfile: '{}'".format(args.outfile))
|
|
98 logging.info("")
|
|
99
|
|
100 # get input iterator
|
|
101 input_handle = open(args.infile, "rU")
|
8
|
102 if args.fasta_format:
|
|
103 input_seq_iterator = SeqIO.parse(input_handle, "fasta")
|
|
104 else:
|
|
105 input_seq_iterator = SeqIO.parse(input_handle, "fastq")
|
2
|
106 convert_seq_iterator = translate_nt_to_RY_iterator(input_seq_iterator)
|
|
107 output_handle = (open(args.outfile, "w") if args.outfile is not None else stdout)
|
8
|
108 if args.fasta_format:
|
|
109 SeqIO.write(convert_seq_iterator, output_handle, "fasta")
|
|
110 else:
|
|
111 SeqIO.write(convert_seq_iterator, output_handle, "fastq")
|
2
|
112 output_handle.close()
|