我逐行读取文件,然后将特定的行添加到数据帧中。下面是我要添加到数据帧中的一行代码的示例:
ATOM 230 CA GLU A 31 66.218 118.140 2.411 1.00 31.82 C
我已经验证了我的检查是正确的,我认为这特别与我的rbind命令有关。谢谢你的帮忙!
编辑:错误如下,数据帧输出为:
Residue AtomCount SideChain XCoord YCoord ZCoord
2 MET 1 A 62.935 97.579 30.223
21 <NA> 2 A 63.155 95.525 27.079
3 <NA> 3 A 65.289 96.895 24.308它似乎停止了拾取残留物的名字..
我使用的代码是:
get.positions <- function(sourcefile, chain_required = "A"){
positions = data.frame()
visited = list()
filedata <- readLines(sourcefile, n= -1)
for(i in 1: length(filedata)){
input = filedata[i]
id = substr(input,1,4)
if(id == "ATOM"){
type = substr(input,14,15)
if(type == "CA"){
#if there are duplicates it takes the first one
residue = substr(input,18,20)
type_of_chain = substr(input,22,22)
atom_count = strtoi(substr(input, 23,26))
if(atom_count >=1){
if(type_of_chain == chain_required && !(atom_count %in% visited) ){
position_string = trim(substr(input,30,54))
position_string = lapply(unlist(strsplit(position_string," +")),as.numeric)
positions<- rbind(positions, list(residue, atom_count, type_of_chain, position_string[[1]], position_string[[2]], position_string[[3]]))
}
}
}
}
}
return (positions)
}发布于 2012-05-21 23:11:33
最终,下面的方法奏效了。首先,我创建了一个大得多的数据框架,然后只替换特定的行(谢谢Joran,他将我与R地狱联系在一起)。
对于问我为什么拆分加号的用户来说,你的假设是不正确的。语法实际上是“+",这是一个空格,所以它在多个spaces.Finally上拆分,至于不正确的索引,我终于想出了如何在表单上显示额外的空格。这是正确的原始行,您将看到索引匹配。
ATOM 2 CA MET A 1 62.935 97.579 30.223 1.00 37.58 C 有效的R代码如下所示。
get.positions <- function(sourcefile, chain_required = "A"){
N <- 10^5
AACount <- 0
positions = data.frame(Residue=rep(NA, N),AtomCount=rep(NA, N),SideChain=rep(NA, N),XCoord=rep(NA, N),YCoord=rep(NA, N),ZCoord=rep(NA, N),stringsAsFactors=FALSE)
visited = list()
filedata <- readLines(sourcefile, n= -1)
for(i in 1: length(filedata)){
input = filedata[i]
id = substr(input,1,4)
if(id == "ATOM"){
type = substr(input,14,15)
if(type == "CA"){
#if there are duplicates it takes the first one
residue = substr(input,18,20)
type_of_chain = substr(input,22,22)
atom_count = strtoi(substr(input, 23,26))
if(atom_count >=1){
if(type_of_chain == chain_required && !(atom_count %in% visited) ){
visited <- c(visited, atom_count)
AACount <- AACount + 1
position_string = trim(substr(input,30,54))
position_string = lapply(unlist(strsplit(position_string," +")),as.numeric)
#print(input)
positions[AACount,]<- c(residue, atom_count, type_of_chain, position_string[[1]], position_string[[2]], position_string[[3]])
}
}
}
}
}
positions<-positions[1:AACount,]
return (positions)}
发布于 2012-05-21 22:22:23
当我用这些数据运行你的代码时,我得到了type=="LU" (所以它没有通过type=="CA"测试),剩下的处理永远不会完成。我认为您可能需要将索引更改为
type = substr(input,10,11)修复这个问题会带来其他问题,而且修复所有问题将非常困难,因为目标没有明确说明,但它建议您编辑代码和数据,使其可重现。这可以是一个可重现的输入/执行方法:
get.positions(textConnection("ATOM 230 CA GLU A 31 66.218 118.140 2.411 1.00 31.82 C") )https://stackoverflow.com/questions/10679408
复制相似问题