Discussion:
[R-sig-phylo] atomic vector for write.nexus
Elaine Kuo
2013-04-05 10:12:45 UTC
Permalink
Hello,

This is Elaine.
I am using the command "write.nexus"in package (ape)
to generate a nexus file after pruning a phylogeny tree.

However, an error appeared, saying
Error in obj[[i]]$tip.label : $ operator is invalid for atomic vectors

I searched the archive but found no similar experience in this command.
Please kindly help explain what it means and how to solve it.

code
library(ape)

# read data and tree
birddata <- read.csv("H:/birddata_family_20130405.csv", header = TRUE)
birdtree <- read.nexus("H:/birddata_family_20130405.nexus")
setwd("H:")

species.to.keep <-read.csv("H:/birddata_family_20130405.csv", header =
TRUE)
pruned.tree<-drop.tip(birdtree,birdtree$tip.label[-
na.omit(match(species.to.keep[,1],birdtree$tip.label))])


## prune the data to include only those taxa in the pruned tree
data.pruned <- birddata[tree.pruned$tip.label, ] (Thanks to Klaus and
others)

write.nexus(tree.pruned, "H:/pruneddata_20130405.nexus", translate =
FALSE)

Elaine

[[alternative HTML version deleted]]
Elaine Kuo
2013-04-05 10:43:43 UTC
Permalink
I changed " pruned.tree" to" tree.pruned."
The error still existed.

Elaine

---------- Forwarded message ----------
From: Elaine Kuo <***@gmail.com>
Date: Fri, Apr 5, 2013 at 6:12 PM
Subject: atomic vector for write.nexus
To: r-sig-***@r-project.org


Hello,

This is Elaine.
I am using the command "write.nexus"in package (ape)
to generate a nexus file after pruning a phylogeny tree.

However, an error appeared, saying
Error in obj[[i]]$tip.label : $ operator is invalid for atomic vectors

I searched the archive but found no similar experience in this command.
Please kindly help explain what it means and how to solve it.

code
library(ape)

# read data and tree
birddata <- read.csv("H:/birddata_family_20130405.csv", header = TRUE)
birdtree <- read.nexus("H:/birddata_family_20130405.nexus")
setwd("H:")

species.to.keep <-read.csv("H:/birddata_family_20130405.csv", header =
TRUE)
tree.pruned<-drop.tip(birdtree,birdtree$tip.label[-
na.omit(match(species.to.keep[,1],birdtree$tip.label))])


## prune the data to include only those taxa in the pruned tree
data.pruned <- birddata[tree.pruned$tip.label, ] (Thanks to Klaus and
others)

write.nexus(tree.pruned, "H:/pruneddata_20130405.nexus", translate =
FALSE)

Elaine

[[alternative HTML version deleted]]
Peter Adler
2013-06-27 19:11:00 UTC
Permalink
Elaine,
Did you ever solve this problem? I have run into the same error in the
same context (trying to write.nexus() after pruning a tree).
-Peter
--
Peter Adler
Dept. Wildland Resources and the Ecology Center
Utah State University
http://www.cnr.usu.edu/htm/facstaff/adler-web
Liam J. Revell
2013-06-27 23:51:51 UTC
Permalink
I'm guessing this is because you need to specify the argument "file" by
name and are not doing so - i.e.:

write.nexus(tree,file="filename")

This is because the first argument in write.nexus, unlike write.tree, is
the ... three-dot argument so subsequent arguments need to be indicated
by name (otherwise they are treated as additional trees to write). Let
us know if specifying the argument file by name fixes your problem.

- Liam

Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: ***@umb.edu
blog: http://blog.phytools.org
Post by Peter Adler
Elaine,
Did you ever solve this problem? I have run into the same error in the
same context (trying to write.nexus() after pruning a tree).
-Peter
Peter Adler
2013-06-28 15:05:01 UTC
Permalink
Bingo, problem solved, thank you Liam!
Post by Liam J. Revell
I'm guessing this is because you need to specify the argument "file"
write.nexus(tree,file="filename")
This is because the first argument in write.nexus, unlike write.tree,
is the ... three-dot argument so subsequent arguments need to be
indicated by name (otherwise they are treated as additional trees to
write). Let us know if specifying the argument file by name fixes your
problem.
- Liam
Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
blog: http://blog.phytools.org
Post by Peter Adler
Elaine,
Did you ever solve this problem? I have run into the same error in the
same context (trying to write.nexus() after pruning a tree).
-Peter
--
Peter Adler
Dept. Wildland Resources and the Ecology Center
Utah State University
http://www.cnr.usu.edu/htm/facstaff/adler-web
Luigi Marongiu
2014-12-27 23:57:26 UTC
Permalink
Dear Liam,
I ran in a similar problem as the one posed by Elaine, but when I used
the argument file = "filename.nex" I still obtained "Error in
y$tip.label : $ operator is invalid for atomic vectors".
To be clear I aligned the sequences using muscle(), created a distance
matrix using dist.dna(), computed the tree using upgma() and now I would
like to save the tree using nexus().
Any tip?
regards
Luigi

_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-***@r-project.org/
Liam J. Revell
2015-01-01 22:03:34 UTC
Permalink
Hi Luigi.

Unfortunately, I think you may need to supply us with some more details
to try and figure out what your problem is.

Does your code look like:

D<-dist.dna(obj)
## obj is class "DNAbin", e.g.:
## data(woodmouse)
## obj<-woodmouse
tree<-upgma(D)
plot(tree) ## works
write.nexus(tree,file="filename.nex") ## does not work?

If so, I was able to get this to run on test datasets without a problem.

All the best, Liam

Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: ***@umb.edu
blog: http://blog.phytools.org
Post by Luigi Marongiu
Dear Liam,
I ran in a similar problem as the one posed by Elaine, but when I used
the argument file = "filename.nex" I still obtained "Error in
y$tip.label : $ operator is invalid for atomic vectors".
To be clear I aligned the sequences using muscle(), created a distance
matrix using dist.dna(), computed the tree using upgma() and now I would
like to save the tree using nexus().
Any tip?
regards
Luigi
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-***@r-project.org/
Luigi Marongiu
2015-01-02 09:26:29 UTC
Permalink
Dear Liam,
I writing the tree itself is not a problem (I actually use the Newick
format) but I can't really plot the tree with plot(tree) because it is
too large. I am using instead zoom() which requires the definition of
one or more subtrees.
I could neither write the all code to reconstruct the Newick file
directly on the message because it is too cumbersome nor I can attach
the file to the message because too large.
Anyway, the tree is saved in the file tr.tre (Newick format), and
passed to the zoom function as follows:

# read tree
tr<-read.tree("tr.tre")
# set margins of the plot
par(mar = c(1, 1, 1, 1))
# plot tree
zoom(
tr,
3:62,
subtree = TRUE,
col = "grey",
no.margin = TRUE,
show.tip.label = TRUE,
font = 1,
cex = 0.3
)

This argument works fine but the subtree is provided by the argument
3:62, which required a bit to try-n-error to find the proper tips to
highlight.
The point is that I have another file, sub.txt which contains the names
of the tips to highlight (this file contains two columns: the number of
rows and the name of the tips and whose name is "tips"). This should
give more control to the definition of the subtree. The code I wrote is:

# read subtree
leaf<-read.table("sub.txt",header=T,row.names=1,sep="\t")
# read tree
tr<-read.tree("tr.tre")
# set margins of the plot
par(mar = c(1, 1, 1, 1))
# plot tree
zoom(
tr,
leaf$tip, # or even just leaf
subtree = TRUE,
col = "grey",
no.margin = TRUE,
show.tip.label = TRUE,
font = 1,
cex = 0.3
)

But this gives the error:

Warning message:
In Ops.factor(focus[[i]]) : - not meaningful for factors

and the zoom does not work because the main tree is plotted again in the
portion of the figure that should provide the zoom.
Is there a way to overcome this problem?
Regards
Luigi
Post by Liam J. Revell
Hi Luigi.
Unfortunately, I think you may need to supply us with some more
details to try and figure out what your problem is.
D<-dist.dna(obj)
## data(woodmouse)
## obj<-woodmouse
tree<-upgma(D)
plot(tree) ## works
write.nexus(tree,file="filename.nex") ## does not work?
If so, I was able to get this to run on test datasets without a problem.
All the best, Liam
Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
blog: http://blog.phytools.org
Post by Luigi Marongiu
Dear Liam,
I ran in a similar problem as the one posed by Elaine, but when I used
the argument file = "filename.nex" I still obtained "Error in
y$tip.label : $ operator is invalid for atomic vectors".
To be clear I aligned the sequences using muscle(), created a distance
matrix using dist.dna(), computed the tree using upgma() and now I would
like to save the tree using nexus().
Any tip?
regards
Luigi
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at
Emmanuel Paradis
2015-01-02 17:59:13 UTC
Permalink
Hi Luigi,

Try:

zoom(tr, which(tr$tip.label %in% leaf$tip), ......

You may also store the numbers extracted by which():

sel <- which(tr$tip.label %in% leaf$tip)
zoom(tr, sel, ......

HTH

Cheers,

Emmanuel
Post by Luigi Marongiu
Dear Liam,
I writing the tree itself is not a problem (I actually use the Newick
format) but I can't really plot the tree with plot(tree) because it is
too large. I am using instead zoom() which requires the definition of
one or more subtrees.
I could neither write the all code to reconstruct the Newick file
directly on the message because it is too cumbersome nor I can attach
the file to the message because too large.
Anyway, the tree is saved in the file tr.tre (Newick format), and
# read tree
tr<-read.tree("tr.tre")
# set margins of the plot
par(mar = c(1, 1, 1, 1))
# plot tree
zoom(
tr,
3:62,
subtree = TRUE,
col = "grey",
no.margin = TRUE,
show.tip.label = TRUE,
font = 1,
cex = 0.3
)
This argument works fine but the subtree is provided by the argument
3:62, which required a bit to try-n-error to find the proper tips to
highlight.
The point is that I have another file, sub.txt which contains the names
of the tips to highlight (this file contains two columns: the number of
rows and the name of the tips and whose name is "tips"). This should
# read subtree
leaf<-read.table("sub.txt",header=T,row.names=1,sep="\t")
# read tree
tr<-read.tree("tr.tre")
# set margins of the plot
par(mar = c(1, 1, 1, 1))
# plot tree
zoom(
tr,
leaf$tip, # or even just leaf
subtree = TRUE,
col = "grey",
no.margin = TRUE,
show.tip.label = TRUE,
font = 1,
cex = 0.3
)
In Ops.factor(focus[[i]]) : - not meaningful for factors
and the zoom does not work because the main tree is plotted again in the
portion of the figure that should provide the zoom.
Is there a way to overcome this problem?
Regards
Luigi
Post by Liam J. Revell
Hi Luigi.
Unfortunately, I think you may need to supply us with some more
details to try and figure out what your problem is.
D<-dist.dna(obj)
## data(woodmouse)
## obj<-woodmouse
tree<-upgma(D)
plot(tree) ## works
write.nexus(tree,file="filename.nex") ## does not work?
If so, I was able to get this to run on test datasets without a problem.
All the best, Liam
Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
blog: http://blog.phytools.org
Post by Luigi Marongiu
Dear Liam,
I ran in a similar problem as the one posed by Elaine, but when I used
the argument file = "filename.nex" I still obtained "Error in
y$tip.label : $ operator is invalid for atomic vectors".
To be clear I aligned the sequences using muscle(), created a distance
matrix using dist.dna(), computed the tree using upgma() and now I would
like to save the tree using nexus().
Any tip?
regards
Luigi
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-si
Luigi Marongiu
2015-01-02 23:21:16 UTC
Permalink
Dear Emmanuel,
it worked perfectly! Many thanks.
Best regards,
Luigi
Post by Emmanuel Paradis
which(tr$tip.label %in% leaf$tip)
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-***@r-project.org/
Loading...