Drawing a graph and extracting structure using R - r

For a production planning software I need a specificly structured Excel input. Therefore, I was looking for a way to make it easy for non-tech people to generate such an input. To achieve that, I thought it might be easiest for users to draw their production network as graph and I could use the resulting graphml output to derive the necessary information and transform them to the format needed.
When looking for an appropriate and easy software I found yEd. To start with I generated a very simple network with 2 sites and 1 part being transfered and exported the graphml.
When I try to read the graphml in R using igraph or casualeffect package the names are lost and I get a lot of warnings.
For igraph the plot also looks incomplete.
What am I doing wrong? Should I use a different software or package? Any recommendations?
I hope my description is sufficient (that is my first post on stackoverflow)
Thank you a lot.
### 1st try
library(igraph)
net1<-read.graph("Graph test.graphml", format = "graphml")
plot(net1)
### 2nd try
library(causaleffect)
net2 <- parse.graphml("Graph test.graphml", use.names = TRUE)
plot(net2)
Link for Graph Test on yEd
Link for graphml file

Related

R Social Network Analysis/Data Manipulation Question: Reading in .edges, .circles, .egofeat, .feat, and .featnames files

So I'm working with a network dataset from Stanford's SNAP Datasets and "SNAP" has wrappers for Python and C++ but not R - however, the data is still usable since I believe it's a mix of CSV files.
I can actually read in the .edges file and form an igraph object but want to read in the other files, get the attributes & add those attributes to the igraph object for analysis. I'm just confused on how to work with the .circles, .egofeat, .feat, and .featnames files since the documentation on the dataset is very scarce. Hoping someone has worked with the dataset in R or even another language and has any pointers to get started.
Thank you!

How to create Newick tree format from raw morphology data in R on Mac OSX

I'm trying to teach myself how to do phylogenetics for historical linguistics in R. I've found a public data set (https://www.cs.rice.edu/~nakhleh/CPHL/IEDATA_112603), and I want to get a Newick format tree from it, so that I can visualize it following these instructions: https://www.r-phylo.org/wiki/HowTo/InputtingTrees. I'm running R 3.4.1 on Max OS 10.12.6.
Here's what I've done so far. I copied the data and used R and a text editor to transform it into a Nexus data file. Since Nexus (as I understand it) can't distinguish between the individual characters 1 and 2, and the combined character 12, I turned all values in the original data set over 9 into letters of the alphabet, in sequence (a-q). Anyone can download it from here: https://ucla.box.com/s/i4fbeagcw8lombg3xuhczfk3h0y7v54m
The problem is, I can't find any instructions or code or guidance to interpret the raw data as a tree. I've found one Python script (Convert csv to Newick tree), but I don't know Python. Can anyone point me in the direction of the right software/library/tutorial, or otherwise help me figure out what my next step should be?
I finally found a colleague who could help me. I did not need to convert the data to Newick or Nexus to make a tree from it, I needed to convert it to phydat (see Phangorn package for R) to make a tree from it. What I did was to use the as.phydat() function in the Phangorn package for R to convert the linguistic data into "phylogenetic data." The way that I did this was by specifying "type = USER" in the function, which let me define my own levels for the data. There's a more detailed example at cran.r-project.org/web/packages/phangorn/vignettes/…. Then, I could create trees from it using the regular Phangorn functions.
Using Phangorn might be a good approach in R (have a look at the "Constructing phylogenetic trees" vignette).
browseVignettes(package = "phangorn")
However, to properly infer the tree, I would advise you to use a "proper" phylogenetic inference software with more options (phangorn is excellent for explorative analysis but can be limited).
I suggest you use the BEAST software that has an entire tutorial dedicated to phylogenetic linguistics (https://www.luke.maurits.id.au/files/research/papers/beastling.pdf). Luke Maurits tutorial on github is really well explained (https://github.com/lmaurits/BEASTling/blob/master/docs/tutorial.rst).
Also, regarding your problem with ambiguous character states in your NEXUS file (i.e. state 12 for 1 and 2) you can code them in the nexus file as (12). For example this is a valid NEXUS format:
#NEXUS
BEGIN DATA;
DIMENSIONS NTAX=2 NCHAR=3;
MATRIX
t1 1(12)2
t2 111
;
END;

Saving sequence tree from GraphViz

I'm new to TraMineR and sequence analysis in general. I am working on a project related to retention and recruitment of educational leaders and finding TraMineR to be very useful. This may be a simple thing (and somewhat unimportant), but I cannot seem to figure out how to name or direct the sequence tree created by GraphViz within the TraMinR package. Right now, my code is:
wardTree=as.seqtree(wardCluster,seqdata=retain.seq,diss=retain.dst,ncluster=15)
seqtreedisplay(wardTree,type="d",border=NA,showdepth=TRUE)
It produces a great graphic, but with a random file name that I cannot relocate if I accidentally close the graphic.
My main goal is to be able to uniquely name and save these graphs and pull into an R Markdown for the full project. To this point, the only thing I cannot pull in is the seqtree graphic.
Well, guess I figured it out! I didn't realize that the order in which filename command mattered...so...what seems to work out beautifully is:
wardTree=as.seqtree(wardCluster,seqdata=retain.seq,diss=retain.dst,ncluster=15)
seqtreedisplay(wardTree,type="d",border=NA,showtree=TRUE,showdepth=TRUE,filename="retaintree.png",imageformat="png")
And to incorporate the file in the Markdown i use the command
```
![](C:\Users\myname\Desktop\retaintree.png)
```{r fig.width=8.5, fig.height=11}
Hope this helps someone else along the way!

Write igraph clustering to file

I am currently testing various community detection algorithms in the igraph package to compare against my implementation.
I am able to run the algorithms on different graphs but I was wondering if there was a way for me to write the clustering to a file, where all nodes in one community are written to one line and so on. I am able to obtain the membership of each node using membership(communities_object) and write that to a file using dput() but I don't know how to write it the way I want.
This is the first time I am working with R as well. I apologize if this has been asked before.
This does not have to do much with igraph, the clustering is given by a simple numeric vector. See ?write.
write(membership(communities_object), file="myfile", ncolumns=1)
write(communities_object$membership, file="myfile", ncolumns=1) also work

How can I create a dendrogram in R using pre-clustered data created elsewhere?

I have clustering code written in Java, from which I can create a nested tree structure, e.g. the following shows a tiny piece of the tree where the two "isRetired" objects were clustered in the first iteration, and this group was clustered with "setIsRequired" in the fifth iteration. The distances between the objects in the clusters are shown in parentheses.
|+5 (dist. = 0.0438171125324851)
|+1 (dist. = 2.220446049250313E-16)
|-isRetired
|-isRetired
|-setIsRetired
I would prefer to present my results in a more traditional dendrogram style, and it looks like R has some nice capabilities, but because I know very little about R, I am unclear on how to take advantage of them.
Is it possible for me to write out a tree structure to a file from Java, and then, with a few lines of R code, produce a dendrogram? From the R program, I'd like to do something like:
Read from a file into a data structure (an "hclust" object?)
Convert the data structure into a dendrogram (using "as-dendrogram"?)
Display the dendrogram using "plot"
I guess the question boils down to whether R provides an easy way of reading from a file and converting that string input into an (hclust) object. If so, what should the data in the input file look like?
I think what you are looking for is phylog. You can print your tree in a file in Newick notation, parse that out and construct a phylog object which you can easily visualize. The end of the webpage gives an example of how to do this. You also might want to consider phylobase. Although you don't want the entire functionality provided by these packages, you can piggyback on the constructs they use to represent trees and their plotting capabilities.
EDIT: It looks like a similar question to yours has been asked before here providing a simpler solution. So basically the only thing you will have to code here is your Newick parser or a parser for any other representation you want to output from Java.
The ape (Analysis of Phylogenetics and Evolution) package contains dendrogram drawing functionality, and it is capable of reading trees in Newick format. Because it is an optional package, you'll need to install it. It is theoretically easy to use, e.g. the following commands produce a dendrogram:
> library("ape")
> gcPhylo <- read.tree(file = "gc.tree")
> plot(gcPhylo, show.node.label = TRUE)
My main complaint thus far is that there is little diagnostic information when there is trouble with the syntax of the file containing the tree information in Newick format. I've had success reading these same files with other tools (which in some cases, may be because the tools are forgiving of certain faults in the syntax).
You can also produce a dendrogram using the phylog package as shown below.
> library(ade4)
> newickString <- system("cat gc.tree", intern = TRUE)
> gcPhylog <- newick2phylog(newickString)
> plot(gcPhylog, clabel.nodes=1)
Both can work with trees in Newick format and both have many plotting options.

Resources