0

I have a data frame (although I don't think that the kind of source object I have matters) that has a list of 51 genes that I would like to get the pathway information for (using rWikiPathways, but, again, this can be applied to much more general issues too).

I would like to create an object with the information retrieved from rWikiPathways for each of these 51 genes.

I have been trying to use for loops, assign and paste functions but nothing is really working as I would like it to, so I ended up copying this line of code 51 times:

pathway.gene.1 <- findPathwaysByXref(genes$Encode.ID[1], "En")

pathway.gene.2 <- findPathwaysByXref(genes$Encode.ID[2], "En")

...

pathway.gene.51 <- findPathwaysByXref(genes$Encode.ID[51], "En")

Note that "...gene.1" and "Encode.ID[1]" all go from 1 to 51.

There has to be a way to do this automagically... I am a beginner and can't really make it happen.

Any tips out there?

Cheerio, Guido

  • 1
    `pathway.gene <- lapply(genes$Encode.ID, findPathwaysByXref, "En")`? – r2evans Dec 17 '20 at 16:45
  • 2
    I prefer to work with a list-of-frames instead of individual, identically-purposes/structured objects in an environment. Take a quick look at https://stackoverflow.com/a/24376207/3358227, and from that you may see the value of using `lapply` and capturing all return values into a single `list`. My `lapply` above can be improved a little in order to *name* the entries (if that's desired), but otherwise is as generic as I recommend. From here, whatever you tend to do on one `pathway.gene.#` object, you can do on all of them with `lapply(pathway.gene, function(pwg) ...)`. – r2evans Dec 17 '20 at 16:47
  • Thanks! It is very similar to what @ThomasIsCoding said, making a list of lists, indeed with naming each of the objects within it. – GTrentadue Dec 17 '20 at 17:09

1 Answers1

1

Maybe you need list2env

list2env(setNames(Vectorize(findPathwaysByXref)(genes$Encode.ID,"En"),paste0("pathway.gene.",1:51)),.GlobalEnv)

or

list2env(setNames(lapply(genes$Encode.ID, findPathwaysByXref, "En"),paste0("pathway.gene.",1:51),.GlobalEnv)

Explanation: The code setNames(lapply(genes$Encode.ID, findPathwaysByXref, "En"),paste0("pathway.gene.",1:51) gives you named list of values obtained from findPathwaysByXref, and list2env helps you produce objects with the given names from the list to global environment.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • 1
    However, then you've created a new problem: with `pathway.gene.1`, `pathway.gene.2`, `pathway.gene.3`, ..., `pathway.gene.51` in your environment, how do you work with them? You can mess a lot with `get` and `assign`, but keeping them in a list (skipping the `list2env` part of this answer) is usually much easier for whatever your next steps are. – Gregor Thomas Dec 17 '20 at 16:56
  • Hey this was great help! Really what I was looking for, because now I have a list of lists and can eventually look into that for pulling individual data and such. What the list2env makes is perhaps somewhat too much for what I might need it for, indeed. Thanks ever so much! – GTrentadue Dec 17 '20 at 17:07
  • 1
    @GregorThomas Yes, that's true, it is better to have all those objects in the list rather than in the global environment for easy use – ThomasIsCoding Dec 17 '20 at 19:10
  • @GTrentadue You are welcome! Maybe you can just skip the `list2env` – ThomasIsCoding Dec 17 '20 at 19:11