snakemake 學習筆記1

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腳本

生成一個名爲:Snakemake的文件

(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默認執行的文件名是: Snakemake, 如果想要指定自己編寫的文件名, 可以加上參數: --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文件被刪掉了, 會重新執行.

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章