【发布时间】:2014-02-07 11:16:59
【问题描述】:
我有fileA,其中信息按间隔显示 - 如果连续位置被分配相同的值,这些连续值将重新组合到一个间隔中。
start end value label
123 78000 0 romeo #value 0 at positions 123 to 77999 included.
78000 78004 56 romeo #value 56 at positions 78000, 78001, 78002 and 78003.
78004 78005 12 romeo #value 12 at position 78004.
78006 78008 21 juliet #value 21 at positions 78006 and 78007.
78008 78056 8 juliet #value 8 at positions 78008 to 78055 included.
我感兴趣的区间显示在fileB:
start end label
77998 78005 romeo
78007 78012 juliet
[编辑]
fileA 中的标签最初是从 fileB 中提取的,因此可以安全地假设标签对于重叠间隔始终是等效的。
我正在尝试提取与第二个文件中的间隔相对应的所有单个位置的信息,由于没有更好的词,我将这个过程称为“反卷积”。输出fileC 应该是这样的:
position value label
77998 0 romeo
77999 0 romeo
78000 56 romeo
78001 56 romeo
78002 56 romeo
78003 56 romeo
78004 12 romeo
78007 21 juliet
78008 8 juliet
78009 8 juliet
78010 8 juliet
78011 8 juliet
这是我的代码:
#read from tab-delimited text files which do not contain column names
A<-read.table("fileA.txt",sep="\t",colClasses=c("numeric","numeric","numeric","character"))
B<-read.table("fileB.txt",sep="\t",colClasses=c("numeric","numeric","character"))
#create empty table.frame for the output
C <- data.frame (1,2,3)
C <- C[-1,]
#add column names
colnames(A)<-c("start","end","value","label")
colnames(B)<-c("start","end","label")
colnames(C)<-c("position","value","label")
#extract position information
deconvolute <- function(x,y,z) {
for x$label %in% y$label {
#compute sequence of overlapping positions
overlap<-seq(max(x$start,y$start),x$end,1)
z$position<-overlap
#assign corresponding values to the other columns
z$value<-rep(x$value,length(overlap))
z$label<-rep(x$label,length(overlap))
}
}
deconvolute(A,B,C)
我的函数中有很多语法错误。如果有人能帮我修复它们,我会很高兴。
【问题讨论】:
-
文件B中的标签重要吗?我的意思是:如果你在文件 B 中有 start=78002、end=78008 和 label=romeo,你会怎么做?或者假设标签始终与文件 A 中的间隔匹配是否安全?
-
是的,标签应始终与文件 A 中的间隔匹配(文件 A 最初没有标签,它们是使用另一段代码从文件 B 中提取的)。将其添加到原始帖子中
标签: r position dataframe intervals overlap