Search code examples
rdirectorymetadatarename

Rename folders/directories based on metadata with R


I have a question that concerns renaming folders based on information in a metadataframe. Underneath you will find my directory structure and an exmaple of the .information of the metadata.

Metadataframe:

date Sample_name S_number
2021_05_20 V_3_4 S1
2021_05_20 V_9_4 S2
2021_05_20 H_13_5 S3

Directory structure:

main Directory
        |
        |___ 2021_metadata.txt
        |___ Experiment
                ├── D0
                   ├── Weather
                           |__ S1
                           |__ S2
                           
                │  └── Temperature
                           |__ S1
                           |__ S2
                └── D1
                   ├── Weather
                           |__ S1
                           |__ S2
                           |__ S3
                   └── Temperature

I'm not really familiar with R, but I think it is possible to manually rename folders with the file.rename() function of R. However, what I would like is to make a code that is able to recognize the name of the folder and rename it into the corresponding sample_name found in the metadatafile. For example the folder name is S1 and change it into V_3_4?

Is someone able to help me with this? Thank you in advance

Edit

name_file <- "./20210325_metadata_r_test_1.txt"

Metadataframe <- read.delim(name_file)


dirs <- list.dirs(path = "./experiment/", recursive = TRUE, full.names = TRUE)

if(any(dirs %in% Metadataframe$S_number)){
    for(dir in dirs[which(dirs %in% Metadataframe$S_number)]){
        shell(paste("move", dir, Metadataframe$Sample_name[which(Metadataframe$S_number == dir)]))
    }
}

Solution

  • You can use list.dirs('/path', recursive = F, full.names = F) to find the current directory names (e.g. S1, S2, ...). Then you can change the names using system(paste('mv', old_dir, new_dir))

    Example:

    main_dir
    └── experiment
        └── D0
            ├── temperature
            │   ├── S1
            │   └── S2
            └── weather
                ├── S1
                └── S2
    
    Metadataframe <- data.frame(Sample_name = c("V_3_4", "V_9_4", "H_13_5"), S_number = c("S1", "S2", "S3"))
    
    setwd("~/main_dir/experiment/D0/temperature/")
    dirs <- list.dirs('./', recursive = F, full.names = F)
    if(any(dirs %in% Metadataframe$S_number)){
        for(dir in dirs[which(dirs %in% Metadataframe$S_number)]){
            system(paste("mv", dir, Metadataframe$Sample_name[which(Metadataframe$S_number == dir)]))
        }
    }
    
    main_dir
    └── experiment
        └── D0
            ├── temperature
            │   ├── V_3_4
            │   └── V_9_4
            └── weather
                ├── S1
                └── S2
    

    You could put all of that into a for loop to do all your folders.

    Edit: To change all the directories:

    main_dir
    └── experiment
        ├── D0
        │   ├── temperature
        │   │   ├── S1
        │   │   └── S2
        │   └── weather
        │       ├── S1
        │       └── S2
        └── D1
            ├── temperature
            │   ├── S1
            │   ├── S2
            │   └── S3
            └── weather
                ├── S1
                ├── S2
                └── S3
    
    Metadataframe <- data.frame(Sample_name = c("V_3_4", "V_9_4", "H_13_5"), S_number = c("S1", "S2", "S3"))
    
    dirs <- list.dirs('~/main_dir', recursive = TRUE, full.names = TRUE)
    if(any(grepl(paste(Metadataframe$S_number, collapse = "|"), dirs))){
        for(dir in dirs[grep(paste(Metadataframe$S_number, collapse = "|"), dirs)]){
            dir_path <- sub("S[0-9]+", "", dir)
            sample <- sub(dir_path, "", dir)
            new_path <- paste0(dir_path, Metadataframe$Sample_name[which(Metadataframe$S_number == sample)])
            system(paste("mv", dir, new_path))
        }
    }
    
    main_dir
    └── experiment
        ├── D0
        │   ├── temperature
        │   │   ├── V_3_4
        │   │   └── V_9_4
        │   └── weather
        │       ├── V_3_4
        │       └── V_9_4
        └── D1
            ├── temperature
            │   ├── H_13_5
            │   ├── V_3_4
            │   └── V_9_4
            └── weather
                ├── H_13_5
                ├── V_3_4
                └── V_9_4