Skip to content

Instantly share code, notes, and snippets.

@rdmpage

rdmpage/animals.csv

Last active May 28, 2021
Embed
What would you like to do?
Summary tree example
node parent label weight
1 0 Animalia 0
2 1 Acanthocephala 1330
3 1 Annelida 15676
4 1 Arthropoda 1114267
5 1 Brachiopoda 396
6 1 Bryozoa 20559
7 1 Chaetognatha 132
8 1 Chordata 0
9 1 Cnidaria 14223
10 1 Ctenophora 214
11 1 Dicyemida 122
12 1 Echinodermata 10684
13 1 Gastrotricha 865
14 1 Gnathostomulida 100
15 1 Hemichordata 139
16 1 Kamptozoa 173
17 1 Kinorhyncha 309
18 1 Loricifera 29
19 1 Micrognathozoa 1
20 1 Mollusca 110863
21 1 Nematoda 25000
22 1 Nematomorpha 356
23 1 Nemertea 1356
24 1 Onychophora 167
25 1 Orthonectida 25
26 1 Phoronida 19
27 1 Placozoa 3
28 1 Platyhelminthes 20575
29 1 Porifera 9418
30 1 Priapulida 22
31 1 Rotifera 2014
32 1 Sipuncula 206
33 1 Tardigrada 1018
34 1 Xenacoelomorpha 456
35 8 Actinopterygii 32513
36 8 Amphibia 8054
37 8 Aves 10479
38 8 Cephalaspidomorphi 47
39 8 Elasmobranchii 1226
40 8 Holocephali 56
41 8 Mammalia 5939
42 8 Myxini 82
43 8 Reptilia 10233
44 8 Sarcopterygii 8
45 4 Branchiopoda 1
46 4 Cephalocarida 1
47 4 Chilopoda 1
48 4 Collembola 1
49 4 Diplopoda 1
50 4 Entognatha 1
51 4 Hexanauplia 1
52 4 Insecta 0
53 4 Malacostraca 34797
54 4 Maxillopoda 1
55 4 Merostomata 1
56 4 Ostracoda 1
57 4 Pauropoda 1
58 4 Pycnogonida 1
59 4 Remipedia 1
60 4 Symphyla 1
61 4 Trilobita 1
62 4 Arachnida 78993
63 52 Archaeognatha 1
64 52 Blattodea 1
65 52 Caloneurodea 1
66 52 Cnemidolestodea 1
67 52 Coleoptera 326344
68 52 Dermaptera 1
69 52 Diaphanopterodea 1
70 52 Diptera 153387
71 52 Embioptera 1
72 52 Eoblattida 1
73 52 Ephemeroptera 1
74 52 Glosselytrodea 1
75 52 Grylloblattodea 1
76 52 Hemiptera 95396
77 52 Hymenoptera 118153
78 52 Hypoperlida 1
79 52 Lepidoptera 149969
80 52 Mantodea 1
81 52 Mantophasmatodea 1
82 52 Mecoptera 1
83 52 Megaloptera 1
84 52 Meganisoptera 1
85 52 Megasecoptera 1
86 52 Miomoptera 1
87 52 Neuroptera 1
88 52 Odonata 1
89 52 Orthoptera 1
90 52 Palaeodictyoptera 1
91 52 Paoliida 1
92 52 Phasmida 1
93 52 Plecoptera 1
94 52 Protocoleoptera 1
95 52 Protorthoptera 1
96 52 Psocodea 1
97 52 Raphidioptera 1
98 52 Reculoidea 1
99 52 Siphonaptera 1
100 52 Strepsiptera 1
101 52 Thysanoptera 1
102 52 Titanoptera 1
103 52 Trichoptera 11513
104 52 Zoraptera 1
105 52 Zygentoma 1
digraph {
rankdir=LR;
1[label="Animalia 0"];
2[label="Chordata 0"];
34[label="Arthropoda 1114267"];
35[label="Insecta 0"];
23[label="Acanthocephala 1330"];
24[label="Nemertea 1356"];
25[label="Rotifera 2014"];
26[label="Porifera 9418"];
27[label="4752 others"];
28[label="34812 others"];
29[label="1419 others"];
30[label="11550 others"];
31[label="Platyhelminthes 20575"];
32[label="Nematoda 25000"];
33[label="Mollusca 110863"];
52[label="Arachnida 78993"];
58[label="Mammalia 5939"];
59[label="Amphibia 8054"];
60[label="Reptilia 10233"];
61[label="Aves 10479"];
62[label="Actinopterygii 32513"];
101[label="Hemiptera 95396"];
102[label="Hymenoptera 118153"];
103[label="Lepidoptera 149969"];
104[label="Diptera 153387"];
105[label="Coleoptera 326344"];
1->2;
1->34;
34->35;
1->23;
1->24;
1->25;
1->26;
1->27;
34->28;
2->29;
35->30;
1->31;
1->32;
1->33;
34->52;
2->58;
2->59;
2->60;
2->61;
2->62;
35->101;
35->102;
35->103;
35->104;
35->105;
}
library(devtools)
install_github("kshirley/summarytrees", build_vignettes = TRUE)
library(summarytrees)
data = read.table('/Users/rpage/Development/summarytrees/animals.csv', header=TRUE,sep=",")
g <- greedy(node = data[, "node"],
parent = data[, "parent"],
weight = data[, "weight"],
label = data[, "label"],
K = 30)
write.csv(g$summary.trees[[30]], '/Users/rpage/Development/summarytrees/summary.csv')
<?php
//----------------------------------------------------------------------------------------
// http://stackoverflow.com/a/5996888/9684
function translate_quoted($string) {
$search = array("\\t", "\\n", "\\r");
$replace = array( "\t", "\n", "\r");
return str_replace($search, $replace, $string);
}
//----------------------------------------------------------------------------------------
$filename = 'summary.csv';
$nodes = array();
$edges = array();
$headings = array();
$row_count = 0;
$file = @fopen($filename, "r") or die("couldn't open $filename");
$file_handle = fopen($filename, "r");
while (!feof($file_handle))
{
$row = fgetcsv(
$file_handle,
0,
translate_quoted(','),
translate_quoted('"')
);
$go = is_array($row);
if ($go)
{
if ($row_count == 0)
{
$headings = $row;
}
else
{
$obj = new stdclass;
foreach ($row as $k => $v)
{
if ($v != '')
{
$obj->{$headings[$k]} = $v;
}
}
// print_r($obj);
$node_obj = new stdclass;
$node_obj->label = $obj->label;
$node_obj->weight = $obj->weight;
$node_obj->label .= ' '. $node_obj->weight;
$node_id = $obj->node;
if ($node_id == 'NA')
{
$node_id = $row_count;
$node_obj->label = $node_obj->weight . ' others';
}
$nodes[$node_id] = $node_obj;
if ($obj->parent != 0)
{
$edges[$node_id] = $obj->parent;
}
}
}
$row_count++;
}
// print_r($nodes);
//print_r($edges);
echo "digraph {\n";
echo "rankdir=LR;\n";
foreach ($nodes as $id => $n)
{
echo $id . '[label="' . addcslashes($n->label, '"') . '"];' . "\n";
}
foreach ($edges as $n => $p)
{
echo $p . "->" . $n . ";\n";
}
echo "}\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment