-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClinVarToSQL.py
67 lines (58 loc) · 2.1 KB
/
ClinVarToSQL.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
__author__ = 'lario'
import csv
import re
import os, urllib, datetime, time, sys
import xml.etree.ElementTree as ET
import clinvar13
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
def processReferenceClinVarAssertion(referenceClinVarAssertion, stream = sys.stdout):
stream.write('DECLARE @newKey AS INT;\n')
stream.write('SET @NewKey = @@IDENTITY;\n')
stream.write("INSERT INTO referenceClinVarAssertion (tbl1_ID,col1, col2) VALUES (@NewKey, 'moredata1', 'moredata2'....);\n'")
stream.write('\n')
def processClinVarSet(clinVarSet, stream):
if 'ID' in clinVarSet.attrib :
stream.write("INSERT INTO ClinVarSet (")
stream.write("RecordStatus")
# some more column names
stream.write(") VALUES (")
stream.write("%s" % (clinVarSet.xpath('RecordStatus/text()[1]')[0]))
stream.write(");\n")
referenceClinVarAssertion = clinVarSet.xpath('ReferenceClinVarAssertion[1]')
if len(referenceClinVarAssertion) > 0 :
processReferenceClinVarAssertion(referenceClinVarAssertion[0], stream)
def main() :
try:
stream = sys.stdout
tree = etree.parse('D:\My Ontologies\ClinVar\VisualizeClinvar-master\database\clinvar-demo.xml')
root = tree.getroot()
for clinVarSet in root:
processClinVarSet(clinVarSet,stream)
except:
e = sys.exc_info()[0]
print( "Error: %s" % e )
main()