forked from 23andMe/yhaplo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snp.py
301 lines (232 loc) · 9.96 KB
/
snp.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: utf-8 -*-
# David Poznik
# 2015.12.29
# snp.py
#
# Defines two classes:
# - SNP
# - PlatformSNP (a subclass of SNP)
#----------------------------------------------------------------------
from __future__ import absolute_import
import re
import six
import sys
from operator import attrgetter
from six.moves import range
from . import utils
class SNP(object):
'''
A snp knows its:
- names
- haplogroup
- physical info: position, ancestral, derived
'''
tree = None
config = None
args = None
errAndLog = None
def __init__(self, name, haplogroup, position, ancestral, derived):
if SNP.tree is None:
sys.exit('ERROR. Before instantiating, must call SNP.setClassVariables(tree).')
self.setLabel(name)
self.nameList = [name]
self.isRepresentative = name in SNP.tree.representativeSNPnameSet
self.haplogroup = haplogroup
self.position = position
self.ancestral = ancestral
self.derived = derived
self.alleleSet = {ancestral, derived}
self.node = SNP.tree.findOrCreateNode(haplogroup)
self.node.addSNP(self)
def setLabel(self, label):
'sets the label and associated ivars'
self.label = label
self.labelLettersRank, self.labelLetters, self.labelNumber = (
SNP.parseLabel(label, SNP.config.snpLabelLettersRankDict))
self.labelCleaned = SNP.cleanLabel(label)
def __str__(self):
'medium-length string representation'
return '%-15s %-25s %8d %s->%s' % (self.label, self.node.label,
self.position, self.ancestral, self.derived)
def strWithAllNames(self):
'long string representation: normal str plus comma-separated list of names'
return '%s %s' % (str(self), ','.join(self.nameList))
def strShort(self):
'short string representation: node label and snp label'
return '%s:%s' % (self.node.label, self.label)
@property
def DFSrank(self):
'returns depth-first search rank'
return self.node.DFSrank
@property
def hgSNP(self):
'string representation: truncated haplogroup label with SNP label. e.g., R-V88'
return '%s-%s' % (self.node.hgTrunc, self.labelCleaned)
def isDerived(self, geno):
return geno == self.derived
def isAncestral(self, geno):
return geno == self.ancestral
def isOnPlatform(self, platformVersion):
return self.position in PlatformSNP.platformPosSetDict[platformVersion]
def backTracePath(self):
'returns the backtrace path (node list) for the corresponding node'
return self.node.backTracePath()
def addName(self, name):
'adds name to list and updates label if appropriate'
self.nameList.append(name)
if name in SNP.tree.representativeSNPnameSet:
self.isRepresentative = True
if SNP.isApreferredName(self.label):
if SNP.isApreferredName(name):
SNP.errAndLog('WARNING. Two preferred names for one SNP: ' +
'%s, %s\n' % (name, self.label))
elif SNP.isApreferredName(name):
self.setLabel(name)
else:
labelLettersRank, labelLetters, labelNumber = (
SNP.parseLabel(name, SNP.config.snpLabelLettersRankDict))
if (labelLettersRank < self.labelLettersRank
or (labelLetters == self.labelLetters
and labelNumber < self.labelNumber)):
self.setLabel(name)
@staticmethod
def isApreferredName(name):
'''checks wither a SNP name is in the set of preferred names,
with or without an extension (e.g., '.1') if present'''
return (name in SNP.tree.preferredSNPnameSet
or name.split('.')[0] in SNP.tree.preferredSNPnameSet)
@staticmethod
def cleanLabel(label):
'removes superfluous text and hyphens from a SNP label'
for superfluousSNPtext in SNP.config.superfluousSNPtextList:
label = label.replace(superfluousSNPtext, '')
label = six.ensure_text(label)
label = label.replace('-', '_').replace('^', '')
label = six.ensure_text(label.replace(u'≤', '<=').encode('utf-8'))
return label
@staticmethod
def parseLabel(name, snpLabelLettersRankDict):
'''
returns the priority rank of a snp name
and a decomposition of the name into letters and a number
'''
match = re.search(r'([a-zA-Z-]*)([0-9]*)', str(name))
labelLetters, labelNumber = match.group(1), match.group(2)
labelNumber = int(labelNumber) if len(labelNumber) > 0 else 0
if labelLetters in snpLabelLettersRankDict:
labelLettersRank = snpLabelLettersRankDict[labelLetters]
else:
labelLettersRank = len(snpLabelLettersRankDict) # max value
return labelLettersRank, labelLetters, labelNumber
@staticmethod
def setClassVariables(tree):
'enables SNP class to know about the tree instance, config, and args'
SNP.tree = tree
SNP.config = tree.config
SNP.args = tree.args
SNP.errAndLog = tree.config.errAndLog
if SNP.config.runFromAblocks or SNP.args.writePlatformTrees:
PlatformSNP.buildPlatformPosSetDict()
if SNP.config.runFromAblocks:
PlatformSNP.buildPlatformSNPlistDict()
@staticmethod
def prioritySortMarkerList(markerList):
'''
sorts a list of markers by priority ranking, with preference given to
those deemed representative for the corresponding haplogroup
'''
markerList = sorted(markerList,
key=attrgetter('labelLettersRank', 'labelLetters', 'labelNumber'))
markerList = sorted(markerList,
key=attrgetter('isRepresentative'), reverse=True)
return markerList
@staticmethod
def mostHighlyRankedMarkerOnList(markerList):
'''
returns the most highly ranked marker on a list.
the purpose of this method is to record the fact that marker lists are
sorted with highest priority first
'''
if markerList:
return markerList[0]
else:
return None
#--------------------------------------------------------------------------
class PlatformSNP(object):
'A platform SNP knows its: position and ablock index'
platformPosSetDict = dict()
platformSNPlistDict = dict()
def __init__(self, position):
self.position = position
self.ablockIndexList = SNP.config.pos2ablockIndexListDict[position]
def __str__(self):
return '%8d %7d' % (self.position, self.ablockIndex)
def getConsensusGenotypeFromAblock(self, ablock):
'given an ablock, returns a consensus genotype for this SNP'
genotypeList = [PlatformSNP.getGenotypeFromAblock(ablock, ablockIndex)
for ablockIndex in self.ablockIndexList]
if len(genotypeList) == 1:
genotype = genotypeList[0]
else:
genotypeSet = set(genotypeList)
genotypeSet.discard(SNP.config.missingGenotype)
if len(genotypeSet) == 1:
genotype = genotypeSet.pop()
else:
genotype = SNP.config.missingGenotype
return genotype
@staticmethod
def getGenotypeFromAblock(ablock, ablockIndex):
'''
gets genotype from ablock
input: ablock : a numpy array of {0, ..., 15}
ablockIndex
output: genotype
'''
if ablockIndex < len(ablock):
diploidGenotype = SNP.config.ablockCodeToGenotypeDict[ablock[ablockIndex]]
if diploidGenotype in SNP.config.homozygousGenotypeSet:
return diploidGenotype[0]
return SNP.config.missingGenotype
@staticmethod
def buildPlatformPosSetDict():
'reads files to build a dictionary: platformVersion -> set of positions'
SNP.errAndLog('%sReading platform positions...\n\n' % utils.DASHES)
for platformVersion in range(1, SNP.config.maxPlatformVersionPlusOne):
platformPosFN = SNP.config.platformPosFNtp % platformVersion
platformPosSet = utils.readPositionsSet(platformPosFN,
logFunction = SNP.errAndLog)
PlatformSNP.platformPosSetDict[platformVersion] = platformPosSet
SNP.errAndLog('\n')
@staticmethod
def buildPlatformSNPlistDict():
'builds dictionary of platformSNP lists. key = platformVersion'
SNP.errAndLog('Building dictionary of platform SNP lists...\n\n')
for platformVersion in range(1, SNP.config.maxPlatformVersionPlusOne):
platformPosSet = PlatformSNP.platformPosSetDict[platformVersion]
platformSNPlist = list()
for position in platformPosSet:
platformSNPlist.append(PlatformSNP(position))
PlatformSNP.platformSNPlistDict[platformVersion] = platformSNPlist
#--------------------------------------------------------------------------
class DroppedMarker(object):
'''
a marker not used for classification but potentially useful for node labeling
examples: non-SNPs, multiallelic SNPs, and SNPs not meeting ISOGG quality guidelines
'''
def __init__(self, name, haplogroup):
self.name = SNP.cleanLabel(name)
self.haplogroup = haplogroup
def addToNode(self):
'adds this dropped marker to the corresponding node, if it exists'
if self.haplogroup in SNP.tree.hg2nodeDict:
self.setSortVariables()
SNP.tree.hg2nodeDict[self.haplogroup].addDroppedMarker(self)
return True
else:
return False
def setSortVariables(self):
'set variables used for priority sorting'
self.labelLettersRank, self.labelLetters, self.labelNumber = (
SNP.parseLabel(self.name, SNP.config.snpLabelLettersRankDict))
self.isRepresentative = self.name in SNP.tree.representativeSNPnameSet