annotate blastxmlToGff3.py @ 49:193e94a43f0f draft

planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 88aa9133c8eccdb21dbc755eb9643c9e7b09449e-dirty
author yating-l
date Wed, 29 Mar 2017 14:46:23 -0400
parents 6803152ea92a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
1 #!/usr/bin/env python
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
2
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
3
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
4 from Bio.Blast import NCBIXML
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
5 from collections import OrderedDict
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
6 import utils
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
7
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
8
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
9 def align2cigar(hsp_query, hsp_reference):
31
d8049deb0c97 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit faeedda55e23f1197bc454d3db2d52af29d786e8-dirty
yating-l
parents: 0
diff changeset
10 """
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
11 Build CIGAR representation from an hsp_query
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
12 input:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
13 hsp_query
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
14 hsp_sbjct
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
15 output:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
16 CIGAR string
31
d8049deb0c97 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit faeedda55e23f1197bc454d3db2d52af29d786e8-dirty
yating-l
parents: 0
diff changeset
17 """
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
18 query = hsp_query
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
19 ref = hsp_reference
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
20 # preType, curType:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
21 # 'M' represents match,
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
22 # 'I' represents insert a gap into the reference sequence,
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
23 # 'D' represents insert a gap into the target (delete from reference)
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
24 # some ideas of this algin2cigar function are coming from
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
25 # https://gist.github.com/ozagordi/099bdb796507da8d9426
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
26 prevType = 'M'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
27 curType = 'M'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
28 count = 0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
29 cigar = []
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
30 num = len(query)
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
31 for i in range(num):
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
32 if query[i] == '-':
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
33 curType = 'D'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
34 elif ref[i] == '-':
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
35 curType = 'I'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
36 else:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
37 curType = 'M'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
38 if curType == prevType:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
39 count += 1
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
40 else:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
41 cigar.append('%s%d' % (prevType, count))
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
42 prevType = curType
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
43 count = 1
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
44 cigar.append('%s%d' % (curType, count))
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
45 return ' '.join(cigar)
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
46
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
47 def gff3_writer(blast_records, gff3_file):
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
48 gff3 = open(gff3_file, 'a')
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
49 gff3.write("##gff-version 3\n")
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
50 seq_regions = dict()
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
51 for blast_record in blast_records:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
52 query_name = blast_record.query.split(" ")[0]
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
53 source = blast_record.application
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
54 method = blast_record.matrix
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
55 for alignment in blast_record.alignments:
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
56 group = {
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
57 "parent_field" : OrderedDict(),
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
58 "parent_attribute" : OrderedDict(),
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
59 "alignments" : []
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
60 }
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
61 title = alignment.title.split(" ")
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
62 contig_name = title[len(title) - 1]
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
63 length = alignment.length
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
64 group['parent_field']['seqid'] = contig_name
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
65 group['parent_field']['source'] = source
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
66 group['parent_field']['type'] = 'match'
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
67 group['parent_attribute']['ID'] = contig_name + '_' + query_name
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
68 group['parent_attribute']['method'] = method
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
69 group['parent_attribute']['length'] = length
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
70 if contig_name not in seq_regions:
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
71 gff3.write("##sequence-region " + contig_name + ' 1 ' + str(length) + '\n')
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
72 seq_regions[contig_name] = length
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
73 match_num = 0
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
74 coords = [length, 0]
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
75 for hsp in alignment.hsps:
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
76 hsp_align = {}
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
77 field = OrderedDict()
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
78 attribute = OrderedDict()
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
79 ref = hsp.sbjct
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
80 query = hsp.query
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
81 field['seqid'] = contig_name
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
82 field['source'] = source
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
83 field['type'] = 'match_part'
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
84
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
85 field['start'] = hsp.sbjct_start
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
86 if field['start'] < coords[0]:
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
87 coords[0] = field['start']
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
88 ref_length = len(ref.replace('-', ''))
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
89 # if run tblastn, the actual length of reference should be multiplied by 3
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
90 if source.lower() == "tblastn":
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
91 ref_length *= 3
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
92 field['end'] = field['start'] + ref_length - 1
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
93 if field['end'] > coords[1]:
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
94 coords[1] = field['end']
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
95 field['score'] = hsp.score
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
96 #decide if the alignment in the same strand or reverse strand
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
97 #reading frame
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
98 # (+, +), (0, 0), (-, -) => +
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
99 # (+, -), (-, +) => -
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
100 if hsp.frame[1] * hsp.frame[0] > 0:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
101 field['strand'] = '+'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
102 elif hsp.frame[1] * hsp.frame[0] < 0:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
103 field['strand'] = '-'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
104 else:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
105 if hsp.frame[0] + hsp.frame[1] >= 0:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
106 field['strand'] = '+'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
107 else:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
108 field['strand'] = '-'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
109 field['phase'] = '.'
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
110
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
111 target_start = hsp.query_start
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
112 target_len = len(query.replace('-', ''))
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
113 # if run blastx, the actual length of query should be multiplied by 3
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
114 if source.lower() == "blastx":
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
115 target_len *= 3
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
116 target_end = target_start + target_len -1
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
117 attribute['ID'] = group['parent_attribute']['ID'] + '_match_' + str(match_num)
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
118 attribute['Parent'] = group['parent_attribute']['ID']
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
119 attribute['Target'] = query_name + " " + str(target_start) + " " + str(target_end)
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
120 attribute['Gap'] = align2cigar(query, ref)
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
121 #store the query sequence and match string in the file in order to display alignment with BlastAlignment plugin
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
122 attribute['subject'] = hsp.sbjct
31
d8049deb0c97 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit faeedda55e23f1197bc454d3db2d52af29d786e8-dirty
yating-l
parents: 0
diff changeset
123 attribute['query'] = hsp.query
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
124 attribute['match'] = hsp.match
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
125 attribute['gaps'] = attribute['match'].count(' ')
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
126 similar = attribute['match'].count('+')
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
127 attribute['identities'] = len(attribute['match']) - similar - attribute['gaps']
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
128 attribute['positives'] = attribute['identities'] + similar
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
129 attribute['expect'] = hsp.expect
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
130 # show reading frame attribute only if the frame is not (0, 0)
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
131 attribute['frame'] = hsp.frame[1]
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
132 match_num += 1
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
133 hsp_align['field'] = field
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
134 hsp_align['attribute'] = attribute
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
135 group['alignments'].append(hsp_align)
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
136 group['parent_field']['start'] = coords[0]
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
137 group['parent_field']['end'] = coords[1]
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
138 group['parent_field']['score'] = group['parent_field']['strand'] = group['parent_field']['phase'] = '.'
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
139 group['parent_attribute']['match_num'] = match_num
48
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
140 group['alignments'].sort(key=lambda x: (x['field']['start'], x['field']['end']))
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
141 utils.write_features(group['parent_field'], group['parent_attribute'], gff3)
48
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
142 prev_end = -1
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
143 for align in group['alignments']:
48
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
144 overlap = ''
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
145 if align['field']['start'] <= prev_end:
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
146 overlap += str(align['field']['start']) + ',' + str(prev_end)
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
147 prev_end = align['field']['end']
6803152ea92a planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 073c8ceda5eddfb4ccd07cfa96a8353e5535ffaf-dirty
yating-l
parents: 45
diff changeset
148 align['attribute']['overlap'] = overlap
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
149 utils.write_features(align['field'], align['attribute'], gff3)
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
150 gff3.close()
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
151
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
152 def blastxml2gff3(xml_file, gff3_file):
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
153 result_handle = open(xml_file)
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
154 blast_records = NCBIXML.parse(result_handle)
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
155 gff3_writer(blast_records, gff3_file)
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
156
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
157 if __name__ == "__main__":
45
15b4a74722d1 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit 73ede51a133dc8914812195a1b4cd512bfb8f23d
yating-l
parents: 31
diff changeset
158 blastxml2gff3("../dbia3/raw/tblastn_dmel-hits-translation-r6.11.fa_vs_nucleotide_BLAST_database_from_data_3.blastxml", "gff3.txt")
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
159