Arora
Arora

Reputation: 29

how to automate series of scripts in R

I am working with series of R scripts which required to run 100 times for 100 files. I am trying but couldn't able to do the task smoothly.

My initial command was :

a1 <- data2haplohh(hap_file="file1.csv", polarize_vcf=FALSE)

followed by :

a2<-subset(a1, min_maf=0.05)

followed by :

a3<-scan_hh(a2, polarized=FALSE)

followed with:

a4<-ihh2ihs(a3, freqbin=100)
write.csv(a4, "newa.csv")

I tried this but it only works for first 2 steps and through error when adding a3 onwards.

list2env(
setNames(
    lapply(
        seq(100),
        function(i) {
            subset(
                data2haplohh(hap_file = sprintf("file%s.csv", i), polarize_vcf = FALSE),
                min_maf = 0.05
            )
        }
    ), 
    paste0("a", seq(100))
),
envir = .GlobalEnv
)

Can you help me in setting up the script. It take long long time to run manually. Thankyou

Upvotes: 0

Views: 57

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145765

Try this? Can't really test without a reproducible example... and you don't even say what error you get. Nor do I know what package most of your functions come from.

do_thing = function(file, output_suffix) {
  a1 <- data2haplohh(hap_file = file, polarize_vcf = FALSE)
  a2 <- subset(a1, min_maf=0.05)
  a3 <- scan_hh(a2, polarized=FALSE)
  a4 <- ihh2ihs(a3, freqbin=100)
  write.csv(a4, paste0("new", output_suffix, ".csv")
}

for(i in 1:100) {
  do_thing(file = sprintf("file%s.csv", i), output_suffix = i)
}

Upvotes: 4

Ronak Shah
Ronak Shah

Reputation: 388907

You can add additional functions in the same lapply code.

lapply(
      seq(100),
      function(i) {
        write.csv(
          ihh2ihs(
            scan_hh(
              subset(
                data2haplohh(hap_file = sprintf("file%s.csv", i), polarize_vcf = FALSE),
                min_maf == 0.05
                ), polarized=FALSE
              ), freqbin=100
          ), sprintf('new%d.csv', i), row.names = FALSE
        )
      }
)

This should create csv files new1.csv, new2.csv to new100.csv in the working directory.

Upvotes: 1

Related Questions