How to achieve dislocation comparison, filtering and fusion of adjacent rows in a data table in R
0
4
Entering edit mode
2.5 years ago
Chilly ★ 1.3k

Below is a small part of my data table. Each row shown in the table represents a transcript, the first column is the chromosome name, the second column is the transcript start site, and the third column is the transcript end site.

What I want to do now is to compare the 'upper row' end site with the 'next row' start site of every two adjacent rows within the same chromosome. If the 'upper row' end site is greater than the 'next row' start site, i.e. the two transcripts overlap, merge the two transcripts into one and use the 'upper row' start site and the 'next line' end site. It should be noted that I am not comparing 'every pair' of adjacent rows, but only adjacent rows within the same chromosome; this means that adjacent rows with different first column names are not compared at all.

scahrs1_1000    84808   85809
scahrs1_1001    1   753
scahrs1_1001    14931   15932
scahrs1_1001    15232   18008
scahrs1_1001    21211   22212
scahrs1_1001    40908   41909
scahrs1_1001    63233   64234
scahrs1_1001    76009   77010
scahrs1_1002    1068    2069
scahrs1_1002    12992   13993
scahrs1_1002    40448   41449
scahrs1_1003    2227    3228
scahrs1_1003    18453   19454
scahrs1_1003    28679   29680
scahrs1_1003    41161   42162
scahrs1_1003    41735   42736
scahrs1_1003    41867   44041
scahrs1_1003    64416   65417
scahrs1_1003    71219   72220
scahrs1_1003    96090   97091
scahrs1_1003    96754   98307
scahrs1_1004    1554    2555
scahrs1_1004    29086   30087
scahrs1_1004    44100   45101
scahrs1_1004    47799   48800
scahrs1_1004    59550   60551
scahrs1_1004    69356   70357
scahrs1_1004    71809   72810
scahrs1_1004    84272   85273
scahrs1_1004    89034   90035
scahrs1_1004    98627   99628
scahrs1_1005    6695    7696
scahrs1_1005    30160   31161
scahrs1_1006    298 1299
scahrs1_1006    70134   71135
scahrs1_1006    93750   94751
scahrs1_1008    3859    4860
scahrs1_1008    5575    6576
scahrs1_1008    7072    8073
scahrs1_1008    9342    10343
scahrs1_1008    11814   12815
scahrs1_1008    15290   16291
scahrs1_1008    40167   41168
scahrs1_1008    42890   43891
scahrs1_1008    44806   45807
scahrs1_1008    74442   75443
scahrs1_1008    82112   83113
scahrs1_1008    93766   94767
scahrs1_1008    95233   96234
scahrs1_1009    8000    9001
scahrs1_1009    37369   38370
scahrs1_1009    53086   54087
scahrs1_1009    83722   84723
scahrs1_1009    83994   91045
scahrs1_1010    11341   12342
scahrs1_1010    33500   34501
scahrs1_1010    34931   35932
scahrs1_1010    37937   38938

The output of the loop I want to get is:

scahrs1_1000    84808   85809
scahrs1_1001    1   753
scahrs1_1001    14931   18008
scahrs1_1001    21211   22212
scahrs1_1001    40908   41909
scahrs1_1001    63233   64234
scahrs1_1001    76009   77010
scahrs1_1002    1068    2069
scahrs1_1002    12992   13993
scahrs1_1002    40448   41449
scahrs1_1003    2227    3228
scahrs1_1003    18453   19454
scahrs1_1003    28679   29680
scahrs1_1003    41161   44041
scahrs1_1003    64416   65417
scahrs1_1003    71219   72220
scahrs1_1003    96090   98307
scahrs1_1004    1554    2555
scahrs1_1004    29086   30087
scahrs1_1004    44100   45101
scahrs1_1004    47799   48800
scahrs1_1004    59550   60551
scahrs1_1004    69356   70357
scahrs1_1004    71809   72810
scahrs1_1004    84272   85273
scahrs1_1004    89034   90035
scahrs1_1004    98627   99628
scahrs1_1005    6695    7696
scahrs1_1005    30160   31161

Where ‘scahrs1_1001’ merges and reduces by 1 row, and ‘scahrs1_1003’ merges and reduces by 3 rows. I have many rows of data starting with 'scahrs1...'. I think it's might easy to achieve via a loop in R, could anyone give me some advice?

r matrix comparison filtering loop • 837 views
ADD COMMENT
1
Entering edit mode

I'm not completely sure what you mean, but if you want to merge all overlapping ranges within the same chromosome you can use the reduce function from GenomicRanges.

Your example data.

df <- structure(list(V1 = c("scahrs1_1000", "scahrs1_1001", "scahrs1_1001", 
"scahrs1_1001", "scahrs1_1001", "scahrs1_1001", "scahrs1_1001", 
"scahrs1_1001", "scahrs1_1002", "scahrs1_1002", "scahrs1_1002", 
"scahrs1_1003", "scahrs1_1003", "scahrs1_1003", "scahrs1_1003", 
"scahrs1_1003", "scahrs1_1003", "scahrs1_1003", "scahrs1_1003", 
"scahrs1_1003", "scahrs1_1003", "scahrs1_1004", "scahrs1_1004", 
"scahrs1_1004", "scahrs1_1004", "scahrs1_1004", "scahrs1_1004", 
"scahrs1_1004", "scahrs1_1004", "scahrs1_1004", "scahrs1_1004", 
"scahrs1_1005", "scahrs1_1005", "scahrs1_1006", "scahrs1_1006", 
"scahrs1_1006", "scahrs1_1008", "scahrs1_1008", "scahrs1_1008", 
"scahrs1_1008", "scahrs1_1008", "scahrs1_1008", "scahrs1_1008", 
"scahrs1_1008", "scahrs1_1008", "scahrs1_1008", "scahrs1_1008", 
"scahrs1_1008", "scahrs1_1008", "scahrs1_1009", "scahrs1_1009", 
"scahrs1_1009", "scahrs1_1009", "scahrs1_1009", "scahrs1_1010", 
"scahrs1_1010", "scahrs1_1010", "scahrs1_1010"), V2 = c(84808L, 
1L, 14931L, 15232L, 21211L, 40908L, 63233L, 76009L, 1068L, 12992L, 
40448L, 2227L, 18453L, 28679L, 41161L, 41735L, 41867L, 64416L, 
71219L, 96090L, 96754L, 1554L, 29086L, 44100L, 47799L, 59550L, 
69356L, 71809L, 84272L, 89034L, 98627L, 6695L, 30160L, 298L, 
70134L, 93750L, 3859L, 5575L, 7072L, 9342L, 11814L, 15290L, 40167L, 
42890L, 44806L, 74442L, 82112L, 93766L, 95233L, 8000L, 37369L, 
53086L, 83722L, 83994L, 11341L, 33500L, 34931L, 37937L), V3 = c(85809L, 
753L, 15932L, 18008L, 22212L, 41909L, 64234L, 77010L, 2069L, 
13993L, 41449L, 3228L, 19454L, 29680L, 42162L, 42736L, 44041L, 
65417L, 72220L, 97091L, 98307L, 2555L, 30087L, 45101L, 48800L, 
60551L, 70357L, 72810L, 85273L, 90035L, 99628L, 7696L, 31161L, 
1299L, 71135L, 94751L, 4860L, 6576L, 8073L, 10343L, 12815L, 16291L, 
41168L, 43891L, 45807L, 75443L, 83113L, 94767L, 96234L, 9001L, 
38370L, 54087L, 84723L, 91045L, 12342L, 34501L, 35932L, 38938L
)), class = "data.frame", row.names = c(NA, -58L))

GRanges code.

library("GenomicRanges")

gr <- makeGRangesFromDataFrame(df, seqnames.field="V1", start.field="V2", end.field="V3")
result <- reduce(gr)

Results.

> result
GRanges object with 53 ranges and 0 metadata columns:
           seqnames      ranges strand
              <Rle>   <IRanges>  <Rle>
   [1] scahrs1_1000 84808-85809      *
   [2] scahrs1_1001       1-753      *
   [3] scahrs1_1001 14931-18008      *
   [4] scahrs1_1001 21211-22212      *
   [5] scahrs1_1001 40908-41909      *
   ...          ...         ...    ...
  [49] scahrs1_1009 83722-91045      *
  [50] scahrs1_1010 11341-12342      *
  [51] scahrs1_1010 33500-34501      *
  [52] scahrs1_1010 34931-35932      *
  [53] scahrs1_1010 37937-38938      *
  -------
  seqinfo: 10 sequences from an unspecified genome; no seqlengths

I was too lazy to cross reference with your desired results so feel free to yell at me if they don't make sense.

ADD REPLY
4
Entering edit mode

Thank you!

ADD REPLY

Login before adding your answer.

Traffic: 1535 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6