【问题标题】:Deconvoluting intervals into position information将区间反卷积为位置信息
【发布时间】: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


【解决方案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

【讨论】:

  • 您的解决方案比我的简单得多。需要更多练习!:D
  • pos 在数据帧 AB 之间匹配。它是B 中的行位置,对应于A 中的给定行。它是在第 8 行定义的 (pos &lt;- unliest(...)
  • 抱歉,我一问就知道了。您的解决方案给了我一个错误,我试图找出原因:“data.frame(s, A$value[pos], A$label[pos]) 中的错误:与参数不同长度的数据框:12 , 0(对不起,我是从日语翻译过来的,所以它可能不是英语中的确切原始措辞)
  • 原来的错误信息可能是:arguments imply different number of rows
  • 检查向量pos的长度。它的长度可能为 0,这表明匹配中有错误。如果pos的长度大于0,那么A$value可能是无效的——列的名称不是value
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2016-11-11
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多