-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_info_nodes.py
67 lines (51 loc) · 1.82 KB
/
create_info_nodes.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
# -*- coding: utf-8 -*-
#!/home/lineo4j/python/bin/python
# mundane modification stuff
import os
import commands
import sys
import datetime
import re # regexp
import time
# use python-embedded to import these few nodes:
from neo4j import GraphDatabase
class MetaInfoCreator( object ):
def __init__( self, databasePath, datasetDict):
#increment to existing db
self.db = GraphDatabase( databasePath )
self.datasetDict = datasetDict
def start(self):
self._createInfoNodes()
self._finish()
def _createInfoNodes( self ):
print "Creating info nodes"
# do all insertions within one transaction: complete failure or success!
with self.db.transaction:
metaVertex = self.db.node() #self.db.reference_node
print "Meta node created, id %i" %( metaVertex.id )
index = self.db.node.indexes.create('meta')
index['meta']['meta'] = metaVertex
for num, (label, patientFile) in enumerate( self.datasetDict.items() ):
patientFile = open( patientFile, 'r')
patientFile.readline() #header
datasetVertex = self.db.node()
datasetVertex['datalabel'] = label
datasetVertex['barcode'] = patientFile.readline().strip("\n")
metaVertex.relationships.create('DATASET', datasetVertex, label=label)
patientFile.close()
def _finish(self):
self.db.shutdown()
print "Infonodes created"
if __name__ == "__main__":
if( len( sys.argv ) is not 4 ):
print "Usage is python databasePath datasetlabel1:datasetlabel2:..:datasetlabelN patientFile1:patientFile2:...:patientFileN"
sys.exit(-1)
dbPath = sys.argv[1]
datasetLabels = sys.argv[2].split(":")
patientFiles = sys.argv[3].split(":")
if not os.path.exists( dbPath ):
print "Database directory does not exist, will not create empty db."
sys.exit(-1)
creator = MetaInfoCreator( dbPath, dict(zip( datasetLabels, patientFiles ) ) )
creator.start()
sys.exit(0)