Convert igraph object to a data frame in R - r

I'm working with the iGraph library and I need to run some statistical analysis on the network. I'm computing several variables using iGraph and then want to use those indicators as the dependent variable in a few regressions and the vertex attributes as the independent variables in the model.
So, I'm able to load the data, run the igraph analysis, but I'm having trouble turning the igraph object back into a data frame. I don't really need the edges to be preserved, just each vertex to be turned into an observation with the attributes serving as a column in each row.
I tried the following:
fg <- fastgreedy.community(uncompg, merges=TRUE)
z<-which.max(fg$modularity)
fgc<- community.to.membership(uncompg, fg$merges,z)
names<-array(V(uncompg)$name)
fccommunity<-array(fgc$membership)
fcresult<-as.matrix(cbind(names,fccommunity))
compg <- set.vertex.attribute(compg, "community", value=fccommunity)
uncompg<-simplify(as.undirected(compg))
hubscore<-hub.score(compg)$vector
authscore<-authority.score(compg)$vector
netdata<-as.data.frame(compg)
But it throws the following error:
cannot coerce class '"igraph"' into a data.frame
Any help or pointers would be greatly appreciated.

I am not quite sure what you are trying to do. Do you want the relationships as a data frame, or the node attribute as a data frame?
To do the former:
> compg.edges <- as.data.frame(get.edgelist(compg))
To do the latter:
> compg.df <- as.data.frame(list(Vertex=V(compg), Community=fccommunity, Hubscore=hubscore, Authscore=authscore), stringsAsFactors=FALSE)

Related

Create Venn Diagram from two DF

I'm trying to create a Venn diagram of two data frames, but am only able receive incorrect results. An example of the data sets of the same structure:
Chemical
ChemID
Oxidopamine
D016627
Melatonin
D016627
I've only received incorrect results from the following:
VennDiagram::venn.diagram(
x = list(Lewy, Park),
category.names = c("ChemID, ChemID"),
filename ="venndiagramm.png",
output=TRUE)
Ideally, I would like to export an image of number of overlapping chemicals between the two sets.
Welcome to SO! As far as I guess your data structure (two dataframes Lewy and Park, each with the column ChemID), try the following:
VennDiagram::venn.diagram(
x = list(Lewy$ChemID, Park$ChemID), # expects vectors, not dataframes
# category.names = c("ChemID, ChemID"), # see if these are rather to construct nice labels
filename ="venndiagramm.png",
output=TRUE)
You may increase the chance of a useful answer by providing minimal working data samples by dput(). Of course you can use simulated data. Try to explain what exactly did not work.
See also ? venn.diagram

Phylo correlogram in R bootstrapping error?

I am trying to create a phylo correlogram based on my data using phyloCorrelogram from the phylosignal package in order to test for presence of a phylogenetic signal. My data is in the so-called phylo4d format and is called tree.
Now, when I run phyloCorrelogram(tree), I am returned the following error:
library("phylobase")
library("ape")
library("phylosignal") # contains phyloCorrelogram()
> phyloCorrelogram(tree, ci.bs = 10, n.points = 10)
Error in boot::boot(X, function(x, z) moranTest(xr = x[z], Wr = prop.table(Wi[z, :
no data in call to 'boot'
I have already done an elaborate search on the internet to look for ways how to solve this issue, but without success.
Since the dimension of the data that I am using is too large to post it here, I have posted my data in .rda format on dropbox.
Does anyone know the flaw?
You have no data associated to your tree. Your tree is a phylo4d object which has the "tree" information but no data attached to it. You need something like that
library(phylobase)
g1 <- as(geospiza_raw$tree, "phylo4")
geodata <- geospiza_raw$data
g2 <- phylo4d(g1, geodata)
pc <- phyloCorrelogram(g2)

R - [DESeq2] - Making DESeq Dataset object from csv of already normalized counts

I'm trying to use DESeq2's PCAPlot function in a meta-analysis of data.
Most of the files I have received are raw counts pre-normalization. I'm then running DESeq2 to normalize them, then running PCAPlot.
One of the files I received does not have raw counts or even the FASTQ files, just the data that has already been normalized by DESeq2.
How could I go about importing this data (non-integers) as a DESeqDataSet object after it has already been normalized?
Consensus in vignettes and other comments seems to be that objects can only be constructed from matrices of integers.
I was mostly concerned with getting the format the same between plots. Ultimately, I just used a workaround to get the plots looking the same via ggfortify.
If anyone is curious, I just ended up doing this. Note, the "names" file is just organized like the meta file for colData for building a DESeq object from DESeqDataSetFrom Matrix, but I changed the name of the design column from "conditions" to "group" so it would match the output of PCAplot. Should look identical.
library(ggfortify)
data<-read.csv('COUNTS.csv',sep = ",", header = TRUE, row.names = 1)
names<-read.csv("NAMES.csv")
PCA<-prcomp(t(data))
autoplot(PCA, data = names, colour = "group", size=3)

Error implementing anc.clim in the Phyloclim R Package

I am attempting to use the anc.clim function in phyloclim, but am stuck on an error I don't know how to fix.
I have three items in my workspace:
etopo is a 50X14 double matrix with the first column corresponding to 50 bins of an environmental variable. Each subsequent column is labeled with a taxon name.
targetTree is an object of class phylo containing 13 taxa with tip labels corresponding with the taxa in etopo (generated by reading in a .tre file from MrBayes using read.nexus)
prunedPosteriorTrees is an object of class multiphylo containing 1000 phylogenetic trees with 13 taxa with tip labels corresponding with the taxa in etopo (generated by reading in a .t file from MrBayes using read.nexus)
I have confirmed that the taxa in all three match using geiger's treedata function.
When I go to implement anc.clim with these data, the following occurs:
> climateReconstruction <- anc.clim(targetTree, posterior = prunedPosteriorTrees, pno = etopo, n = 2)
Error in noi(old, clades, monophyletic = TRUE) :
tips are not numbered consecutively. Type '?fixTips' for help.
When I type ?fixtips, or ??fixTips for that matter, no documentation is found. I have also searched the web, and the package documentation, to no avail. Has anyone had experience with this error? What do I do?
I have solved this problem. For the aid of others:
targetTree nees to be an ultrametric tree such as one that would result from a BEAST analysis. Mr. Bayes trees are not ultrametric. The same is true for the prunedPosteriorTrees file.
fixTips no longer exists. It has been replaced with fixNodes. Using this function solves the error.

R spCbind error

I have successfully added information to shapefiles before (see my post on http://rusergroup.swansea.ac.uk/Healthmap.ashx?HL=map ).
However, I just tried to do it again with a slightly different shapefile (new local health boards for Wales) and the code fails at spCbind with a "row names not identical error"
o <- match(wales.lonlat$NEW_LABEL, wds$HB_CD)
wds.xtra <- wds[o,]
wales.ncchd <- spCbind(wales.lonlat, wds.xtra)
My rows did have different names before and that didn't cause any problems. I relabeled the column in wds.xtra to match "NEW_LABEL" and that doesn't help.
The labels and order of labels do match exactly between wales.lonlat and wds.xtra.
(I'm using Revolution R 5.0, which is built on R 2.13.2)
I use match to merge data to the sp data slot based on rownames (or any other common ID). This avoids the necessity of maptools for the spCbind function.
# Based on rownames
sdata#data=data.frame(sdata#data, new.df[match(rownames(sdata#data), rownames(new.df)),])
# Based on common ID
sdata#data=data.frame(sdata#data, new.df[match(sdata#data$ID, new.df$ID),])
# where; sdata is your sp object and new.df is a data.frame object that you want to merge to sdata.
I had the same error and could resolve it by deleting all other data, which were not actually to be added. I suppose, they confused spCbind because the matching wanted to match all row-elements, not only the one given. In my example, I used
xtra2 <- data.frame(xtra$ID_3, xtra$COMPANY)
to extract the relevant fields and fed them to spCbind afterwards
gadm <- spCbind(gadm, xtra2)

Resources