英文:
How to remove character strings that are detected/contained within other character strings, but only within a specified group_by()-argument
问题
以下是您要求的翻译的内容:
让我们假设我有:
> w
digest gene seq
1 InS AB0583 AAB
2 InS AB0583 AABKR
3 InS AB0583 GFHGHGG
4 PAC PU83022 EUT
5 PAC PU83022 HSFSFJF
6 PAC PU83022 EUTCK
7 PAC PU83022 EUTCKJ
8 InS PO93853 HDGJ
9 InS PO93853 HDGJU
10 InS PO93853 YTYEYD
11 InS PO93853 YTYEYDJHSGSG
12 InS PO93853 SALGHAGGEE
我已经应用了两种不同的方法来识别蛋白质(使用它们的基因名w$gene
解码)。这些方法编码在w$digest
中。如您所见,w$gene
中的每个w$digest
内可能存在w$seq
的重叠序列,例如EUT
也在EUTCK
内,而后者又在EUTCKJ
内。
我想知道已经识别出了多少个唯一的氨基酸,即w$seq
中的每个字母。因此,在grouped_by(digest, gene)
时,我需要删除任何被检测到在另一个字符串中的字符字符串,但仅保留具有最多字符的字符字符串。
我在tidyverse
中寻求解决方案。
帮助需要:
(1) 计算字符数并按以下方式排列:
w <- w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count)
因此:
> w
digest gene seq count
1 InS AB0583 AAB 3
2 InS AB0583 AABKR 5
3 InS AB0583 GFHGHGG 7
4 InS PO93853 HDGJ 4
5 InS PO93853 HDGJU 5
6 InS PO93853 YTYEYD 6
(2) group_by(digest, gene)
,现在删除在另一个w$seq
内被检测到的行(在此分组内),保留具有最多字符的行。
输出
> w
digest gene seq count
1 InS AB0583 AABKR 5 #*
2 InS AB0583 GFHGHGG 7
3 InS PO93853 HDGJU 5 #**
4 InS PO93853 SALGHAGGEE 10
5 InS PO93853 YTYEYDJHSGSG 12 #***
6 PAC PU83022 EUTCKJ 6 #****
7 PAC PU83022 HSFSFJF 7
因此,预期的输出:
> w
digest gene seq count
1 InS AB0583 AABKR 5
2 InS AB0583 GFHGHGG 7
3 InS PO93853 HDGJU 5
4 InS PO93853 SALGHAGGEE 10
5 InS PO93853 YTYEYDJHSGSG 12
6 PAC PU83022 EUTCKJ 6
7 PAC PU83022 HSFSFJF 7
数据
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
希望这能帮助您理解问题的解决方案。如果您有任何进一步的问题,请随时提出。
英文:
Let's say I have:
> w
digest gene seq
1 InS AB0583 AAB
2 InS AB0583 AABKR
3 InS AB0583 GFHGHGG
4 PAC PU83022 EUT
5 PAC PU83022 HSFSFJF
6 PAC PU83022 EUTCK
7 PAC PU83022 EUTCKJ
8 InS PO93853 HDGJ
9 InS PO93853 HDGJU
10 InS PO93853 YTYEYD
11 InS PO93853 YTYEYDJHSGSG
12 InS PO93853 SALGHAGGEE
I have applied two different methods to identify proteins (decoded with their gene name, w$gene
). The methods are encoded in w$digest
. As you can see, there may be overlapping sequences of w$seq
within each w$gene
within each w$digest
- e.g. EUT
is also within EUTCK
, which is within EUTCKJ
.
I want to know how many unique amino acids, each letter in w$seq
, were identified. Therefore, I need to remove any/all character string(s) that where detected within another character string, but only when grouped_by(digest, gene)
. The character string with most characters should be kept.
I seek a solution in tidyverse
Help need:
(1) Count the number of characters, and arrange as follows:
w <- w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count)
So that
> w
digest gene seq count
1 InS AB0583 AAB 3
2 InS AB0583 AABKR 5
3 InS AB0583 GFHGHGG 7
4 InS PO93853 HDGJ 4
5 InS PO93853 HDGJU 5
6 InS PO93853 YTYEYD 6
(2) group_by(digest, gene)
, and now remove rows that contain a w$seq
that is detected within another w$seq
(within this grouping), and keep the row where the w$seq
has most characters.
Output
> w
digest gene seq count
1 InS AB0583 AAB 3 #* found within:
2 InS AB0583 AABKR 5 #*
3 InS AB0583 GFHGHGG 7
4 InS PO93853 HDGJ 4 #** found within:
5 InS PO93853 HDGJU 5 #**
6 InS PO93853 YTYEYD 6 #***
7 InS PO93853 SALGHAGGEE 10
8 InS PO93853 YTYEYDJHSGSG 12 #***
9 PAC PU83022 EUT 3 #****
10 PAC PU83022 EUTCK 5 #****
11 PAC PU83022 EUTCKJ 6 #****
12 PAC PU83022 HSFSFJF 7
Therefore, Expected output
> w
digest gene seq count
1 InS AB0583 AABKR 5
2 InS AB0583 GFHGHGG 7
3 InS PO93853 HDGJU 5
4 InS PO93853 SALGHAGGEE 10
5 InS PO93853 YTYEYDJHSGSG 12
6 PAC PU83022 EUTCKJ 6
7 PAC PU83022 HSFSFJF 7
Data
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
答案1
得分: 3
对于group_by()
中的每个组,您可以创建一个新的列表列,其中每一行包含该组的所有seq
值。然后,您可以进行逐行操作,计算每个seq
值在所有值中出现的次数。保留仅出现一次的值将给您想要的结果。
library(dplyr)
library(stringr)
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
w <- w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count)
w %>% group_by(digest, gene) %>%
mutate(all_vals = list(seq)) %>%
rowwise() %>%
mutate(win = sum(grepl(seq, all_vals))) %>%
filter(win == 1) %>%
dplyr::select(-c(win, all_vals))
<sup>创建于2023年5月25日,使用reprex v2.0.2</sup>
英文:
For each group in group_by()
you could make a new list column where each row contains all the seq
values for that group. You could then do a row-wise operation where you count the number of times each value of seq
shows up in all the values. Keeping the ones that only show up once will give you the result you want.
library(dplyr)
library(stringr)
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
w <- w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count)
w %>% group_by(digest, gene) %>%
mutate(all_vals = list(seq)) %>%
rowwise() %>%
mutate(win = sum(grepl(seq, all_vals))) %>%
filter(win == 1) %>%
dplyr::select(-c(win, all_vals))
#> # A tibble: 7 × 4
#> # Rowwise: digest, gene
#> digest gene seq count
#> <chr> <chr> <chr> <int>
#> 1 InS AB0583 AABKR 5
#> 2 InS AB0583 GFHGHGG 7
#> 3 InS PO93853 HDGJU 5
#> 4 InS PO93853 SALGHAGGEE 10
#> 5 InS PO93853 YTYEYDJHSGSG 12
#> 6 PAC PU83022 EUTCKJ 6
#> 7 PAC PU83022 HSFSFJF 7
<sup>Created on 2023-05-25 with reprex v2.0.2</sup>
答案2
得分: 1
以下是一个潜在的解决方案:
library(tidyverse)
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count) %>%
group_by(digest, gene) %>%
filter(str_count(paste0(seq, collapse = "_"), seq) == 1)
#> # A tibble: 7 × 4
#> # Groups: digest, gene [3]
#> digest gene seq count
#> <chr> <chr> <chr> <int>
#> 1 InS AB0583 AABKR 5
#> 2 InS AB0583 GFHGHGG 7
#> 3 InS PO93853 HDGJU 5
#> 4 InS PO93853 SALGHAGGEE 10
#> 5 InS PO93853 YTYEYDJHSGSG 12
#> 6 PAC PU83022 EUTCKJ 6
#> 7 PAC PU83022 HSFSFJF 7
Created on 2023-05-25 with reprex v2.0.2
原始答案:
这有点尴尬,但它“有效”:
library(tidyverse)
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count) %>%
group_by(digest, gene) %>%
mutate(strings = paste0(seq, collapse = "|")) %>%
rowwise() %>%
mutate(strings = gsub(paste0("\\b", seq, "\\b"), "", strings)) %>%
filter(!grepl(seq, strings)) %>%
select(-strings) %>%
ungroup()
#> # A tibble: 7 × 4
#> digest gene seq count
#> <chr> <chr> <chr> <int>
#> 1 InS AB0583 AABKR 5
#> 2 InS AB0583 GFHGHGG 7
#> 3 InS PO93853 HDGJU 5
#> 4 InS PO93853 SALGHAGGEE 10
#> 5 InS PO93853 YTYEYDJHSGSG 12
#> 6 PAC PU83022 EUTCKJ 6
#> 7 PAC PU83022 HSFSFJF 7
Created on 2023-05-25 with reprex v2.0.2
英文:
Edit:
Here is a potential solution:
library(tidyverse)
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count) %>%
group_by(digest, gene) %>%
filter(str_count(paste0(seq, collapse = "_"), seq) == 1)
#> # A tibble: 7 × 4
#> # Groups: digest, gene [3]
#> digest gene seq count
#> <chr> <chr> <chr> <int>
#> 1 InS AB0583 AABKR 5
#> 2 InS AB0583 GFHGHGG 7
#> 3 InS PO93853 HDGJU 5
#> 4 InS PO93853 SALGHAGGEE 10
#> 5 InS PO93853 YTYEYDJHSGSG 12
#> 6 PAC PU83022 EUTCKJ 6
#> 7 PAC PU83022 HSFSFJF 7
<sup>Created on 2023-05-25 with reprex v2.0.2</sup>
Original answer:
This is a bit awkward, but it 'works':
library(tidyverse)
w <- data.frame(
digest = c(rep("InS", 3), rep("PAC", 4), rep("InS", 5)),
gene = c(rep("AB0583", 3), rep("PU83022", 4), rep("PO93853", 5)),
seq = c("AAB", "AABKR", "GFHGHGG",
"EUT", "HSFSFJF", "EUTCK", "EUTCKJ",
"HDGJ", "HDGJU", "YTYEYD", "YTYEYDJHSGSG", "SALGHAGGEE")
)
w %>%
mutate(count = str_count(seq)) %>%
arrange(digest, gene, count) %>%
group_by(digest, gene) %>%
mutate(strings = paste0(seq, collapse = "|")) %>%
rowwise() %>%
mutate(strings = gsub(paste0("\\b", seq, "\\b"), "", strings)) %>%
filter(!grepl(seq, strings)) %>%
select(-strings) %>%
ungroup()
#> # A tibble: 7 × 4
#> digest gene seq count
#> <chr> <chr> <chr> <int>
#> 1 InS AB0583 AABKR 5
#> 2 InS AB0583 GFHGHGG 7
#> 3 InS PO93853 HDGJU 5
#> 4 InS PO93853 SALGHAGGEE 10
#> 5 InS PO93853 YTYEYDJHSGSG 12
#> 6 PAC PU83022 EUTCKJ 6
#> 7 PAC PU83022 HSFSFJF 7
<sup>Created on 2023-05-25 with reprex v2.0.2</sup>
答案3
得分: 1
Using base:
# 添加计数并排序
w$count <- nchar(w$seq)
w <- w[ with(w, order(digest, gene, count)), ]
# 子集
w[ sapply(Map(grepl, w$seq, ave(w$seq, w[ 1:2 ], FUN = list)), sum) == 1, ]
# digest gene seq count
# 2 InS AB0583 AABKR 5
# 3 InS AB0583 GFHGHGG 7
# 9 InS PO93853 HDGJU 5
# 12 InS PO93853 SALGHAGGEE 10
# 11 InS PO93853 YTYEYDJHSGSG 12
# 7 PAC PU83022 EUTCKJ 6
# 5 PAC PU83022 HSFSFJF 7
英文:
Using base:
# add count and sort
w$count <- nchar(w$seq)
w <- w[ with(w, order(digest, gene, count)), ]
# subset
w[ sapply(Map(grepl, w$seq, ave(w$seq, w[ 1:2 ], FUN = list)), sum) == 1, ]
# digest gene seq count
# 2 InS AB0583 AABKR 5
# 3 InS AB0583 GFHGHGG 7
# 9 InS PO93853 HDGJU 5
# 12 InS PO93853 SALGHAGGEE 10
# 11 InS PO93853 YTYEYDJHSGSG 12
# 7 PAC PU83022 EUTCKJ 6
# 5 PAC PU83022 HSFSFJF 7
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论