예제 #1
0
def test_chromosome_names():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    names = test_fasta.chromosome_names()
    assert type(names) == tuple
    assert names == ("seq1", "seq2")
예제 #2
0
def test_chromosome_lengths():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    lengths = test_fasta.chromosome_lengths()
    assert type(lengths) == tuple
    assert lengths == (20, 40)
예제 #3
0
def test_RefFasta():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    test_fasta2 = reftools.RefFasta(os.path.join(dir, "toy2.fasta"),
                                    "samtools",
                                    "bwa",
                                    no_initial_index=True)
    os.utime(os.path.join(dir, "toy.dict"), None)
    assert test_fasta.filepath == os.path.join(dir, "toy.fasta")
    assert test_fasta.is_faidxed() is True
    assert test_fasta.check_bwa_index() is True
    assert test_fasta.check_seq_dict() is True
    assert test_fasta2.filepath == os.path.join(dir, "toy2.fasta")
    assert test_fasta2.is_faidxed() is False
    assert test_fasta2.check_bwa_index() is False
    assert test_fasta2.check_seq_dict() is False
예제 #4
0
def test_check_bam_fasta_compatibility():
    test_bam = bam.BamFile(os.path.join(dir, "toy.bam"),
                           "samtools",
                           no_initial_index=True)
    test2_bam = bam.BamFile(os.path.join(dir, "tinyheader.bam"),
                            "samtools",
                            no_initial_index=True)
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    test2_fasta = reftools.RefFasta(os.path.join(dir, "toy3.fasta"),
                                    "samtools",
                                    "bwa",
                                    no_initial_index=True)
    assert utils.check_bam_fasta_compatibility(test_bam, test_fasta) is True
    assert utils.check_bam_fasta_compatibility(test2_bam, test_fasta) is False
    assert utils.check_bam_fasta_compatibility(test_bam, test2_fasta) is False
예제 #5
0
def test_get_chrom_length():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    lengths = test_fasta.get_chrom_length("seq1")
    assert type(lengths) == int
    assert lengths == 20
    with pytest.raises(RuntimeError):
        lengths = test_fasta.get_chrom_length("foo")
예제 #6
0
def test_mask_reference():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    new_path = test_fasta.mask_reference(os.path.join(dir, "toy_mask.bed"),
                                         os.path.join(dir, "toy.masked.fasta"))
    assert new_path == os.path.join(dir, "toy.masked.fasta")
    assert os.path.exists(os.path.join(dir, "toy.masked.fasta.fai"))
    contents = read_file_to_list(os.path.join(dir, "toy.masked.fasta"))
    assert contents[1] == "NNNNNNNNNNNNNNNNNNNN"
예제 #7
0
def test_RefFasta_setup():
    test_fasta2 = reftools.RefFasta(os.path.join(dir, "toy2.fasta"),
                                    "samtools",
                                    "bwa",
                                    no_initial_index=False)
    test_fasta2.conditional_index_bwa()
    test_fasta2.conditional_seq_dict()
    assert os.path.exists(os.path.join(dir, "toy2.fasta.amb"))
    assert os.path.exists(os.path.join(dir, "toy2.fasta.ann"))
    assert os.path.exists(os.path.join(dir, "toy2.fasta.bwt"))
    assert os.path.exists(os.path.join(dir, "toy2.fasta.fai"))
    assert os.path.exists(os.path.join(dir, "toy2.fasta.pac"))
    assert os.path.exists(os.path.join(dir, "toy2.fasta.sa"))
    assert os.path.exists(os.path.join(dir, "toy2.fasta.dict"))
예제 #8
0
def test_chromosome_bed():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    path1 = test_fasta.chromosome_bed(os.path.join(dir, "reftest1.bed"),
                                      ["seq1"])
    path2 = test_fasta.chromosome_bed(os.path.join(dir, "reftest2.bed"),
                                      ["seq1", "seq2"])
    assert path1 == os.path.join(dir, "reftest1.bed")
    assert path2 == os.path.join(dir, "reftest2.bed")
    assert read_bed(os.path.join(dir, "reftest1.bed")) == [["seq1", "0", "20"]]
    assert read_bed(os.path.join(dir, "reftest2.bed")) == [["seq1", "0", "20"],
                                                           ["seq2", "0", "40"]]
    with pytest.raises(RuntimeError):
        path3 = test_fasta.chromosome_bed(os.path.join(dir, "reftest1.bed"),
                                          ["foo"])
예제 #9
0
def test_isolate_chroms():
    test_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"),
                                   "samtools",
                                   "bwa",
                                   no_initial_index=True)
    seq1 = test_fasta.isolate_chroms(os.path.join(dir, "newfasta"), ["seq1"],
                                     bed_mask=None)
    seq1_masked = test_fasta.isolate_chroms(os.path.join(dir, "newfasta"),
                                            ["seq1"],
                                            bed_mask=os.path.join(
                                                dir, "toy_mask.bed"))
    assert seq1 == os.path.join(dir, "newfasta.fa")
    assert seq1_masked == os.path.join(dir, "newfasta.masked.fa")
    assert os.path.exists(os.path.join(dir, "newfasta.fa.fai"))
    assert os.path.exists(os.path.join(dir, "newfasta.masked.fa.fai"))
    contents1 = read_file_to_list(os.path.join(dir, "newfasta.fa"))
    contents2 = read_file_to_list(os.path.join(dir, "newfasta.masked.fa"))
    assert contents1[1] == "AAAATTTTAAAATTTTGGGG"
    assert contents2[1] == "NNNNNNNNNNNNNNNNNNNN"
예제 #10
0
def test_bwa_mem_mapping_sambamba():
    # Test single end, no rg
    toy_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"))
    test_bam = assemble.bwa_mem_mapping_sambamba(
        "bwa",
        "samtools",
        "sambamba",
        toy_fasta,
        os.path.join(dir, "assemble_test"), [os.path.join(dir, "toy_1.fastq")],
        1,
        "None", [""],
        cram=False)
    assert os.path.exists(os.path.join(dir, "assemble_test.sorted.bam"))
    assert pysam.idxstats(os.path.join(dir, "assemble_test.sorted.bam")
                          ) == 'seq1\t20\t0\t0\nseq2\t40\t0\t0\n*\t0\t0\t3\n'
    # Test single end, with rg
    toy_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"))
    test_bam = assemble.bwa_mem_mapping_sambamba(
        "bwa",
        "samtools",
        "sambamba",
        toy_fasta,
        os.path.join(dir,
                     "test_assemble.rg"), [os.path.join(dir, "toy_1.fastq")],
        1,
        "@RG\tID:{}".format("test"), [""],
        cram=False)
    assert os.path.exists(os.path.join(dir, "test_assemble.rg.sorted.bam"))
    assert pysam.idxstats(os.path.join(dir, "test_assemble.rg.sorted.bam")
                          ) == 'seq1\t20\t0\t0\nseq2\t40\t0\t0\n*\t0\t0\t3\n'
    header = pysam.view("-H", os.path.join(dir, "test_assemble.rg.sorted.bam"))
    assert header.find("@RG\\tID:test") != -1
    # Test raising error for inaccessible reference
    with pytest.raises(RuntimeError):
        fake_fasta = reftools.RefFasta(os.path.join(dir,
                                                    "DOES_NOT_EXIST.fasta"),
                                       no_initial_index=True)
        test_bam = assemble.bwa_mem_mapping_sambamba(
            "bwa",
            "samtools",
            "sambamba",
            fake_fasta,
            os.path.join(dir,
                         "test_assemble"), [os.path.join(dir, "toy_1.fastq")],
            1,
            "None", [""],
            cram=False)
    # Test raising error for no fastqs
    toy_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"))
    with pytest.raises(RuntimeError):
        test_bam = assemble.bwa_mem_mapping_sambamba("bwa",
                                                     "samtools",
                                                     "sambamba",
                                                     toy_fasta,
                                                     os.path.join(
                                                         dir, "test_assemble"),
                                                     [],
                                                     1,
                                                     "None", [""],
                                                     cram=False)
    # Test raising error for inaccessible fastq
    with pytest.raises(RuntimeError):
        toy_fasta = reftools.RefFasta(os.path.join(dir, "toy.fasta"))
        test_bam = assemble.bwa_mem_mapping_sambamba(
            "bwa",
            "samtools",
            "sambamba",
            toy_fasta,
            os.path.join(dir, "test_assemble"),
            [os.path.join(dir, "toy_FOO_fake.fastq")],
            1,
            "None", [""],
            cram=False)