-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphMLconverter.ps1
56 lines (49 loc) · 1.71 KB
/
GraphMLconverter.ps1
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
# Define file locations
$edgeListFile = "edgelist.csv"
$nodeListFile = "nodelist.csv"
$graphMLFile = "graph_file.graphml"
# Write the beginning information
$beginning = @"
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
"@
# Write the attribute information
$attr1 = @"
<key id="d0" for="node" attr.name="attr" attr.type="string">
<default>none</default>
</key>
<key id="tag" for="node" attr.name="tag" attr.type="string">
<default>none</default>
</key>
"@
# The beginning of the graph
$graph_head = '<graph id="G" edgedefault="undirected">'
# Create and open graphML file
$graphMLContent = $beginning + $attr1 + $graph_head
# Process the nodes
Get-Content $nodeListFile | ForEach-Object {
$linenode = $_ -split ';'
$node = ($linenode[0] -replace '[<>]', '_').Trim()
$tag = ($linenode[1] -replace '[<>]', '_').Trim()
if ($node -ne "") {
$graphMLContent += "<node id=`"$node`"><data key=`"tag`">$tag</data></node>`n"
}
}
# Process the edges
$i = 0
Get-Content $edgeListFile | ForEach-Object {
$line = $_ -split ';'
$source = ($line[0] -replace '[<>]', '_').Trim()
$target = ($line[1] -replace '[<>]', '_').Trim()
if ($source -ne "" -and $target -ne "") {
$edgeContent = "<edge id=`"e$i`" source=`"$source`" target=`"$target`"/>`n"
$graphMLContent += $edgeContent
$i++
}
}
# Format the end and write to file
$graphMLContent += "</graph>`n</graphml>"
Set-Content -Path $graphMLFile -Value $graphMLContent