You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中筛选指定区间内的蛋白质修饰位点并生成新列

筛选蛋白质修饰位点至指定区间的R实现

我们有一个蛋白质数据框,其中ProteinModificationMotifs列是带位点编号的蛋白质修饰信息(逗号分隔),AA_Range列是氨基酸位点的区间字符串(格式如x-y)。需要新增一列PeptideModificationMotifs,只保留位点编号落在对应区间内的修饰信息,无匹配则留空,且不删除任何行。

示例数据

# 示例数据
ProteinModificationMotifs <- c(
    "Glycosylation_49, Glycosylation_255, Glycosylation_399, Glycosylation_437, Glycosylation_455, Glycosylation_536",
    "Glycosylation_32, Glycosylation_101", 
    "Glycosylation_555"
)

AA_Range <- c("400-637", "0-50", "0-444")

peptide_df <- data.frame(ProteinModificationMotifs = ProteinModificationMotifs, AA_Range = AA_Range)

解决方案

可以用dplyr结合stringr包实现需求,代码如下:

library(dplyr)
library(stringr)

peptide_df <- peptide_df %>%
  rowwise() %>%
  mutate(
    # 拆分区间字符串,提取并转换为整数型的上下限
    range_min = as.integer(str_split(AA_Range, "-")[[1]][1]),
    range_max = as.integer(str_split(AA_Range, "-")[[1]][2]),
    # 将修饰字符串拆分为单个修饰条目列表
    mod_list = str_split(ProteinModificationMotifs, ", ")[[1]],
    # 筛选出位点落在区间内的修饰条目
    matched_mods = list(mod_list[as.integer(str_extract(mod_list, "\\d+")) >= range_min & 
                                   as.integer(str_extract(mod_list, "\\d+")) <= range_max]),
    # 将匹配结果合并为字符串,无匹配则留空
    PeptideModificationMotifs = ifelse(length(matched_mods[[1]]) == 0, "", str_c(matched_mods[[1]], collapse = ", "))
  ) %>%
  # 移除中间生成的辅助列
  select(-range_min, -range_max, -mod_list, -matched_mods)

# 查看新列结果
print(peptide_df$PeptideModificationMotifs)

代码说明

  • rowwise():指定按行处理数据,保证每一行的区间和修饰信息对应匹配
  • 拆分AA_Range得到区间上下限,转为整数用于后续判断
  • 拆分ProteinModificationMotifs为单个修饰条目,方便逐个检查位点
  • 用str_extract提取修饰条目里的数字位点,判断是否在区间内,筛选符合条件的修饰
  • 将筛选后的修饰条目合并为逗号分隔的字符串,无匹配时设为空字符串
  • 最后移除辅助列,保留原数据列和新生成的PeptideModificationMotifs列

运行结果

[1] "Glycosylation_437, Glycosylation_455, Glycosylation_536"
[2] "Glycosylation_32"                                       
[3] ""

内容的提问来源于stack exchange,提问作者John H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:03:12