我有三个文件:文件1 包含许多叮当声:
AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU
文件2 包含文件1中与左一侧匹配的字符串的子字符串:
AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU
AGAGCUUAGCUGAUUGGUGAAC
文件3 包含文件1中与右侧一侧匹配的字符串的子字符串:
AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU
UUCACAGUGGCUAAGUUCUGC
我想提取位于匹配
的两个字符串之间的字符串AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU
AGAGCUUAGCUGAUUGGUGAAC UUCACAGUGGCUAAGUUCUGC
输出:
AGUGAUUGGUUUCCGCUUUG
我想为 file 1 (约200个字符串)中的所有字符串执行此操作
示例文件1 :
>mmu-let-7g
CCAGGCTGAGGTAGTAGTTTGTACAGTTTGAGGGTCTATGATACCACCCGGTACAGGAGATAACTGTACAGGCCACTGCCTTGCCAGG
>mmu-let-7i
CTGGCTGAGGTAGTAGTTTGTGCTGTTGGTCGGGTTGTGACATTGCCCGCTGTGGAGATAACTGCGCAAGCTACTGCCTTGCTAG
>mmu-miR-1a-1
GCTTGGGACACATACTTCTTTATATGCCCATATGAACCTGCTAAGCTATGGAATGTAAAGAAGTATGTATTTCAGGC
示例文件2 和文件3 :
>mmu-let-7g-5p MIMAT0000121 Mus musculus let-7g-5p
UGAGGUAGUAGUUUGUACAGUU
>mmu-let-7g-3p MIMAT0004519 Mus musculus let-7g-3p
ACUGUACAGGCCACUGCCUUGC
>mmu-let-7i-5p MIMAT0000122 Mus musculus let-7i-5p
UGAGGUAGUAGUUUGUGCUGUU
答案 0 :(得分:2)
假设你正在使用这样的东西:
file1 <- "AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU"
file2 <- "AGAGCUUAGCUGAUUGGUGAAC"
file3 <- "UUCACAGUGGCUAAGUUCUGC"
您可以尝试使用&#34; file2&#34;构建搜索模式。和&#34; file3&#34;并使用gsub
。
示例:
gsub(paste0("(^.*", file2, ")(.*)(", file3, ".*)$"), "\\2", file1)
# [1] "AGUGAUUGGUUUCCGCUUUG"
答案 1 :(得分:1)
您也可以尝试:
str1 <- "AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU"
leftstr <- "AGAGCUUAGCUGAUUGGUGAAC"
rightstr <- "UUCACAGUGGCUAAGUUCUGC"
library(qdap)
unname(genXtract(str1,leftstr, rightstr))
#[1] "AGUGAUUGGUUUCCGCUUUG"
如果您有多个字符串
str2 <- c(str1, "AGGUGUCCCCCCUUUUUGGAGUGGCCGCUUUUGCGGGGUAAUUG")
leftstr1 <- c(leftstr, "CCCCCUUUU")
rightstr1 <- c(rightstr, "UUUUGCGG")
unlist(genXtract(str2, leftstr1, rightstr1))
#[1] "AGUGAUUGGUUUCCGCUUUG" "UGGAGUGGCCGC"
将结果与@Venkataramanan的帖子进行比较
temp1 <- mapply(gsub, leftstr1, "|", str2)
temp2 <- mapply(gsub, rightstr1, "|", temp1)
unlist(strsplit(temp2, "|", fixed=T),use.names=F)[c(F,T,F)]
#[1] "AGUGAUUGGUUUCCGCUUUG" "UGGAGUGGCCGC"
或
scan(text=gsub(paste(rightstr1, collapse="|"),
" ",gsub(paste(leftstr1,collapse="|")," ",str2)),
what="character")[c(F,T,F)]
#Read 6 items
# [1] "AGUGAUUGGUUUCCGCUUUG" "UGGAGUGGCCGC"
或
library(stringr)
str_extract_all(str1, perl(paste0("(?<=",leftstr,")",".*(?=", rightstr,")")))[[1]]
#[1] "AGUGAUUGGUUUCCGCUUUG"
答案 2 :(得分:1)
以下代码完成了这项工作:
temp1<- gsub("AGAGCUUAGCUGAUUGGUGAAC", "|", "AGGUGCAGAGCUUAGCUGAUUGGUGAACAGUGAUUGGUUUCCGCUUUGUUCACAGUGGCUAAGUUCUGCACCU")
temp1
temp2<- gsub("UUCACAGUGGCUAAGUUCUGC","|", temp1)
temp2
unlist(strsplit(temp2, "|", fixed=T))[2]