[adegenet-forum] chooseCN question

Jombart, Thibaut t.jombart at imperial.ac.uk
Tue Mar 3 11:04:40 CET 2009


Stephen Douglas Petersen wrote:

Hi again,
> Thanks for the insight on the last question - I have another.
Great !
> I would like to know how to save a modified connection network. Is there a way to export a modified graph (or an unmodified one for that matter) as an adjacency matrix or edge list? I think the network is stored as a nb object - which is an edgelist (?) but I can't seem to save it in a way that will let me reuse it in a later session.
nb objects can be converted toward different other formats. I think for most uses in spatial statistics, storing info in nb objects make sense since it is the basic class implemented in spdep.

Several functions are in "nb2..." where '...' is the target class.
Type 'nb2' +tab +tab to have a list:
> nb2
nb2blocknb  nb2lines    nb2listw    nb2mat      nb2neig     nb2WB      

The result depends on which package is loaded, of course (here, spdep and ade4). For instance, nb2listw converts to a list of spatial weights; nb2mat converts to an adjacency matrix,...

Oddly enough, I cannot find a convertion toward graphNEL class from the graph package. This is the class to use for more serious graph operations. But both classes are quite close anyway, since they both rely on lists of neighbours. To do such conversion, just use:
##
> library(adegenet)
> library(graph)
> example(chooseCN) #[output hidden]

> cn1
Neighbour list object:
Number of regions: 17
Number of nonzero links: 82
Percentage nonzero weights: 28.3737
Average number of links: 4.823529
> str(cn1)
List of 17
 $ : int [1:5] 5 9 12 16 17
 $ : int [1:4] 5 8 9 10
 $ : int [1:5] 6 11 14 16 17
 $ : int [1:5] 10 12 13 15 16
 $ : int [1:6] 1 2 6 7 9 17
 $ : int [1:5] 3 5 7 14 17
 $ : int [1:4] 5 6 11 14
 $ : int [1:4] 2 9 10 12
 $ : int [1:5] 1 2 5 8 12
 $ : int [1:4] 2 4 8 12
 $ : int [1:6] 3 7 13 14 15 16
 $ : int [1:6] 1 4 8 9 10 16
 $ : int [1:4] 4 11 15 16
 $ : int [1:4] 3 6 7 11
 $ : int [1:3] 4 11 13
 $ : int [1:7] 1 3 4 11 12 13 17
 $ : int [1:5] 1 3 5 6 16
 - attr(*, "region.id")= chr [1:17] "1" "2" "3" "4" ...
 - attr(*, "class")= chr "nb"
 - attr(*, "tri")= logi TRUE
 - attr(*, "call")= language tri2nb(coords = xy)
 - attr(*, "sym")= logi TRUE
 - attr(*, "xy")= num [1:17, 1:2] 263 184 391 459 183 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:17] "P01" "P02" "P03" "P04" ...
  .. ..$ : chr [1:2] "x" "y"


> myList = cn1[1:length(cn1)]
> names(myList) = attr(cn1,"region.id")


> myGraph=new("graphNEL", nodes=names(myList), edgeL=myList) # conversion to graphNEL
> myGraph
A graphNEL graph with undirected edges
Number of Nodes = 17
Number of Edges = 41

And here you go with your graphNEL object. The class is (unusually well) documented in graph and RBGL.

Best,

Thibaut.
 
>  
> Thanks
> Stephen
>
> Stephen D. Petersen, M.Sc. Ph.D.
> Visiting Fellow, Fisheries & Oceans Canada
> Winnipeg, MB
> E-mail:
> stephen.petersen at dfo-mpo.gc.ca
> stephenpetersen at trentu.ca
> spetersen at nrdpfc.ca
> <http://sites.google.com/site/stephendpetersen/>
> <http://people.trentu.ca/stephenpetersen/>
> <http://ace.acadiau.ca/science/biol/Stephen/stephen_dp_home.htm>
> _______________________________________________
> adegenet-forum mailing list
> adegenet-forum at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/adegenet-forum
>
>


-- 
######################################
Dr Thibaut JOMBART
MRC Centre for Outbreak Analysis and Modelling
Department of Infectious Disease Epidemiology
Imperial College - Faculty of Medicine
St Mary’s Campus
Norfolk Place
London W2 1PG
United Kingdom
Tel. : 0044 (0)20 7594 3658
t.jombart at imperial.ac.uk
http://biomserv.univ-lyon1.fr/%7Ejombart/
http://adegenet.r-forge.r-project.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.r-forge.r-project.org/pipermail/adegenet-forum/attachments/20090303/6e0c0a28/attachment.htm 


More information about the adegenet-forum mailing list