【问题标题】:Rename Multi-FASTA sequence headers using Master File of similar names使用类似名称的主文件重命名多 FASTA 序列标题
【发布时间】:2021-07-13 01:22:58
【问题描述】:

我有多个 FASTA 文件,它们使用非常简单的标头来识别样本。但是,我想通过添加地理位置、来源和文化日期来使标题更加详细。

我的第一个想法是使用R 中的stringr 包来读取每个FASTA 并将任何匹配的序列ID 替换为适当的字符串。

使用带有必要数据和样本 ID 的 .xlsx,我可以创建一个带有一系列字符串的 .txt,其中包含我想要的新名称。使用这个“主文本”文件,我想通过匹配标本 ID 来适当地重命名每个 FASTA 中的每个序列。

所以我创建了rename.txt,格式如下:

SpecimenID|ST|Geographic Location|Source|CultDate
VRE32491|736|PUH - 10C|Blood|2016-12-07
VRE32493|1471|PUH - 10N|Tissue/Surgical|2016-12-08
VRE32503|1471|PUH - 11N|Wound|2017-01-05
VRE32504|1471|PUH - EMEP|Blood|2017-01-10
VRE32514|1471|PUH - 6F|Wound|2017-01-20

使用Biostrings::readDNAStringSet(*.fasta) 我可以在对象上使用names() 获取每个序列的名称。我想从rename.txt 创建一个匹配的字符串,这将使我能够使用names({DNAStringSet object}) <- {matching string} 简单地重命名DNAStringSet 对象。

我的问题是我似乎无法从rename.txt 中提取字符串集。

下面是一些任何人都可以使用 reprex 来模拟​​我的问题的代码:

cat(
  ">VRE32493", "AGCT",
  ">VRE32503", "CAGT",
  ">VRE32504", "TCAA",
  file = "example.fasta", sep = "\n"
)

cat(
  "SpecimenID|ST|Geographic Location|Source|CultDate",
  "VRE32491|736|PUH - 10C|Blood|2016-12-07",
  "VRE32493|1471|PUH - 10N|Tissue/Surgical|2016-12-08",
  "VRE32503|1471|PUH - 11N|Wound|2017-01-05",
  "VRE32504|1471|PUH - EMEP|Blood|2017-01-10",
  "VRE32514|1471|PUH - 6F|Wound|2017-01-20",
  file = "example.txt", sep = "\n"
)

origMult <- Biostrings::readDNAStringSet("example.fasta")
fasta_rename <- read.delim("example.txt", skip = 1, header = F)

example.fasta 的预期输出:

>VRE32493|1471|PUH - 10N|Tissue/Surgical|2016-12-08
AGCT
>VRE32503|1471|PUH - 11N|Wound|2017-01-05
CAGT
>VRE32504|1471|PUH - EMEP|Blood|2017-01-10
TCAA

【问题讨论】:

标签: r bioinformatics fasta


【解决方案1】:

感谢其他地方的帮助制定了这个解决方案。

cat(
  ">VRE32493", "AGCT",
  ">VRE32503", "CAGT",
  ">VRE32504", "TCAA",
  file = "example.fasta", sep = "\n"
)

cat(
  "SpecimenID|ST|Geographic Location|Source|CultDate",
  "VRE32491|736|PUH - 10C|Blood|2016-12-07",
  "VRE32493|1471|PUH - 10N|Tissue/Surgical|2016-12-08",
  "VRE32503|1471|PUH - 11N|Wound|2017-01-05",
  "VRE32504|1471|PUH - EMEP|Blood|2017-01-10",
  "VRE32514|1471|PUH - 6F|Wound|2017-01-20",
  file = "example.txt", sep = "\n"
)

# Read in from example.txt
fasta_rename <- readr::read_file("example.txt")
fasta_rename <- unlist(strsplit(fasta_rename, "\\r"))
fasta_rename <- stringr::str_remove(fasta_rename[-1], "\\n")
fasta_rename <- fasta_rename[-length(fasta_rename)]
# Remove everything after first | to get the pattern to match off of
patterns <- stringr::str_remove(fasta_rename, "\\|(.+)$")
# Make fasta_rename a named character vector in the form of patterns = fasta_rename
names(fasta_rename) <- patterns

fasta_rename # print to verify

example_fasta <- readr::read_file("example.fasta")
example_fasta <- unlist(strsplit(example_fasta, "\\r"))
example_fasta <- stringr::str_remove(example_fasta, "\\n")
example_fasta <- example_fasta[-length(example_fasta)]

example_fasta #print to verify

cat(stringr::str_replace_all(example_fasta, fasta_rename), 
    file = "example.fasta", 
    sep = "\n")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多