-
Notifications
You must be signed in to change notification settings - Fork 21
/
pubParseDb
executable file
·1024 lines (905 loc) · 38.2 KB
/
pubParseDb
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# load default python packages
import logging, optparse, sys, glob, gzip, collections, copy, gzip, os, doctest, re
from os.path import *
from collections import defaultdict
#try:
#from lxml import etree
#except ImportError:
## Python 2.5
#print "py 2.5 fallback etree"
import xml.etree.cElementTree as etree
# doesn't work anymore, we need the partial parsing option
# add <scriptDir>/lib/ to package search path
sys.path.insert(0, join(dirname(abspath(__file__)), "lib"))
import pubGeneric, maxCommon, pubConf, maxbio
# example file /hive/data/outside/pdb/aa/pdb1aa0.ent.gz
pdbHeaders = ["acc", "isMain", "authors", "title", "ref", "issn", "pmid", "doi"]
pdbToHeader = {'AUTH' : "authors", "TITL" : "title", "REFN": "issn", "PMID":"pmid", "DOI":"doi", "REF":"ref", "isMain":"isMain", "acc": "acc"}
PdbRefRec = collections.namedtuple("pdbRef", pdbHeaders)
def parsePdbRefLine(data, line):
" add line to data dict "
keyword, entry = line[12:16].strip(), line[19:].strip()
if keyword in data:
data[keyword] = data[keyword]+" "+entry
else:
data[keyword] = entry
def parsePdb(pdbDir, outDir):
" write pdb.tab to outDir, parsing an ftp mirror from PDB "
# get list of infnames
if isdir(pdbDir):
logging.info("Scanning for input files in %s" % pdbDir)
inDirs = [d for d in glob.glob(pdbDir+"/*") if isdir(d)]
inFnames = []
for inDir in inDirs:
dirFnames = glob.glob(inDir+"/*.ent.gz")
inFnames.extend(dirFnames)
logging.info("Found %d input files under %s" % (len(inFnames), pdbDir))
elif isfile(pdbDir):
inFnames = [pdbDir]
else:
raise Exception("pdbDir %s does not exist" % pdbDir)
# write headers and open outfile
outFname = join(outDir, "pdb.tab")
logging.info("Writing to %s" % outFname)
ofh = open(outFname, "w")
ofh.write("\t".join(pdbHeaders))
ofh.write("\n")
tp = maxCommon.ProgressMeter(len(inFnames))
for inFname in inFnames:
logging.debug("Parsing %s" % inFname)
ifh = gzip.open(inFname)
refs = []
refData = {}
for line in ifh:
if line.startswith("HEADER "):
acc = line.split()[-1]
if line.startswith("JRNL"):
refData["isMain"]="1"
refData["acc"] = acc
parsePdbRefLine(refData, line)
elif line.startswith("REMARK 1 "):
if line[11:].startswith("REFERENCE"):
refs.append(refData)
refData = {}
refData["isMain"] = "0"
refData["acc"] = acc
continue
parsePdbRefLine(refData, line)
refs.append(refData)
# translate keys from PDB to our own ones and write to outfile
newRefs = []
for ref in refs:
if '' in ref:
del ref['']
if 'EDIT' in ref:
del ref['EDIT']
if 'PUBL' in ref:
del ref['PUBL']
if 'REFE' in ref: # looks like a typo in /hive/data/outside/pdb/o9/pdb1o91.ent.gz
logging.warn("REFE typo ignored")
del ref['REFE']
newRef = {}
for k, v, in ref.iteritems():
newRef[pdbToHeader[k]] = v
for h in pdbHeaders:
if not h in newRef:
newRef[h] = ""
newRef["issn"] = newRef["issn"].replace("ISSN ","")
row = PdbRefRec(**newRef)
ofh.write("\t".join(row))
ofh.write("\n")
tp.taskCompleted()
# UNIPROT PARSING
# remove these PMIDs from all evidences
pmidBlackList = set([17344846]) # high-throughput study
# only parse these feature types
featTypes = {
"sequence variant": "variant",
"mutagenesis site": "mutagen",
"modified residue": "modif",
"cross-link": "cross-link",
"region of interest": "interest",
"short sequence motif": "motif",
"metal ion-binding site": "ion-binding",
"site": "site",
"topological domain" : "topo",
"transmembrane region" : "transmem",
"disulfide bond" : "disulf bond",
"glycosylation site" : "glyco",
"binding site" : "bind",
"active site" : "enzyme act site",
"signal peptide" : "signal pep",
"transit peptide" : "trans pep",
"calcium-binding region" : "calcium bind",
"lipid moiety-binding region" : "lipid",
"propeptide" : "propep",
"intramembrane region" : "intramem",
"peptide" : "peptide",
"nucleotide phosphate-binding region" : "nucl phos bind",
"helix" : "helix",
"chain" : "chain",
"coiled-coil region" : "coiled-coil",
"turn" : "turn",
"strand" : "beta",
"domain" : "domain",
"zinc finger region" : "zinc finger",
"repeat" : "repeat",
"compositionally biased region" : "biased",
"initiator methionine" : "init Met",
"non-standard amino acid" : "non-std",
"non-consecutive residues" : "non-consec",
"unsure residue" : "unsure",
"DNA-binding region" : "DNA-binding",
"non-terminal residue" : "nonTerm"
}
# main record info
entryHeaders = ["dataset", "acc", "mainIsoAcc", "orgName", "orgCommon", "taxonId", "name", "accList", \
"protFullNames", "protShortNames", "protAltFullNames", "protAltShortNames", \
"geneName", "geneSynonyms", "isoNames", \
"geneOrdLocus", "geneOrf", \
"hgncSym", "hgncId", "refSeq", "refSeqProt", "entrezGene", "ensemblGene", "ensemblProt", \
"kegg", "emblMrna", "emblMrnaProt", "emblDna", "emblDnaProt", \
"pdb", "ec", \
"uniGene", "omimGene", "omimPhenotype", "subCellLoc", "functionText", "mainSeq", "isoIds", "isoSeqs"]
EntryRec = collections.namedtuple("uprec", entryHeaders)
# disease associated mutation
mutHeaders = ["acc", "mainIsoAcc", "varId", "featType", "shortFeatType", "begin", "end", "origAa", "mutAa", "dbSnpId", "disRelated", "disease", "disCode", "pmid", "comment"]
MutRec = collections.namedtuple("mutrec", mutHeaders)
# references from record
refHeaders = ["name", "citType", "year", "journal", "vol", "page", \
"title", "authors", "doi", "pmid", "scopeList"]
RefRec = collections.namedtuple("refRec", refHeaders)
emptyRef = dict(zip(refHeaders, len(refHeaders)*[""]))
def strip_namespace_inplace(etree, namespace=None,remove_from_attr=True):
""" Takes a parsed ET structure and does an in-place removal of all namespaces,
or removes a specific namespacem (by its URL).
Can make node searches simpler in structures with unpredictable namespaces
and in content given to be non-mixed.
By default does so for node names as well as attribute names.
(doesn't remove the namespace definitions, but apparently
ElementTree serialization omits any that are unused)
Note that for attributes that are unique only because of namespace,
this may attributes to be overwritten.
For example: <e p:at="bar" at="quu"> would become: <e at="bar">
I don't think I've seen any XML where this matters, though.
"""
if namespace==None: # all namespaces
for elem in etree.getiterator():
tagname = elem.tag
if not isinstance(elem.tag, basestring):
continue
if tagname[0]=='{':
elem.tag = tagname[ tagname.index('}',1)+1:]
if remove_from_attr:
to_delete=[]
to_set={}
for attr_name in elem.attrib:
if attr_name[0]=='{':
old_val = elem.attrib[attr_name]
to_delete.append(attr_name)
attr_name = attr_name[attr_name.index('}',1)+1:]
to_set[attr_name] = old_val
for key in to_delete:
elem.attrib.pop(key)
elem.attrib.update(to_set)
else: # asked to remove specific namespace.
ns = '{%s}' % namespace
nsl = len(ns)
for elem in etree.getiterator():
if elem.tag.startswith(ns):
elem.tag = elem.tag[nsl:]
if remove_from_attr:
to_delete=[]
to_set={}
for attr_name in elem.attrib:
if attr_name.startswith(ns):
old_val = elem.attrib[attr_name]
to_delete.append(attr_name)
attr_name = attr_name[nsl:]
to_set[attr_name] = old_val
for key in to_delete:
elem.attrib.pop(key)
elem.attrib.update(to_set)
def parseDiseases(fname):
" parse the file humanDiseases.txt from uniprot to resolve disease IDs to disease names "
dis = {}
for line in open(fname).read().splitlines():
if line.startswith("ID"):
name = line[5:].strip(".")
if line.startswith("AR"):
code = line[5:].strip(".")
dis[code]=name
return dis
def findSaveList(el, path, dataDict, key, attribKey=None, attribVal=None, useAttrib=None, subSubEl=None):
""" find all text of subelemets matching path with given optionally attrib and save into dataDict with key
You can specify a subSubEl of the element to get the text from.
"""
l = []
for se in el.findall(path):
if attribKey!=None and se.attrib.get(attribKey, None)!=attribVal:
continue
if useAttrib:
val = se.attrib[useAttrib]
else:
if subSubEl:
val = se.find(subSubEl).text
else:
val = se.text
l.append(val)
s = "|".join(l)
dataDict[key] = s
def openOutTabFile(subDir, outName, headers):
" create outdir and open outfile, write headers "
#subDir = join(outDir, outSubDir)
if not isdir(subDir):
logging.info("Creating dir %s" % subDir)
os.makedirs(subDir)
outPath = join(subDir, outName)
logging.info("Writing output to %s" % outPath)
ofh = open(outPath, "w")
ofh.write("\t".join(headers)+"\n")
return ofh
def findDisCodes(text, disToName):
""" find disease codes in text, return as a set of disease codes
>>> findDiseases("Defects in HAL are the cause of histidinemia (HISTID) ")
set(['HISTID'])
"""
disSet = set()
for m in re.finditer("[(]([a-zA-Z0-9- ]+)[)]", text):
word = m.group(1)
if word in disToName:
disSet.add(word)
return disSet
#def findDiseases(text):
# """ find disease codes and their names in text, return as dict code -> name
# >>> findDiseases("Defects in CEACAM16 are the cause of deafness autosomal dominant type 4B (DFNA4B) [MIM:614614].")
# {'DFNA4B': 'deafness autosomal dominant type 4B'}
# >>> findDiseases("Defects in ALX4 are the cause of parietal foramina 2 (PFM2) [MIM:609597]; also known as foramina parietalia permagna (FPP). PFM2 is an autosomal dominant disease characterized by oval defects of the parietal bones caused by deficient ossification around the parietal notch, which is normally obliterated during the fifth fetal month. PFM2 is also a clinical feature of Potocki-Shaffer syndrome.")
# {'PFM2': 'parietal foramina 2', 'FPP': 'foramina parietalia permagna'}
#
# # disease is only one word, but long enough
# >>> findDiseases("Defects in HAL are the cause of histidinemia (HISTID) ")
# {'HISTID': 'histidinemia'}
# """
# result = {}
# phrases = re.split("[;.] ", text)
# notDisease = set(["of", "with", "to", "as", "or", "also", "in"])
#
# for phrase in phrases:
# words = phrase.split()
# revWords = reversed(words)
#
# grabWords = False
# disWords = []
# disCode = None
# # go backwords over words and look for acronym, then grab all words before that
# # until we find a common English word
# for word in revWords:
# m = re.match("[(]([A-Z0-9-]+)[)]", word)
# if m!=None:
# disCode = m.group(1)
# grabWords = True
# continue
#
# if word in notDisease and (len(disWords)>1 or len("".join(disWords))>=9):
# disName = " ".join(list(reversed(disWords)))
# if disCode==None:
# logging.debug("Found disease %s, but no code for it" % disName)
# continue
# result[disCode] = disName
# disCode = None
# disWords = []
# grabWords = False
#
# if grabWords:
# disWords.append(word)
#
# return result
#def parseDiseaseComment(entryEl):
# """ return two dicts
# one with evidence code -> disease code
# one with disease code -> disease name
# """
# disRefs = {}
# disCodes = {}
# for commentEl in entryEl.findall("comment"):
# textEl = commentEl.find("text")
# if commentEl.attrib["type"]=="disease":
# refStr = commentEl.attrib.get("evidence", None)
# # website xml is different, has evidence attribute on text element
# if refStr==None:
# refStr = textEl.attrib.get("evidence", None)
# if refStr==None:
# continue
#
# refs = refStr.split(" ")
#
# text = textEl.text
# logging.debug("Disease comment: %s, evidence %s" % (text, refStr))
# disCodes.update(findDiseases(text))
#
# for refId in refs:
# disRefs[refId] = disCodes
#
# logging.debug("Found disease evidences: %s" % disRefs)
# logging.debug("Found disease names: %s" % disCodes)
# return disRefs, disCodes
def parseDiseaseComment(entryEl, disToName):
"""
parse the general comments, disease section from up record
return evidence codes that refer to diseases
also return disease codes
"""
disRefs = {}
disCodes = set()
for commentEl in entryEl.findall("comment"):
textEl = commentEl.find("text")
if commentEl.attrib["type"]=="disease":
refStr = commentEl.attrib.get("evidence", None)
# website xml is different, has evidence attribute on text element
if refStr==None:
refStr = textEl.attrib.get("evidence", None)
if refStr==None:
continue
refs = refStr.split(" ")
text = textEl.text
logging.debug("Disease comment: %s, evidence %s" % (text, refStr))
disCodes.update(findDisCodes(text, disToName))
for refId in refs:
disRefs[refId] = disCodes
logging.debug("Found disease evidences: %s" % disRefs)
#logging.debug("Found disease names: %s" % disCodes)
return disRefs, disCodes
def parseIsoforms(acc, mainSeq, entryEl, isoSeqs):
" parse sequences of isoforms, returns lists: isoIds, isoNames, seqs "
isoDefined = False
isoIds = []
isoNames = []
seqs = []
for isoEl in entryEl.findall("comment/isoform"):
isoDefined = True
# get id
idEl = isoEl.find("id")
isoId = idEl.text
# get names (just as gene synonyms)
for nameEl in isoEl.find("name"):
isoNames.append(nameEl.text)
seqEl = isoEl.find("sequence")
# get sequences
seqType = seqEl.attrib["type"]
if seqType=="displayed":
seqs.append(mainSeq)
isoIds.append(isoId)
elif seqType=="external":
pass # weird anyways
else:
if isoId not in isoSeqs:
logging.debug("sequence %s does not exist" % isoId)
else:
seqs.append(isoSeqs[isoId])
isoIds.append(isoId)
if not isoDefined:
isoIds = [acc]
seqs = [mainSeq]
assert(len(seqs)==len(isoIds))
return isoIds, isoNames, seqs
def parseDbRefs(entryEl):
" return dict with db -> id (various special cases) "
dbRefs = defaultdict(set)
dbRefs["emblMrna"] = []
dbRefs["emblMrnaProt"] = []
dbRefs["emblDna"] = []
dbRefs["emblDnaProt"] = []
for dbRefEl in entryEl.findall("dbReference"):
db = dbRefEl.attrib["type"]
mainId = dbRefEl.attrib["id"]
if db=="EMBL": # special case, don't add yet
emblId = mainId
else:
dbRefs[db].add(mainId)
propEls = dbRefEl.findall("property")
emblProtId = "na"
id = None
for propEl in propEls:
propType = propEl.attrib["type"]
propDb = db
if (db, propType) ==("RefSeq", "nucleotide sequence ID"):
id = propEl.attrib["value"]
propDb = "refseqNucl"
elif db=="HGNC" and propType=="gene designation":
id = propEl.attrib["value"]
propDb = "hgncGene"
elif db=="Ensembl" and propType=="gene ID":
id = propEl.attrib["value"]
propDb = "ensemblGene"
elif db=="Ensembl" and propType=="protein sequence ID":
id = propEl.attrib["value"]
propDb = "ensemblProt"
elif db=="EMBL" and propType=="protein sequence ID":
emblProtId = propEl.attrib["value"]
continue # don't add yet
elif db=="MIM" and propType=="type":
omimCat = propEl.attrib["value"]
if omimCat=="phenotype":
dbRefs["omimPhenotype"].add(mainId)
elif omimCat=="gene" or omimCat=="gene+phenotype":
dbRefs["omimGene"].add(mainId)
else:
assert(False)
elif db=="EMBL" and propType=="molecule type":
val = propEl.attrib["value"]
if val=="mRNA":
# add now
dbRefs["emblMrna"].append(emblId)
dbRefs["emblMrnaProt"].append(emblProtId)
else:
dbRefs["emblDna"].append(emblId)
dbRefs["emblDnaProt"].append(emblProtId)
continue # don't add any id
else:
id = dbRefEl.attrib["id"]
if id!=None:
dbRefs[propDb].add(id)
result = {}
for db, valList in dbRefs.iteritems():
result[db] = "|".join(valList)
logging.debug("dbRefs: %s" % result)
return result
def splitAndResolve(disName, disCodes, splitWord):
" split and split word, try to resolve via disCodes and rejoin again "
subDises = disName.split(splitWord)
newDises = []
for subDis in subDises:
subDis = subDis.strip()
if subDis in disCodes:
newDises.append(disCodes[subDis])
else:
newDises.append(subDis)
disName = ",".join(newDises)
return disName
def parseFeatDesc(text, disToName):
"""
parse the description of a feature to find code name of disease, snpId and comments
return tuple: (disease name, dbSnpId, otherComments)
>>> parseFeatDesc("In sporadic cancers; somatic mutation; dbSNP:rs11540654.", {})
('sporadic cancers', 'rs11540654', 'somatic mutation')
>>> parseFeatDesc("In RIEG1; pointless comment", {"RIEG1" : "Axel-Riegerfeldt syndrome"})
('Axel-Riegerfeldt syndrome', '', 'pointless comment')
"""
# find disease name and try to resolve via disToNames
logging.debug("Feature description: %s " % (text))
text = text.strip(".").strip()
parts = text.split("; ")
disCode = ""
comments = []
for part in parts:
part = part.replace("a patient with", "")
part = part.replace("in a ", "in ")
partLow = part.lower()
if partLow.startswith("in ") and "dbSNP" not in part and "allele" not in part:
disCode = " ".join(part.split()[1:])
# some entries contain two disease names
else:
if "dbSNP" not in part:
comments.append(part)
# we got a plain disease code
if disCode in disToName:
disLongName = disToName[disCode]
# or two dis codes with and
elif " and " in disCode:
disLongName = splitAndResolve(disCode, disToName, " and ")
else:
# there are dis code somewhere inside the text
intDisCodes = findDisCodes(disCode, disToName)
if len(intDisCodes)!=0:
disLongName = disCode
disCode = ",".join(intDisCodes)
# ok nothing worked, keep it as it is
else:
disLongName = disCode
# find snpId
snpId = ""
for m in re.finditer("dbSNP:(rs[0-9]+)", text):
if m!=None:
#assert(snpId=="")
snpId = m.group(1)
logging.debug("Disease: %s, snpId: %s" % (disLongName, snpId))
return disCode, disLongName, snpId, "; ".join(comments)
ignoredTypes = collections.Counter()
def parseFeatures(entryEl, disRefs, defaultDisCodes, disToName, evidPmids, mainIsoAcc):
" go over features and yield mutation records "
acc = entryEl.find("accession").text
mutations = []
for featEl in entryEl.findall("feature"):
featType = featEl.attrib["type"]
if featType not in featTypes:
ignoredTypes[featType] += 1
continue
if featType in ["sequence variant"]:
isVariant = True
else:
isVariant = False
shortFeatType = featTypes[featType]
logging.debug("type: %s" % featType)
varId = featEl.attrib.get("id", "")
logging.debug("Variant ID %s" % varId)
origEl = featEl.find("original")
if origEl==None:
#logging.debug("No original residue")
#continue
orig = ""
else:
orig = origEl.text
varEl = featEl.find("variation")
if varEl==None:
variant = ""
else:
variant = varEl.text
logging.debug("residue change: %s->%s" % (orig, variant))
posEl = featEl.find("location/position")
if posEl!=None:
begin = posEl.attrib["position"]
end = str(int(begin)+1)
else:
beginEl = featEl.find("location/begin")
begin = beginEl.attrib.get("position", None)
if begin==None:
logging.debug("Unknown start, skipping a feature")
continue
endEl = featEl.find("location/end")
end = endEl.attrib.get("position", None)
if end==None:
logging.debug("Unknown end, skipping a feature")
continue
end = str(int(end)+1)
desc = featEl.attrib.get("description", None)
if desc==None:
#logging.debug("No description")
#continue
desc = ""
if "sulfinic" in desc:
shortFeatType = "sulfo"
descWords = desc.split()
if len(descWords)>0:
desc1 = descWords[0].lower()
if "phos" in desc1:
shortFeatType = "phos"
elif "acetyl" in desc1:
shortFeatType = "acetyl"
elif "methyl" in desc1:
shortFeatType = "methyl"
elif "lipo" in desc1:
shortFeatType = "lipo"
elif "hydroxy" in desc1:
shortFeatType = "hydroxy"
elif "nitro" in desc1:
shortFeatType = "nitro"
evidStr = featEl.attrib.get("evidence", "")
logging.debug("variant pos %s-%s, desc %s, evidence %s" % (begin, end, desc, evidStr))
desc = desc.strip("() ")
#if desc=="":
#logging.debug("No description")
#continue
#if evidStr==None:
#logging.debug("No evidence")
#continue
evidList = evidStr.split()
if isVariant:
# only do this for natural variants
disCode, disName, snpId, comments = parseFeatDesc(desc, disToName)
# if no disease annotated to feature, use the one from the record
if disCode=="" and len(defaultDisCodes)==1:
disCode = list(defaultDisCodes)[0]
disName = disToName.get(disCode, disCode)+" (not annotated on variant but on gene record)"
disCode = disCode + "?"
else:
disCode, disName, snpId, comments = "", "", "", desc
varPmids = []
if disCode!="":
diseaseRelated = "disRelated"
else:
diseaseRelated = "noEvidence"
for evidId in evidList:
if evidId in disRefs:
diseaseRelated="disRelated"
else:
diseaseRelated="notDisRelated"
logging.debug("evidence is not a disease evidence or blacklisted, check description")
pmids = evidPmids.get(evidId, [])
assert(len(pmids)<=1)
if len(pmids)>0:
pmid = list(pmids)[0]
varPmids.append(pmid)
if len(varPmids)==1 and set(varPmids).intersection(pmidBlackList)==len(varPmids):
logging.debug("only blacklist pmids, skipping feature")
mut = MutRec(acc, mainIsoAcc, varId, featType, shortFeatType, begin, end, orig, variant, snpId, diseaseRelated, disName, disCode, ",".join(varPmids), comments)
logging.debug("Accepted variant: %s" % str(mut))
# rewrite disulfide bonds to two separate features, one for each cysteine involved
if featType=="disulfide bond":
end = mut.end
comment = mut.comment
if comment!="":
comment+= "; "
newComment = comment + "disulfide bond to position %s" % str(int(mut.end)-1)
mut1 = mut._replace(end=str(int(mut.begin)+1), comment=newComment)
yield mut1
newComment = comment + "disulfide bond to position %s" % mut.begin
mut2 = mut._replace(begin=str(int(mut.end)-1), comment=newComment)
yield mut2
else:
yield mut
def parseEvidence(entryEl):
" return a dict with evidCode -> PMID "
result = {}
for evidEl in entryEl.findall("evidence"):
evidCode = evidEl.attrib["key"]
for dbRefEl in evidEl.findall("source/dbReference"):
dbType = dbRefEl.attrib["type"]
if dbType=="PubMed":
pmid = dbRefEl.attrib["id"]
#if pmid in pmidBlackList:
#continue
result.setdefault(evidCode, [])
result[evidCode].append(pmid)
return result
def parseVariants(entryEl, mainIsoAcc, disToName):
" return MutRecs with disease associated variants "
# parse the general record comment about diseases
disRefs, allDiseaseCodes = parseDiseaseComment(entryEl, disToName)
#if len(disRefs)==0:
#logging.debug("No disease evidence")
#return []
acc = entryEl.find("accession").text
logging.debug("Diseases in %s" % acc)
evidPmids = parseEvidence(entryEl)
mutRecs = list(parseFeatures(entryEl, disRefs, allDiseaseCodes, disToName, evidPmids, mainIsoAcc))
return mutRecs
def parseRecInfo(entryEl, entry, isoSeqs):
"""parse uniprot general record info into entry dict
use isoform sequences from isoSeqs
only process certain taxonIds
"""
dataset = entryEl.attrib["dataset"]
entry["dataset"] = dataset
findSaveList(entryEl, "name", entry, "name")
findSaveList(entryEl, "accession", entry, "accList")
entry["acc"] = entry["accList"].split("|")[0]
logging.debug("Parsing rec info for acc %s" % entry["acc"])
findSaveList(entryEl, "protein/recommendedName/fullName", entry, "protFullNames")
findSaveList(entryEl, "protein/recommendedName/shortName", entry, "protShortNames")
findSaveList(entryEl, "protein/alternativeName/fullName", entry, "protAltFullNames")
findSaveList(entryEl, "protein/alternativeName/shortName", entry, "protAltShortNames")
findSaveList(entryEl, "gene/name", entry, "geneName", attribKey="type", attribVal="primary")
findSaveList(entryEl, "gene/name", entry, "geneSynonyms", attribKey="type", attribVal="synonym")
findSaveList(entryEl, "gene/name", entry, "geneOrdLocus", attribKey="type", attribVal="ordered locus")
findSaveList(entryEl, "gene/name", entry, "geneOrf", attribKey="type", attribVal="ORF")
findSaveList(entryEl, "organism/name", entry, "orgName", attribKey="type", attribVal="scientific")
findSaveList(entryEl, "organism/name", entry, "orgCommon", attribKey="type", attribVal="common")
findSaveList(entryEl, "organism/dbReference", entry, "taxonId", useAttrib="id")
findSaveList(entryEl, "comment/isoform/id", entry, "isoIds")
findSaveList(entryEl, "comment/isoform/name", entry, "isoNames")
findSaveList(entryEl, "comment/subcellularLocation/location", entry, "subCellLoc")
findSaveList(entryEl, "comment", entry, "functionText", attribKey="type", attribVal="function", subSubEl="text")
mainSeq = entryEl.find("sequence").text
entry["mainSeq"] = mainSeq
isoIds, isoNames, seqs = parseIsoforms(entry["acc"], mainSeq, entryEl, isoSeqs)
dbRefs = parseDbRefs(entryEl)
entry["mainIsoAcc"] = isoIds[0]
entry["hgncSym"] = dbRefs.get("hgncGene", "")
entry["hgncId"] = dbRefs.get("HGNC", "")
entry["refSeq"] = dbRefs.get("refseqNucl", "")
entry["refSeqProt"] = dbRefs.get("RefSeq", "")
entry["ensemblProt"] = dbRefs.get("ensemblProt", "")
entry["ensemblGene"] = dbRefs.get("ensemblGene", "")
entry["entrezGene"] = dbRefs.get("GeneID", "")
entry["kegg"] = dbRefs.get("KEGG", "")
entry["uniGene"] = dbRefs.get("UniGene", "")
entry["omimGene"] = dbRefs.get("omimGene", "")
entry["omimPhenotype"] = dbRefs.get("omimPhenotype", "")
entry["emblMrna"] = dbRefs.get("emblMrna", "") # mrnas
entry["emblMrnaProt"] = dbRefs.get("emblMrnaProt", "") # the protein accessions for mrnas
entry["emblDna"] = dbRefs.get("EmblDna", "") # anything not an mrna
entry["emblDnaProt"] = dbRefs.get("EmblDnaProt", "") # protein accessions for non-mrnas
entry["pdb"] = dbRefs.get("PDB", "")
entry["ec"] = dbRefs.get("EC", "")
entry["isoIds"]="|".join(isoIds)
entry["isoSeqs"]="|".join(seqs)
entry["isoNames"]="|".join(isoNames)
entryRow = EntryRec(**entry)
return entryRow
def parseRefInfo(entryEl, recName):
for refEl in entryEl.findall("reference"):
ref = copy.copy(emptyRef)
ref["name"] = recName
citEl = refEl.find("citation")
ref["citType"] = citEl.attrib["type"]
year = citEl.attrib.get("date", "")
ref["year"] = year.split("-")[0]
ref["journal"] = citEl.attrib.get("name", "")
if ref["journal"]=="":
ref["journal"] = citEl.attrib.get("db", "") # for submissions
ref["vol"] = citEl.attrib.get("volume", "")
ref["page"] = citEl.attrib.get("first", "")
for titleEl in citEl.findall("title"):
ref["title"] = titleEl.text
authorList = []
for personEl in citEl.findall("authorList/person"):
if "name" in personEl.attrib:
name = personEl.attrib["name"]
name = name.replace(" ", ",", 1)
authorList.append(name)
ref["authors"]=";".join(authorList)
for dbRefEl in citEl.findall("dbReference"):
if "type" in dbRefEl.attrib:
if dbRefEl.attrib["type"]=="DOI":
ref["doi"] = dbRefEl.attrib["id"]
if dbRefEl.attrib["type"]=="PubMed":
ref["pmid"] = dbRefEl.attrib["id"]
findSaveList(refEl, "scope", ref, "scopeList")
refRow = RefRec(**ref)
yield refRow
def readIsoforms(inDir):
" return all isoform sequences as dict isoName (eg. P48347-2) -> sequence "
isoFname = join(inDir, "uniprot_sprot_varsplic.fasta.gz")
isoFile = gzip.open(isoFname)
logging.info("reading isoform sequences from %s" % isoFname)
isoSeqs = maxbio.parseFastaAsDict(isoFile)
result = {}
seqNames = []
for id, seq in isoSeqs.iteritems():
idParts = id.split("|")
isoName = idParts[1]
result[isoName] = seq
seqNames.append(idParts[2].split()[0])
logging.info("Found %d isoform sequences" % len(result))
return result, len(set(seqNames))
def writeFaSeqs(entry, faFiles, allVariants=False):
""" write main sequence to faFile with the right taxonId
base sequence always has accession as ID
"""
#seqIds = entry.isoIds.split("|")
if allVariants:
seqIds = entry.isoIds.split("|")
seqs = entry.isoSeqs.split("|")
else:
seqIds = [entry.acc]
seqs = [entry.isoSeqs.split("|")[0]]
taxonId = entry.taxonId
if "all" in faFiles:
ofh = faFiles["all"]
else:
ofh = faFiles[taxonId]
#for seqId, seq in zip(seqIds, seqs):
#ofh.write(">%s\n%s\n" % (seqId, seq))
c = 0
for seqId, seq in zip(seqIds, seqs):
# this was to make sure that the first variant has the uniprot ID
# sounds like not such a good idea but maybe necessary for the
# uniprot lifter file?
#if c==0 and allVariants:
#seqId = entry.acc
ofh.write(">%s\n%s\n" % (seqId.strip(), seq.strip()))
c+=1
def openFaFiles(taxonIds, outDir, outPrefix, seqType="base"):
faFiles = {}
if taxonIds == None:
taxonIds = ["all"]
for taxonId in taxonIds:
taxonId = str(taxonId)
seqQual = ""
if seqType!="base":
seqQual = "."+seqType
faFname = join(outDir, outPrefix+"."+taxonId+seqQual+".fa.gz")
faFiles[taxonId] = gzip.open(faFname, "w")
logging.info("Writing fasta seqs for taxon %s to %s (seqType: %s)" % (taxonId, faFname, seqType))
return faFiles
def parseUniprot(db, inDir, outDir, taxonIds):
" parse uniprot, write records and refs to outdir "
if options.parse:
fname = options.parse
logging.info("Debug parse of %s" % fname)
xmlFile = open(fname)
isoSeqs, recCount = {}, 1
outDir = "."
outPrefix = "temp"
else:
isoSeqs, recCount = readIsoforms(inDir)
if db=="uniprot":
xmlBase = "uniprot_sprot.xml.gz"
outPrefix = "uniprot"
recCount = 500000
elif db=="uniprotTrembl":
xmlBase = "uniprot_trembl.xml.gz"
outPrefix = "uniprotTrembl"
recCount = 500000*37
else:
raise Exception("unknown db")
xmlFile = gzip.open(join(inDir, xmlBase))
logging.info("Parsing main XML file %s" % xmlFile.name)
# create a dict taxonId -> output file handles for record info, pmid reference info and mutation info
outFhs = {}
for taxId in taxonIds:
entryOf = openOutTabFile(outDir, "%s.%s.tab" % (outPrefix, taxId), entryHeaders)
refOf = openOutTabFile(outDir, "%s.%s.refs.tab" % (outPrefix, taxId), refHeaders)
mutOf = openOutTabFile(outDir, "%s.%s.mut.tab" % (outPrefix, taxId), mutHeaders)
outFhs[taxId] = (entryOf, refOf, mutOf)
disToName = parseDiseases(join(inDir, "humdisease.txt"))
# base and variant sequence filehandles
faFiles = openFaFiles(taxonIds, outDir, outPrefix)
varFaFiles = openFaFiles(taxonIds, outDir, outPrefix, "var")
emptyEntry = dict(zip(entryHeaders, len(entryHeaders)*[""]))
pm = maxCommon.ProgressMeter(recCount)
#for _, entryEl in etree.iterparse(xmlFile.name, tag='{http://uniprot.org/uniprot}entry'):
for _, entryEl in etree.iterparse(xmlFile):
if entryEl.tag!="{http://uniprot.org/uniprot}entry":
continue
strip_namespace_inplace(entryEl) # die, die stupid namespaces!!
entry = copy.copy(emptyEntry)
pm.taskCompleted()
entryTax = int(entryEl.find("organism/dbReference").attrib["id"])
if taxonIds==['all']:
taxId = "all"
else:
if entryTax not in taxonIds:
continue
entryOf, refOf, mutOf = outFhs[taxId]
entryRow = parseRecInfo(entryEl, entry, isoSeqs)
writeFaSeqs(entryRow, faFiles)
writeFaSeqs(entryRow, varFaFiles, allVariants=True)
entryOf.write("\t".join(entryRow)+"\n")
recName = entryRow.name
refRows = list(parseRefInfo(entryEl, recName))
for refRow in refRows:
refOf.write("\t".join(refRow)+"\n")
mutRecs = parseVariants(entryEl, entryRow.mainIsoAcc, disToName)
for mutRow in mutRecs:
logging.debug("writing row %s" % str(mutRow))
mutOf.write("\t".join(mutRow)+"\n")
entryEl.clear()
logging.info("Skipped annotation types: %s" % ignoredTypes.most_common())
def main(args, options):
#logFname = join(outDir, "dbParse.log")
if options.test:
import doctest
doctest.testmod()
sys.exit(0)
pubGeneric.setupLogging("pubParseDb", options)
db = args[0]
refDir = pubConf.dbRefDir
maxCommon.mustExistDir(refDir, makeDir=True)
if db=="pdb":
dbDir = pubConf.pdbBaseDir
#parsePdb("/hive/data/outside/pdb/o9/pdb1o91.ent.gz", refDir)
parsePdb(dbDir, refDir)
elif db in ["uniprot", "uniprotTrembl"]:
dbDir = pubConf.uniProtBaseDir
#taxonIds = set(pubConf.uniProtTaxonIds)
if len(args)==1:
raise Exception("Please specify a taxonId or list of taxonIds, like 9606, or 'all'")
taxonIds = args[1]
if taxonIds=="all":
taxonIds = ['all']
else:
taxonIds=[int(x) for x in taxonIds.split(",")]
parseUniprot(db, dbDir, refDir, taxonIds)
else:
assert(False) # illegal db arg