首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按群集拆分bam,然后使用检查点按群集合并bam

按群集拆分bam,然后使用检查点按群集合并bam
EN

Stack Overflow用户
提问于 2019-07-03 09:54:59
回答 1查看 197关注 0票数 2

我有三个来自3个不同样本的单细胞bam文件,我需要通过集群将它们分成更小的bam。然后,我需要为相同的集群合并来自不同样本的bam文件。我试着使用检查点,但有点迷路了。https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html

这是我在split bam files to (variable) pre-defined number of small bam files depending on the sample上发布的这个问题的延续

代码语言:javascript
复制
SAMPLE_cluster = { "SampleA" : [ "1", "2", "3" ], "SampleB" :  [ "1" ], "SampleC" : [ "1", "2" ] }

CLUSTERS = []
for sample in SAMPLE_cluster:
    CLUSTERS.extend(SAMPLE_cluster[sample])
CLUSTERS = sorted(set(CLUSTERS)

rule all:
    input: expand("01merged_bam/{cluster_id}.bam, cluster_id = CLUSTERS)

checkpoint split_bam:
    input: "{sample}.bam"
    output: directory("01split_bam/{sample}/")
    shell:
       """
       split_bam.sh {input} 
       """
## the split_bam.sh will split the bam file to "01split_bam/{sample}/{sample}_{cluster_id}.bam" 

def merge_bam_input(wildcards):
    checkpoint_output = checkpoints.split_bam.get(**wildcards).output[0]
    return expand("01split_bam/{sample}/{sample}_{{cluster_id}}.bam", \
                sample = glob_wildcards(os.path.join(checkpoint_output, "{sample}_{cluster_id}.bam")).sample)


rule merge_bam_per_cluster:
    input: merge_bam_input
    output: "01merged_bam/{cluster_id}.bam"
    log: "00log/{cluster_id}.merge_bam.log"
    threads: 2
    shell:
        """
        samtools merge -@ 2 -r {output} {input}
        """

根据集群编号,规则merge_bam_per_cluster的输入将发生变化:

例如,对于集群1:“01 01split_bam/SampleA/SampleA_1.bam”,"01split_bam/SampleB/SampleB_1.bam","01split_bam/SampleC/SampleC_1.bam“。

对于集群2:"01split_bam/SampleA/SampleA_2.bam","01split_bam/SampleC/SampleC_2.bam“。

对于集群3:"01split_bam/SampleA/SampleA_3.bam“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-04 01:07:28

我决定不使用检查点,而是使用输入函数来获取

代码语言:javascript
复制
SAMPLE_cluster = { "SampleA" : [ "1", "2", "3" ], "SampleB" :  [ "1" ], "SampleC" : [ "1", "2" ] }

# reverse the mapping
cluster_sample = {'1':['sampleA','sample'B','sampleC'], '2':['sampleA', 'sampleC'], '3':['sampleA']}

rule split_bam:
    input: "{sample}.bam"
    output: "split.touch"
    shell:
       """
       split_bam {input} 
       touch split.touch
       """
rule index_split_bam:
    input: "split.touch"
    output: "split_bam/{sample}_{cluster_id}.bam.bai"
    shell:
        """
        samtools index 01split_bam/{wildcards.sample}/{wildcards.sample}_{wildcards.cluster_id}.bam
        """

def get_merge_bam_input(wildcards):
    samples = cluster_sample[wildcards.cluster_id]
    return expand("01split_bam/{sample}/{sample}_{{cluster_id}}.bam.bai", sample = samples)


rule merge_bam_per_cluster:
    input: get_merge_bam_input
    output: "01merged_bam/{cluster_id}.bam"
    params:
            bam = lambda wildcards, input: " ".join(input).replace(".bai", "")
    log: "00log/{cluster_id}.merge_bam.log"
    threads: 2
    shell:
        """
        samtools merge -@ 2 -r {output} {params.bam}
        """

它似乎起作用了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56861913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档