forked from maximilianh/pubMunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubFindMutations
executable file
·324 lines (272 loc) · 11.4 KB
/
pubFindMutations
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
# load default python packages
import logging, optparse, sys, glob, gzip, gdbm, marshal, zlib, copy, struct, hashlib
from os.path import join, basename, isfile, dirname, abspath, splitext, isdir
from collections import defaultdict
# add <scriptDir>/lib/ to package search path
sys.path.insert(0, join(dirname(abspath(__file__)), "lib"))
import pubGeneric, maxCommon, pubConf, maxbio, pubAlg, maxTables, pslMapBed
import geneFinder, varFinder
import pubStore
#import kent.psl, kent.pslTransMap
#geneData = None
seqCache = None
cacheMode = "c"
geneCache = None
geneInitDone = False
def findGenesCached(pmid, text):
""" return a list of genes, caching the genes in dbm file
"""
# get the sha1 digest of the text string
m = hashlib.sha1()
m.update(text.encode("utf8"))
hashStr = m.digest()
if hashStr in geneCache:
genes, exclPos = marshal.loads(geneCache[hashStr])
return genes, exclPos
logging.warn("No gene info in cache")
global geneInitDone
if not geneInitDone:
geneFinder.initData(exclMarkerTypes=["snp"])
geneInitDone = True
#geneTypes = geneFinder.findMarkersAsDict(text)
global seqCache
assert(seqCache!=None)
genes, exclPos = geneFinder.findGenes(text, pmid, seqCache)
geneCache[hashStr] = marshal.dumps((genes, exclPos))
return genes, exclPos
def writeHeaders(outFh):
prefixHeaders = ["docId", "isConfirmed"]
outFh.write("\t".join(prefixHeaders)+"\t"+"\t".join(varFinder.mutFields)+"\n")
def readFile(inFname):
logging.debug("Reading file %s" % inFname)
text = open(inFname).read()
title = ""
abstract = ""
pmid = splitext(basename(inFname))[0]
return pmid, title, abstract, text
def initCaches(inDir):
" setup the global vars seqCache and geneCache "
global seqCache
global geneCache
scFname = join(inDir, "seqCache.gdbm")
logging.debug("Opening %s" % scFname)
seqCache = gdbm.open(scFname, cacheMode)
gcFname = join(inDir, "geneCache.gdbm")
logging.debug("Opening %s" % gcFname)
geneCache = gdbm.open(gcFname, cacheMode)
def processText(pmid, title, abstract, text, writer, dbAnnots):
text = text.replace("\a", "\n")
genes, exclPos = findGenesCached(pmid, text)
mutations = varFinder.findVariantDescriptions(text, exclPos)
dbAnnots.newDocument(pmid)
for variant, mentions in mutations["dna"]+mutations["prot"]+mutations["intron"]:
groundedMuts, ungroundVar, beds = \
varFinder.groundVariant(pmid, text, variant, mentions, mutations["dbSnp"], genes, False) #set insertion_rv=False
isInDb = dbAnnots.checkCoords(beds)
if isInDb:
for gm in groundedMuts:
gm.inDb = "invitae"
writer.writeMuts(pmid, "confirmed", groundedMuts)
if ungroundVar!=None:
writer.writeMuts(pmid, "notConfirmed", [ungroundVar])
writer.writeBeds(beds)
dbAnnots.writeUnmappedMuts(writer.outFh)
def findInLocalFile(inFname, writer, dbAnnots):
if seqCache==None:
initCaches(dirname(inFname))
pmid, title, abstract, text = readFile(inFname)
processText(pmid, title, abstract, text, writer, dbAnnots)
def findInPubStore(inDir, writer, dbAnnots):
" read text from a directory in pubStore format "
initCaches(inDir)
logging.info("Looking for *.article.gz and *.file.gz files in %s" % inDir)
for art, fileRows in pubStore.iterArticleDirList(inDir, None):
for fileRow in fileRows:
processText(art.pmid, art.title, art.abstract, fileRow.content, writer, dbAnnots)
def findInLocalDir(inDir, writer, dbAnnots):
" reading text from a directory full of *.txt files "
logging.info("Looking for input text files in %s" % inDir)
fnames = glob.glob(join(inDir, "*.txt"))
assert(len(fnames)!=0) # no text files found in input directory
for fname in fnames:
docId = splitext(basename(fname))[0]
if not dbAnnots.isInRef(docId):
logging.info("Ignoring %s" % fname)
continue
logging.info(fname)
findInLocalFile(fname, writer, dbAnnots)
class DbAnnotator(object):
"""collect document/gene/variant tuples and evaluate against a benchmark set """
def __init__(self, fname):
self.docCount = 0
self.mutCount = 0
self.target = None
self.mutHits = 0
self.hgvsCount = 0
self.predDocRefCount = 0
self._parseBench(fname)
self.foundHgvs = set()
self.foundCoords = set()
def isInRef(self, docId):
if not str(docId) in self.target:
return False
else:
return True
def _parseBench(self, fname):
" parse invitae file into dict with pmid -> list of coding HGVS descriptions "
logging.info("Parsing benchmark file %s" % fname)
self.target = defaultdict(set)
self.annots = defaultdict(list)
self.benchCount = 0
if not isfile("invitae.bed"):
return
#for row in maxCommon.iterTsvRows(fname):
#self.target[row.Mapping_reference].append(row.HGVS)
#self.annots[row.HGVS].append("%s (%s)" % (row.Manual_mapping_1, row.Manual_mapping_2))
#self.benchCount += 1
for row in maxCommon.iterTsvRows(fname):
#self.target[row.pmid].append((row.chrom, row.chromStart, row.chromEnd, row.wtNucl, row.mutNucl))
coord = (row.chrom, row.chromStart, row.chromEnd)
self.target[row.pmid].add(coord)
self.annots[coord].append((row.name, "%s" % (row.annotation)))
self.benchCount += 1
def newDocument(self, docId):
if docId in self.target:
self.docCount += 1
self.predDocRefCount += len(self.target[docId])
else:
logging.warn("document %s is not in gold standard" % docId)
self.docId = docId
#def addCheckVariant(self, mut):
# refs = self.target[self.docId]
# self.mutCount += 1
# mutFound = False
# for hgvs in mut.hgvsCoding.split("|"):
# self.hgvsCount += 1
# if hgvs in refs:
# mutFound = True
# self.foundHgvs.add(hgvs)
# break
# if mutFound:
# self.mutHits += 1
# return mutFound
def checkCoords(self, beds):
" check for one mutation, if any of its coords are found in the reference "
self.mutCount += 1
for b in beds:
chrom, start, end = b[:3]
logging.debug("Checking %s:%s-%s" % (chrom, start, end))
#wtNucl, mutNucl
refs = self.target[self.docId]
self.mutCount += 1
mutFound = False
predMut = (chrom, start, end)
if predMut in refs:
logging.debug("Found in reference, total hits:%d" % self.mutHits)
mutFound = True
self.foundCoords.add(predMut)
self.mutHits += 1
return True
def writeUnmappedMuts(self, outFh):
" this is called after a document has been processed to output the missed mutations "
refs = self.target[self.docId]
preds = self.foundCoords
#print refs
#print preds
missed = set(refs).difference(preds)
if len(missed)==0:
outFh.write("%s All mutations found\n" % self.docId)
else:
for m in missed:
chrom, start, end = m
missDescs = []
for hgvs, annots in self.annots[m]:
missDescs.append("%s / %s / %s:%s-%s" % (hgvs, annots, chrom, start, end))
outFh.write("%s Not found %s\n" % (self.docId, ",".join(missDescs)))
#outFh.write("Manual annotations of these: %s" % ",".join(annots))
def printResults(self, outFh):
# total count of all coordinates
allCoords = set()
for docId, coords in self.target.iteritems():
allCoords.update(coords)
coordCount = len(allCoords)
outFh.write("%d total mutations in db file\n" % self.benchCount)
outFh.write("%d documents processed, %d db mutations for these, %d genome positions, %d mutations output, %d hits\n" % \
#(self.docCount, self.predDocRefCount, self.mutCount, self.hgvsCount, self.mutHits))
(self.docCount, self.predDocRefCount, coordCount, self.mutCount, self.mutHits))
class OutputWriter(object):
def __init__(self, outFname, benchFname=None):
self.outFh = maxTables.openFile(outFname, "w")
self.bedFh = maxTables.openFile(outFname+".bed", "w")
writeHeaders(self.outFh)
def writeMuts(self, docId, status, muts):
for mut in muts:
prefixFields=[docId, status]
self.outFh.write("\t".join(prefixFields)+"\t")
row = mut.asRow()
row = [r.replace("\n", " ") for r in row]
row = [r.replace("\t", " ") for r in row]
#row = [r.encode("utf8") for r in row]
row = [x.encode("utf8") for x in row]
self.outFh.write("\t".join(row))
self.outFh.write("\n")
def writeBeds(self, beds):
for bed in beds:
self.bedFh.write("\t".join(bed))
self.bedFh.write("\n")
def close(self):
self.outFh.close()
def runTests():
#geneData = SeqData(mutDataDir, 9606)
#assert( geneData.lookupDbSnp("chr4", 111547605,111547606)=="rs143894132")
#assert( geneData.lookupDbSnp("chr4", 111547606,111547606)==None)
text = open("test.txt").read()
varFinder.loadDb()
variants = varFinder.findVariantMentions(text)
for v in variants:
print v
def main(args, options):
if options.test:
import doctest
doctest.testmod()
runTests()
sys.exit(0)
if options.readOnly:
global cacheMode
cacheMode = "ru"
if options.shuffle:
varFinder.doShuffle = True
pubGeneric.setupLogging("", options)
inFname, outFname = args
writer = OutputWriter(outFname)
varFinder.loadDb()
#dbAnnots = DbAnnotator("mutation_annotations.coding.tab")
dbAnnots = DbAnnotator("invitae.bed")
if isfile(inFname):
findInLocalFile(inFname, writer, dbAnnots)
elif isdir(inFname):
inDir = inFname
artPath = join(inDir, "articles.db")
if isfile(artPath):
findInPubStore(inDir, writer, dbAnnots)
else:
findInLocalDir(inFname, writer, dbAnnots)
else:
assert(False)
logging.info("Wrote output to %s" % outFname)
dbAnnots.printResults(writer.outFh)
writer.close()
# === COMMAND LINE INTERFACE, OPTIONS AND HELP ===
parser = optparse.OptionParser("""usage: %prog [options] inTxtFileOrDir outFileTab - resolve mutations in text file or directory with papers in pubMunch tabular format""")
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="show debug messages")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="show more debug messages")
parser.add_option("-t", "--test", dest="test", action="store_true", help="run tests")
parser.add_option("-r", "--readOnly", dest="readOnly", action="store_true", help="open the caches in read-only mode")
parser.add_option("", "--shuffle", dest="shuffle", action="store_true", help="shuffle the protein sequence before matching")
(options, args) = parser.parse_args()
if args==[] and not options.test:
parser.print_help()
exit(1)
pubGeneric.setupLogging(__file__, options)
main(args, options)