DN1
DN1

Reputation: 218

How to select lines of file based on multiple conditions of another file in R?

I have 2 genetic datasets. I filter file1 based on a column in file2. However, I also need to account for a second column in file2 and I'm not sure how to do this.

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.

For example my data looks like:

File 1:

Variant   Chromsome   Chromosome Position  
Variant1      2             14000     
Variant2      1             9000              
Variant3      8             37000          
Variant4      1             21000     

File 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):

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. 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.

Upvotes: 0

Views: 149

Answers (1)

Wimpel
Wimpel

Reputation: 27732

Using your sample data and methods, I came up with this 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

Upvotes: 1

Related Questions