Skip to content

Instantly share code, notes, and snippets.

@soh-i
Created May 4, 2016 04:25
Show Gist options
  • Save soh-i/3f527be2ed1e8273839708e4c9ccc8f0 to your computer and use it in GitHub Desktop.
Save soh-i/3f527be2ed1e8273839708e4c9ccc8f0 to your computer and use it in GitHub Desktop.
Blast parser
class BlastOut(object):
def __init__(self, line):
col = line.strip().split()
self.col_string = line.strip()
self.q = col[0]
self.s = col[1]
self.identity = float(col[2])
self.alignment_len = int(col[3])
self.mismatches = int(col[4])
self.gap_open = int(col[5])
self.q_start = int(col[6])
self.q_end = int(col[7])
self.s_start = int(col[8])
self.s_end = int(col[9])
self.e_value = float(col[10])
self.bit = float(col[11])
self.primary_key = uuid.uuid4().hex
self.q_pair = (self.q_start, self.q_end)
self.s_pair = (self.s_start, self.s_end)
if abs(self.q_start - self.q_end) < abs(self.s_start - self.s_end):
self.pairs = [self.s_pair, self.q_pair]
else:
self.pairs = [self.q_pair, self.s_pair]
def __str__(self):
return "query: %s, subject: %s" % (self.q, self.s)
class BlastIter(object):
def __init__(self, _file):
self.file = _file
def __iter__(self):
with open(self.file, "r") as f:
for line in f:
yield BlastOut(line)
@soh-i
Copy link
Author

soh-i commented May 4, 2016

it can be used as follows:

for b in BlastIter("blast/blast.out"):
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment