Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Remi PLANEL
bioviz-js
Commits
bc587acc
Commit
bc587acc
authored
Jun 11, 2019
by
Remi PLANEL
Browse files
Rename layout phylotree to phylogram
parent
3583766c
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/scripts/layout/phylo
tree
.ts
→
src/scripts/layout/phylo
gram
.ts
View file @
bc587acc
import
{
hierarchy
,
HierarchyNode
,
HierarchyPointNode
}
from
"
d3-hierarchy
"
;
import
{
RawPhyloTreeNode
,
PhyloTreeNode
}
from
"
../types
"
;
import
{
scaleLinear
}
from
"
d3-scale
"
;
import
{
getLeftNode
,
getRightNode
,
getDeepestNode
}
from
"
../utils/tree
"
;
export
default
function
()
{
let
dx
=
1
;
...
...
@@ -12,53 +10,73 @@ export default function () {
function
phylotree
(
data
:
RawPhyloTreeNode
)
{
// Apply the layout hierarchy.
const
root
=
hierarchy
(
data
);
// Compute the lengthFromRoot
root
.
eachBefore
(
computeLengthFromRoot
);
const
{
pointPhylotreeRoot
,
leftNode
,
rightNode
,
deepestNode
}
=
setRelativePosition
(
root
);
return
setAbsolutePosition
(
pointPhylotreeRoot
,
leftNode
,
rightNode
,
deepestNode
);
}
function
setRelativePosition
()
{
const
nodeWithPoint
=
root
as
HierarchyPointNode
<
PhyloTreeNode
>
;
nodeWithPoint
.
x
=
0
;
nodeWithPoint
.
y
=
0
;
let
previous
:
HierarchyPointNode
<
PhyloTreeNode
>
|
null
=
null
;
let
leftNode
=
nodeWithPoint
;
let
rightNode
=
nodeWithPoint
;
let
deepestNode
=
nodeWithPoint
;
function
computeXposition
(
node
:
HierarchyPointNode
<
PhyloTreeNode
>
)
{
const
children
=
node
.
children
;
if
(
children
)
{
node
.
x
=
(
children
[
0
].
x
+
children
[
children
.
length
-
1
].
x
)
/
2
;
return
node
.
x
}
else
{
node
.
x
=
(
previous
)
?
previous
.
x
+
separation
(
node
,
previous
)
:
0
;
previous
=
node
;
return
node
.
x
;
}
}
return
{
pointPhylotreeRoot
:
nodeWithPoint
.
eachAfter
((
node
)
=>
{
node
.
y
=
node
.
data
.
lengthFromRoot
;
node
.
x
=
computeXposition
(
node
);
leftNode
=
(
node
.
x
<=
leftNode
.
x
)
?
node
:
leftNode
;
rightNode
=
(
node
.
x
>=
rightNode
.
x
)
?
node
:
rightNode
;
deepestNode
=
(
node
.
y
>=
deepestNode
.
y
)
?
node
:
deepestNode
;
// Functions
function
sizeNode
(
node
:
HierarchyPointNode
<
PhyloTreeNode
>
)
{
node
.
x
*=
dx
;
node
.
y
=
node
.
depth
*
dy
;
}
}),
leftNode
,
rightNode
,
deepestNode
};
function
computeLengthFromRoot
(
node
:
HierarchyNode
<
RawPhyloTreeNode
>
)
{
const
{
data
:
{
branchLength
}
}
=
node
;
node
.
data
.
branchLength
=
parseFloat
(
branchLength
.
toFixed
(
10
))
node
.
data
.
lengthFromRoot
=
(
node
.
parent
&&
node
.
parent
.
data
.
lengthFromRoot
)
?
parseFloat
((
node
.
parent
.
data
.
lengthFromRoot
+
branchLength
).
toFixed
(
10
))
:
branchLength
;
return
node
.
data
.
lengthFromRoot
;
}
function
setRelativePosition
(
root
:
HierarchyNode
<
RawPhyloTreeNode
>
)
{
const
nodeWithPoint
=
root
as
HierarchyPointNode
<
PhyloTreeNode
>
;
nodeWithPoint
.
x
=
0
;
nodeWithPoint
.
y
=
0
;
let
previous
:
HierarchyPointNode
<
PhyloTreeNode
>
|
null
=
null
;
let
leftNode
=
nodeWithPoint
;
let
rightNode
=
nodeWithPoint
;
let
deepestNode
=
nodeWithPoint
;
function
computeXposition
(
node
:
HierarchyPointNode
<
PhyloTreeNode
>
)
{
const
children
=
node
.
children
;
if
(
children
)
{
node
.
x
=
parseFloat
(((
children
[
0
].
x
+
children
[
children
.
length
-
1
].
x
)
/
2
).
toFixed
(
10
));
return
node
.
x
}
else
{
node
.
x
=
(
previous
)
?
previous
.
x
+
separation
(
node
,
previous
)
:
0
;
previous
=
node
;
return
node
.
x
;
}
}
// const maxLengthFromRoot = getMaxLenghtFromRoot();
const
{
pointPhylotreeRoot
,
leftNode
,
rightNode
,
deepestNode
}
=
setRelativePosition
();
pointPhylotreeRoot
.
eachBefore
(
n
=>
{
console
.
log
(
n
.
data
.
name
,
'
- [
'
,
n
.
x
,
'
,
'
+
n
.
y
+
'
]
'
,
"
=>
"
,
n
.
data
.
lengthFromRoot
);
})
console
.
log
(
'
left =
'
,
leftNode
.
data
.
name
,
'
-
'
,
leftNode
.
x
);
console
.
log
(
'
right =
'
,
rightNode
.
data
.
name
,
'
-
'
,
rightNode
.
x
);
console
.
log
(
'
deep =
'
,
deepestNode
.
data
.
name
,
'
-
'
,
deepestNode
.
y
);
return
{
pointPhylotreeRoot
:
nodeWithPoint
.
eachAfter
((
node
)
=>
{
node
.
y
=
node
.
data
.
lengthFromRoot
;
node
.
x
=
computeXposition
(
node
);
leftNode
=
(
node
.
x
<=
leftNode
.
x
)
?
node
:
leftNode
;
rightNode
=
(
node
.
x
>=
rightNode
.
x
)
?
node
:
rightNode
;
deepestNode
=
(
node
.
y
>=
deepestNode
.
y
)
?
node
:
deepestNode
;
}),
leftNode
,
rightNode
,
deepestNode
};
}
function
setAbsolutePosition
(
pointPhylotreeRoot
:
HierarchyPointNode
<
PhyloTreeNode
>
,
leftNode
:
HierarchyPointNode
<
PhyloTreeNode
>
,
rightNode
:
HierarchyPointNode
<
PhyloTreeNode
>
,
deepestNode
:
HierarchyPointNode
<
PhyloTreeNode
>
)
{
const
s
=
leftNode
===
rightNode
?
1
:
separation
(
leftNode
,
rightNode
)
/
2
,
tx
=
s
-
leftNode
.
x
,
kx
=
dx
/
(
rightNode
.
x
+
s
+
tx
),
...
...
@@ -69,37 +87,17 @@ export default function () {
pointPhylotreeRoot
.
eachBefore
(
sizeNode
)
}
else
{
pointPhylotreeRoot
.
eachBefore
(
function
(
node
)
{
node
.
x
=
(
node
.
x
+
tx
)
*
kx
;
node
.
y
=
node
.
data
.
lengthFromRoot
*
ky
;
node
.
x
=
parseFloat
((
(
node
.
x
+
tx
)
*
kx
).
toFixed
(
1
))
;
node
.
y
=
parseFloat
((
node
.
data
.
lengthFromRoot
*
ky
).
toFixed
(
1
))
;
});
}
pointPhylotreeRoot
.
eachBefore
(
n
=>
{
console
.
log
(
n
.
data
.
name
,
'
- [
'
,
n
.
x
,
'
,
'
+
n
.
y
+
'
]
'
);
})
return
pointPhylotreeRoot
;
}
// FUnctions
function
sizeNode
(
node
:
HierarchyPointNode
<
PhyloTreeNode
>
)
{
node
.
x
*=
dx
;
node
.
y
=
node
.
depth
*
dy
;
}
function
computeLengthFromRoot
(
node
:
HierarchyNode
<
RawPhyloTreeNode
>
)
{
const
{
data
:
{
branchLength
}
}
=
node
;
node
.
data
.
branchLength
=
parseFloat
(
branchLength
.
toFixed
(
10
))
node
.
data
.
lengthFromRoot
=
(
node
.
parent
&&
node
.
parent
.
data
.
lengthFromRoot
)
?
parseFloat
((
node
.
parent
.
data
.
lengthFromRoot
+
branchLength
).
toFixed
(
10
))
:
branchLength
;
return
node
.
data
.
lengthFromRoot
;
}
// PUBLIC
phylotree
.
nodeSize
=
function
(
size
:
[
number
,
number
])
{
nodeSize
=
true
;
dx
=
+
size
[
0
];
...
...
@@ -112,6 +110,11 @@ export default function () {
dy
=
+
size
[
1
];
return
phylotree
;
}
return
phylotree
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment