Find matching pattern between columns and count for each distinct element in one column


Find matching pattern between columns and count for each distinct element in one column



I have a tab delimited file with around 70 million rows like the following;


chr1:25851453-25869713W:6655:4522:4234:2258:2508:S 6655
chr1:25851453-25869713W:6655:4523:4234:2258:2508:A 6655
chr1:25851453-25869713W:0000:4524:4234:2258:2508:S 6655
chr1:25851453-25869713W:6655:4525:4234:2258:2508:S 6655
chr1:26235471-26237662W:6663:124:1864:311:455:S 6663
chr1:26235471-26237662W:6663:125:1864:311:455:S 6663



I am trying to report for each distinct element in column 2, how many times the pattern after the second ":" in column 1 matches the pattern in column 2.



So something like this for the above example;


6655 matches 3
6655 mismatches 1
6663 matches 2
6663 mismatches 0



(to be specific there are 15559 distinct values in column 2)



Thanks!





What have you tried so far? Could you post some code you've done?
– matov
Jul 1 at 7:55





Please take a look at editing-help.
– Cyrus
Jul 1 at 8:24




3 Answers
3


$ cat tst.awk
BEGIN { FS="[:t]" }
prev != $NF { if (NR>1) prt(); prev = $NF }
{ cnt[$3 == $NF]++ }
END { prt() }
function prt() {
printf "%d matches %dn", prev, cnt[1]
printf "%d mismatches %dn", prev, cnt[0]
delete cnt
}

$ awk -f tst.awk file
6655 matches 3
6655 mismatches 1
6663 matches 2
6663 mismatches 0



Since your file is large, the above uses the fact that the key values (the last number on each line) are grouped together to allow us to just print the results when the current key value changes instead of storing the data for all key values in an array and then printing it at the end of the file.





Thank you! this worked fine and very fast after sorting the file by the second column.
– zkaragoz
Jul 2 at 10:17



We can use strsplit for this:


strsplit


lapply(setNames(unique(df[, 2]), unique(df[, 2])), function(x)
sum(x == sapply(strsplit(as.character(df[, 1]), ":"), function(y) y[3])))
#$`6655`
#[1] 3
#
#$`6663`
#[1] 2



Explanation: We first determine unique entries from column 2; then for every unique entry we count the number of matches with entries after the second ":" by splitting column 1 entries on ":".


":"


":"



This returns the number of matches as a list.


list



The converse case for the number of mismatches is straightforward and I leave that up to you.


df <- read.table(text =
"chr1:25851453-25869713W:6655:4522:4234:2258:2508:S 6655
chr1:25851453-25869713W:6655:4523:4234:2258:2508:A 6655
chr1:25851453-25869713W:0000:4524:4234:2258:2508:S 6655
chr1:25851453-25869713W:6655:4525:4234:2258:2508:S 6655
chr1:26235471-26237662W:6663:124:1864:311:455:S 6663
chr1:26235471-26237662W:6663:125:1864:311:455:S 6663", header = F)





Thanks for the answer & explanation. This works fine but my problem was mostly that the file is huge and it takes around 20hrs. But apparently working good for smaller data sets.
– zkaragoz
Jul 2 at 10:19





No worries @zkaragoz; I hadn't realised that you were looking for awk/non-R solutions.
– Maurits Evers
Jul 2 at 10:24


awk



One can first extract the FirstElement after 2nd : in column1 using gsub. Then a solution using dplyr can be to group on column2 (e.g. V2) and then count V2 matching and non-matching with FirstElement as:


FirstElement


:


column1


gsub


dplyr


V2


V2


FirstElement


library(dplyr)

df %>%
mutate(FirstElement = gsub("^chr1:[^:]*:(\d+):.*","\1",V1)) %>%
group_by(V2) %>%
summarise(matches = sum(V2==FirstElement), mismatches = sum(V2!=FirstElement))

# # A tibble: 2 x 3
# V2 matches mismatches
# <int> <int> <int>
# 1 6655 3 1
# 2 6663 2 0



Data:


df <- read.table(text="
chr1:25851453-25869713W:6655:4522:4234:2258:2508:S 6655
chr1:25851453-25869713W:6655:4523:4234:2258:2508:A 6655
chr1:25851453-25869713W:0000:4524:4234:2258:2508:S 6655
chr1:25851453-25869713W:6655:4525:4234:2258:2508:S 6655
chr1:26235471-26237662W:6663:124:1864:311:455:S 6663
chr1:26235471-26237662W:6663:125:1864:311:455:S 6663",
stringsAsFactors = FALSE)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

What's the difference between DDS and SOME/IP?

How to make file upload 'Required' in Contact Form 7?