Skip to content

Commit

Permalink
Name update
Browse files Browse the repository at this point in the history
— switched to more intuitive prid, ptid, and prdst
— this should make differentiating between a node and its ID easier
  • Loading branch information
DomBennett committed Jan 21, 2016
1 parent 1e4226d commit 31d71ce
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 84 deletions.
4 changes: 2 additions & 2 deletions R/calc-methods.R
@@ -1,7 +1,7 @@
# TODO: calc imbalance, calc tree dists

calcPhyDv <- function(tree, ids) {
prids <- unlist(getNodesPre(tree, ids))
prids <- unlist(getNodesPrid(tree, ids))
counts <- table(prids)
prids <- names(counts)[counts < length(ids)]
spans <- unlist(lapply(tree@nodelist[c(ids, prids)],
Expand All @@ -21,7 +21,7 @@ calcFrPrp <- function(tree, ids) {
span/n
}
.calc <- function(tip) {
ids <- c(tip, getNodePre(tree, tip))
ids <- c(tip, getNodePrid(tree, tip))
shares <- unlist(sapply(ids, .share))
sum(shares)
}
Expand Down
10 changes: 5 additions & 5 deletions R/display-methods.R
Expand Up @@ -56,15 +56,15 @@ setGeneric ("viz", signature=c("tree", "taxonyms"),
setMethod ('viz', 'TreeMan',
function(tree, taxonyms){
get_pnts <- function (node, y, pnts) {
pstids <- node[['post']]
pstids <- node[['ptid']]
low_y_diff <- -node[['pd']]/2
high_y_diff <- node[['pd']]/2
y_diffs <- seq(from=low_y_diff, to=high_y_diff,
length.out=length(pstids))
counter <- 1
for (pstid in pstids) {
pstnd <- tree@nodelist[[pstid]]
pstnd_x <- pstnd[['predist']]
pstnd_x <- pstnd[['prdst']]
pstnd_y <- y + y_diffs[counter]
pnts <- rbind (pnts, data.frame (node=pstid,
x=pstnd_x, y=pstnd_y))
Expand All @@ -78,8 +78,8 @@ setMethod ('viz', 'TreeMan',
for(i in 1:length(tree@nodelist)) {
tree@nodelist[[i]][['span']] <- 1
tree@nodelist[[i]][['pd']] <- length(tree@nodelist[[i]][['children']])
prids <- getNodePre(tree, tree@nodelist[[i]][['id']])
tree@nodelist[[i]][['predist']] <- length(prids)
prids <- getNodePrid(tree, tree@nodelist[[i]][['id']])
tree@nodelist[[i]][['prdst']] <- length(prids)
}
tree@pd <- length(tree@nodelist) - 1
}
Expand All @@ -104,7 +104,7 @@ setMethod ('viz', 'TreeMan',
}
# draw lines
for (i in 2:nrow (pnts)) {
prenode <- tree@nodelist[[pnts$node[i]]][['pre']]
prenode <- tree@nodelist[[pnts$node[i]]][['prid']]
ind <- c (i, which (pnts$node == prenode))
lines (x=pnts$x[ind], y=pnts$y[ind])
}
Expand Down
8 changes: 4 additions & 4 deletions R/gen-methods.R
Expand Up @@ -18,11 +18,11 @@ randTree <- function (n) {
pd <- 0
node <- list ('id'=id,
'span'=span,
'pre'=pre,
'post'=post,
'prid'=pre,
'ptid'=post,
'children'=children,
'pd'=pd,
'predist'=predist)
'prdst'=predist)
nodelist[[id]] <- node
# if there are enough ns left to have children
n_left <- n_left - 1
Expand Down Expand Up @@ -56,7 +56,7 @@ randTree <- function (n) {
}
}
nodelist[[id]][['children']] <- children
nodelist[[id]][['post']] <- post
nodelist[[id]][['ptid']] <- post
nodelist[[id]][['pd']] <- pd
nodelist
}
Expand Down
38 changes: 19 additions & 19 deletions R/get-methods.R
Expand Up @@ -27,7 +27,7 @@ getNodesChildren <- function(tree, ids) {
#TODO: how to effectively handle unrooted trees, age has no meaning
getNodeAge <- function(tree, id) {
node <- tree@nodelist[[id]]
tree@age - node[['predist']]
tree@age - node[['prdst']]
}

getNodesAge <- function(tree, ids) {
Expand All @@ -36,22 +36,22 @@ getNodesAge <- function(tree, ids) {
}

getEdgeAge <- function(tree, id) {
max <- getNodeAge(tree, tree@nodelist[[id]][['pre']])
max <- getNodeAge(tree, tree@nodelist[[id]][['prid']])
min <- getNodeAge(tree, id)
data.frame(edge=id, max, min)
}

getEdgesAge <- function(tree, ids) {
maxs <- sapply(ids, function(tree, id) {
getNodeAge(tree, tree@nodelist[[id]][['pre']])
getNodeAge(tree, tree@nodelist[[id]][['prid']])
}, tree=tree)
mins <- sapply(ids, getNodeAge, tree=tree)
data.frame(edge=ids, max=maxs, min=mins, row.names=NULL)
}

# @name getParent
getParent <- function(tree, ids) {
prids <- getNodesPre(tree, ids)
prids <- getNodesPrid(tree, ids)
rf <- prids[[1]]
mn_rnk <- 0
for(n in prids[-1]) {
Expand All @@ -63,18 +63,18 @@ getParent <- function(tree, ids) {

# @name getPath
getPath <- function(tree, from, to) {
pre_1 <- getNodePre(tree, from)
pre_2 <- getNodePre(tree, to)
pre_1 <- getNodePrid(tree, from)
pre_2 <- getNodePrid(tree, to)
parent <- pre_1[which(pre_1 %in% pre_2)[1]]
path_1 <- c(from ,pre_1[!pre_1 %in% pre_2])
path_2 <- c(pre_2[!pre_2 %in% pre_1], to)
c(path_1, parent, path_2)
}

# @name get_Pre
getNodePre <- function(tree, id) {
getNodePrid <- function(tree, id) {
.get <- function(nd, prids) {
prid <- tree@nodelist[[nd]][['pre']]
prid <- tree@nodelist[[nd]][['prid']]
if(!is.null(prid)) {
prids <- c(prid, .get(prid, prids))
}
Expand All @@ -83,13 +83,13 @@ getNodePre <- function(tree, id) {
.get(id, NULL)
}

getNodesPre <- function(tree, ids) {
sapply(ids, getNodePre, tree=tree, simplify=FALSE)
getNodesPrid <- function(tree, ids) {
sapply(ids, getNodePrid, tree=tree, simplify=FALSE)
}

# @name get_Lineage
getNodeLineage <- function(tree, id) {
prids <- getNodePre(tree, id)
prids <- getNodePrid(tree, id)
lineage <- sapply(prids, function(n) tree@nodelist[[n]][['taxonym']])
if(length(lineage) > 0) {
lineage <- c(tree@nodelist[[id]][['taxonym']], lineage)
Expand All @@ -106,11 +106,11 @@ getNodesLineage <- function(tree, ids) {
}

# @name get_Post
getNodePost <- function(tree, id) {
getNodePtid <- function(tree, id) {
.get <- function(nds, pstids) {
new_nds <- c()
for(nd in nds) {
new_nds <- c(new_nds, tree@nodelist[[nd]][['post']])
new_nds <- c(new_nds, tree@nodelist[[nd]][['ptid']])
}
pstids <- c(pstids, new_nds)
if(length(new_nds) > 0) {
Expand All @@ -121,20 +121,20 @@ getNodePost <- function(tree, id) {
.get(nds=id, pstids=NULL)
}

getNodesPost <- function(tree, ids) {
sapply(ids, getNodePost, tree=tree)
getNodesPtid <- function(tree, ids) {
sapply(ids, getNodePtid, tree=tree)
}

# @name getSubtree
getSubtree <- function(tree, id) {
pstids <- getNodePost(tree, id)
pstids <- getNodePtid(tree, id)
ndlst <- tree@nodelist[c(id, pstids)]
nd_prdst <- ndlst[[id]][['predist']]
nd_prdst <- ndlst[[id]][['prdst']]
ndlst <- lapply(ndlst, function(x) {
x[['predist']] <- x[['predist']] - nd_prdst
x[['prdst']] <- x[['prdst']] - nd_prdst
x
})
ndlst[[id]][['pre']] <- NULL
ndlst[[id]][['prid']] <- NULL
ndlst[[id]][['span']] <- 0
new_tree <- new('TreeMan', nodelist=ndlst, root=id)
.update(new_tree)
Expand Down
22 changes: 11 additions & 11 deletions R/manip-methods.R
Expand Up @@ -19,24 +19,24 @@ addTip <- function(tree, id, sister, start, end,
tip[['span']] <- start - end
age <- getNodeAge(tree, sister)
new_sister <- sister <- tree@nodelist[[sister]]
new_parent <- tree@nodelist[[sister[['pre']]]]
new_parent[['post']] <- new_parent[['post']][!new_parent[['post']] %in% sister[['id']]]
new_parent[['post']] <- c(new_parent[['post']], node[['id']])
new_parent <- tree@nodelist[[sister[['prid']]]]
new_parent[['ptid']] <- new_parent[['ptid']][!new_parent[['ptid']] %in% sister[['id']]]
new_parent[['ptid']] <- c(new_parent[['ptid']], node[['id']])
new_sister[['span']] <- start - age
new_sister[['pre']] <- node[['id']]
new_sister[['prid']] <- node[['id']]
node[['span']] <- sister[['span']] - new_sister[['span']]
node[['pd']] <- new_sister[['span']] + tip[['span']]
node[['predist']] <- sister[['predist']] - new_sister[['span']]
node[['pre']] <- sister[['pre']]
node[['post']] <- node[['children']] <- c(tip[['id']], sister[['id']])
node[['prdst']] <- sister[['prdst']] - new_sister[['span']]
node[['prid']] <- sister[['prid']]
node[['ptid']] <- node[['children']] <- c(tip[['id']], sister[['id']])
tip[['pd']] <- 0
tip[['predist']] <- node[['predist']] + tip[['span']]
tip[['pre']] <- node[['id']]
tip[['prdst']] <- node[['prdst']] + tip[['span']]
tip[['prid']] <- node[['id']]
tree@nodelist[[tip[['id']]]] <- tip
tree@nodelist[[node[['id']]]] <- node
tree@nodelist[[new_sister[['id']]]] <- new_sister
tree@nodelist[[new_parent[['id']]]] <- new_parent
pres <- getNodePre(tree, node[['id']])
pres <- getNodePrid(tree, node[['id']])
tree@nodelist[pres] <- lapply(tree@nodelist[pres],
updatePre)
.update(tree)
Expand All @@ -49,7 +49,7 @@ pinTip <- function(tree, tip_id, lineage, end) {
if(length(edges) == 0) {
next
}
edges <- c(edges, unlist(sapply(edges, function(n) tree@nodelist[[n]][['post']])))
edges <- c(edges, unlist(sapply(edges, function(n) tree@nodelist[[n]][['ptid']])))
edges <- edges[edges != tree@root]
rngs <- getEdgesAge(tree, ids=edges)
bool <- rngs[ ,'max'] > end
Expand Down
22 changes: 11 additions & 11 deletions R/node-declaration.R
Expand Up @@ -6,26 +6,26 @@
span <- node[['span']]
}
if(length(tree@age) > 0) {
age <- tree@age - node[['predist']]
age <- tree@age - node[['prdst']]
} else {
age <- numeric()
}
new('Node', id=node[['id']], span=span, pre=as.character(node[['pre']]),
post=as.character(node[['post']]), children=as.character(node[['children']]),
new('Node', id=node[['id']], span=span, prid=as.character(node[['prid']]),
ptid=as.character(node[['ptid']]), children=as.character(node[['children']]),
nchildren=length(as.character(node[['children']])), pd=node[['pd']],
predist=node[['predist']], root=tree@root == node[['id']],
age=age, tip=length(node[['post']]) == 0)
prdst=node[['prdst']], root=tree@root == node[['id']],
age=age, tip=length(node[['ptid']]) == 0)
}

setClass ('Node', representation=representation (
id='character', # unique ID for node in tree@nodelist
span='numeric', # length of preceding branch
pre='character', # parent node ID
post='vector', # child node IDs
prid='character', # parent node ID
ptid='vector', # child node IDs
children='vector', # descending tip IDs
nchildren='numeric', # number of descending tips
pd='numeric', # total branch length represented by node
predist='numeric', # total branch length of connected pres
prdst='numeric', # total branch length of connected pres
age='numeric', # age of node in tree
root='logical', # T/F root node?
tip='logical') # T/F tip node?
Expand All @@ -51,10 +51,10 @@ setMethod ('print', c('x'='Node'),
}
msg <- paste0(msg, ' + ID: \"', x@id, '\"\n')
if(!x@root) {
msg <- paste0(msg, ' + pre: \"', x@pre, '\"\n')
msg <- paste0(msg, ' + preid: \"', x@prid, '\"\n')
}
if(!x@tip) {
msg <- paste0(msg, ' + post: \"', paste0(x@post, collapse='\", \"'), '\"\n')
msg <- paste0(msg, ' + postid: \"', paste0(x@ptid, collapse='\", \"'), '\"\n')
msg <- paste0(msg, ' + nchildren: ', length(x@children), '\n')
}
if(length(x@span) > 0) {
Expand All @@ -64,7 +64,7 @@ setMethod ('print', c('x'='Node'),
if(length(x@age) > 0) {
msg <- paste0(msg, ' + age: ', signif(x@age, 2), '\n')
} else {
msg <- paste0(msg, ' + predist: ', signif(x@predist, 2), '\n')
msg <- paste0(msg, ' + predist: ', signif(x@prdst, 2), '\n')
}
msg <- paste0(msg, ' + pd: ', signif(x@pd, 2), '\n')
}
Expand Down
32 changes: 16 additions & 16 deletions R/read-write-methods.R
Expand Up @@ -28,12 +28,12 @@ readTree <- function(file=NULL, tree_string=NULL) {
NULL
}
add <- function(prndid, rdrenv, ndid=prndid) {
if(!is.null(rdrenv$nodelist[[prndid]][['post']])) {
if(!is.null(rdrenv$nodelist[[prndid]][['ptid']])) {
rdrenv$nodelist[[prndid]][['pd']] <- rdrenv$nodelist[[prndid]][['pd']] +
rdrenv$nodelist[[ndid]][['span']]
}
if(!is.null(rdrenv$nodelist[[prndid]][['pre']])) {
add(rdrenv$nodelist[[prndid]][['pre']], rdrenv, ndid)
if(!is.null(rdrenv$nodelist[[prndid]][['prid']])) {
add(rdrenv$nodelist[[prndid]][['prid']], rdrenv, ndid)
}
NULL
}
Expand All @@ -45,24 +45,24 @@ readTree <- function(file=NULL, tree_string=NULL) {
# add children
.addChildren <- function(rdrenv) {
add <- function(ndid, rdrenv, tpid=ndid) {
if(!is.null(rdrenv$nodelist[[ndid]][['post']])) {
if(!is.null(rdrenv$nodelist[[ndid]][['ptid']])) {
rdrenv$nodelist[[ndid]][['children']] <- c(tpid, rdrenv$nodelist[[ndid]][['children']])
}
if(!is.null(rdrenv$nodelist[[ndid]][['pre']])) {
add(rdrenv$nodelist[[ndid]][['pre']], rdrenv, tpid)
if(!is.null(rdrenv$nodelist[[ndid]][['prid']])) {
add(rdrenv$nodelist[[ndid]][['prid']], rdrenv, tpid)
}
NULL
}
tips <- sapply(rdrenv$nodelist, function(n) length(n[['post']]) == 0)
tips <- sapply(rdrenv$nodelist, function(n) length(n[['ptid']]) == 0)
tips <- names(tips)[tips]
sapply(tips, add, rdrenv=rdrenv)
NULL
}

.addRoot <- function(rdrenv) {
root_i <- which(unlist(lapply(rdrenv$nodelist, function(n) n[['pre']] == "root")))
root_i <- which(unlist(lapply(rdrenv$nodelist, function(n) n[['prid']] == "root")))
if(length(root_i) > 0) {
rdrenv$nodelist[[root_i]][['pre']] <- NULL
rdrenv$nodelist[[root_i]][['prid']] <- NULL
rdrenv$nodelist[[root_i]][['span']] <- 0
rdrenv$root <- names(rdrenv$nodelist)[root_i]
} else {
Expand All @@ -75,14 +75,14 @@ readTree <- function(file=NULL, tree_string=NULL) {
.addPredist <- function(rdrenv) {
calc <- function(nd, d) {
nd <- rdrenv$nodelist[[nd]]
if(!is.null(nd[['pre']])) {
if(!is.null(nd[['prid']])) {
d <- nd[['span']] + d
d <- calc(nd[['pre']], d)
d <- calc(nd[['prid']], d)
}
d
}
assgn <- function(i) {
rdrenv$nodelist[[i]][['predist']] <- ds[[i]]
rdrenv$nodelist[[i]][['prdst']] <- ds[[i]]
NULL
}
ds <- sapply(names(rdrenv$nodelist), calc, d=0)
Expand Down Expand Up @@ -135,19 +135,19 @@ readTree <- function(file=NULL, tree_string=NULL) {
# TODO: utilise nd$id, e.g. node labels are taxonym or support
nd$id <- rdrenv$prnds[[rdrenv$i]][['id']]
rdrenv$prnds[[rdrenv$i]][['span']] <- nd[['span']]
rdrenv$prnds[[rdrenv$i]][['pre']] <-
rdrenv$prnds[[rdrenv$i]][['prid']] <-
rdrenv$prnds[[rdrenv$i-1]][['id']]
rdrenv$nodelist[[nd[['id']]]] <- rdrenv$prnds[[rdrenv$i]]
rdrenv$prnds <- rdrenv$prnds[-rdrenv$i]
rdrenv$i <- rdrenv$i - 1
rdrenv$nxt_is_intrnl <- FALSE
} else {
nd[['pre']] <- rdrenv$prnds[[rdrenv$i]][['id']]
nd[['prid']] <- rdrenv$prnds[[rdrenv$i]][['id']]
rdrenv$nodelist[[nd[['id']]]] <- nd
}
if(length(rdrenv$prnds) > 0) {
rdrenv$prnds[[rdrenv$i]][['post']] <-
c(rdrenv$prnds[[rdrenv$i]][['post']], nd[['id']])
rdrenv$prnds[[rdrenv$i]][['ptid']] <-
c(rdrenv$prnds[[rdrenv$i]][['ptid']], nd[['id']])
}
if(grepl("\\)$", ndstr)) {
rdrenv$nxt_is_intrnl <- TRUE
Expand Down

0 comments on commit 31d71ce

Please sign in to comment.