annotate jbrowse2/blastxml_to_gapped_gff3.py @ 1:af3fba6c21aa draft

add datatypes_conf.xml with hic
author fubar
date Wed, 03 Jan 2024 01:50:05 +0000
parents cd5d63cd0eb5
children 88b9b105c09b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
1 #!/usr/bin/env python
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
2 import argparse
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
3 import copy
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
4 import logging
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
5 import re
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
6 import sys
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
7
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
8 from BCBio import GFF
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
9 logging.basicConfig(level=logging.INFO)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
10 log = logging.getLogger(name='blastxml2gff3')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
11
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
12 __doc__ = """
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
13 BlastXML files, when transformed to GFF3, do not normally show gaps in the
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
14 blast hits. This tool aims to fill that "gap".
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
15 """
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
16
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
17
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
18 def blastxml2gff3(blastxml, min_gap=3, trim=False, trim_end=False, include_seq=False):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
19 from Bio.Blast import NCBIXML
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
20 from Bio.Seq import Seq
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
21 from Bio.SeqRecord import SeqRecord
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
22 from Bio.SeqFeature import SeqFeature, SimpleLocation
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
23
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
24 blast_records = NCBIXML.parse(blastxml)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
25 for idx_record, record in enumerate(blast_records):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
26 # http://www.sequenceontology.org/browser/release_2.4/term/SO:0000343
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
27 match_type = { # Currently we can only handle BLASTN, BLASTP
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
28 'BLASTN': 'nucleotide_match',
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
29 'BLASTP': 'protein_match',
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
30 }.get(record.application, 'match')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
31
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
32 recid = record.query
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
33 if ' ' in recid:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
34 recid = recid[0:recid.index(' ')]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
35
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
36 rec = SeqRecord(Seq("ACTG"), id=recid)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
37 for idx_hit, hit in enumerate(record.alignments):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
38 for idx_hsp, hsp in enumerate(hit.hsps):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
39 qualifiers = {
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
40 "ID": 'b2g.%s.%s.%s' % (idx_record, idx_hit, idx_hsp),
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
41 "source": "blast",
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
42 "score": hsp.expect,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
43 "accession": hit.accession,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
44 "hit_id": hit.hit_id,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
45 "length": hit.length,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
46 "hit_titles": hit.title.split(' >'),
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
47 }
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
48 if include_seq:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
49 qualifiers.update({
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
50 'blast_qseq': hsp.query,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
51 'blast_sseq': hsp.sbjct,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
52 'blast_mseq': hsp.match,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
53 })
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
54
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
55 for prop in ('score', 'bits', 'identities', 'positives',
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
56 'gaps', 'align_length', 'strand', 'frame',
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
57 'query_start', 'query_end', 'sbjct_start',
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
58 'sbjct_end'):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
59 qualifiers['blast_' + prop] = getattr(hsp, prop, None)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
60
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
61 desc = hit.title.split(' >')[0]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
62 qualifiers['description'] = desc[desc.index(' '):]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
63
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
64 # This required a fair bit of sketching out/match to figure out
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
65 # the first time.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
66 #
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
67 # the match_start location must account for queries and
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
68 # subjecst that start at locations other than 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
69 parent_match_start = hsp.query_start - hsp.sbjct_start
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
70 # The end is the start + hit.length because the match itself
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
71 # may be longer than the parent feature, so we use the supplied
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
72 # subject/hit length to calculate the real ending of the target
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
73 # protein.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
74 parent_match_end = hsp.query_start + hit.length + hsp.query.count('-')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
75
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
76 # If we trim the left end, we need to trim without losing information.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
77 used_parent_match_start = parent_match_start
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
78 if trim:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
79 if parent_match_start < 1:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
80 used_parent_match_start = 0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
81
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
82 if trim or trim_end:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
83 if parent_match_end > hsp.query_end:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
84 parent_match_end = hsp.query_end + 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
85
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
86 # The ``match`` feature will hold one or more ``match_part``s
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
87 top_feature = SeqFeature(
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
88 SimpleLocation(used_parent_match_start, parent_match_end, strand=0),
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
89 type=match_type,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
90 qualifiers=qualifiers
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
91 )
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
92
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
93 # Unlike the parent feature, ``match_part``s have sources.
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
94 part_qualifiers = {
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
95 "source": "blast",
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
96 }
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
97 top_feature.sub_features = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
98 for idx_part, (start, end, cigar) in \
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
99 enumerate(generate_parts(hsp.query, hsp.match,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
100 hsp.sbjct,
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
101 ignore_under=min_gap)):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
102 part_qualifiers['Gap'] = cigar
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
103 part_qualifiers['ID'] = qualifiers['ID'] + ('.%s' % idx_part)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
104
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
105 # Otherwise, we have to account for the subject start's location
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
106 match_part_start = parent_match_start + hsp.sbjct_start + start - 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
107
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
108 # We used to use hsp.align_length here, but that includes
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
109 # gaps in the parent sequence
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
110 #
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
111 # Furthermore align_length will give calculation errors in weird places
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
112 # So we just use (end-start) for simplicity
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
113 match_part_end = match_part_start + (end - start)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
114
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
115 top_feature.sub_features.append(
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
116 SeqFeature(
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
117 SimpleLocation(match_part_start, match_part_end, strand=1),
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
118 type="match_part",
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
119 qualifiers=copy.deepcopy(part_qualifiers))
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
120 )
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
121
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
122 rec.features.append(top_feature)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
123 rec.annotations = {}
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
124 yield rec
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
125
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
126
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
127 def __remove_query_gaps(query, match, subject):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
128 """remove positions in all three based on gaps in query
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
129
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
130 In order to simplify math and calculations...we remove all of the gaps
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
131 based on gap locations in the query sequence::
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
132
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
133 Q:ACTG-ACTGACTG
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
134 S:ACTGAAC---CTG
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
135
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
136 will become::
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
137
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
138 Q:ACTGACTGACTG
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
139 S:ACTGAC---CTG
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
140
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
141 which greatly simplifies the process of identifying the correct location
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
142 for a match_part
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
143 """
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
144 prev = 0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
145 fq = ''
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
146 fm = ''
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
147 fs = ''
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
148 for position in re.finditer('-', query):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
149 fq += query[prev:position.start()]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
150 fm += match[prev:position.start()]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
151 fs += subject[prev:position.start()]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
152 prev = position.start() + 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
153 fq += query[prev:]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
154 fm += match[prev:]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
155 fs += subject[prev:]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
156
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
157 return (fq, fm, fs)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
158
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
159
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
160 def generate_parts(query, match, subject, ignore_under=3):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
161 region_q = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
162 region_m = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
163 region_s = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
164
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
165 (query, match, subject) = __remove_query_gaps(query, match, subject)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
166
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
167 region_start = -1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
168 region_end = -1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
169 mismatch_count = 0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
170 for i, (q, m, s) in enumerate(zip(query, match, subject)):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
171
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
172 # If we have a match
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
173 if m != ' ' or m == '+':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
174 if region_start == -1:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
175 region_start = i
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
176 # It's a new region, we need to reset or it's pre-seeded with
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
177 # spaces
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
178 region_q = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
179 region_m = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
180 region_s = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
181 region_end = i
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
182 mismatch_count = 0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
183 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
184 mismatch_count += 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
185
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
186 region_q.append(q)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
187 region_m.append(m)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
188 region_s.append(s)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
189
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
190 if mismatch_count >= ignore_under and region_start != -1 and region_end != -1:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
191 region_q = region_q[0:-ignore_under]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
192 region_m = region_m[0:-ignore_under]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
193 region_s = region_s[0:-ignore_under]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
194 yield region_start, region_end + 1, \
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
195 cigar_from_string(region_q, region_m, region_s, strict_m=True)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
196 region_q = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
197 region_m = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
198 region_s = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
199
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
200 region_start = -1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
201 region_end = -1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
202 mismatch_count = 0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
203
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
204 yield region_start, region_end + 1, \
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
205 cigar_from_string(region_q, region_m, region_s, strict_m=True)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
206
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
207
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
208 def _qms_to_matches(query, match, subject, strict_m=True):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
209 matchline = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
210
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
211 for (q, m, s) in zip(query, match, subject):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
212 ret = ''
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
213
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
214 if m != ' ' or m == '+':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
215 ret = '='
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
216 elif m == ' ':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
217 if q == '-':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
218 ret = 'D'
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
219 elif s == '-':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
220 ret = 'I'
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
221 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
222 ret = 'X'
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
223 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
224 log.warn("Bad data: \n\t%s\n\t%s\n\t%s\n" % (query, match, subject))
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
225
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
226 if strict_m:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
227 if ret == '=' or ret == 'X':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
228 ret = 'M'
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
229
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
230 matchline.append(ret)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
231 return matchline
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
232
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
233
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
234 def _matchline_to_cigar(matchline):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
235 cigar_line = []
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
236 last_char = matchline[0]
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
237 count = 0
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
238 for char in matchline:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
239 if char == last_char:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
240 count += 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
241 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
242 cigar_line.append("%s%s" % (last_char, count))
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
243 count = 1
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
244 last_char = char
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
245 cigar_line.append("%s%s" % (last_char, count))
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
246 return ' '.join(cigar_line)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
247
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
248
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
249 def cigar_from_string(query, match, subject, strict_m=True):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
250 matchline = _qms_to_matches(query, match, subject, strict_m=strict_m)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
251 if len(matchline) > 0:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
252 return _matchline_to_cigar(matchline)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
253 else:
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
254 return ""
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
255
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
256
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
257 if __name__ == '__main__':
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
258 parser = argparse.ArgumentParser(description='Convert Blast XML to gapped GFF3', epilog='')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
259 parser.add_argument('blastxml', type=argparse.FileType("r"), help='Blast XML Output')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
260 parser.add_argument('--min_gap', type=int, help='Maximum gap size before generating a new match_part', default=3)
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
261 parser.add_argument('--trim', action='store_true', help='Trim blast hits to be only as long as the parent feature')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
262 parser.add_argument('--trim_end', action='store_true', help='Cut blast results off at end of gene')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
263 parser.add_argument('--include_seq', action='store_true', help='Include sequence')
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
264 args = parser.parse_args()
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
265
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
266 for rec in blastxml2gff3(**vars(args)):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
267 if len(rec.features):
cd5d63cd0eb5 Uploaded
fubar
parents:
diff changeset
268 GFF.write([rec], sys.stdout)