| 1 |
import argparse |
| 2 |
import sys |
| 3 |
import os |
| 4 |
from pyfastaq import sequences, utils |
| 5 |
|
| 6 |
def run(description): |
| 7 |
parser = argparse.ArgumentParser( |
| 8 |
description = 'Takes a sequence file. Makes a BAM file containing perfect (unpaired) reads tiling the whole genome', |
| 9 |
usage = 'fastaq to_tiling_bam [options] <infile> <read_length> <read_step> <read_prefix> <outfile>', |
| 10 |
epilog = 'Important: assumes that samtools is in your path') |
| 11 |
parser.add_argument('infile', help='Name of input fasta/q file') |
| 12 |
parser.add_argument('read_length', type=int, help='Length of reads') |
| 13 |
parser.add_argument('read_step', type=int, help='Distance between start of each read') |
| 14 |
parser.add_argument('read_prefix', help='Prefix of read names') |
| 15 |
parser.add_argument('outfile', help='Name of output BAM file') |
| 16 |
parser.add_argument('--qual_char', help='Character to use for quality score [%(default)s]', default='I') |
| 17 |
parser.add_argument('--read_group', help='Add the given read group ID to all reads [%(default)s]' ,default='42') |
| 18 |
options = parser.parse_args() |
| 19 |
|
| 20 |
# make a header first - we need to add the @RG line to the default header made by samtools |
| 21 |
tmp_empty_file = options.outfile + '.tmp.empty' |
| 22 |
f = utils.open_file_write(tmp_empty_file) |
| 23 |
utils.close(f) |
| 24 |
try: |
| 25 |
f = os.popen('samtools view -H -T ' + options.infile + ' ' + tmp_empty_file) |
| 26 |
except IOError: |
| 27 |
print('Error making tmp header file', file=sys.stderr) |
| 28 |
sys.exit(1) |
| 29 |
|
| 30 |
header_lines = f.readlines() |
| 31 |
header_lines.append('@RG\tID:' + options.read_group + '\tSM:FAKE') |
| 32 |
f.close() |
| 33 |
os.unlink(tmp_empty_file) |
| 34 |
|
| 35 |
seq_reader = sequences.file_reader(options.infile) |
| 36 |
try: |
| 37 |
f = os.popen('samtools view -hbS - > ' + options.outfile, 'w') |
| 38 |
except IOError: |
| 39 |
print("Error opening for writing BAM file '" + options.outfile + "'", file=sys.stderr) |
| 40 |
sys.exit(1) |
| 41 |
|
| 42 |
print(''.join(header_lines), file=f) |
| 43 |
|
| 44 |
for seq in seq_reader: |
| 45 |
end_range = len(seq) |
| 46 |
if len(seq) < options.read_length: |
| 47 |
end_range = 1 |
| 48 |
for i in range(0, end_range, options.read_step): |
| 49 |
if len(seq) <= options.read_length: |
| 50 |
start = 0 |
| 51 |
end = len(seq) - 1 |
| 52 |
else: |
| 53 |
start = i |
| 54 |
end = start + options.read_length - 1 |
| 55 |
|
| 56 |
if end > len(seq) - 1: |
| 57 |
end = len(seq) - 1 |
| 58 |
start = end - options.read_length + 1 |
| 59 |
|
| 60 |
read = sequences.Fastq(options.read_prefix + ':' + seq.id + ':' + str(start + 1) + ':' + str(end + 1), seq[start:end+1], options.qual_char * (end - start + 1)) |
| 61 |
|
| 62 |
print ('\t'.join([read.id, |
| 63 |
'0', |
| 64 |
seq.id, |
| 65 |
str(start + 1), |
| 66 |
'60', |
| 67 |
str(len(read)) + 'M', |
| 68 |
'*', |
| 69 |
'*', |
| 70 |
'*', |
| 71 |
read.seq, |
| 72 |
read.qual, |
| 73 |
'RG:Z:' + options.read_group]), file=f) |
| 74 |
|
| 75 |
if end == len(seq) - 1: |
| 76 |
break |
| 77 |
|
| 78 |
f.close() |