育种数据分析之放飞自我分享 http://blog.sciencenet.cn/u/yijiaobai 关注:生物统计,数量遗传,混合线性模型,生物信息,R,Perl,Python,GWAS,GS相关方法,文章及代码

博文

snakemake 学习笔记1

已有 4099 次阅读 2019-4-3 09:15 |个人分类:便捷操作|系统分类:科研笔记| snakemake, python, 流程自动化

1, snakemake介绍

Snakemake是用Python3写的一个流程化工具, 非常方便. 官网上的例子有点难度, 这里用最简单的案例解释一下snakemake的应用方法.
安装方法

easy_install3 snakemake

或者:

pip3 install snakemake

也可以从源文件安装:

git clone https://bitbucket.org/snakemake/snakemake.git
cd snakemake
virtualenv -p python3 .venv
source .venv/bin/activate
python setup.py install

2, 一个简单的案例

思路:

  • 1, 生成一个1.txt文件
  • 2, 生成一个2.txt文件
  • 3, 使用cat命令, 将两者合并为hebing.txt
 echo "hello number1" >1.txt
 echo "hello number2" >2.txt
 cat 1.txt 2.txt 
 cat 1.txt 2.txt >hebing.txt

3, 生成snakemake脚本

生成一个名为:Snakefile的文件

(base) [dengfei@localhost example]$ cat Snakefile 
rule test_cat:
    input:
        "1.txt",
        "2.txt"
    output:
        "hebing.txt"
    shell:
        "cat  {input}  >> {output}"

这里有四个参数:

  • rule: 是名称, 这里命名为test_cat
  • Input: 输入文件, 这里是”1.txt”, “2.txt”
  • Output: 输出文件, 这里是”hebing.txt”
  • Shell: 这里是要执行的脚本, 输入文件是{input}, 输出文件是{output}

4, snakemake -np

使用-np查看转化后的命令

(base) [dengfei@localhost example]$ snakemake -np

rule test_cat:
    input: 1.txt, 2.txt
    output: hebing.txt
    jobid: 0

cat  1.txt 2.txt  >> hebing.txt
Job counts:
    count    jobs
    1    test_cat
    1

5, 执行命令 snakemake

snakemake默认执行的文件名是: Snakefile, 如果想要指定自己编写的文件名, 可以加上参数: —snakefile
比如: 文件名为a.snake

 snakemake --snakefile a.snake

如果文件名是默认的Snakemake, 不用加参数, 直接运行snakemake即可直接执行.

(base) [dengfei@localhost example]$ snakemake
Provided cores: 1
Rules claiming more threads will be scaled down.
Job counts:
    count    jobs
    1    test_cat
    1

rule test_cat:
    input: 1.txt, 2.txt
    output: hebing.txt
    jobid: 0

Finished job 0.
1 of 1 steps (100%) done

查看结果:

(base) [dengfei@localhost example]$ cat hebing.txt 
hello number1
hello number2

可以看到, 使用snakemake, 成功的将1.txt 和2.txt 合并为hebing.txt.

6, 运行成功, 重新运行时

显示Nothing to be done, 即不会执行.

(base) [dengfei@localhost example]$ snakemake
Nothing to be done.

如果heibng.txt文件被删掉了, 会重新执行.

这是一小步, 也是一大步.

公众号.png




https://wap.sciencenet.cn/blog-2577109-1171191.html

上一篇:从读书到别人思想的跑马场
下一篇:snakemake-学习笔记2
收藏 IP: 47.75.72.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-24 13:12

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部