igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] network neighborhood


From: Gábor Csárdi
Subject: Re: [igraph] network neighborhood
Date: Thu, 7 Oct 2010 10:09:14 +0200

Hi Anupam,

here is your code, properly indented, for readability:

library("igraph")
path <- "where-the-files-are"
files <- dir(path)
for(i in 1:length(files)) {
  file <- paste(path,"/",files[i],sep="")
  g<-read.graph(file,format="ncol", directed=FALSE, weights=FALSE)
  g<-simplify(g)
  subgraphs<-graph.neighborhood(g,1)
  for(j in subgraphs){
    gr_name <- paste(path, files[i], j, sep="")
    write.graph(j, file=gr_name, format="ncol")
  }
}

I am not sure how you compared the results, but I cannot even run
this, it has an error. In your "for (j in subgraphs)" loop you iterate
over the graphs, so you cannot use 'j' in paste(), 'j' is the graph
itself. Plus '1:length(files)' does no work it the directory in empty,
use 'seq_along(files)' instead. Here is the correct code (again):

library("igraph")
path <- "where-the-files-are"
files <- dir(path)
for(i in seq_along(files)) {
  file <- paste(path, "/", files[i], sep="")
  g <- read.graph(file, format="ncol", directed=FALSE, weights=FALSE)
  g <- simplify(g)
  subgraphs <- graph.neighborhood(g, 1)
  for (j in seq_along(subgraphs)){
    gr_name <- paste(path, files[i], j, sep="")
    write.graph(subgraphs[[j]], file=gr_name, format="ncol")
  }
}

I am not sure what you mean by being not the same if done manually,
they look pretty much the same for me:

> gg <- read.graph(paste(path, files[1], 3, sep=""), format="ncol")
> gg
Vertices: 8
Edges: 7
Directed: FALSE
Edges:

[0] 'ENST00000260600' -- 'ENST00000305762'
[1] 'ENST00000305762' -- 'ENST00000327367'
[2] 'ENST00000305762' -- 'ENST00000361099'
[3] 'ENST00000305762' -- 'ENST00000252699'
[4] 'ENST00000305762' -- 'ENST00000356978'
[5] 'ENST00000305762' -- 'ENST00000360472'
[6] 'ENST00000305762' -- 'ENST00000262053'
> graph.neighborhood(g, 1, 2)[[1]]            # g was the original graph
Vertices: 8
Edges: 7
Directed: FALSE
Edges:

[0] 'ENST00000260600' -- 'ENST00000305762'
[1] 'ENST00000305762' -- 'ENST00000360472'
[2] 'ENST00000305762' -- 'ENST00000262053'
[3] 'ENST00000305762' -- 'ENST00000327367'
[4] 'ENST00000305762' -- 'ENST00000361099'
[5] 'ENST00000305762' -- 'ENST00000356978'
[6] 'ENST00000305762' -- 'ENST00000252699'
>

Don't forget that vertex ids start with 0, so the file with '3'
appended to its name has the neighborhood of vertex 2.

I found a bug in reading ncol files, however. Trying to read an empty
file gives an error and you will need to handle this case separately
when reading back the files. (If you ever want to read it back.)

Best,
Gabor

On Mon, Oct 4, 2010 at 3:36 PM, anupam sinha <address@hidden> wrote:
>
>
> Hi Gabor,
>                   The subgraphs obtained when I run the code are different
> from what I obtain when I run graph.neighborhood on individual nodes
> manually. For example :
> With the code for third node
> :
> Manually for third node :
>
> ENST00000304808
> ENST00000228306
> [[1]]
>
> Vertices: 8
>
> Edges: 7
>
> Directed: FALSE
>
> Edges:
>
>
> [0] 'ENST00000260600' -- 'ENST00000305762'
>
> [1] 'ENST00000305762' --'ENST00000360472'
>
> [2] 'ENST00000305762' --'ENST00000262053'
>
> [3] 'ENST00000305762' --'ENST00000327367'
>
> [4] 'ENST00000305762' --'ENST00000361099'
>
> [5] 'ENST00000305762' -- 'ENST00000356978'
>
> [6] 'ENST00000305762' -- 'ENST00000252699'
> Attached herewith is the file on which I did these operations. Also attached
> is the code file.Where am I going wrong ? Thanks in advance
>
> Regards,
>
> Anupam
>
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]