0

It's my first time using R, and I just need to input a package with some files and save the outputs in some other files. I have a directory in which there are some files with ".pdb" file extensions. I need to go through each of these files in a for loop, apply some functions, and save the output of each ,which is a matrix, in a file that I could later use in Matlab.

These are the functions that I wish to apply to each of the .pdb files.

p=extractPDB("1HXH.pdb")
cm<-build.contacts(n=p$lca,xyz=p$coords,cuts=169)$cm

cm is the output matrix which I want to save in a file to be later used in Matlab.

  • 1
    What have you already tried? Why did it not work? Start small: how would you handle one file? How do you export it? If you can do that, see how you can find all files in your directory (hint: `list.files`) and repeat your actions. – Heroka Sep 10 '15 at 10:03
  • If you `.pdb` file are `Protein Data Bank` you can open them directly in MatLab with the [readpdb](http://it.mathworks.com/help/bioinfo/ref/pdbread.html) function – il_raffa Sep 10 '15 at 10:36

1 Answers1

0

Looping over files with a certain extension is easy. Actually you loop over the filenames.

  1. get a list of all files in the directory
  2. get only files ending in pdb
  3. run a loop over the names

You might have to change the working directory using setwd

all.files <- list.files()
my.files <- grep(".*pdb", all_files, value=T)
for(i in my_files){
  # do your operations here
  p=extractPDB("1HXH.pdb")
  cm<-build.contacts(n=p$lca,xyz=p$coords,cuts=169)$cm
  # save
  output.filename <- gsub("(.*?).pdb", "\\1.csv", i)
  write.table(cm, output.filename)
}

For saving the Files there are many options, for reading the file in matlab it's probably the best to save the matrices as csv-file. Look into the documentation of write.table and adjust the parameters so it's convinient for reading.

snaut
  • 2,261
  • 18
  • 37