Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hub-courses
python_one_week_4_biologists_solutions
Commits
a68ba39d
Verified
Commit
a68ba39d
authored
Mar 11, 2019
by
Bertrand NÉRON
Browse files
use unwinding operator to parse blast output
parent
0b208e19
Changes
1
Hide whitespace changes
Inline
Side-by-side
source/_static/code/parse_blast.py
View file @
a68ba39d
from
operator
import
itemgetter
from
collections
import
namedtuple
Hit
=
namedtuple
(
"Hit"
,
"id percent
identity
align_len
mis_num, open_gap_num
\
query_start, query_end, subject_start, subject_end, E_value, HSP_bit_score"
)
Hit
=
namedtuple
(
"Hit"
,
(
"query"
,
"subject"
,
"
identity
"
,
"
align_len
"
,
"
mis_num
"
,
"
open_gap_num
"
,
"
query_start
"
,
"
query_end
"
,
"
subject_start
"
,
"
subject_end
"
,
"
E_value
"
,
"
HSP_bit_score"
)
)
def
parse_blast_output
(
input_file
):
"""
...
...
@@ -14,13 +14,13 @@ def parse_blast_output(input_file):
with
open
(
input_file
,
'r'
)
as
infile
:
table
=
[]
for
line
in
infile
:
col
=
line
.
split
(
'
\t
'
)
query
,
subject
,
identity
,
*
stuff
,
bit_score
=
line
.
split
(
'
\t
'
)
try
:
col
[
2
]
=
float
(
col
[
2
]
)
identity
=
float
(
identity
)
except
ValueError
as
err
:
raise
RuntimeError
(
"error in parsing {} : {}"
.
format
(
input_file
,
err
))
col
[
-
1
]
=
col
[
-
1
][:
-
1
]
table
.
append
(
col
)
bit_score
=
bit_score
.
strip
()
table
.
append
(
[
query
,
subject
,
identity
,
*
stuff
,
bit_score
]
)
return
table
...
...
@@ -42,10 +42,8 @@ def write_blast_output(hits, output_file):
if
__name__
==
'__main__'
:
table_hits
=
parse_blast_output
(
'blast2.txt'
)
#table_hits = parse_blast_output('blast.txt')
table_sorted
=
sorted
(
table_hits
,
key
=
itemgetter
(
2
),
reverse
=
True
)
table_sorted
=
sorted
(
table_hits
,
key
=
itemgetter
(
2
),
reverse
=
True
)
# alternative
# table_sorted = sorted(table, key = lambda x : x[2], reversed = True)
write_blast_output
(
table_hits
,
'blast_sorted.txt'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment