Group by terms that matched partially with tidyverse or equivalent (collapse sequencing data into groups)

I am not sure whether tidyverse supports fuzzy search or grouping and I would like to collapse some sequencing data into groups. This is because the sequencer may generate some artificial Ns at the ends, while the entire middle stretch is still identical. I would like to find an efficient way to isolate these sequences and group them (and count them).

For example,
ATCGACACACACACACACACACAATC, NTCGACACACACACACACACACAATC, and NNCGACACACACACACACACACAATN will be grouped into the same group by sharing the CGACACACACACACACACACAAT. Importantly, CGACACACACACACACACACAAT is the entire and representative sequence, and it will be used for the group name.

Some other constraints are

  1. The Ns should only be found at both ends (at either one end, or both ends). Hence, if the Ns are found in the middle of the reads, they should be categorized as separate groups.
  2. I am only concerning standard DNA (or amino acid) codes. Hence, DNA will use ATCG and N represents the random base while amino acid will use ARNDBCEQZGHILKMFPSTWYV and X represents the random residue.

Below is a toy dataset and the expected result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>data<- data.frame(DNA = c("ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "AGAGTCTCGATCG", "TCTCTGATGCTAAA", "TAGCTAGACTAGCATCGACTACGACT", "TAGCTAGACNNGCATCGACTACGACT", "TAGCTAGACNAGCATCGACTACGACT", "TAGCTAGACTAGCATCGACTACGACT", "TAGCTAGACTAGCATCGACTACGACT", "NAGCTAGACTAGCATCGACTACGACT", "NNCTAGCATCGACTACGACT", "NNCTAGCATCGACTACNACT"))
grouped_data <- data.frame(DNA_group = c("CGACACACACACACACACACAAT", # as mentioned in the description above
"AGAGTCTCGATCG", # noted that the sequence might have differen length, ideally, the suggested code let me define minimum matched length between 2 sequence, but let say 10 now, while reality will be over 100
"TCTCTGATGCTAAA",
"CTAGCATCGACTACGACT", # same as the first example
"TAGCTAGACNNGCATCGACTACGACT", # Ns were found at the middle
"TAGCTAGACNAGCATCGACTACGACT", # Ns were found at the middle
"CTAGCATCGACTACNACT"), # Ns were found at the middle and no matter Ns were also found at the ends, the displayed results should ideally (but optionally to this question's answer) have the Ns at the end trimmed
count = c(11, 1, 1, 5, 1, 1, 1))
</code>
<code>data<- data.frame(DNA = c("ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "AGAGTCTCGATCG", "TCTCTGATGCTAAA", "TAGCTAGACTAGCATCGACTACGACT", "TAGCTAGACNNGCATCGACTACGACT", "TAGCTAGACNAGCATCGACTACGACT", "TAGCTAGACTAGCATCGACTACGACT", "TAGCTAGACTAGCATCGACTACGACT", "NAGCTAGACTAGCATCGACTACGACT", "NNCTAGCATCGACTACGACT", "NNCTAGCATCGACTACNACT")) grouped_data <- data.frame(DNA_group = c("CGACACACACACACACACACAAT", # as mentioned in the description above "AGAGTCTCGATCG", # noted that the sequence might have differen length, ideally, the suggested code let me define minimum matched length between 2 sequence, but let say 10 now, while reality will be over 100 "TCTCTGATGCTAAA", "CTAGCATCGACTACGACT", # same as the first example "TAGCTAGACNNGCATCGACTACGACT", # Ns were found at the middle "TAGCTAGACNAGCATCGACTACGACT", # Ns were found at the middle "CTAGCATCGACTACNACT"), # Ns were found at the middle and no matter Ns were also found at the ends, the displayed results should ideally (but optionally to this question's answer) have the Ns at the end trimmed count = c(11, 1, 1, 5, 1, 1, 1)) </code>
data<- data.frame(DNA = c("ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "NNCGACACACACACACACACACAATN", "ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "ATCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "NTCGACACACACACACACACACAATC", "AGAGTCTCGATCG", "TCTCTGATGCTAAA", "TAGCTAGACTAGCATCGACTACGACT", "TAGCTAGACNNGCATCGACTACGACT", "TAGCTAGACNAGCATCGACTACGACT", "TAGCTAGACTAGCATCGACTACGACT", "TAGCTAGACTAGCATCGACTACGACT", "NAGCTAGACTAGCATCGACTACGACT", "NNCTAGCATCGACTACGACT", "NNCTAGCATCGACTACNACT"))


grouped_data <- data.frame(DNA_group = c("CGACACACACACACACACACAAT", # as mentioned in the description above
                                         "AGAGTCTCGATCG", # noted that the sequence might have differen length, ideally, the suggested code let me define minimum matched length between 2 sequence, but let say 10 now, while reality will be over 100
                                         "TCTCTGATGCTAAA", 
                                         "CTAGCATCGACTACGACT", # same as the first example
                                         "TAGCTAGACNNGCATCGACTACGACT", # Ns were found at the middle
                                         "TAGCTAGACNAGCATCGACTACGACT", # Ns were found at the middle
                                         "CTAGCATCGACTACNACT"), # Ns were found at the middle and no matter Ns were also found at the ends, the displayed results should ideally (but optionally to this question's answer) have the Ns at the end trimmed
                           count = c(11, 1, 1, 5, 1, 1, 1))

I tried to use grep or gsub to make a temporary column and count for that column. However, I am not sure if that can accommodate dynamic grep (as the group name will be different and I am not sure what would be the longest representative sequence for each group).

I prefer to do the job in R, but I will consider other open-source languages if they do the job efficiently. Thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật