annotate long2short.py @ 4:060fc04af21c draft default tip

planemo upload for repository https://github.com/kavonrtep/galaxy_tools commit dcff0045d0b212ebc5c4bef6a824bdbd3bb16ac3
author petr-novak
date Fri, 08 Dec 2023 13:11:02 +0000
parents dd46956ff61f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
1 #!/usr/bin/env python3
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
2 import argparse
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
3 import sys
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
4 from argparse import ArgumentDefaultsHelpFormatter
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
5 from collections import namedtuple
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
6 from collections import OrderedDict
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
7 from Bio import SeqIO
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
8
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
9 SEQ_FORMAT = "fasta"
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
10 # Default sampling, used on argparse:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
11 DEFAULT_READ_LENGTH = 200
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
12 DEFAULT_INSERT_LENGTH = 700
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
13 DEFAULT_COVERAGE = 0.1
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
14
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
15 Sequences_summary = namedtuple('Fasta_summary',
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
16 ['total_length', 'number_of_sequence',
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
17 'id_length', 'file_path', 'format'])
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
18
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
19 Coordinates = namedtuple('Coordinates', "id start1 end1 start2 end2")
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
20
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
21
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
22 def get_sequences_summary(seq_file):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
23 ''' return basic characteristic of sequences '''
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
24 id_length = OrderedDict()
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
25 totat_length = 0
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
26 N = 0
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
27 for seqs in SeqIO.parse(seq_file, SEQ_FORMAT):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
28 id_length[seqs.id] = len(seqs)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
29 totat_length += len(seqs)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
30 N += 1
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
31 return Sequences_summary(totat_length, N, id_length, seq_file, SEQ_FORMAT)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
32
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
33
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
34 def get_short_pseudoreads_position(fasta_summary, sampling_options):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
35 """Return selected position on long read sequences
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
36 Arguments:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
37 fasta_summary - namedtuple Fasta_summaty containing information about sequences
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
38 sampling options - namedtuple, specified how sequences should be sampled
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
39 Return value:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
40 (sequence_id, start1, end1, start2, end2)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
41 """
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
42 interval = int(2 * sampling_options.read_length /
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
43 sampling_options.coverage)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
44 for seqname, length in fasta_summary.id_length.items():
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
45 start_positions = range(1, length, interval)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
46 for s in start_positions:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
47 yield Coordinates(seqname, s, s + sampling_options.read_length,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
48 s + sampling_options.insert_length -
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
49 sampling_options.read_length,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
50 s + sampling_options.insert_length)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
51
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
52
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
53 def extract_short_reads(summary, args):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
54 '''yield short reades sampled from long reads
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
55 Arguments:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
56 summary.. named tuple specifie sequences properties, path, length, idslist
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
57 args ..... Define how short sequences should be generated
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
58 '''
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
59 pos = get_short_pseudoreads_position(summary, args)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
60 coords = next(pos)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
61 index = 0
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
62 for i in SeqIO.parse(summary.file_path, summary.format):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
63 index += 1
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
64 while True:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
65 if coords.id == i.id:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
66 # forward read
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
67 subseq_f = i[coords.start1:coords.end1]
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
68 subseq_f.id = "{}_{}_{}_f".format(index, coords.start1,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
69 coords.end1)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
70 subseq_f.description = ""
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
71 # reverse complement read
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
72 subseq_r = i[coords.start2:coords.end2].reverse_complement()
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
73 subseq_r.id = "{}_{}_{}_r".format(index, coords.start1,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
74 coords.end1)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
75 subseq_r.description = ""
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
76 # return only if sequences are long enough
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
77 if len(subseq_r) == args.read_length:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
78 yield subseq_f
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
79 yield subseq_r
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
80 coords = next(pos)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
81 else:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
82 break
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
83
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
84
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
85 def long2short(args):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
86 '''Sample short reads from long sequences
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
87 args contain these attributes::
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
88 ------------
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
89 input_file - path to file in fasta format
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
90 output_file - path to output file, fasta format
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
91 options - options is named tuple and specifies read length
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
92 coverage, insert length, max number of sequences which will be return
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
93
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
94 '''
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
95 summary = get_sequences_summary(args.input.name)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
96 with open(args.output.name, 'w') as f:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
97 for i in extract_short_reads(summary, args):
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
98 SeqIO.write(i, f, SEQ_FORMAT)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
99
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
100
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
101 def get_args():
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
102 '''Parses command line arguments '''
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
103 description = "Creates pseudo short reads from long oxford nanopore reads"
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
104 parser = argparse.ArgumentParser(
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
105 description=description,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
106 formatter_class=ArgumentDefaultsHelpFormatter)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
107 parser.add_argument('-i',
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
108 '--input',
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
109 type=argparse.FileType('r'),
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
110 help="file with long reads in fasta format")
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
111 parser.add_argument('-o',
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
112 '--output',
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
113 type=argparse.FileType('w'),
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
114 help="Output file name")
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
115 parser.add_argument("-cov",
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
116 "--coverage",
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
117 type=float,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
118 default=DEFAULT_COVERAGE,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
119 help="samplig coverage")
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
120 parser.add_argument(
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
121 "-L",
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
122 "--insert_length",
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
123 type=int,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
124 default=DEFAULT_INSERT_LENGTH,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
125 help="length of insert, must be longer than read length")
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
126 parser.add_argument("-l",
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
127 "--read_length",
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
128 type=int,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
129 default=DEFAULT_READ_LENGTH,
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
130 help="read length")
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
131
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
132 args = parser.parse_args()
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
133 if len(sys.argv) == 1:
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
134 parser.print_help()
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
135 sys.exit(1)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
136
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
137 #hassert args.insert_length > args.read_length, "read length must be shorter than insert length"
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
138 return args
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
139
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
140
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
141 def main():
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
142 '''Sample short reads from long sequences
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
143 Files path are passed as command line positional arguments
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
144 '''
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
145 args = get_args()
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
146 long2short(args)
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
147
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
148
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
149 if __name__ == "__main__":
dd46956ff61f Uploaded
petr-novak
parents:
diff changeset
150 main()