跳到正文
章节与参考

数据与工作流 · 第 2 节,共 12 节

6.1 读取序列与注释文件

下载 R 脚本包含本页准备与全部步骤。文件示例须先下载输入;预览和保存调用保留。
本页目录
R · 示例代码
library(ggchord)
library(dplyr)
准备数据与配置(展开)
R · 示例代码
library(dplyr)
seqs <- seq_data_example |>
  select(accver, length)
links <- ribbon_data_example |>
  select(qaccver, saccver, qstart, qend, sstart, send)
# 从原始注释字段恢复真实链。
genes <- gene_data_example |>
  transmute(accver, start, end, strand = source_strand)

先解压真实输入包,工作目录须含 examples/;每节单列可选依赖。

1 把已有表格转换成需要的列

这里使用随包的实际序列、连接和基因, 只重命名列来演示角色映射;没有额外生成示意序列或比对。

R · 示例代码
renamed_sequences <- seqs |>
  rename(chrom = accver, size = length)
sequence_input <- as_ggchord_seq(renamed_sequences, mapping = c(accver = "chrom", length = "size"))
renamed_features <- genes |>
  rename(chrom = accver, left = start, right = end)
feature_input <- as_ggchord_feature(renamed_features,
  mapping = c(accver = "chrom", start = "left", end = "right")
)
link_input <- as_ggchord_link(links)
输出 / 文本
  .source_row    qaccver    saccver qstart  qend sstart  send
1           1 MT108731.1 MT118296.1  26298 51139   7121 31959
2           2 MT108731.1 MT118296.1  21513 25922   2365  6772
3           3 MT108731.1 MT118296.1  20376 20677    403   704
4           4 MT108731.1 OQ646790.1  45278 50839  27955 33476

转换返回普通数据框,并记录 ggchord_conversion 信息。标识原样保留;缺少完整 序列长度时应补充可靠来源,不能把最大注释端点当作全长。核酸与蛋白比对的最小列 和单位见导入比对结果。

2 GenBank:保留记录身份

R · 示例代码
# NCBI 原始 L09137.2 记录名为 pUC19c,保留该标识。
genbank_result <- NULL
if (nzchar(Sys.which("python3")) && identical(suppressWarnings(system2("python3",
  c("-c", shQuote("from Bio import SeqIO")),
  stdout = FALSE, stderr = FALSE
)), 0L)) {
  genbank_result <- read_genbank("examples/format-inputs/pUC19c-L09137.2.gb")
}
输出 / 文本
 NULL

这个文件是 NCBI 的原始 L09137.2 记录,名称是 pUC19c;不要把它强行改成随包 pUC19。 返回值包括 seq_data、features$annotation 与 report,分段位置及记录/注释来源 保留。这里需要所选 Python 已安装 Biopython;函数不自动安装依赖。 unsupported = "report" 将不支持的位置记入报告,不把远端或复杂位置猜成区间。

本例已在启用 Python Biopython 1.85 的环境重跑,实际返回 L09137.2、2686 bp 及一行注释。可在独立文档环境使用对应 Python 的 python3 -m pip install biopython 安装,并在启动 R 前将其可执行文件加入 PATH。可选依赖导致跳过或返回 NULL 时,不应记作导入成功。

显式 python 路径保留选定虚拟环境,包括解释器符号链接;不安装或改动环境。

解压说明输入包,使工作目录包含 examples/。每次先读一种文件并检查返回的几行,再按序列 ID 对齐输入。

3 FASTA 长度与 GFF3 注释

R · 示例代码
head(read_fasta_lengths(files = "examples/fasta/*.fna"), 4)
accver length .source_line
MT108731.1 64323 1
MT118296.1 32090 1
OQ646790.1 57367 1
OR222515.1 83080 1

FASTA 给出完整长度。GFF3 返回区间、类型和原始链,绘图前可按需保留 CDS。

R · 示例代码
gff <- read_gff3(files = "examples/gff3/*.gff3", unstranded = "keep")
head(gff[c("accver", "start", "end", "strand", "type")], 4)
accver start end strand type
MT108731.1 13064 14554 + CDS
MT108731.1 22920 23705 + CDS
MT108731.1 23894 24814 + CDS
MT108731.1 27115 28053 + CDS

4 读取 BLAST 表

R · 示例代码
hits <- read_blast(file = "examples/blastn-standard/MT108731.1__MT118296.1.tsv")
head(hits[c("qaccver", "saccver", "qstart", "qend", "sstart", "send")], 3)
qaccver saccver qstart qend sstart send
MT108731.1 MT118296.1 26298 51139 7121 31959
MT108731.1 MT118296.1 21513 25922 2365 6772
MT108731.1 MT118296.1 20691 21146 1032 1495

每一行是一对区间。保留两端坐标顺序,核对两列序列 ID 能在 FASTA 中找到。如何生成标准 BLAST 文件见比对输入。

5 直接转换 Bioconductor 对象

已安装 GenomicRanges 时,可从一条区间转换成特征表。已知完整长度写在 seqlengths 中,可另用 as_ggchord_seq(gr) 提取。

R · 示例代码
gr <- GenomicRanges::GRanges("a", IRanges::IRanges(10, 30), strand = "+", seqlengths = c(a = 100))
as_ggchord_feature(gr)
accver start end strand .source_row directionality source_strand
a 10 30 + 1 forward +

GInteractions 的第一端是 query,第二端是 subject。下面的最小输入用两个区间构成一条连接。此示例另需 InteractionSet。

R · 示例代码
query <- GenomicRanges::GRanges("a", IRanges::IRanges(10, 30), seqlengths = c(a = 100))
subject <- GenomicRanges::GRanges("b", IRanges::IRanges(40, 60), seqlengths = c(b = 120))
pair <- InteractionSet::GInteractions(query, subject)
as_ggchord_link(pair)
qaccver saccver qstart qend sstart send qstrand sstrand .source_row .query_anchor .subject_anchor orientation_known direction query_directionality subject_directionality
a b 10 30 40 60 * * 1 1 2 FALSE None none none

其他输入对象见导入与转换。用转换后的表添加图层。

搜索说明

↑ ↓ 选择 · Enter 打开 · Esc 关闭;也可按任务查找。 API 与功能