# Extracting the number SNP in each range

I have called copy number and I have 2 files (I have shared the link of my files ); One contains some ranges

> head(cndata[,c(2,3,4)])
Chromosome   Start      End
4470          1   51479   817980
4471          1  818499  1136753
4472          1 1138735  2558308
4473          1 2558740  5724264
4474          1 5724940  5749083
4475          1 5749226 12529544
>


https://www.dropbox.com/s/wei3i787c9nrz8q/WTSI-OESO_121_1pre.copynumber.caveman.txt.csv?dl=0

I have another file like below

              Chromosome         Position
rs62635286       1                 51479
rs75454623       1                 114930
rs806731         1                 30923


https://www.dropbox.com/s/tiqjhkrpfr9d9gu/WTSI-OESO_121_1pre.copynumber.txt?dl=0

In first column I have some SNPs, how I can count the number of SNP in each range? For example based on the second table how many SNP are in range of 51479 to 817980?

• Repost of biostars.org/p/376315 Apr 26, 2019 at 12:26
• We need to "merge with overlap/range" then "group by count", searching these terms should help you solve the problem. Apr 26, 2019 at 12:52
• @FereshTeh I think people are getting a bit upset if you crosspost in different sites and it's not like you are a new user who just does not know. You are here for a while and you know how this site works. You must have noticed that crossposting is not very popular and if you do, at least you can explicitly mention it in the question. Apr 26, 2019 at 13:50
• Even better, if you get answers in one of the two places you ask, at least take the time to post them in the other. So if you get an answer here, also post it on your question at biostars and if you get an answer there, also post it here. That way, both communities benefit. Apr 26, 2019 at 14:53
• @FereshTeh I found a good article for you. Try always explain the problem (you do that moreless already), what have you tried to resolve it (this one you need to work on) and how to reproduce the problem you find (this one you do, but it would be nicer if you shared samples, not dropbox links). Apr 29, 2019 at 15:00

There is like a thousand different ways how to achieve this. You could use a specialised software for this (like bedtools) or calculate it simply in R.

R solution: You can make a function that calculates the number of SNPs in a range and than you can apply it on all the ranges in the table with genomic ranges.

snp_table <- read.table('WTSI-OESO_121_1pre.copynumber.txt')
genomic_ranges <- read.table('WTSI-OESO_121_1pre.copynumber.caveman.txt.csv', sep = ',', col.names = c('Chromosome', 'Start', 'End'), stringsAsFactors = F)

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)


Now the table genomic_ranges has a variable that sais how many SNPs are in the range.

• You know, the problem others have with your question is not about your coding skills, but more about the crossposting that really annoys the heck out of everybody, also your questions do not clearly show much research effort. It might be that you spent days trying to figure it out, but from your question, we can not tell, because you did not write what you have tried. Finally, I don't think self-hatred is useful to improve your coding skills, reading manuals and trying it out is way more efficient. Apr 26, 2019 at 21:03