pat<-c('comp','mcar','mcar0','mcar1','mcar2','mar','mar0','mar1','mar2','mnar','mnar0','mnar1','mnar2','mnar0-1','mnar1-1','mnar2-1')

## create folders
for (mis in pat){
  system(paste('mkdir ',mis, sep=''))
  for (i in 1:3){
    system(paste('mkdir ',mis, '/', i, sep=''))
  }
}

## copy bmem to the folders
for (mis in pat){
  for (i in 1:3){
    system(paste('cp bmem ',mis, '/', i,'/.', sep=''))
  }
}

## generate submit script

pat1<-c('mcar','mar','mnar')

for (mis in pat1){

     filename <- paste('/afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/BMEMnew/', mis, '/runsim.R', sep='')
     cat("gen.batch<-function(datafile){",'\n', file=filename)
     cat(" filename<-'batch.txt' ",'\n', file=filename)
     cat(' cat("out.txt\n", file=filename) ','\n', file=filename)
     cat(' cat(datafile, file=filename, append=T) ','\n', file=filename)
     cat(' cat("\n", file=filename, append=T) ','\n', file=filename)
     cat(' cat("99999\n", file=filename, append=T) ','\n', file=filename)
     cat(' cat(".95\n", file=filename, append=T) ','\n', file=filename)
     cat(' cat(runif(1), file=filename, append=T) ','\n', file=filename)
     cat(' cat("\n", file=filename, append=T) ','\n', file=filename)
     cat(' cat("1000\n", file=filename, append=T) ','\n', file=filename)
     cat(' } ','\n', file=filename)
     
     
     cat('pat<-"',mis,'"\n', file=filename, sep='')
     cat('rate<-',rate[i],'\n', file=filename, append=T)
     cat("for (r in rate){",'\n', file=filename, append=T)
     cat("  resfile <- paste(pat ,'-',r,'.txt', sep='')",'\n', file=filename, append=T)
     cat("  for (i in 1:1000){",'\n', file=filename, append=T)
     cat("    datafile <-paste('cp ~/Private/bootmedem/mbr/newdata/',pat,'/','data-', i,'-',r,'.txt data.txt',sep='')",'\n', file=filename, append=T)
     cat("    system(datafile)",'\n', file=filename, append=T)
     cat("    system('sas mi.sas')",'\n', file=filename, append=T)
     cat("    if (file.exists('bcci.txt')){",'\n', file=filename, append=T)
     cat("      temp<-scan('bcci.txt')",'\n', file=filename, append=T)
     cat("      cat(c(i, temp), file=resfile, append=T)",'\n', file=filename, append=T)
     cat('      cat("\\n", file=resfile,append=T)','\n', file=filename, append=T)
     cat("      system('rm -f data.txt bcci.txt mi.log mi.lst')}}}",'\n', file=filename, append=T)
     
     ## copy sas program to the folder 
     cpsas <- paste('cp mi.sas /afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MInew/',mis,'/',i,'/mi.sas', sep='')
     system(cpsas)
     
          ## generate submission scripts
     cpsub<-paste('cp qsub.sub /afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MInew/',mis,'/',i,'/',mis,'-',i,'.sub', sep='')
     system(cpsub) 
           
  }


pat2<-c('mcar0','mcar1','mcar2','mar0','mar1','mar2','mnar0','mnar1','mnar2','mnar0-1','mnar1-1','mnar2-1')

for (mis in pat2){
 if (substr(mis,1,3)=="mar"){
      rate<-c(1.5,4,9)
    }else{
      rate<-c(.1,.2,.4)
    }

    for (i in 1:3){
     filename = paste('/afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MInew/',mis,'/',i,'/runsim.R', sep='')
     cat('pat<-"',mis,'"\n', file=filename, sep='')
     cat('rate<-',rate[i],'\n', file=filename, append=T)
     cat("for (r in rate){",'\n', file=filename, append=T)
     cat("  resfile <- paste(pat ,'-',r,'.txt', sep='')",'\n', file=filename, append=T)
     cat("  for (i in 1:1000){",'\n', file=filename, append=T)
     cat("    datafile <-paste('cp ~/Private/bootmedem/mbr/newdata/',pat,'/','data-', i,'-',r,'.txt data.txt',sep='')",'\n', file=filename, append=T)
     cat("    system(datafile)",'\n', file=filename, append=T)
     cat("    system('sas mi.sas')",'\n', file=filename, append=T)
     cat("    if (file.exists('bcci.txt')){",'\n', file=filename, append=T)
     cat("      temp<-scan('bcci.txt')",'\n', file=filename, append=T)
     cat("      cat(c(i, temp), file=resfile, append=T)",'\n', file=filename, append=T)
     cat('      cat("\\n", file=resfile,append=T)','\n', file=filename, append=T)
     cat("      system('rm -f data.txt bcci.txt mi.log mi.lst')}}}",'\n', file=filename, append=T)
     ## copy sas program to the folder 
     cpsas <- paste('cp mi.aux.sas /afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MInew/',mis,'/',i,'/mi.sas', sep='')
     system(cpsas)
          ## generate submission scripts
     cpsub<-paste('cp qsub.sub /afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MInew/',mis,'/',i,'/',mis,'-',i,'.sub', sep='')
     system(cpsub) 
     }
           
  }


## Generate submission scripts
pat<-c('mcar','mcar0','mcar1','mcar2','mar','mar0','mar1','mar2','mnar','mnar0','mnar1','mnar2','mnar0-1','mnar1-1','mnar2-1')

filename<-'allsub.sub'
  for (mis in pat){
for (i in 1:3){

    folder<-paste('cd /afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MInew/',mis,'/',i,'/', sep='')
    cat(folder, '\n', file=filename, append=T)
    cat('qsub *.sub', '\n', file=filename, append=T)  
  }  
}

filename<-'rm.sub'
pat<-c('mcar','mcar0','mcar1','mcar2','mar','mar0','mar1','mar2','mnar','mnar0','mnar1','mnar2','mnar0-1','mnar1-1','mnar2-1')

for (i in 1:3){
  for (mis in pat){
    folder<-paste('cd /afs/crc.nd.edu/user/z/zzhang4/Private/bootmedem/mbr/MI/',mis,'/',i,'/', sep='')
    cat(folder, '\n', file=filename, append=T)
    cat('rm -f *.*', '\n', file=filename, append=T)  
  }  
}