diff PLIDflow/scripts/pdbqtcorrector2step.R @ 0:6fcfa4756040 draft

Uploaded
author bitlab
date Tue, 14 Jan 2020 06:09:42 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PLIDflow/scripts/pdbqtcorrector2step.R	Tue Jan 14 06:09:42 2020 -0500
@@ -0,0 +1,1127 @@
+#pdbqtcorrector2step.R prepare the out PDBQT file from pdbqtcorrector.R to run Vina 
+
+#!/usr/bin/env Rscript      
+args = commandArgs(trailingOnly=TRUE)
+
+if(length(args) < 2){
+  stop("USE: Rscript pdbqtcorrector.R <receptor_name> <galaxy_file_pdbqt> <receptor_types>")
+}
+
+#Argument definition
+receptor_name <- args[1]
+receptor_types_file_out <- args[3]
+
+#Scan file
+original_file <- scan(args[1], what = character(), quiet = TRUE, na.strings = "NULLAJSKAJSFL")
+
+#13FEB2019
+#In order to avoid col 1 and col 2 fussion, split by characters all words. Then paste characters fron col 1 in a word, and those for col 2, rest columns change by " "
+
+buscador <-  c()
+for(of in 1:length(original_file)){
+  buscador <-  c(buscador,strsplit(original_file[of], ""))
+}
+
+fix_row_1 <- 0
+for(off in 1:length(buscador)){
+  if(buscador[[off]][1] == "A"){
+    if(length(buscador[[off]]) >= 3){
+      if(buscador[[off]][2] == "T"){
+        if(buscador[[off]][3] == "O"){
+          if(buscador[[off]][4] == "M"){
+            fix_row_1 <- fix_row_1 + 1
+            buscador[[off]][1] <- paste(buscador[[off]][1], buscador[[off]][2], buscador[[off]][3], buscador[[off]][4], sep = "")
+            if(length(buscador[[off]]) == 4){
+              for(off1 in 2:length(buscador[[off]])){
+                buscador[[off]][off1] <- ""
+              }
+              for(qu in 1:(length(buscador[[off]])-1)){
+                buscador[[off]] <- buscador[[off]][-length(buscador[[off]])]
+                
+                
+              }
+            }
+            if(length(buscador[[off]]) > 4){
+              var_aux <- c()
+              for(off1 in 5:length(buscador[[off]])){
+                var_aux <- c(var_aux, paste(buscador[[off]][off1]))
+              }
+              termino <- var_aux[1]
+              for(ofu in 2:length(var_aux)){
+                termino <- paste(termino, var_aux[ofu], sep = "") 
+              }
+              
+              buscador[[off]][2] <- termino
+              
+              for(off1 in 3:length(buscador[[off]])){
+                buscador[[off]][off1] <- ""
+              }
+              for(qu in 2:(length(buscador[[off]])-1)){
+                buscador[[off]] <- buscador[[off]][-length(buscador[[off]])]
+                
+                
+              }
+            }
+          }
+        }    
+      }
+    }
+  }
+}
+#print(fix_row_1)
+
+
+fix_row_2 <- 0
+for(off in 1:length(buscador)){
+  if(buscador[[off]][1] == "H"){
+    if(length(buscador[[off]]) >= 3){
+      if(buscador[[off]][2] == "E"){
+        if(buscador[[off]][3] == "T"){
+          if(buscador[[off]][4] == "A"){
+            if(buscador[[off]][5] == "T"){
+              if(buscador[[off]][6] == "M"){
+                fix_row_2 <- fix_row_2 + 1
+                buscador[[off]][1] <- paste(buscador[[off]][1], buscador[[off]][2], buscador[[off]][3], buscador[[off]][4], buscador[[off]][5], buscador[[off]][6], sep = "")
+                if(length(buscador[[off]]) == 6){
+                  for(off1 in 2:length(buscador[[off]])){
+                    buscador[[off]][off1] <- ""
+                  }
+                  for(qu in 1:(length(buscador[[off]])-1)){
+                    buscador[[off]] <- buscador[[off]][-length(buscador[[off]])]
+                    
+                    
+                  }
+                }
+                if(length(buscador[[off]]) > 6){
+                  var_aux <- c()
+                  for(off1 in 7:length(buscador[[off]])){
+                    var_aux <- c(var_aux, paste(buscador[[off]][off1]))
+                  }
+                  termino <- var_aux[1]
+                  for(ofu in 2:length(var_aux)){
+                    termino <- paste(termino, var_aux[ofu], sep = "") 
+                  }
+                  
+                  buscador[[off]][2] <- termino
+                  
+                  for(off1 in 3:length(buscador[[off]])){
+                    buscador[[off]][off1] <- ""
+                  }
+                  
+                  for(qu in 2:(length(buscador[[off]])-1)){
+                    buscador[[off]] <- buscador[[off]][-length(buscador[[off]])]
+                  }
+                }
+              }  
+            }
+          }  
+        }    
+      }
+    }
+  }
+}
+#print(fix_row_2)
+
+fix_row_3 <- 0
+for(off in 1:length(buscador)){
+  if(buscador[[off]][1] == "T"){
+    if(length(buscador[[off]]) >= 3){
+      if(buscador[[off]][2] == "E"){
+        if(buscador[[off]][3] == "R"){
+          fix_row_3 <- fix_row_3 + 1
+        }    
+      }
+    }
+  }
+}
+#print(fix_row_3)
+
+final_row <-  fix_row_1 + fix_row_2 + fix_row_3
+
+#counter <- final_row
+#print(counter)
+
+# At this point roots "ATOM", "HETATM" and "TER" are isolated in case fusion. This step component differrent for those are pasted
+for(off in 1:length(buscador)){
+  if(as.character(buscador[[off]][1]) != "ATOM" && buscador[[off]][1] != "HETATM"){
+    #print(off)
+    pegado <- buscador[[off]][1]
+    for(fus in 2:length(buscador[[off]])){
+      if(length(buscador[[off]]) > 1){
+        pegado <- paste(pegado, buscador[[off]][fus], sep = "")
+        buscador[[off]][1] <- pegado
+      }
+    }
+    if(length(buscador[[off]]) > 1){
+      for(qu in 1:(length(buscador[[off]])-1)){
+        buscador[[off]] <- buscador[[off]][-length(buscador[[off]])]
+      }
+    }
+  }
+}
+
+
+# strsplit applied to the original_file return a list format. In this steps it is for changing list for vector format for buscador varible
+
+total_original_file <- c()
+for(si in 1:length(buscador)){
+  total_original_file <- c(total_original_file, buscador[[si]])
+}
+#13feb2019
+
+original_file <- total_original_file
+
+#Starts pdbqtcorrector
+
+#Calculate first root word and "TER"
+for(e in 1:length(original_file)){
+  if(original_file[e] == "ATOM" || original_file[e] == "HETATM" || original_file[e] == "TER"){
+    k <- e
+    break
+  }
+}
+
+#Calculate the row number for table which conteins aligned x,y,z coordenates 
+counter <- 0
+for(c in e:length(original_file)){
+  if(original_file[c] == "ATOM" || original_file[c] == "HETATM" || original_file[c] == "TER"){
+    counter <- counter + 1
+  }
+}
+
+#Table to be fills in named tabla_ordenados
+tabla_ordenados <- matrix(1, nrow = (counter), ncol = 13)
+
+#Calculate TER times and make a vector
+ele_ter_vector <- 1
+ter_vector <- c()
+k_position <- c()
+#ter_pos <- 0 #21sep2018
+for(o in 1:(length(original_file))){
+  if(original_file[o] =="TER"){
+    k_position <- c(k_position, o)
+    ter_pos <- o #21sep
+    for(g in o:(o+4)){
+      ter_vector <- c(ter_vector,original_file[g] )
+    }
+    
+  }
+}
+
+
+#Fill in the tabla_ordenados with values scaned until counter-1 since last row (ter) has diferent length and this could give you back an error
+for(i in 1:(counter-1)){
+  for(j in 1:13){
+    tabla_ordenados[i,j] <- original_file[k]
+    if(tabla_ordenados[i,13]=="ATOM" || tabla_ordenados[i,13]== "HETATM"|| tabla_ordenados[i,13]=="TER"){
+      corte <- c()
+      for(v in 4:12){
+        corte <- c(corte,tabla_ordenados[i,v])
+      }
+      coo_corte <- 1
+      tabla_ordenados[i,4] <- ""
+      for(m in 5:13){
+        tabla_ordenados[i,m] <- corte[coo_corte]
+        coo_corte <- coo_corte + 1
+      }
+      k <- k-1
+    }
+      # #################02julio2019################################
+    
+      if(tabla_ordenados[i,12]=="ATOM" || tabla_ordenados[i,12]== "HETATM"|| tabla_ordenados[i,12]=="TER"){
+
+
+        #print("ENTERING")
+        #stop()
+
+         corte_2 <- c()
+        for(iv in 5:11){
+           corte_2 <- c(corte_2,tabla_ordenados[i,iv])
+        }
+
+         #print(corte_2)
+
+         first_number <- substr(corte_2[5], 1, 4)
+         second_number <- substr(corte_2[5], 5, 1000)
+
+
+
+         coo_corte_2 <- 1
+         tabla_ordenados[i,5] <- ""
+         tabla_ordenados[i,12] <- ""
+         tabla_ordenados[i,13] <- ""
+       for(im in 6:13){
+           tabla_ordenados[i,im] <- corte_2[coo_corte_2]
+           coo_corte_2 <- coo_corte_2 + 1
+         }
+         k <- k-2
+
+         tabla_ordenados[i,12] <- corte_2[6]
+         tabla_ordenados[i,10] <- first_number
+         tabla_ordenados[i,11] <- second_number
+
+
+
+
+       }
+      
+    
+    
+    #################02julio2019################################
+      
+    
+    k <- k + 1 
+  }
+  if(tabla_ordenados[i,1]=="TER"){
+    k <- k - 8
+  } 
+}
+
+#Fill in TER row until last one 
+star_ter <- 2
+mmm <- 1
+for(ii in 1:counter){
+  if(tabla_ordenados[ii,1]=="TER"){
+    k <- k_position[mmm] + 4
+    mmm <- mmm + 1
+    y <- 2
+    for(l in star_ter:(star_ter+3)){
+      if(y%%3==0 && y!=6){
+        tabla_ordenados[ii,y] <- ""
+        y <- y + 1
+        tabla_ordenados[ii,y] <- ter_vector[l]
+      }
+      tabla_ordenados[ii,y] <- ter_vector[l]
+      y <- y +1
+    }
+    star_ter <- star_ter + 5
+    for(b in 7:13){
+      tabla_ordenados[ii,b] <- ""
+    }
+  }
+}
+
+##Fill if last row is not TER root
+if(ter_pos < (length(original_file)-14)){ 
+  tpnt <- 0
+  ntn <- length(original_file) - 12
+  for(jnt in 1:13){
+    tabla_ordenados[nrow(tabla_ordenados),jnt] <- original_file[ntn]
+    
+    ntn <- ntn + 1
+  }
+}
+
+##Fill in last row with TER row values ##
+#ajustcel4 <-0
+if(ter_pos > (length(original_file)-14)){
+  for(rtv in 1:length(ter_vector)){
+    ajustcel4 <- rtv #14nov2018
+    if(is.na(ter_vector[rtv])){
+      ajustcel4 <- rtv - 1
+    }
+  }
+  
+  ter_vector_numeric <- strsplit(ter_vector[ajustcel4], "")
+  
+  last_ter <- length(ter_vector)-4
+  if(length(ter_vector_numeric[[1]]) != 1){
+    if(length(ter_vector) < 5){
+      
+      for(jj in 1:6){
+        tabla_ordenados[counter,jj] <- ter_vector[last_ter]
+        
+        if(jj%%3==0 && jj !=6 || jj == 4){    #he incluido el jj == 4
+          tabla_ordenados[counter, jj] <- ""
+          last_ter <- last_ter - 1
+        }
+        last_ter <- last_ter + 1
+      }
+      for(jj in 7:13){
+        tabla_ordenados[counter, jj] <- ""
+      }
+    }
+    
+    if(length(ter_vector) >= 5){
+      for(jj in 1:7){
+        tabla_ordenados[counter,jj] <- ter_vector[last_ter]
+        if(jj%%3==0 && jj !=6){    #he incluido el jj == 4
+          tabla_ordenados[counter, jj] <- ""
+          last_ter <- last_ter -1 
+        }
+        last_ter <- last_ter + 1
+      }
+      for(jj in 8:13){
+        tabla_ordenados[counter, jj] <- ""
+      }
+    }
+  }
+  
+  if(length(ter_vector_numeric[[1]]) == 1){
+    if(length(ter_vector) <= 5){
+      
+      for(jj in 1:6){
+        tabla_ordenados[counter,jj] <- ter_vector[last_ter]
+        
+        if(jj%%3==0 && jj !=6 ){    #he incluido el jj == 4
+          tabla_ordenados[counter, jj] <- ""
+          last_ter <- last_ter - 1
+        }
+        last_ter <- last_ter + 1
+      }
+      for(jj in 7:13){
+        tabla_ordenados[counter, jj] <- ""
+      }
+    }
+    
+    if(length(ter_vector) > 5){
+      for(jj in 1:7){
+        tabla_ordenados[counter,jj] <- ter_vector[last_ter]
+        if(jj%%3==0 && jj !=6){    #he incluido el jj == 4
+          tabla_ordenados[counter, jj] <- ""
+          last_ter <- last_ter -1 
+        }
+        last_ter <- last_ter + 1
+      }
+      for(jj in 8:13){
+        tabla_ordenados[counter, jj] <- ""
+      }
+    }
+  }
+  
+}
+
+
+#View(tabla_ordenados)
+
+#19octubre2018
+
+#listacol11 <- matrix(0, nrow = nrow(tabla_ordenados), ncol = 1)
+twopoint <- c()
+for(dc11 in  1:nrow(tabla_ordenados)){
+  listacol11 <-strsplit(tabla_ordenados[dc11,11], "")
+  
+  if(length(listacol11[[1]]) != 0){
+    num_poin <- 0
+    for(findpoint in 1:length(listacol11[[1]])){
+      if(listacol11[[1]][findpoint] == "."){
+        num_poin <- num_poin + 1
+      }
+    }
+    if(num_poin == 2){
+      twopoint <- c(twopoint, dc11)
+      unopto0 <- listacol11[[1]][1] 
+      dospto0 <- listacol11[[1]][5] 
+      for(div1pto in 2:4){
+        unopto0 <- paste(unopto0, listacol11[[1]][div1pto], sep = "")
+      }
+      for(div2pto in 6:length(listacol11[[1]])){
+        dospto0 <- paste(dospto0, listacol11[[1]][div2pto], sep = "")
+      }
+      #19oct2018 Voy a guardar cada fila en un vector para luego pegarlo
+      vec_guarfilas <- c()
+      for(vgf in 1:10){
+        
+        if(tabla_ordenados[dc11,vgf] != ""){
+          vec_guarfilas <- c(vec_guarfilas, tabla_ordenados[dc11,vgf])
+        }
+      }
+      vec_guarfilas[10] <- unopto0
+      vec_guarfilas[11] <- dospto0
+      vec_guarfilas[12] <- tabla_ordenados[dc11,12]
+      vec_guarfilas[13] <- tabla_ordenados[dc11,13]
+      
+      #print(vec_guarfilas)
+      for(mvc in 1:13){
+        tabla_ordenados[dc11,mvc] <- vec_guarfilas[mvc]
+      }
+      
+    }
+  }
+}
+
+#View(tabla_ordenados)
+#acaba 19octubre2018
+
+
+
+#Make a table containing for each cell the number of characters for each for the tabla_ordenados
+tabla_ordenados_numcarac <- matrix(1, nrow = nrow(tabla_ordenados), ncol = ncol(tabla_ordenados))
+
+for(i in 1:nrow(tabla_ordenados)){
+  for(j in 1:ncol(tabla_ordenados)){
+    caracteres <- strsplit(tabla_ordenados[i,j], "")
+    num_carac <- 0
+    # if(tabla_ordenados[i,j] == ""){
+    #   num_carac <- ""
+    # }
+    #if(tabla_ordenados[i,j] != ""){ 
+    for(k in 1:length(caracteres[[1]])){
+      num_carac <- num_carac + 1
+    }
+    tabla_ordenados_numcarac[i,j] <- num_carac
+    #} 
+  }
+}
+#View(tabla_ordenados_numcarac) #27mar2019
+
+#################03julio2019###################3     ##
+
+for(i in 1:nrow(tabla_ordenados)){
+  
+    if(tabla_ordenados_numcarac[i,4] == 2 && tabla_ordenados_numcarac[i,5] == 3){
+      cambio_posicion5 <- tabla_ordenados[i,5]
+      tabla_ordenados[i,5] <- ""
+      tabla_ordenados_numcarac[i,5] <- 2
+      tabla_ordenados[i,4] <- cambio_posicion5
+      tabla_ordenados_numcarac[i,4] <- 3 
+    }
+  }
+
+for(i in 1:nrow(tabla_ordenados)){
+  if(tabla_ordenados_numcarac[i,5] == 2 && tabla_ordenados_numcarac[i,6] == 5){
+    Aaposicion5 <- substr(tabla_ordenados[i,6], 1, 1)
+    cambio_posicion5 <- substr(tabla_ordenados[i,6], 2, 5)
+    tabla_ordenados[i,5] <- Aaposicion5
+    tabla_ordenados_numcarac[i,5] <- 1
+    tabla_ordenados[i,6] <- cambio_posicion5
+    tabla_ordenados_numcarac[i,6] <- 4
+  }
+}
+
+for(i in 1:nrow(tabla_ordenados)){
+  if(tabla_ordenados[i, 3] < 6 && tabla_ordenados[i, 4] == ""){
+  tabla_ordenados[i, 4] <- "XX"
+  }
+}
+
+#################03julio2019###################3     ##
+
+# &&&&& 03sep2019 &&&&&&&&&&&&
+if(tabla_ordenados_numcarac[counter,5] >= 5){
+  ter_5_col <- substr(tabla_ordenados[counter, 5], 1, 1)
+  ter_6_col <- substr(tabla_ordenados[counter, 5], 2, 1000)
+  
+  
+  tabla_ordenados[counter, 5] <- ter_5_col
+  tabla_ordenados[counter, 6] <- ter_6_col
+}
+
+# &&&&& 03sep2019 &&&&&&&&&&&&
+
+
+####····̣# &&&&& 30oct2019 &&&&&&&&&&&&
+for(ac3 in 1:counter){
+  if(tabla_ordenados_numcarac[ac3,3] >= 7){
+  ter_3_col <- substr(tabla_ordenados[ac3, 3], 1, 4)
+  ter_4_col <- substr(tabla_ordenados[ac3, 3], 5, 1000)
+  
+  
+  tabla_ordenados[ac3, 3] <- ter_3_col
+  tabla_ordenados[ac3, 4] <- ter_4_col
+  tabla_ordenados_numcarac[ac3, 3] <- 4
+  tabla_ordenados_numcarac[ac3, 4] <- 3
+}
+
+}
+
+####····̣# &&&&& 30oct2019 &&&&&&&&&&&&
+
+
+
+
+
+#:;:;:;:;:;:;:;:;________09septiembre2019. Remove line with "Na" in column 13 in tabla_ordenados and tabla_ordenados_numcarac 
+
+# quitar_fila <- 0
+# for(ena in 1:counter){
+#   if(tabla_ordenados[ena,13] == "Na"){
+#     print(ena)
+#     tabla_ordenados_numcarac <- tabla_ordenados_numcarac[-ena,]
+#     ena <- ena - 1
+#     quitar_fila <- quitar_fila + 1
+#     counter <- counter - quitar_fila
+#     
+#   }
+# }
+# 
+# tabla_ordenados <- subset(tabla_ordenados, tabla_ordenados[,13] != "Na")
+
+#:;:;:;:;:;:;:;:;________09septiembre2019
+
+#View(tabla_ordenados)
+#View(tabla_ordenados_numcarac) 
+
+
+
+
+#Fit spaces for write()
+#Fit spaces for write() 
+#Pairwise 1col-2col con 1col == "ATOM"
+
+for(i in 1:nrow(tabla_ordenados)){
+  linea <- c()
+  if(tabla_ordenados_numcarac[i,1] == 4){
+    num_espacios <- 6
+    ajuste <- 0
+    for(a in 1:(tabla_ordenados_numcarac[i,2])){
+      if(a != tabla_ordenados_numcarac[i,2]){
+        ajuste <- ajuste + 1
+      }
+      if(a == tabla_ordenados_numcarac[i,2]){
+        espacios <- rep(" ", num_espacios-ajuste)
+        linea <- c(espacios, tabla_ordenados[i,2])
+        break
+      }
+    }
+  }
+  
+  #1col == HETATM INI
+  if(tabla_ordenados_numcarac[i,1] == 6){
+    num_espacios <- 4
+    ajuste <- 0
+    for(a in 1:(tabla_ordenados_numcarac[i,2])){
+      if(a != tabla_ordenados_numcarac[i,2]){
+        ajuste <- ajuste + 1
+      }
+      if(a == tabla_ordenados_numcarac[i,2]){
+        espacios <- rep(" ", num_espacios-ajuste)
+        linea <- c(espacios, tabla_ordenados[i,2])
+        
+        ###21feb2019
+        if(tabla_ordenados_numcarac[i,2] == 5){
+          espacios <- rep(" ", (num_espacios-ajuste) + 1)
+          linea <- c(espacios, tabla_ordenados[i,2])
+        }
+        ###21feb2019
+        
+        break
+      }
+    }
+  }
+  #1col == HETATM FIN
+  
+  ##1col == TER INICIO
+  if(tabla_ordenados_numcarac[i,1] == 3){
+    num_espacios <- 7
+    ajuste <- 0
+    for(a in 1:(tabla_ordenados_numcarac[i,2])){
+      if(a != tabla_ordenados_numcarac[i,2]){
+        ajuste <- ajuste + 1
+        espacios <- rep(" ", num_espacios-ajuste)
+      }
+      if(a == tabla_ordenados_numcarac[i,2]){
+        espacios <- rep(" ", num_espacios-ajuste)
+        linea <- c(espacios, tabla_ordenados[i,2])
+        break
+      }
+    }
+  }
+  ##1col == TER FIN
+  
+  ##Pairwise 2col-3col 
+  linea2 <- c()
+  for(b in 1:(tabla_ordenados_numcarac[i,3])){
+    if(b < 4){
+      espacios2 <- rep(" ", 2)
+    }
+    if(b >= 4){
+      if(tabla_ordenados[i,4] == ""){
+        espacios2 <- rep(" ", 2)
+      }
+      
+      if(tabla_ordenados[i,4]!= ""){
+        espacios2 <- rep(" ", 1) 
+      }
+    }
+  }
+  
+  if(tabla_ordenados_numcarac[i,3] >= 8){
+    e_espacios2 <- length(espacios2)-1
+    espacios2 <- rep(" ",e_espacios2)
+    
+    
+  }
+  
+  linea2 <- c(linea, espacios2, tabla_ordenados[i,3])
+  
+  ###21feb2019
+  if(tabla_ordenados_numcarac[i,1] == 6 && tabla_ordenados_numcarac[i,2] == 5){
+    espacios2 <- espacios2[-length(espacios2)]
+    linea2 <- c(linea, espacios2, tabla_ordenados[i,3])
+  }
+  ###21feb2019
+  
+  ###Pairwise 3col-4col 
+  linea3 <- c()
+  
+  for(c in 1:(tabla_ordenados_numcarac[i,4])){
+    
+    #28nov
+    if(c == 2){
+      if(tabla_ordenados_numcarac[i,1] == 6 && tabla_ordenados_numcarac[i,3] == 1){
+        espacios3 <- rep(" ", 2)
+      }
+      if(tabla_ordenados_numcarac[i,1] == 6 && tabla_ordenados_numcarac[i,3] == 2){
+        espacios3 <- rep(" ", 1)
+      }
+      if(tabla_ordenados_numcarac[i,1] == 6 && tabla_ordenados_numcarac[i,3] == 2 && tabla_ordenados_numcarac[i,5] == 1){
+        espacios3 <- rep(" ", 3)
+      }
+      
+      #########21junio2019
+      
+      if(tabla_ordenados_numcarac[i,1] == 4 &&  tabla_ordenados_numcarac[i,3] == 1 && tabla_ordenados_numcarac[i,6] == 5){
+        espacios3 <- rep(" ", 2) 
+      }
+      if(tabla_ordenados_numcarac[i,1] == 4 &&  tabla_ordenados_numcarac[i,3] == 2 && tabla_ordenados_numcarac[i,6] == 5){
+        espacios3 <- rep(" ", 1) 
+      }
+      
+      ########21junio2019
+      
+      
+      
+      
+    }
+    #28nov. Hace falta corregir para el resto de HETATM
+    
+    
+    if(c == 3){
+      if(b == 1){
+        espacios3 <- rep(" ", 3)
+      }
+      if(b == 2){
+        espacios3 <- rep(" ", 2)
+      }
+      if(b >= 3){
+        espacios3 <- rep(" ", 1)
+      }
+    }
+    if(c == 4){
+      if(b == 1){
+        espacios3 <- rep(" ", 2)
+      }
+      if(b >= 2){
+        espacios3 <- rep(" ", 1)
+      }
+    }
+    #27mar2019 ######################################
+    if(c == 5){
+      if(b >= 2){
+        espacios3 <- rep("", 1)
+      }
+    }
+    #27mar2019 ######################################
+  }
+    
+  linea3 <- c(linea2, espacios3, tabla_ordenados[i,4])
+                                                      ####03julio2019####################### #####################
+# if(tabla_ordenados_numcarac[i,3] == 3 && tabla_ordenados_numcarac[i,4] == 3 && tabla_ordenados_numcarac[i,5] == 2 && tabla_ordenados_numcarac[i,6] == 5){
+#   linea3 <- c(linea2, " ", tabla_ordenados[i,4])
+# }
+  ####03julio2019####################### #####################
+  
+  #TER
+  if(tabla_ordenados_numcarac[i,1] == 3){
+    num_espacios4 <- 8  
+    ajuste_t1 <- 0
+    for(l in 1:tabla_ordenados_numcarac[i,4]){
+      if(l != tabla_ordenados_numcarac[i,4]){
+        ajuste_t1 <-ajuste_t1 + 1
+        num_espacios4 <- 8 - ajuste_t1
+      }
+      if(l == tabla_ordenados_numcarac[i,4]){
+        num_espacios4 <- 8 - ajuste_t1
+      }
+    }
+    linea3 <- c(linea, rep(" ", num_espacios4), tabla_ordenados[i,4])
+    
+  }
+  
+  ####Pairwise 4col-5col 
+  linea4 <- c()
+  if(tabla_ordenados_numcarac[i,1] != 3){
+    if(tabla_ordenados_numcarac[i,3] >= 7){
+      linea4 <- c(linea3, tabla_ordenados[i,5])
+    }
+    if(tabla_ordenados_numcarac[i,3] < 7){
+      linea4 <- c(linea3, " ", tabla_ordenados[i,5])
+      ###18oct2018
+      if(tabla_ordenados_numcarac[i,3] >= 3 && tabla_ordenados[i,4] == ""){
+        linea4 <- c(linea2, " ", tabla_ordenados[i,5])
+      }
+      ###18oct2018
+    }
+    #------------29julio2019--------------------#
+    
+    if(isTRUE(tabla_ordenados[i,3] == "NA") && tabla_ordenados[i,4] == ""){
+      linea4 <- c(linea2, rep(" ", 6), tabla_ordenados[i,5])
+    }
+    
+    
+    #------------29julio2019--------------------#
+  }
+  
+  
+  
+  
+  if(tabla_ordenados_numcarac[i,1] == 3){
+    
+    linea4 <- c(linea3, " ", tabla_ordenados[i,5])
+    ##
+    if(tabla_ordenados[i,4] == ""){
+      linea4 <- c(linea2,rep(" ", 4),tabla_ordenados[i,5])
+    }
+    
+    ##
+    
+    
+  }
+  
+  #####Pairwise 5col-6col. NO espacios4
+  if(tabla_ordenados_numcarac[i,1] != 3){
+    linea5 <-c()
+    for(d in 1:(tabla_ordenados_numcarac[i,6])){
+      if(d == 1){
+        espacios5 <- rep(" ", 3)  
+      }
+      if(d == 2){
+        espacios5 <- rep(" ", 2)  
+      }
+      if(d >= 3){
+        espacios5 <- rep(" ", 1)  
+      }
+      
+      ##################21junio2109#############
+      
+      if(tabla_ordenados_numcarac[i,3] == 2 && tabla_ordenados_numcarac[i,5] == 2 && tabla_ordenados_numcarac[i,6] == 5){
+        espacios5 <- rep(" ", 2)
+      }
+      
+      ##################21junio2109#############
+                                                    ###########03julio################                 ############
+      # if(tabla_ordenados_numcarac[i,3] == 1 && tabla_ordenados_numcarac[i,4] == 3 && tabla_ordenados_numcarac[i,5] == 2 && tabla_ordenados_numcarac[i,6] == 5){
+      #   
+      #   espacios5 <- ""
+      # }
+      
+      ###########03julio################                 ############
+      
+      
+    }
+    linea5 <- c(linea4, espacios5, tabla_ordenados[i,6])
+    
+    
+  }
+  if(tabla_ordenados_numcarac[i,1] == 3){
+    for(dd in 1:(tabla_ordenados_numcarac[i,6])){
+      if(dd == 1){
+        espacios55 <- rep(" ", 3) 
+      }
+      
+      #14nov
+      if(dd == 1 && tabla_ordenados_numcarac[i,5] == 3){
+        espacios55 <- rep(" ", 1)
+      }
+      #14nov
+      
+      if(dd == 2){
+        espacios55 <- rep(" ", 2)
+      }
+      if(dd >= 3){
+        espacios55 <- rep(" ", 1)
+      }
+      
+      
+    }
+    linea5 <- c(linea4, espacios55, tabla_ordenados[i,6])
+    
+    #------------------05sep2019------------#
+    
+    if(tabla_ordenados_numcarac[i,6] == 1 && tabla_ordenados_numcarac[i,5] >= 5){
+      espacios55 <- rep(" ", 1)
+      linea5 <- c(linea4, espacios55, tabla_ordenados[i,6])
+    }
+    
+    #------------------05sep2019------------#
+    
+  }
+  
+  
+  
+  
+  ######Pairwise 6col-7col  
+  linea6 <- c()
+  if(tabla_ordenados_numcarac[i,7] == 5){
+    espacios6 <- rep(" ", 7)
+    linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+    
+    ###################21junio2109#################
+    if(tabla_ordenados_numcarac[i,3] == 4 && tabla_ordenados_numcarac[i,6] == 5){
+      espacios6 <- rep(" ", 6)
+      linea6 <-c(linea5, espacios6, tabla_ordenados[i,7])
+    }
+    
+    ###################21junio2109#################
+    
+    ##    03julio2019   #########
+    
+    if(tabla_ordenados_numcarac[i,6] == 4 && tabla_ordenados_numcarac[i,6] == 4){
+      espacios6 <- rep(" ", 6)
+      linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+    }
+    
+    ##    03julio2019   #########
+    
+    
+  }
+  
+  if(tabla_ordenados_numcarac[i,7] > 5){
+    out_spaces <- tabla_ordenados_numcarac[i,7] - 5
+    espacios6 <- rep(" ", (7-out_spaces))
+    linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+    
+    ###################21junio2109#################
+    if(tabla_ordenados_numcarac[i,3] == 4 && tabla_ordenados_numcarac[i,6] == 5){
+      out_spaces <- tabla_ordenados_numcarac[i,7] - 4
+      espacios6 <- rep(" ", (7-out_spaces))
+      linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+    }
+    
+    ###################21junio2109#################
+    ##    03julio2019   #########
+    
+    if(tabla_ordenados_numcarac[i,6] == 4 && tabla_ordenados_numcarac[i,6] == 4){
+      out_spaces_2 <- tabla_ordenados_numcarac[i,7] - 4
+      espacios6 <- rep(" ", (7-out_spaces_2))
+      linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+    }
+    
+    ##    03julio2019   #########
+    
+    
+  }
+  
+  ######################################27mar2019
+  # 
+  # if(tabla_ordenados_numcarac[i,4] == 5){
+  #   espacios6 <- rep(" ",5)
+  #   linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+  # }
+  # 
+  # if(tabla_ordenados_numcarac[i,4] > 5){
+  #   espacios6 <- rep(" ",4)
+  #   linea6 <-c(linea5, espacios6, tabla_ordenados[i,7]) 
+  # }
+ 
+  ######################################27mar2019
+  
+  
+  #######Pairwise 7col-8col 
+  linea7 <- c()
+  ajuste3 <- 0
+  if(tabla_ordenados_numcarac[i,8] == 5){
+    espacios7 <- rep(" ", 3)
+    linea7 <-c(linea6, espacios7, tabla_ordenados[i,8]) 
+  }
+  if(tabla_ordenados_numcarac[i,8] == 6){
+    espacios7 <- rep(" ", 2)
+    linea7 <-c(linea6, espacios7, tabla_ordenados[i,8]) 
+  }
+  if(tabla_ordenados_numcarac[i,8] > 6){
+    espacios7 <- rep(" ", 1)
+    linea7 <-c(linea6, espacios7, tabla_ordenados[i,8]) 
+  }
+  
+  ########Pairwise 8col-9col
+  linea8 <- c()
+  ajuste4 <- 0
+  if(tabla_ordenados_numcarac[i,9] == 5){
+    espacios8 <- rep(" ", 3)
+    linea8 <-c(linea7, espacios8, tabla_ordenados[i,9]) 
+  }
+  if(tabla_ordenados_numcarac[i,9] == 6){
+    espacios8 <- rep(" ", 2)
+    linea8 <-c(linea7, espacios8, tabla_ordenados[i,9]) 
+  }
+  if(tabla_ordenados_numcarac[i,9] > 6){
+    espacios8 <- rep(" ", 1)
+    linea8 <-c(linea7, espacios8, tabla_ordenados[i,9]) 
+  }
+  
+  #########Pairwise 9col-10col
+  linea9 <- c()
+  if(tabla_ordenados_numcarac[i,10] == 4){
+    espacios9 <- rep(" ", 2)
+    linea9 <- c(linea8, espacios9, tabla_ordenados[i,10])
+  }
+  if(tabla_ordenados_numcarac[i,10] > 4){
+    espacios9 <- rep(" ", 1)
+    linea9 <- c(linea8, espacios9, tabla_ordenados[i,10])
+  }
+  
+  ##########Pairwise 10col-11col
+  # linea10 <- c()
+  # if(tabla_ordenados_numcarac[i,11] == 4){
+  #   espacios10 <- rep(" ", 2)
+  #   linea10 <- c(linea9, espacios10, tabla_ordenados[i,11])
+  # }
+  # if(tabla_ordenados_numcarac[i,11] > 4){
+  #   espacios10 <- rep(" ", 1)
+  #   linea10 <- c(linea9, espacios10, tabla_ordenados[i,11])
+  # }
+  #22oct2018
+  ##########Pairwise 10col-11col
+  linea10 <- c()
+  if(tabla_ordenados_numcarac[i,11] == 4){
+    espacios10 <- rep(" ", 3)
+    linea10 <- c(linea9, espacios10, tabla_ordenados[i,11])
+  }
+  if(tabla_ordenados_numcarac[i,11] == 5){
+    espacios10 <- rep(" ", 2)
+    linea10 <- c(linea9, espacios10, tabla_ordenados[i,11])
+  }
+  
+  if(tabla_ordenados_numcarac[i,11] > 5){
+    espacios10 <- rep(" ", 1)
+    linea10 <- c(linea9, espacios10, tabla_ordenados[i,11])
+  }
+  #22oct2018
+  
+  
+  ###########Pairwise 11col-12col
+  linea11 <- c()
+  if(tabla_ordenados_numcarac[i,12] == 5){
+    espacios11 <- rep(" ", 5)
+    linea11 <-c(linea10, espacios11, tabla_ordenados[i,12])
+  } 
+  ajuste5 <- 0
+  for(h in 6:tabla_ordenados_numcarac[i,12]){
+    if(tabla_ordenados_numcarac[i,12] > 5){
+      ajuste5 <- ajuste5 + 1
+      espacios11 <- rep(" ", (5-ajuste5))
+      ##espacios11 <- rep(" ", (5-ajuste5))
+      linea11 <-c(linea10, espacios11, tabla_ordenados[i,12])
+    }  
+  }
+  
+  ############Pairwise 12col-13col
+  linea12 <- c(linea11," ", tabla_ordenados[i,13])
+  
+  #TER
+  if(tabla_ordenados_numcarac[i,1] == 3){
+    cabecera <- tabla_ordenados[i,1]
+    for(w in 1:length(linea5)){
+      cabecera <- paste(cabecera,linea5[w], sep = "")
+    }
+    #write(cabecera, file = "1z95_wo_198andwater_ph7_MOD.pdbqt", append = TRUE)
+    write(cabecera, file = args[2], append = TRUE)
+  }
+  
+  #!= TER
+  if(tabla_ordenados_numcarac[i,1] != 3){
+    cabecera <- tabla_ordenados[i,1]
+    for(w in 1:length(linea12)){
+      cabecera <- paste(cabecera,linea12[w], sep = "")
+    }
+    #write(cabecera, file = "1z95_wo_198andwater_ph7_MOD.pdbqt", append = TRUE)
+    write(cabecera, file = args[2], append = TRUE)
+  }
+}
+
+#21junio2019
+# #### Make file receptor_atm_types.txt to be paste in the GPF file ###
+
+prereceptor_atmtypes <- c()
+for(i in 1:nrow(tabla_ordenados)){
+  if(tabla_ordenados[i,13] != ""){
+    prereceptor_atmtypes <- c(prereceptor_atmtypes, tabla_ordenados[i,13])
+  }
+}
+# 
+ receptor_atmtypes <- unique(prereceptor_atmtypes)
+#
+
+filtered_receptor <- c()
+for(i in 1:length(receptor_atmtypes)){
+
+	#print(receptor_atmtypes[i])
+	#print(is.na(as.numeric(receptor_atmtypes[i])))
+
+	if(suppressWarnings(is.na(as.numeric(receptor_atmtypes[i])))){
+		filtered_receptor <- c(filtered_receptor, receptor_atmtypes[i])
+	}
+
+}
+
+receptor_atmtypes <- filtered_receptor
+
+cabeceratm <- receptor_atmtypes[1]
+for(rat in 2:length(receptor_atmtypes)){
+
+  cabeceratm <- paste(cabeceratm, receptor_atmtypes[rat], sep = " ")
+}
+
+write(cabeceratm, file = receptor_types_file_out, append = FALSE)
+
+
+#21junio2019
+
+
+
+
+# #::::::::06sep2019:::::::::::::::::#
+# 
+# #Make the file receptor_types_file_out.txt removing duplicates and Na 
+# #21junio2019
+# 
+# # 
+# prereceptor_atmtypes <- c()
+# for(i in 1:nrow(tabla_ordenados)){
+#   if(tabla_ordenados[i,13] != ""){
+#     prereceptor_atmtypes <- c(prereceptor_atmtypes, tabla_ordenados[i,13])
+#   }
+# }
+# # 
+# # #print(prereceptor_atmtypes)
+# # 
+# receptor_atmtypes <- unique(prereceptor_atmtypes)
+# #21junio2019
+# 
+# quitar <- 0
+# for(i in 1:length(receptor_atmtypes)){
+#   if(isTRUE(receptor_atmtypes[i] == "Na")){
+#     quitar <- i
+#     break
+#   }
+# }
+# 
+# if(quitar == 0){
+#   cabeceratm <- receptor_atmtypes[1]
+#   for(rat in 2:length(receptor_atmtypes)){
+#     cabeceratm <- paste(cabeceratm, receptor_atmtypes[rat], sep = " ")
+#   }
+#   
+#   write(cabeceratm, file = "receptor_types_file_out.txt", append = FALSE)
+# }
+# 
+# if(quitar != 0){
+#   receptor_atmtypes <- receptor_atmtypes[-quitar]    
+#   cabeceratm_types <- receptor_atmtypes[1]
+#   for(at_t in 2:length(receptor_atmtypes)){
+#    cabeceratm_types <- paste(cabeceratm_types, receptor_atmtypes[at_t], sep = " ")
+#   }
+#   write(cabeceratm_types, file = "receptor_types_file_out.txt", append = FALSE)
+# }
+
+
+
+
+
+
+
+
+
+
+