[英]How to select lines of file based on multiple conditions of another file in R?
I have 2 genetic datasets.我有 2 个遗传数据集。 I filter file1 based on a column in file2.
我根据 file2 中的列过滤 file1。 However, I also need to account for a second column in file2 and I'm not sure how to do this.
但是,我还需要考虑 file2 中的第二列,但我不确定如何执行此操作。
The condition for file 1 row extraction is that only rows that have a chromosome position either more than 5000 larger or more than 5000 smaller than any chromosome positions for variants on the same chromosome in file 2 are selected.文件 1 行提取的条件是,仅选择染色体位置比文件 2 中同一染色体上的变异的任何染色体位置大 5000 以上或小 5000 以上的行。
For example my data looks like:例如我的数据看起来像:
File 1:文件 1:
Variant Chromsome Chromosome Position
Variant1 2 14000
Variant2 1 9000
Variant3 8 37000
Variant4 1 21000
File 2:文件2:
Variant Chromosome Chromosome Position
Variant1 1 10000
Variant2 1 20000
Variant3 8 30000
Expected output (of variants with a greater than +/-5000 position distance in comparison to any line of file 2 on the same chromosome):预期输出(与同一染色体上文件 2 的任何行相比,位置距离大于 +/-5000 的变异):
Variant Chromosome Position Chromosome
Variant1 14000 2
Variant3 37000 8
#Variant1 at 14000, whilst within 5000 + of Variant1 at 10000 in file2 is on a different chromosome and therefore not compared and is kept.
#Variant3 is on the same chromosome as Variant4 in file1 but larger than 5000+ distance and is kept.
I've tried coding using unix, however only got the larger than 5000 +/- filtering for each variant without chromosome consideration and been advised to try coding in R, however I'm new to R and I'm not sure where to start.我尝试过使用 unix 进行编码,但是在没有考虑染色体的情况下,每个变体只得到了大于 5000 +/- 的过滤,并被建议尝试在 R 中编码,但是我是 R 的新手,我不确定从哪里开始. I assume I need an if statement for "if line of file1 has matching chromosome number as file2, then perform the larger than 5000 +/- filtering within that chromosome number only" with a for loop for going over each row - even just guidance on how to learn how to do this would be appreciated.
我假设我需要一个 if 语句,“如果 file1 的行具有与 file2 匹配的染色体编号,则仅在该染色体编号内执行大于 5000 +/- 的过滤”,并使用 for 循环遍历每一行 - 甚至只是指导如何学习如何做到这一点将不胜感激。
Using your sample data and methods, I came up with this data.table
-solution使用您的示例数据和方法,我想出了这个
data.table
-solution
A short explanation is commented in the code.代码中注释了一个简短的解释。
library( data.table)
#sample data
dt1 <- fread("Variant Chromosome Chromosome_Position
Variant1 2 14000
Variant2 1 9000
Variant3 8 37000
Variant4 1 21000")
dt2 <- fread("Variant Chromosome Chromosome_Position
Variant1 1 10000
Variant2 1 20000
Variant3 8 30000")
#create lower&upper boundaries for dt2 chromosome position
dt2[, c("low", "high") := .(Chromosome_Position - 5000, Chromosome_Position + 5000)]
#dt2 now looks like this:
#-------------------------------------------------------------
# Variant Chromosome Chromosome_Position low high
# 1: Variant1 1 10000 5000 15000
# 2: Variant2 1 20000 15000 25000
# 3: Variant3 8 30000 25000 35000
#find matches on chromosome, with position bewtene low-high
# this is done using a non-equi join using the lower and upper boundaries
# created in dt2 in the previous line.
# on = .(...) means: Chromosome in dt1 and dt2 have to be the same
# Chromosome_Position in dt1 has to be between
# low and high of dt2. Y
# You can (of course) use >= and <= if desired.
# match := i.Variant creates a new column in dt1, with the value of
# Variant from dt2 (if a match is found).
# If no match is found, the columns gets a <NA>.
dt1[ dt2, match := i.Variant,
on = .(Chromosome, Chromosome_Position > low, Chromosome_Position < high ) ]
#dt1 now looks like this
#see the match-column for found dt1-matches in dt2
#-------------------------------------------------------------
# Variant Chromosome Chromosome_Position match
# 1: Variant1 2 14000 <NA>
# 2: Variant2 1 9000 Variant1
# 3: Variant3 8 37000 <NA>
# 4: Variant4 1 21000 Variant2
#discard all found matches (i.e. is.na(Match) == TRUE), and drop match-column,
# since we no longer need it.
dt1[ is.na(match) ][, match := NULL ][]
# Variant Chromosome Chromosome_Position
# 1: Variant1 2 14000
# 2: Variant3 8 37000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.