samsift-筛选你的路线
samsift的Python项目详细描述
samsift是一个高级过滤和标记sam/bam对齐的程序 使用python表达式。
开始
# clone this repo and add it to PATH git clone http://github.com/karel-brinda/samsift cd samsift exportPATH=$(pwd)/samsift:$PATH# filtering: keep only alignments with score >94, save them as filtered.bam samsift -i tests/test.bam -o filtered.bam -f 'AS>94'# filtering: keep only unaligned reads samsift -i tests/test.bam -f 'FLAG & 0x04'# filtering: keep only aligned reads samsift -i tests/test.bam -f 'not(FLAG & 0x04)'# filtering: keep only sequences containing ACCAGAGGAT samsift -i tests/test.bam -f 'SEQ.find("ACCAGAGGAT")!=-1'# filtering: keep only sequences containing A and T only (defined using regular expressions) samsift -i tests/test.bam -f 're.match(r"^[AT]*$", SEQ)'# filtering: sample alignments with 25% rate samsift -i tests/test.bam -f 'random.random()<0.25'# filtering: sample alignments with 25% rate with a fixed RNG seed samsift -i tests/test.bam -f 'random.random()<0.25' -0 'random.seed(42)'# filtering: keep only alignments of reads specified in tests/qnames.txt samsift -i tests/test.bam -0 'q=open("tests/qnames.txt").read().splitlines()' -f 'QNAME in q'# filtering: keep only first 5000 reads from chr1 and 5000 reads from chr2 samsift -i tests/test.bam -0 'c={"chr1":5000,"chr2":5000}' -f 'c[RNAME]>0' -c 'c[RNAME]-=1' -m nonstop-remove # tagging: add tags 'ln' with sequence length and 'ab' with average base quality samsift -i tests/test.bam -c 'ln=len(SEQ);ab=1.0*sum(QUALa)/ln'# tagging: add a tag 'ii' with the number of the current alignment samsift -i tests/test.bam -0 'i=0' -c 'i+=1;ii=i'# updating: removing sequences and base qualities samsift -i tests/test.bam -c 'a.query_sequence=""'# updating: switching all reads to unaligned samsift -i tests/test.bam -c 'a.flag|=0x4;a.reference_start=-1;a.cigarstring="";a.reference_id=-1;a.mapping_quality=0'
安装
使用bioconda:
# add all necessary Bioconda channels conda config --add channels defaults conda config --add channels conda-forge conda config --add channels bioconda # install samsift conda install samsift
使用来自pypi的pip:
pip install --upgrade samsift
使用github中的pip:
pip install --upgrade git+https://github.com/karel-brinda/samsift
命令行参数
Program: samsift (advanced filtering and tagging of SAM/BAM alignments using Python expressions) Version: 0.2.5 Author: Karel Brinda <kbrinda@hsph.harvard.edu> Usage: samsift.py [-i FILE] [-o FILE] [-f PY_EXPR] [-c PY_CODE] [-m STR] [-0 PY_CODE] [-d PY_EXPR] [-t PY_EXPR] Basic options: -h, --help show this help message and exit -v, --version show program's version number and exit -i FILE input SAM/BAM file [-] -o FILE output SAM/BAM file [-] -f PY_EXPR filtering expression [True] -c PY_CODE code to be executed (e.g., assigning new tags) [None] -m STR mode: strict (stop on first error) nonstop-keep (keep alignments causing errors) nonstop-remove (remove alignments causing errors) [strict] Advanced options: -0 PY_CODE initialization [None] -d PY_EXPR debugging expression to print [None] -t PY_EXPR debugging trigger [True]
算法
exec(INITIALIZATION)forALIGNMENTinALIGNMENTS:ifeval(DEBUG_TRIGER):print(eval(DEBUG_EXPR))ifeval(FILTER):exec(CODE)print(ALIGNMENT)
python表达式和代码。所有表达式和代码都应与 关于Python 3。计算表达式 使用eval 函数和代码使用exec函数执行。 初始化可用于导入python模块,设置全局 变量(如计数器)或从磁盘加载数据。一些模块(即 random和re)加载时没有显式请求。
example(打印所有对齐):
samsift -i tests/test.bam -f 'True'
sam字段。表达式和代码可以访问镜像字段的变量 从SAM specification的对齐部分,即QNAME,FLAG, ^{TT5}$,^{TT6}$(基于1),^{TT7}$,^{TT8}$,^{TT9}$,^{TT10}$, TLEN、SEQ和QUAL。多个附加变量被定义为 简单地访问一些有用的信息:QUALa存储基本质量 作为整数数组;SEQs,QUALs,QUALsa跳过软剪裁基; 以及RNAMEi和RNEXTi将引用id存储为整数。
example(只保留最左边位置的对齐<;=10000):
samsift -i tests/test.bam -f 'POS<=10000'
samsift在内部使用PySam库和 当前对齐的表示(类的实例 pysam.AlignedSegment)是 可用作变量a。因此,前面的示例相当于
samsift -i tests/test.bam -f 'a.reference_start+1<=10000'
变量a也可用于修改当前对齐记录。
example(从每个记录中删除序列和基):
samsift -i tests/test.bam -c 'a.query_sequence=""'
sam标记。每个sam标记都转换为同名的变量。
example(删除分数小于或等于序列长度的对齐):
samsift -i tests/test.bam -f 'AS>len(SEQ)'
如果提供了CODE,则除了re(python regex 模块)在代码执行后返回到标记。
example(添加一个标签ab携带平均基本质量):
samsift -i tests/test.bam -c 'ab=1.0*sum(QUALa)/len(QUALa)'
错误。如果在表达式或 执行代码(例如,由于访问未定义的标记),然后samsift 行为取决于指定的模式(-m)。使用strict模式(-m strict,默认值),samsift将立即中断计算并 报告错误。使用-mnonstop-keep选项,samsift将继续 处理路线,同时将导致错误的路线保留在 输出。使用-mnonstop-remove选项,所有导致对齐的错误 从输出中跳过和修改。
类似程序
- samtools view可以根据标记、读取组标记和雪茄字符串筛选对齐。
- sambamba view除了samtools之外,还支持使用simple Perl-like expressions进行过滤。但是,不能使用float或比较不同的标记。
- BamQL提供了一种简单的查询语言,用于筛选sam/bam文件。
- bamPals添加标记xb、xe、xp和xl。
- SamJavascript可以使用javascript表达式过滤对齐。
- Picard FilterSamReads还可以使用javascript表达式过滤对齐。
问题
请使用Github issues。
更改日志
见Releases。