bam 可视化:samtools tview 详细解释

bam可视化软件大家可能熟悉的是IGV,然而,IGV对于大多数linux用户来说并不友好,而samtools tview可以很好满足该需求。

话不多说直接上命令行:

samtools tview -p chr1:3128088 NA12878.bam hg38.fasta

-p 指定染色体的位置,tview从指定的位置开始显示
NA12878.bam 比对结果bam文件,需要构建索引(NA12878.bam.bai)
hg38.fasta 比对时使用的fasta文件,如果不提供则第一行会显示为N. 需要提供fai索引(hg38.fasta.fai)

运行之后就会进入samtools tview 的交互界面:
在这里插入图片描述
结果如上图所示,图是不是让你很蒙圈,其实符号用的是Pileup_format表示的,参考https://en.wikipedia.org/wiki/Pileup_format
. (dot) 与正链匹配的碱基
, (comma) 与反链匹配的碱基
</> (less-/greater-than sign) denotes a reference skip. This occurs, for example, if a base in the reference genome is intronic and a read maps to two flanking exons. If quality scores are given in a sixth column, they refer to the quality of the read and not the specific base.
AGTCN (upper case) denotes a base that did not match the reference on the forward strand
agtcn (lower case) denotes a base that did not match the reference on the reverse strand
A sequence matching the regular expression +[0-9]+[ACGTNacgtn]+ denotes an insertion of one or more bases starting from the next position. For example, +2AG means insertion of AG in the forward strand
A sequence matching the regular expression -[0-9]+[ACGTNacgtn]+ denotes a deletion of one or more bases starting from the next position. For example, -2ct means deletion of CT in the reverse strand
^ (caret) marks the start of a read segment and the ASCII of the character following `^’ minus 33 gives the mapping quality
$ (dollar) marks the end of a read segment
* (asterisk) is a placeholder for a deleted base in a multiple basepair deletion that was mentioned in a previous line by the -[0-9]+[ACGTNacgtn]+ notation

此外, 按下 “shift+?” 即可显示帮助菜单栏,如下图所示:
在这里插入图片描述

  1. 按下 g ,则提示输入要到达基因组的某一个位点
  2. 使用H(左)J(上)K(下)L(右)移动显示界面。
  3. Ctrl+H 向左移动1kb碱基; Ctrl+L 向右移动1kb碱基
  4. 可以用颜色标注比对质量,碱基质量,核苷酸等。
    30~40的碱基质量或比对质量使用白色表示;
    20~30黄色;
    10~20绿色;
    0~10蓝色。
  5. 使用点号’.'切换显示碱基和点号;
  6. 使用r切换显示read name
    7)其他功能等等
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
samtools tview 是一个用于查看 BAM/SAM 文件的交互式工具,其中包含了基因组序列、测序深度、序列比对等信息。要实现类似的功能,可以使用 C 语言结合 ncurses 库来实现。 具体实现过程如下: 1. 读取 BAM/SAM 文件,解析出每个碱基的信息,包括碱基序列、测序深度、比对情况等。 2. 解析出参考基因组序列,并将其与 BAM/SAM 文件中的碱基信息对齐。可以采用字符串匹配算法,如 KMP 算法、Boyer-Moore 算法等。 3. 使用 ncurses 库来绘制终端界面,包括参考基因组序列、碱基序列、测序深度等。 4. 监听用户的输入,根据用户的输入来移动视窗、放大缩小等操作。可以使用 getch() 函数来获取用户输入。 5. 在屏幕上实时更新视窗中的内容,保持界面的交互性。 需要注意的是,由于 BAM/SAM 文件可能非常大,因此需要采用分块读取的方式,避免内存溢出和速度过慢的问题。同时,为了提高程序的效率,可以使用多线程技术来实现并行读取和处理。 以下是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ncurses.h> #define MAX_SEQ_LEN 10000 #define MAX_READ_LEN 10000 #define MAX_DEPTH 1000 typedef struct { char seq[MAX_SEQ_LEN]; int len; } Sequence; typedef struct { char qname[256]; int flag; char rname[256]; int pos; int mapq; char cigar[256]; char rnext[256]; int pnext; int tlen; char seq[MAX_READ_LEN]; char qual[MAX_READ_LEN]; int len; } Read; typedef struct { Sequence ref; Read *reads; int num_reads; } Alignment; void read_alignment(const char *filename, Alignment *alignment) { // TODO: 读取 BAM/SAM 文件,解析出参考基因组序列与碱基信息 // 将解析出的信息存储到 alignment 结构体中 } void draw_alignment(const Alignment *alignment, int win_start, int win_end) { int ref_pos = 0; int read_pos[MAX_DEPTH] = {0}; int depth[MAX_SEQ_LEN] = {0}; // 绘制参考基因组序列 move(0, 0); for (int i = win_start; i <= win_end; i++) { addch(alignment->ref.seq[i]); } // 绘制碱基序列和测序深度 for (int i = 0; i < alignment->num_reads; i++) { const Read *read = &alignment->reads[i]; int read_len = read->len; int read_start = read->pos; int read_end = read_start + read_len - 1; // 检查 read 是否在视窗内 if (read_start > win_end || read_end < win_start) { continue; } // 绘制测序深度 for (int j = read_start; j <= read_end; j++) { depth[j]++; } // 绘制碱基序列 move(i + 1, 0); for (int j = win_start; j <= win_end; j++) { if (j >= read_start && j <= read_end) { addch(read->seq[j - read_start]); } else { addch(' '); } } } // 绘制测序深度 for (int i = 0; i <= win_end - win_start; i++) { int d = depth[i + win_start]; if (d > 0) { move(alignment->num_reads + d, i); addch(' '); } } } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s <bam/sam file>\n", argv[0]); return 1; } // 初始化 ncurses 库 initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); // 读取 BAM/SAM 文件 Alignment alignment; read_alignment(argv[1], &alignment); // 计算视窗大小和起始位置 int win_start = 0; int win_end = COLS - 1; if (win_end > alignment.ref.len) { win_end = alignment.ref.len - 1; } // 绘制初始界面 draw_alignment(&alignment, win_start, win_end); refresh(); // 处理用户输入 int ch; while ((ch = getch()) != KEY_F(1)) { switch (ch) { case KEY_LEFT: if (win_start > 0) { win_start--; win_end--; draw_alignment(&alignment, win_start, win_end); } break; case KEY_RIGHT: if (win_end < alignment.ref.len - 1) { win_start++; win_end++; draw_alignment(&alignment, win_start, win_end); } break; case KEY_UP: // TODO: 上下滚动视窗 break; case KEY_DOWN: // TODO: 上下滚动视窗 break; case KEY_RESIZE: // TODO: 处理终端大小改变事件 break; } // 刷新屏幕 refresh(); } // 关闭 ncurses 库 endwin(); return 0; } ``` 需要注意的是,以上代码只是一个简单的示例,实际上还有很多细节需要处理,如输入法切换、超长行处理、多字节字符的显示等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值