-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstringsalign.py
executable file
·226 lines (192 loc) · 7.22 KB
/
mstringsalign.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'''From a file of mstrings, produce output of the same file,
but formatted so that the components of the mstrings are aligned
'''
import re
import itertools as it
import argparse
import verbose as v
import intlist
import mutant
def _getargs():
'''parse options from command line'''
argparser = argparse.ArgumentParser(description=__doc__)
paa = argparser.add_argument
paa("mstringfile",
help="file with mstrings")
paa("--nolabels",action="store_true",
help="only print mstrings, no text to left")
paa("--compact",action="store_true",
help="print a compact version of the aligned mutations")
paa("--output","-o",
help="output file")
paa("--verbose","-v",action="count",default=0,
help="verbose")
args = argparser.parse_args()
return args
def read_label_mstring(filename):
'''
read line of input of form "LABEL [MSTRING]
and yield label,mstring tuple
'''
with open(filename) as fin:
for fullline in fin:
line = fullline.strip()
line = re.sub(r'#.*','',line)
if not line:
continue
label_mstring = re.match(r'(.*)\s*(\[.*\])',line)
if not label_mstring:
v.vprint(f'no match for: {fullline}')
label = label_mstring[1]
mstring = label_mstring[2]
label= label.strip()
yield label,mstring
def get_allsites(mutlist):
'''
return a list of all sites that appear in all mutations
list is sorted, and duplicates are removed
'''
msites = [ssm.site for mut in mutlist for ssm in mut]
msites = sorted(set(msites))
return msites
def get_sub_site_ref(mutlist):
'''
get a dict of ref values for all the sites
associated with substitutions and deletions
'''
return {ssm.site: ssm.ref
for mut in mutlist
for ssm in mut
if ssm.ref != '+'}
def get_ins_site_len(mutlist):
'''
get a dict of maximum string length associated with each insertion site
'''
ins_site_len = dict()
for mut in mutlist:
for ssm in mut:
if ssm.ref != '+':
continue
ins_site_len[ssm.site] = max( [ins_site_len.get(ssm.site,0), len(ssm.mut)] )
return ins_site_len
def get_sssm_bysite(mut):
'''
return dict of ssm's keyed by site: only substitution and deletion ssm's
'''
return {ssm.site: ssm for ssm in mut if ssm.ref != "+"}
def get_issm_bysite(mut):
'''
return dict of ssm's keyed by site: only insertion ssm's
'''
return {ssm.site: ssm for ssm in mut if ssm.ref == "+"}
def emptysite(site):
'''return empty string associated iwth AnnnB for site=nnn'''
return " "*(2+len(str(site)))
class MStringFramework:
'''
framework for dealing with mstring's
'''
def __init__(self,mutlist):
self.allsites = get_allsites(mutlist)
self.ins_site_len = get_ins_site_len(mutlist)
self.sub_site_ref = get_sub_site_ref(mutlist)
def full_sitelist(self):
'''
create list of ints for sites, with extras for insertions
yield list items one at a time
'''
for site in self.allsites:
if site in self.sub_site_ref:
yield site
for _ in range(self.ins_site_len.get(site,0)):
yield site
def format_mstring(self,mut):
'''
format the mstring associated with mutation
by adding extra spaces so that the all the mstrings will line up
'''
sssm_bysite = get_sssm_bysite(mut)
issm_bysite = get_issm_bysite(mut)
## begin with a list of ssm strings
## later, will convert list to a single mstring using join
mstring = []
## build up mstring for every potential site
for site in self.allsites:
if site in self.sub_site_ref:
ssm = sssm_bysite.get(site,None)
ssm_string = str(ssm) if ssm else emptysite(site)
mstring.append( ssm_string )
if site in self.ins_site_len:
## insertion site formatting is a little convoluted
if site in issm_bysite:
sstr = str(issm_bysite[site])
if not issm_bysite[site].mut:
sstr += "x"
## pad with tildes so comma is not removed
sstr += "~" * (self.ins_site_len[site] + 1 + len(str(site)) - len(sstr))
else:
sstr = " " * (self.ins_site_len[site] + 1 + len(str(site)))
mstring.append(sstr)
## joint ssm's in mstring list into a single mstring
mstring = "[" + ",".join(mstring) + "]"
## remove extraneous commas
mstring = re.sub(" ,"," ",mstring)
## remove tilde characters
mstring = re.sub(r'([~]+),',r',\1',mstring)
mstring = re.sub("~"," ",mstring)
return mstring
def compact_format_mstring(self,mut=None):
'''
return compactly-formatted mstring for input mutation
'''
mut = [] if mut is None else mut
sssm_bysite = get_sssm_bysite(mut)
issm_bysite = get_issm_bysite(mut)
## begin with a list of ssm strings
## later, will convert list to a single mstring using join
mstring = []
for site in self.allsites:
if site in self.sub_site_ref:
if mut:
ssm = sssm_bysite.get(site,None)
ssm_char = ssm.mut if ssm else "."
else:
ssm_char = self.sub_site_ref[site]
mstring.append(ssm_char)
if site in self.ins_site_len:
if mut:
ssm = issm_bysite.get(site,None)
ssm_str = ssm.mut if ssm else ""
else:
ssm_str = ""
ssm_str += "~"*(self.ins_site_len[site] - len(ssm_str))
mstring.append(ssm_str)
return "".join(mstring)
def _main(args):
'''main'''
v.vprint(args)
label_mstring_list = list(read_label_mstring(args.mstringfile))
labels = [label for label,_ in label_mstring_list]
mstrings = [mstring for _,mstring in label_mstring_list]
mutants = [mutant.Mutation(mstring) for _,mstring in label_mstring_list]
maxlabellen = max(len(label) for label in labels)
fmtlabel = "%%%ds " % maxlabellen
for label,mstring in zip(labels,mstrings):
v.print((fmtlabel + "%s") % (label,mstring))
frame = MStringFramework(mutants)
for label,mut in zip(labels,mutants):
labeltext = fmtlabel % label if not args.nolabels else ""
mstring = frame.format_mstring(mut)
print(labeltext + mstring)
allsites = list( frame.full_sitelist() )
for headerstring in intlist.write_numbers_vertically(allsites):
labeltext = fmtlabel % "" if not args.nolabels else ""
print(labeltext + headerstring)
for label,mut in it.chain( [("",None)], zip(labels,mutants) ):
labeltext = fmtlabel % label if not args.nolabels else ""
mstring = frame.compact_format_mstring(mut)
print(labeltext + mstring)
if __name__ == "__main__":
_args = _getargs()
v.verbosity(_args.verbose)
_main(_args)