我有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)
我的函数中出现了很多语法错误。如果有人能帮我修理它们,我会很高兴。
答案 0 :(得分:1)
# create sequence of positions
s <- unlist(apply(B, MARGIN=1, FUN=function(x) seq(x[1], as.numeric(x[2])-1)))
s
[1] 77998 77999 78000 78001 78002 78003 78004 78007 78008 78009 78010 78011
# matching between files A and B
pos <- unlist(sapply(s, FUN=function(x)
which(
apply(A, MARGIN=1, FUN=function(y) as.numeric(y[1])<=as.numeric(x) & as.numeric(x) < as.numeric(y[2])))
))
# new dataframe
deconvoluted <- data.frame(s, A$value[pos], A$label[pos])
deconvoluted
s A.value.pos. A.label.pos.
1 77998 0 romeo
2 77999 0 romeo
3 78000 56 romeo
4 78001 56 romeo
5 78002 56 romeo
6 78003 56 romeo
7 78004 12 romeo
8 78007 21 juliet
9 78008 8 juliet
10 78009 8 juliet
11 78010 8 juliet
12 78011 8 juliet