-
Notifications
You must be signed in to change notification settings - Fork 0
/
jitu.py
88 lines (68 loc) · 2.02 KB
/
jitu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sys
import itertools
import re
from Bio.Seq import Seq
from Bio import SeqIO
from collections import defaultdict
import pprint
from itertools import groupby
from dotmap import DotMap
def avg(v):
return 1.0*sum(v)/len(v)
from contextlib import contextmanager
from time import time
from sys import stdout
@contextmanager
def duration(outfile=stdout):
start = time()
yield
end = time()
outfile.write(str(end - start) + '\n')
def strMUT(text, dic):
""" Replaces keys of dic with values of dic in text. 2005-02 by Emanuel Rumpf """
pat = "(%s)" % "|".join(map(re.escape, dic.keys()))
return re.sub(pat, lambda m: dic[m.group()], text)
def getFast(fastaFiler = "93-11_AltReferenceGenome.fasta"):
return SeqIO.to_dict(SeqIO.parse(fastaFiler, "fasta"))
def getEasy(f):
for header, group in itertools.groupby(f, key = lambda x: x.startswith(">") ):
if header:
line = next(group)
tag = line[1:].strip().split()
else:
sequence = ''.join(line.strip() for line in group)
yield tag, sequence.strip()
def getSeqD( fastaFiler = "93-11_AltReferenceGenome.fasta" ):
"""
chinaD = loadWholeSeq("9311_combinedREF.fasta")
"""
seqD = {}
tube =[]
with open(fastaFiler, 'r' ) as inpFast:
for h, seq in getEasy(inpFast):
seqD[tuple(h)]= seq
tube.append(tuple(h))
return tube, seqD
def getTubeD( fastaFiler = "93-11_AltReferenceGenome.fasta" ):
"""
chinaD = loadWholeSeq("9311_combinedREF.fasta")
"""
seqD = {}
tube =[]
with open(fastaFiler, 'r' ) as inpFast:
for h, seq in getEasy(inpFast):
tag = ''.join(h)
seqD[tag] = seq
tube.append(tag)
return tube, seqD
class Viv(dict):
def __missing__(self, key):
val = self[key] = type(self)()
return val
class viv_dotdict(defaultdict):
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, val):
self[key] = val
def dotree(): return viv_dotdict(dotree)
tree = lambda: defaultdict(tree)