Last active
December 17, 2015 15:19
-
-
Save ivan-krukov/5630693 to your computer and use it in GitHub Desktop.
fancy consensus builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#http://rosalind.info/problems/cons/ | |
import fastaparse | |
letters = ["A","C","G","T"] | |
sequences = [i.seq for i in fastaparse.sequences("temp2")] | |
#create empty profile matrix | |
profile = [dict(zip(letters,[0 for col in range(0,4)])) for row in range(0,len(sequences[0]))] | |
for col,_ in enumerate(sequences[0]): | |
for i,_ in enumerate(sequences): | |
base = sequences[i][col] | |
profile[col][base] += 1 | |
consensus = "".join([max(col,key=col.get) for col in profile]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from collections import namedtuple | |
def sequences(fasta_file): | |
seq_buffer = [] | |
header = "" | |
Sequence = namedtuple("Sequence",["header","seq"]) | |
for line in open(fasta_file): | |
line = line.strip() | |
if line.startswith(">"): | |
if seq_buffer: | |
yield Sequence(header,"".join(seq_buffer)) | |
seq_buffer = [] | |
header = line; | |
else: | |
seq_buffer.append(line) | |
yield Sequence(header,"".join(seq_buffer)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment