Calculating the number of probes for a given genomic range

I have a big data frame like this for copy number (exome seq)

    > head(df)
Chromosome    Start      End
1 1             64613  5707515
2 1           5712940  5732322
3 1           5732399 16383682
4 1          16383742 16389288
5 1          16390813 16830026
6 1          16830201 17278112


I also have coordinate of human exams like this

Exone   Choromosome     Start_of_exone  End_of_exone
uc001aaa.3  chr1    11873   12227
uc001aaa.3  chr1    12612   12721
uc001aaa.3  chr1    13220   14409
uc010nxr.1  chr1    11873   12227
uc010nxr.1  chr1    12645   12697
uc010nxr.1  chr1    13220   14409
uc010nxq.1  chr1    11873   12227
uc010nxq.1  chr1    12594   12721
uc010nxq.1  chr1    13402   14409


Actually I need to interset my copy number coordinates and human exome coordinates to see there are how many exomes in these coordinates

I have used this code but all returns 0

snp_table <- expanse coordinate file
genomic_ranges <- df

how_many_in_range <- function(coords){
coords = as.vector(coords)
sum(snp_table$$Chromosome == coords[1] & snp_table$$Position > as.numeric(coords[2]) & snp_table$Position < as.numeric(coords[3])) } genomic_ranges$number_of_snps <- apply(genomic_ranges, 1, how_many_in_range)


Can you help me?

• I am wondering how you got that calculation from google about the X2 thing. Can you describe in detail what is in your data frame? – Phoenix Mu Apr 2 at 16:00
• I'm sure that's not what @PhoenixMu is asking you - you're literally just telling us what the header already tells us. Some real information would give us context on the data contained in the data frame. Are they probes? exons? random regions? And "google says" is not a valid citation. What is the source of your information on probes to exons ratio? – Ram RS Apr 2 at 17:41
• This is called by "merge by overlap" see for example this (my preferred) answer using data.table::foverlap() – zx8754 Apr 2 at 19:01
• Merge then aggregate. – zx8754 Apr 2 at 20:29

Regardless of what your specific data is, the goal of your analysis is really to find overlaps between two data frames of genomic coordinations. My favorite package for doing this is GenomicRanges. It is very fast and can take care of different strands (if your data contains strand information).

• Sorry I am too stupid in calculation, how I can do that? – Exhausted Apr 3 at 8:48
• Just a quick example, suppose I have two data frames. df1.gr=GRanges(Rle(df1$chr), IRanges(df1$start, df1$end)), df2.gr=GRanges(Rle(df2$chr), IRanges(df2$start, df2$end)). And then ov = findOverlaps(df1.gr, df2.gr). You can also customize some arguments in the findOverlaps function. You can also find more on the GenomicRanges webpage. – Phoenix Mu Apr 3 at 16:48

This command gave me the number of axons I hope this is correct

Please correct me if I am wrong

how_many_in_range <- function(coords){
coords = as.vector(coords)
sum(human_exome_coordinates$$Choromosome == coords[1] & human_exome_coordinates$$Position > as.numeric(coords[2]) & human_exome_coordinates$Position < as.numeric(coords[3])) } genomic_ranges$number_of_exon <- apply(segment_mean_table, 1, how_many_in_range)