Angelo
Angelo

Reputation: 5059

automating the files location in R

I am trying to get my head around an R code and to automate it so that I don't have to specify the file location every single time but only the location of the parent folder. Within the parent folder there will be n number of child folders housing the files of need.

Presently, what I am doing is to manually specify the locations of all the child folders.

The code goes like this:

First a function:

```{r include = T}

        loadSC <- function(path){
        sc <- read10XResults(path)


        Genes <- as.character(rowData(sc)[startsWith(rowData(sc)$symbol, "mt-"),]$id)

        isSpike(sc, "MT") <- rownames(sc) %in% Genes

        sc <- calculateQCMetrics(sc, 
                                  feature_controls = list(
                                    MT =  isSpike(sc, "MT")
        ))
    }

``` 

pointing to the files (This is the want, where I want it to automatically identify the number of child folders. Here, we have 5 child folders K1,K2,K3,K4,K5 and look for files within them)

```{r include = T}
        sc_1 = loadSC("Sample/K1/outs/matrices/mm10/")
        sc_2 = loadSC("Sample/K2/outs/matrices/mm10/")
        sc_3 = loadSC("Sample/K3/outs/matrices/mm10/")
        sc_4 = loadSC("Sample/K4/outs/matrices/mm10/")
        sc_5 = loadSC("Sample/K5/outs/matrices/mm10/")

    ```

Also, i have similar chunk below this one that will also need to be looped in.

```{r include = T}
    metadata(sc_1)["name"] <- "iM-1"
    metadata(sc_2)["name"] <- "iM-2"
    metadata(sc_3)["name"] <- "iM-3"
    metadata(sc_4)["name"] <- "iM-4"
    metadata(sc_5)["name"] <- "iM-5"
```

I am doing this in Rmarkdown.

A little guidance or a working example will really be helpful.

Thank you in advance

Upvotes: 0

Views: 169

Answers (1)

puraky
puraky

Reputation: 11

Imagine your data is stored like:

  • Sample (parent folder)
  • --> /K1 (subfolder)
  • --> /K2
  • --> /K3
  • ...

An easy way to access the data is:

# This function gets all the folder paths inside "Sample" 
paths <- list.dirs(path = "Sample", recursive = FALSE)
# To access the data, you should get used to lapply()
data <- lapply(paths, loadSC)
# To keep an overview where the data is from
names(data) <- paths

As a result you get a comprehensive named list with your desired data. You might have to adapt your loadSC(), but in future you only have to change your parent folder to analyze another data set. Cheers

Upvotes: 1

Related Questions