Debian Codegraph @

pyfastaq/runners/to_perfect_reads.py

raw
1 import argparse
2 import random
3 from math import floor, ceil
4 import sys
5 from pyfastaq import sequences, utils
6
7 def run(description):
8 parser = argparse.ArgumentParser(
9 description = 'Makes perfect paired end fastq reads from a sequence file, with insert sizes sampled from a normal distribution. Read orientation is innies. Output is an interleaved FASTQ file.',
10 usage = 'fastaq to_perfect_reads [options] <infile> <outfile> <mean insert size> <insert std deviation> <mean coverage> <read length>')
11 parser.add_argument('infile', help='Name of input file')
12 parser.add_argument('outfile', help='Name of output file')
13 parser.add_argument('mean_insert', type=int, help='Mean insert size of read pairs', metavar='mean insert size')
14 parser.add_argument('insert_std', type=float, help='Standard devation of insert size', metavar='insert std deviation')
15 parser.add_argument('coverage', type=float, help='Mean coverage of the reads', metavar='mean coverage')
16 parser.add_argument('readlength', type=int, help='Length of each read', metavar='read length')
17 parser.add_argument('--fragments', help='Write FASTA sequences of fragments (i.e. read pairs plus sequences in between them) to the given filename', metavar='FILENAME')
18 parser.add_argument('--no_n', action='store_true', help='Don\'t allow any N or n characters in the reads')
19 parser.add_argument('--seed', type=int, help='Seed for random number generator. Default is to use python\'s default', default=None, metavar='INT')
20 options = parser.parse_args()
21
22 random.seed(a=options.seed)
23
24 seq_reader = sequences.file_reader(options.infile)
25 fout = utils.open_file_write(options.outfile)
26 pair_counter = 1
27
28 if options.fragments:
29 fout_frags = utils.open_file_write(options.fragments)
30
31 for ref in seq_reader:
32 # check if current seq is long enough
33 if len(ref) < options.mean_insert + 4 * options.insert_std:
34 print('Warning, sequence ', ref.id, ' too short. Skipping it...', file=sys.stderr)
35 continue
36
37 # work out how many reads to simulate
38 read_pairs = int(0.5 * options.coverage * len(ref) / options.readlength)
39
40 # it's possible that we pick the same fragment twice, in which case the
41 # reads would get the same name. So remember the frag coords
42 used_fragments = {} # (middle_position, length) => count
43
44 # do the simulation: pick insert size from normal distribution, and
45 # position in genome from uniform distribution
46 x = 0
47 while x < read_pairs:
48 isize = int(random.normalvariate(options.mean_insert, options.insert_std))
49 while isize > len(ref) or isize < options.readlength:
50 isize = int(random.normalvariate(options.mean_insert, options.insert_std))
51 middle_pos = random.randint(ceil(0.5 *isize), floor(len(ref) - 0.5 * isize))
52 read_start1 = int(middle_pos - ceil(0.5 * isize))
53 read_start2 = read_start1 + isize - options.readlength
54
55 readname = ':'.join([ref.id, str(pair_counter), str(read_start1+1), str(read_start2+1)])
56
57 fragment = (middle_pos, isize)
58 if fragment in used_fragments:
59 used_fragments[fragment] += 1
60 readname += '.dup.' + str(used_fragments[fragment])
61 else:
62 used_fragments[fragment] = 1
63
64 read1 = sequences.Fastq(readname + '/1', ref.seq[read_start1:read_start1 + options.readlength], 'I' * options.readlength)
65 read2 = sequences.Fastq(readname + '/2', ref.seq[read_start2:read_start2 + options.readlength], 'I' * options.readlength)
66
67
68 if options.no_n and ('n' in read1.seq or 'N' in read1.seq or 'n' in read2.seq or 'N' in read2.seq):
69 continue
70
71 read2.revcomp()
72
73 print(read1, file=fout)
74 print(read2, file=fout)
75
76 if options.fragments:
77 frag = sequences.Fasta(readname, ref.seq[read_start1:read_start2 + options.readlength])
78 print(frag, file=fout_frags)
79
80 pair_counter += 1
81 x += 1
82
83 utils.close(fout)
84 if options.fragments:
85 utils.close(fout_frags)