2
|
1 #!/usr/bin/env python
|
|
2
|
50
|
3 import argparse
|
|
4 import logging
|
|
5 import re
|
|
6 from sys import stdout
|
|
7 from Bio.SeqIO.QualityIO import FastqGeneralIterator
|
|
8 # avoid ugly python IOError when stdout output is piped into another program
|
|
9 # and then truncated (such as piping to head)
|
|
10 from signal import signal, SIGPIPE, SIG_DFL
|
|
11 signal(SIGPIPE, SIG_DFL)
|
|
12
|
2
|
13 tool_description = """
|
|
14 Exract barcodes from a FASTQ file according to a user-specified pattern.
|
|
15
|
|
16 By default output is written to stdout.
|
|
17
|
|
18 Example usage:
|
5
|
19 - remove barcode nucleotides at positions 1-3 and 6-7 from FASTQ; write modified
|
|
20 FASTQ entries to output.fastq and barcode nucleotides to barcodes.fa:
|
7
|
21 fastq_extract_barcodes.py barcoded_input.fastq XXXNNXX --out output.fastq --bcs barcodes.fastq
|
2
|
22 """
|
|
23
|
|
24 epilog = """
|
|
25 Author: Daniel Maticzka
|
|
26 Copyright: 2015
|
|
27 License: Apache
|
|
28 Email: maticzkd@informatik.uni-freiburg.de
|
|
29 Status: Testing
|
|
30 """
|
|
31
|
|
32 # parse command line arguments
|
|
33 parser = argparse.ArgumentParser(description=tool_description,
|
|
34 epilog=epilog,
|
|
35 formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
36 # positional arguments
|
|
37 parser.add_argument(
|
|
38 "infile",
|
|
39 help="Path to fastq file.")
|
|
40 parser.add_argument(
|
|
41 "pattern",
|
|
42 help="Pattern of barcode nucleotides starting at 5'-end. X positions will be moved to the header, N positions will be kept.")
|
|
43 # optional arguments
|
|
44 parser.add_argument(
|
|
45 "-o", "--outfile",
|
|
46 help="Write results to this file.")
|
|
47 parser.add_argument(
|
|
48 "-b", "--bcs",
|
|
49 dest="out_bc_fasta",
|
7
|
50 help="Write barcodes to this file in FASTQ format.")
|
|
51 parser.add_argument(
|
|
52 "--fasta-barcodes",
|
|
53 dest="save_bcs_as_fa",
|
|
54 action="store_true",
|
|
55 help="Save extracted barcodes in FASTA format.")
|
2
|
56 parser.add_argument(
|
5
|
57 "-a", "--add-bc-to-fastq",
|
|
58 dest="add_to_head",
|
7
|
59 help="Append extracted barcodes to the FASTQ headers.",
|
|
60 action="store_true")
|
5
|
61 parser.add_argument(
|
2
|
62 "-v", "--verbose",
|
|
63 help="Be verbose.",
|
|
64 action="store_true")
|
|
65 parser.add_argument(
|
|
66 "-d", "--debug",
|
|
67 help="Print lots of debugging information",
|
|
68 action="store_true")
|
|
69 parser.add_argument(
|
|
70 '--version',
|
|
71 action='version',
|
7
|
72 version='0.1.0')
|
2
|
73
|
|
74 args = parser.parse_args()
|
|
75 if args.debug:
|
|
76 logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(filename)s - %(levelname)s - %(message)s")
|
|
77 elif args.verbose:
|
|
78 logging.basicConfig(level=logging.INFO, format="%(filename)s - %(levelname)s - %(message)s")
|
|
79 else:
|
|
80 logging.basicConfig(format="%(filename)s - %(levelname)s - %(message)s")
|
|
81 logging.info("Parsed arguments:")
|
|
82 logging.info(" infile: '{}'".format(args.infile))
|
|
83 logging.info(" pattern: '{}'".format(args.pattern))
|
|
84 if args.outfile:
|
|
85 logging.info(" outfile: enabled writing to file")
|
|
86 logging.info(" outfile: '{}'".format(args.outfile))
|
|
87 if args.out_bc_fasta:
|
7
|
88 logging.info(" bcs: enabled writing barcodes to fastq file")
|
2
|
89 logging.info(" bcs: {}".format(args.out_bc_fasta))
|
7
|
90 if args.save_bcs_as_fa:
|
|
91 logging.info(" fasta-barcodes: write barcodes in fasta format instead of fastq")
|
2
|
92 logging.info("")
|
|
93
|
|
94 # check if supplied pattern is valid
|
|
95 valid_pattern = re.compile("^[XN]+$")
|
|
96 pattern_match = valid_pattern.match(args.pattern)
|
|
97 if pattern_match is None:
|
|
98 raise ValueError("Error: supplied pattern '{}' is not valid.".format(args.pattern))
|
|
99
|
|
100 # check if at least one barcode position is included in the pattern
|
|
101 has_bcpos_pattern = re.compile("X")
|
|
102 pattern_match = has_bcpos_pattern.search(args.pattern)
|
|
103 if pattern_match is None:
|
|
104 raise ValueError("Error: supplied pattern '{}' does not contain a barcode position 'X'.".format(args.pattern))
|
|
105
|
|
106 logging.info("Barcode pattern analysis:")
|
|
107 # get X positions of pattern string
|
|
108 barcode_nt_pattern = re.compile("X+")
|
|
109 barcode_positions = []
|
|
110 for m in re.finditer(barcode_nt_pattern, args.pattern):
|
|
111 logging.info(' found barcode positions in pattern: %02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
|
|
112 barcode_positions.append((m.start(), m.end()))
|
|
113 logging.info(" barcode positions: {}".format(barcode_positions))
|
|
114 # get last position of a barcode nt in the pattern
|
|
115 # reads must be long enough for all
|
|
116 min_readlen = barcode_positions[-1][-1]
|
|
117 logging.info(" last position of a barcode nt in pattern: {}".format(min_readlen))
|
|
118 logging.info("")
|
|
119
|
|
120 # get coordinates of nucleotides to keep
|
|
121 # the tail after the last barcode nt is handled separately
|
|
122 seq_positions = []
|
|
123 last_seq_start = 0
|
|
124 for bcstart, bcstop in barcode_positions:
|
|
125 seq_positions.append((last_seq_start, bcstart))
|
|
126 last_seq_start = bcstop
|
|
127 logging.info(" sequence positions: {}".format(seq_positions))
|
|
128 logging.info(" start of sequence tail: {}".format(last_seq_start))
|
|
129
|
|
130 samout = (open(args.outfile, "w") if args.outfile is not None else stdout)
|
|
131 if args.out_bc_fasta is not None:
|
|
132 faout = open(args.out_bc_fasta, "w")
|
|
133 for header, seq, qual in FastqGeneralIterator(open(args.infile)):
|
|
134
|
|
135 # skip reads that are too short to extract the full requested barcode
|
|
136 if len(seq) < min_readlen:
|
|
137 logging.warning("skipping read '{}', is too short to extract the full requested barcode".format(header))
|
|
138 logging.debug("seq: {}".format(seq))
|
|
139 logging.debug("len(seq): {}".format(len(seq)))
|
|
140 continue
|
|
141
|
|
142 # extract barcode nucleotides
|
|
143 barcode_list = []
|
7
|
144 barcode_qual_list = []
|
2
|
145 for bcstart, bcstop in barcode_positions:
|
|
146 barcode_list.append(seq[bcstart:bcstop])
|
7
|
147 barcode_qual_list.append(qual[bcstart:bcstop])
|
2
|
148 barcode = "".join(barcode_list)
|
7
|
149 barcode_quals = "".join(barcode_qual_list)
|
2
|
150 logging.debug("extracted barcode: {}".format(barcode))
|
|
151
|
|
152 # create new sequence and quality string without barcode nucleotides
|
|
153 new_seq_list = []
|
|
154 new_qual_list = []
|
|
155 for seqstart, seqstop in seq_positions:
|
|
156 new_seq_list.append(seq[seqstart:seqstop])
|
|
157 new_qual_list.append(qual[seqstart:seqstop])
|
|
158 new_seq_list.append(seq[last_seq_start:])
|
|
159 new_qual_list.append(qual[last_seq_start:])
|
|
160 new_seq = "".join(new_seq_list)
|
|
161 new_qual = "".join(new_qual_list)
|
|
162 # check if at least one nucleotide is left. having none would break fastq
|
|
163 if len(new_seq) == 0:
|
|
164 logging.warning("skipping read '{}', no sequence remains after barcode extraction".format(header))
|
|
165 logging.debug("seq: {}".format(seq))
|
|
166 logging.debug("len(seq): {}".format(len(seq)))
|
|
167 continue
|
|
168
|
|
169 # write barcode nucleotides into header
|
5
|
170 if args.add_to_head:
|
|
171 annotated_header = " ".join([header, barcode])
|
|
172 else:
|
|
173 annotated_header = header
|
2
|
174 samout.write("@%s\n%s\n+\n%s\n" % (annotated_header, new_seq, new_qual))
|
|
175
|
|
176 # write barcode to fasta if requested
|
|
177 if args.out_bc_fasta is not None:
|
7
|
178 if args.save_bcs_as_fa:
|
|
179 faout.write(">{}\n{}\n".format(header, barcode))
|
|
180 else:
|
|
181 faout.write("@{}\n{}\n+\n{}\n".format(header, barcode, barcode_quals))
|
2
|
182
|
|
183 # close files
|
|
184 samout.close()
|
|
185 if args.out_bc_fasta is not None:
|
|
186 faout.close()
|