def testBaseCallHetTest(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgaccg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgattg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgattg', '########', 0, 4, cigar='8M') # ref: atcgatcgatcg # atcgaccg # atcgattg # accgattg # * * out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join(os.path.dirname(__file__), 'test.fa'), variants=True, hettest=True, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|alt. allele freq|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts test2|6|T|3|C|T|1.0|0.333333333333|2.9097148334|0|2|0|1|0|0|0|0| test2|11|C|2|T||1.0|0.0|4.24102241591|0|0|0|2|0|0|0|0| '''.replace('|', '\t') # print '---' # print out.getvalue().replace('\t', '|') # print '---' # print valid.replace('\t', '|') self.assertEqual(valid, out.getvalue())
def testBaseCallMinQual(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', tags=[('IH', 2), ('HI', 1)]) out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join(os.path.dirname(__file__), 'test.fa'), min_qual=10, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts test2|1|A|1|A||1.0|0.91686730441|1|0|0|0|0|0|0|0| test2|2|T|1|T||1.0|3.25488750216|0|0|0|1|0|0|0|0| test2|3|C|1|C||1.0|3.25488750216|0|1|0|0|0|0|0|0| test2|4|G|1|G||1.0|0.91686730441|0|0|1|0|0|0|0|0| test2|5|A|2|A||1.0|1.36179536943|2|0|0|0|0|0|0|0| test2|6|T|2|T||1.0|4.24102241591|0|0|0|2|0|0|0|0| test2|7|C|2|C||1.0|4.24102241591|0|2|0|0|0|0|0|0| test2|8|G|2|G||1.0|1.36179536943|0|0|2|0|0|0|0|0| test2|9|A|1|A||1.0|0.91686730441|1|0|0|0|0|0|0|0| test2|10|T|1|T||1.0|3.25488750216|0|0|0|1|0|0|0|0| test2|11|C|1|C||1.0|3.25488750216|0|1|0|0|0|0|0|0| test2|12|G|1|G||1.0|0.91686730441|0|0|1|0|0|0|0|0| '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testIncludeExcludeRegion(self): 'Include/Exclude region' bam = MockBam(['chr1', 'chr2']) read1 = MockRead('foo1', tid=0, pos=1, aend=51) read2 = MockRead('foo2', tid=0, pos=100, aend=150) read3 = MockRead('foo3', tid=0, pos=125, aend=175) read4 = MockRead('foo4', tid=0, pos=200, aend=250) read5 = MockRead('foo5', tid=1, pos=125, aend=175) region = 'chr1:100-150' excludefilter = ngsutils.bam.filter.ExcludeRegion(region) self.assertTrue(excludefilter.filter(bam, read1)) self.assertFalse(excludefilter.filter(bam, read2)) self.assertFalse(excludefilter.filter(bam, read3)) self.assertTrue(excludefilter.filter(bam, read4)) self.assertTrue(excludefilter.filter(bam, read5)) excludefilter.close() includefilter = ngsutils.bam.filter.IncludeRegion(region) self.assertFalse(includefilter.filter(bam, read1)) self.assertTrue(includefilter.filter(bam, read2)) self.assertTrue(includefilter.filter(bam, read3)) self.assertFalse(includefilter.filter(bam, read4)) self.assertFalse(includefilter.filter(bam, read5)) includefilter.close()
def testExtractBED3(self): outbam = MockBam(['chr1']) ngsutils.bam.extract.bam_extract(testbam1, outbam, self.fname2, quiet=True) passed = [x.qname for x in outbam] self.assertTrue(_matches(['foo2', 'foo5', 'foo1', 'foo4'], passed))
def testBaseCallStrand(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', is_reverse=True) bam.add_read('foo4', 'atcgactgatcg', '############', 0, 0, cigar='12M') out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join(os.path.dirname(__file__), 'test.fa'), showstrand=True, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts|+ strand %|A minor %|C minor %|G minor %|T minor %|N minor %|Deletion minor %|Insertion minor % test2|1|A|2|A||1.0|1.36179536943|2|0|0|0|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|2|T|2|T||1.0|4.24102241591|0|0|0|2|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|3|C|2|C||1.0|4.24102241591|0|2|0|0|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|4|G|2|G||1.0|1.36179536943|0|0|2|0|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|5|A|4|A||1.0|1.87433193885|4|0|0|0|0|0|0|0||0.75|0.25|0.0|0.0|0.0|0.0|0.0|0.0 test2|6|T|4|T/C||1.0|2.94335833285|0|2|0|2|0|0|0|0||0.75|0.0|0.5|0.0|0.0|0.0|0.0|0.0 test2|7|C|4|C|T|1.0|3.45990045591|0|3|0|1|0|0|0|0||0.75|0.0|0.333333333333|0.0|0.0|0.0|0.0|0.0 test2|8|G|4|G||1.0|1.87433193885|0|0|4|0|0|0|0|0||0.75|0.0|0.0|0.25|0.0|0.0|0.0|0.0 test2|9|A|3|A||1.0|1.65545182665|3|0|0|0|0|0|0|0||0.666666666667|0.333333333333|0.0|0.0|0.0|0.0|0.0|0.0 test2|10|T|3|T||1.0|4.85048518563|0|0|0|3|0|0|0|0||0.666666666667|0.0|0.0|0.0|0.333333333333|0.0|0.0|0.0 test2|11|C|3|C||1.0|4.85048518563|0|3|0|0|0|0|0|0||0.666666666667|0.0|0.333333333333|0.0|0.0|0.0|0.0|0.0 test2|12|G|3|G||1.0|1.65545182665|0|0|3|0|0|0|0|0||0.666666666667|0.0|0.0|0.333333333333|0.0|0.0|0.0|0.0 '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testBaseCallRegionStrand(self): # confirm that BED strand *isn't* used bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', is_reverse=True) bam.add_read('foo4', 'atcgactgatcg', '############', 0, 0, cigar='12M') bed = BedFile(fileobj=StringIO.StringIO('''\ test2|4|7|foo|1|- '''.replace('|', '\t'))) # remember: bed is 0-based, basecall is 1-based, so 4->7, corresponds to 5->8 out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join(os.path.dirname(__file__), 'test.fa'), showstrand=True, regions=bed, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts|+ strand %|A minor %|C minor %|G minor %|T minor %|N minor %|Deletion minor %|Insertion minor % test2|5|A|4|A||1.0|1.87433193885|4|0|0|0|0|0|0|0||0.75|0.25|0.0|0.0|0.0|0.0|0.0|0.0 test2|6|T|4|T/C||1.0|2.94335833285|0|2|0|2|0|0|0|0||0.75|0.0|0.5|0.0|0.0|0.0|0.0|0.0 test2|7|C|4|C|T|1.0|3.45990045591|0|3|0|1|0|0|0|0||0.75|0.0|0.333333333333|0.0|0.0|0.0|0.0|0.0 test2|8|G|4|G||1.0|1.87433193885|0|0|4|0|0|0|0|0||0.75|0.0|0.0|0.25|0.0|0.0|0.0|0.0 '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testMismatch(self): mismatch = ngsutils.bam.filter.Mismatch(1) bam = MockBam([ 'test1', ]) read1 = MockRead( 'foo1', 'aaaaaaaaaa', tid=0, pos=0, aend=10, cigar='10M', tags=[('NM', 0)], ) read2 = MockRead('foo2', 'aaaaaaaaat', tid=0, pos=0, aend=10, cigar='10M', tags=[('NM', 1)]) read3 = MockRead('foo3', 'aaaaaaaatt', tid=0, pos=0, aend=10, cigar='10M', tags=[('NM', 2)]) read4 = MockRead('foo4', 'aaaaaaaaaa') read5 = MockRead('foo5', 'aaaatttaaa', tid=0, pos=0, aend=7, cigar='4M3I3M', tags=[('NM', 3)]) read6 = MockRead('foo6', 'aaaatttaat', tid=0, pos=0, aend=7, cigar='4M3I3M', tags=[('NM', 4)]) self.assertTrue(mismatch.filter(bam, read1)) self.assertTrue(mismatch.filter(bam, read2)) # 1 mismatch self.assertFalse(mismatch.filter(bam, read3)) # 2 mismatches self.assertFalse(mismatch.filter(bam, read4)) # unmapped self.assertTrue(mismatch.filter(bam, read5)) # 1 mismatch (1 indel) self.assertFalse(mismatch.filter( bam, read6)) # 2 mismatches (1 indel + 1 mm) mismatch.close()
def testBaseCallMinCount(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', tags=[('IH', 2), ('HI', 1)]) out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join(os.path.dirname(__file__), 'test.fa'), min_count=2, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts test2|5|A|3|A||1.33333333333|1.65545182665|3|0|0|0|0|0|0|0| test2|6|T|3|T|C|1.33333333333|2.9097148334|0|1|0|2|0|0|0|0| test2|7|C|3|C||1.33333333333|4.85048518563|0|3|0|0|0|0|0|0| test2|8|G|3|G||1.33333333333|1.65545182665|0|0|3|0|0|0|0|0| test2|9|A|2|A||1.5|1.36179536943|2|0|0|0|0|0|0|0| test2|10|T|2|T||1.5|4.24102241591|0|0|0|2|0|0|0|0| test2|11|C|2|C||1.5|4.24102241591|0|2|0|0|0|0|0|0| test2|12|G|2|G||1.5|1.36179536943|0|0|2|0|0|0|0|0| '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testBaseCallInDel(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgaTtcg', '.........', 0, 0, cigar='5M1I3M') # inserted T @6 bam.add_read('foo2', 'atcgacg', 'AAAAAAA', 0, 4, cigar='5M1D2M') # deleted T @ 10 out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join( os.path.dirname(__file__), 'test.fa'), out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts test2|1|A|1|A||1.0|0.91686730441|1|0|0|0|0|0|0|0| test2|2|T|1|T||1.0|3.25488750216|0|0|0|1|0|0|0|0| test2|3|C|1|C||1.0|3.25488750216|0|1|0|0|0|0|0|0| test2|4|G|1|G||1.0|0.91686730441|0|0|1|0|0|0|0|0| test2|5|A|2|A||1.0|1.36179536943|2|0|0|0|0|0|0|0| test2|6|T|2|T||1.0|4.24102241591|0|0|0|2|0|0|0|1|T:1 test2|7|C|2|C||1.0|4.24102241591|0|2|0|0|0|0|0|0| test2|8|G|2|G||1.0|1.36179536943|0|0|2|0|0|0|0|0| test2|9|A|1|A||1.0|0.91686730441|1|0|0|0|0|0|0|0| test2|10|T|0|N||1.0|0|0|0|0|0|0|1|0|0| test2|11|C|1|C||1.0|3.25488750216|0|1|0|0|0|0|0|0| test2|12|G|1|G||1.0|0.91686730441|0|0|1|0|0|0|0|0| '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testBaseCallHetTest(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgaccg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgattg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgattg', '########', 0, 4, cigar='8M') # ref: atcgatcgatcg # atcgaccg # atcgattg # accgattg # * * out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join( os.path.dirname(__file__), 'test.fa'), variants=True, hettest=True, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|alt. allele freq|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts test2|6|T|3|C|T|1.0|0.333333333333|2.9097148334|0|2|0|1|0|0|0|0| test2|11|C|2|T||1.0|0.0|4.24102241591|0|0|0|2|0|0|0|0| '''.replace('|', '\t') # print '---' # print out.getvalue().replace('\t', '|') # print '---' # print valid.replace('\t', '|') self.assertEqual(valid, out.getvalue())
def testBaseCallMinCount(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', tags=[('IH', 2), ('HI', 1)]) out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join( os.path.dirname(__file__), 'test.fa'), min_count=2, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts test2|5|A|3|A||1.33333333333|1.65545182665|3|0|0|0|0|0|0|0| test2|6|T|3|T|C|1.33333333333|2.9097148334|0|1|0|2|0|0|0|0| test2|7|C|3|C||1.33333333333|4.85048518563|0|3|0|0|0|0|0|0| test2|8|G|3|G||1.33333333333|1.65545182665|0|0|3|0|0|0|0|0| test2|9|A|2|A||1.5|1.36179536943|2|0|0|0|0|0|0|0| test2|10|T|2|T||1.5|4.24102241591|0|0|0|2|0|0|0|0| test2|11|C|2|C||1.5|4.24102241591|0|2|0|0|0|0|0|0| test2|12|G|2|G||1.5|1.36179536943|0|0|2|0|0|0|0|0| '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testBaseCallStrand(self): bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', is_reverse=True) bam.add_read('foo4', 'atcgactgatcg', '############', 0, 0, cigar='12M') out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join( os.path.dirname(__file__), 'test.fa'), showstrand=True, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts|+ strand %|A minor %|C minor %|G minor %|T minor %|N minor %|Deletion minor %|Insertion minor % test2|1|A|2|A||1.0|1.36179536943|2|0|0|0|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|2|T|2|T||1.0|4.24102241591|0|0|0|2|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|3|C|2|C||1.0|4.24102241591|0|2|0|0|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|4|G|2|G||1.0|1.36179536943|0|0|2|0|0|0|0|0||1.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0 test2|5|A|4|A||1.0|1.87433193885|4|0|0|0|0|0|0|0||0.75|0.25|0.0|0.0|0.0|0.0|0.0|0.0 test2|6|T|4|T/C||1.0|2.94335833285|0|2|0|2|0|0|0|0||0.75|0.0|0.5|0.0|0.0|0.0|0.0|0.0 test2|7|C|4|C|T|1.0|3.45990045591|0|3|0|1|0|0|0|0||0.75|0.0|0.333333333333|0.0|0.0|0.0|0.0|0.0 test2|8|G|4|G||1.0|1.87433193885|0|0|4|0|0|0|0|0||0.75|0.0|0.0|0.25|0.0|0.0|0.0|0.0 test2|9|A|3|A||1.0|1.65545182665|3|0|0|0|0|0|0|0||0.666666666667|0.333333333333|0.0|0.0|0.0|0.0|0.0|0.0 test2|10|T|3|T||1.0|4.85048518563|0|0|0|3|0|0|0|0||0.666666666667|0.0|0.0|0.0|0.333333333333|0.0|0.0|0.0 test2|11|C|3|C||1.0|4.85048518563|0|3|0|0|0|0|0|0||0.666666666667|0.0|0.333333333333|0.0|0.0|0.0|0.0|0.0 test2|12|G|3|G||1.0|1.65545182665|0|0|3|0|0|0|0|0||0.666666666667|0.0|0.0|0.333333333333|0.0|0.0|0.0|0.0 '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testBaseCallRegionStrand(self): # confirm that BED strand *isn't* used bam = MockBam(['test2']) bam.add_read('foo1', 'atcgatcg', '........', 0, 0, cigar='8M') bam.add_read('foo2', 'atcgatcg', 'AAAAAAAA', 0, 4, cigar='8M') bam.add_read('foo3', 'accgatcg', '########', 0, 4, cigar='8M', is_reverse=True) bam.add_read('foo4', 'atcgactgatcg', '############', 0, 0, cigar='12M') bed = BedFile(fileobj=StringIO.StringIO('''\ test2|4|7|foo|1|- '''.replace('|', '\t'))) # remember: bed is 0-based, basecall is 1-based, so 4->7, corresponds to 5->8 out = StringIO.StringIO('') ngsutils.bam.basecall.bam_basecall(bam, os.path.join( os.path.dirname(__file__), 'test.fa'), showstrand=True, regions=bed, out=out) valid = '''chrom|pos|ref|count|consensus call|minor call|ave mappings|entropy|A|C|G|T|N|Deletions|Gaps|Insertions|Inserts|+ strand %|A minor %|C minor %|G minor %|T minor %|N minor %|Deletion minor %|Insertion minor % test2|5|A|4|A||1.0|1.87433193885|4|0|0|0|0|0|0|0||0.75|0.25|0.0|0.0|0.0|0.0|0.0|0.0 test2|6|T|4|T/C||1.0|2.94335833285|0|2|0|2|0|0|0|0||0.75|0.0|0.5|0.0|0.0|0.0|0.0|0.0 test2|7|C|4|C|T|1.0|3.45990045591|0|3|0|1|0|0|0|0||0.75|0.0|0.333333333333|0.0|0.0|0.0|0.0|0.0 test2|8|G|4|G||1.0|1.87433193885|0|0|4|0|0|0|0|0||0.75|0.0|0.0|0.25|0.0|0.0|0.0|0.0 '''.replace('|', '\t') self.assertEqual(valid, out.getvalue())
def testMismatchRef(self): mismatch = ngsutils.bam.filter.MismatchRef( 1, os.path.join(os.path.dirname(__file__), 'test.fa')) bam = MockBam(['test1', 'chr1']) read0 = MockRead('foo0', 'aaaaaaaaaa', tid=1, pos=0, aend=10, cigar='10M') read1 = MockRead('foo1', 'aaaaaaaaaa', tid=0, pos=0, aend=10, cigar='10M') read2 = MockRead('foo2', 'aaaaaaaaat', tid=0, pos=0, aend=10, cigar='10M') read3 = MockRead('foo3', 'aaaaaaaatt', tid=0, pos=0, aend=10, cigar='10M') read4 = MockRead('foo4', 'aaaaaaaaaa') self.assertRaises(ValueError, mismatch.filter, bam, read0) # 'chr1' not in FASTA file self.assertTrue(mismatch.filter(bam, read1)) self.assertTrue(mismatch.filter(bam, read2)) # 1 mismatch self.assertFalse(mismatch.filter(bam, read3)) # 2 mismatches self.assertFalse(mismatch.filter(bam, read4)) # unmapped mismatch.close()
#!/usr/bin/env python ''' Tests for bamutils extract ''' import unittest import os import ngsutils.bam from ngsutils.bam.innerdist import bam_innerdist from ngsutils.bam.t import MockBam, _matches testbam1 = MockBam(['chr1'], insert_order=True) testbam1.add_read('foo1', tid=0, pos=100, aend=150, cigar='50M') testbam1.add_read('foo2', tid=0, pos=200, aend=250, cigar='50M') testbam1.add_read('foo3', tid=0, pos=300, aend=350, cigar='50M') testbam1.add_read('foo4', tid=0, pos=400, aend=450, cigar='50M') testbam1.add_read('foo5', tid=0, pos=400, aend=450, cigar='50M') testbam1.add_read('foo6', tid=-1) testbam2 = MockBam(['chr1'], insert_order=True) testbam2.add_read('foo1', tid=0, pos=200, aend=250, cigar='50M') testbam2.add_read('foo2', tid=0, pos=300, aend=350, cigar='50M') testbam2.add_read('foo3', tid=0, pos=450, aend=500, cigar='50M') testbam2.add_read('foo4', tid=0, pos=550, aend=600, cigar='50M') testbam2.add_read('foo5', tid=-1) testbam2.add_read('foo6', tid=0, pos=500, aend=550, cigar='50M') testbam3 = MockBam(['chr1'], insert_order=True) testbam3.add_read('foo1', tid=0, pos=100, aend=150, cigar='50M')
#!/usr/bin/env python ''' Tests for bamutils count ''' import unittest import StringIO import ngsutils.bam import ngsutils.bam.count import ngsutils.bam.count.models from ngsutils.bam.t import MockBam testbam1 = MockBam(['chr1']) testbam1.add_read('foo1', 'A' * 50, tid=0, pos=100, cigar='50M', tags=[('IH', 2)]) testbam1.add_read('foo1', 'A' * 50, tid=0, pos=1000, cigar='50M', tags=[('IH', 2)]) testbam1.add_read('foo2', 'A' * 50, tid=0, pos=101, cigar='50M') testbam1.add_read('foo3', 'A' * 50, tid=0, pos=101, cigar='50M') testbam1.add_read('foo4', 'A' * 50, tid=0, pos=103, cigar='50M') testbam1.add_read('foo5', 'A' * 50, tid=0, pos=200, cigar='25M') testbam1.add_read('foo6', 'A' * 50, tid=0, pos=200, cigar='25M') testbam1.add_read('foo7', 'A' * 50, tid=0, pos=225, cigar='25M') testbam1.add_read('foo8', 'A' * 50, tid=0, pos=225, cigar='25M') testbam1.add_read('foo9', 'A' * 50, tid=0, pos=300, cigar='50M') testbam1.add_read('foo10', 'A' * 50, tid=0, pos=301, cigar='50M') testbam1.add_read('foo11', 'A' * 50, tid=0, pos=302, cigar='50M', is_reverse=True) testbam1.add_read('foo12', 'A' * 50, tid=0, pos=303, cigar='50M', is_reverse=True) class CountTest(unittest.TestCase):
def testIncludeExcludeBED(self): 'Include/Exclude BED (BED3, BED6)' tmp_fname = os.path.join(os.path.dirname(__file__), 'tmp_list') with open(tmp_fname, 'w') as f: f.write('chr1\t100\t150\nchr1\t200\t250\n') bam = MockBam(['chr1', 'chr2']) read1 = MockRead('foo1', tid=0, pos=1, aend=51) read2 = MockRead('foo2', tid=0, pos=100, aend=150) read3 = MockRead('foo3', tid=0, pos=125, aend=175, is_reverse=True) read4 = MockRead('foo4', tid=0, pos=200, aend=250, is_reverse=True) read5 = MockRead('foo5', tid=1, pos=125, aend=175) include3 = ngsutils.bam.filter.IncludeBED(tmp_fname, 'nostrand') self.assertFalse(include3.filter(bam, read1)) self.assertTrue(include3.filter(bam, read2)) self.assertTrue(include3.filter(bam, read3)) self.assertTrue(include3.filter(bam, read4)) self.assertFalse(include3.filter(bam, read5)) include3.close() exclude3 = ngsutils.bam.filter.ExcludeBED(tmp_fname, 'nostrand') self.assertTrue(exclude3.filter(bam, read1)) self.assertFalse(exclude3.filter(bam, read2)) self.assertFalse(exclude3.filter(bam, read3)) self.assertFalse(exclude3.filter(bam, read4)) self.assertTrue(exclude3.filter(bam, read5)) exclude3.close() tmp_fname = os.path.join(os.path.dirname(__file__), 'tmp_list') with open(tmp_fname, 'w') as f: f.write( 'chr1\t100\t150\tfoo\t1\t+\nchr1\t200\t250\tfoo\t1\t-\nchr1\t1000\t1250\tfoo\t1\t+\n' ) include6 = ngsutils.bam.filter.IncludeBED(tmp_fname) self.assertFalse(include6.filter(bam, read1)) self.assertTrue(include6.filter(bam, read2)) self.assertFalse(include6.filter(bam, read3)) self.assertTrue(include6.filter(bam, read4)) self.assertFalse(include6.filter(bam, read5)) include6.close() exclude6 = ngsutils.bam.filter.ExcludeBED(tmp_fname) self.assertTrue(exclude6.filter(bam, read1)) self.assertFalse(exclude6.filter(bam, read2)) self.assertTrue(exclude6.filter(bam, read3)) self.assertFalse(exclude6.filter(bam, read4)) self.assertTrue(exclude6.filter(bam, read5)) exclude6.close() os.unlink(tmp_fname)
#!/usr/bin/env python ''' Tests for bamutils export ''' import StringIO import unittest import ngsutils.bam import ngsutils.bam.export from ngsutils.bam.t import MockBam testbam1 = MockBam(['chr1']) testbam1.add_read('foo1', 'atcgatcg', 'AAAAAAAA', 0, 0, 8, '8M') testbam1.add_read('foo2', 'atcgtttt', 'AAAABBBB', 0, 4, 12, '8M', is_reverse=True) testbam1.add_read('foo3', 'gggggggg', 'CCCCCCCC') testbam2 = MockBam(['chr1']) testbam2.add_read('bar1', 'atcgatcg', 'AAAAAAAA', 0, 0, 8, '8M', rnext=0, pnext=100, is_paired=True, is_read1=True) testbam2.add_read('bar1', 'gctagcta', 'AAAAAAAA', 0, 100, 108, '8M', is_paired=True, is_read2=True) testbam2.add_read('bar2', 'atcgatcg', 'AAAAAAAA', is_paired=True, is_read1=True) testbam2.add_read('bar2', 'gctagcta', 'AAAAAAAA', is_paired=True, is_read2=True) testbam3 = MockBam(['chr1']) testbam3.add_read('baz1', 'atcgatcg', 'AAAAAAAA', 0, 0, 8, '8M', mapq=10, isize=100, tlen=120, tags=[('ZZ', 'foo'), ('ZY', 100), ('ZX', 1.0), ('ZW', 'a'), ]) class ExportTest(unittest.TestCase): def testExport1(self):
#!/usr/bin/env python ''' Tests for bamutils export ''' import StringIO import unittest import ngsutils.bam import ngsutils.bam.export from ngsutils.bam.t import MockBam testbam1 = MockBam(['chr1']) testbam1.add_read('foo1', 'atcgatcg', 'AAAAAAAA', 0, 0, 8, '8M') testbam1.add_read('foo2', 'atcgtttt', 'AAAABBBB', 0, 4, 12, '8M', is_reverse=True) testbam1.add_read('foo3', 'gggggggg', 'CCCCCCCC') testbam2 = MockBam(['chr1']) testbam2.add_read('bar1', 'atcgatcg', 'AAAAAAAA', 0, 0,
#!/usr/bin/env python ''' Tests for bamutils extract ''' import unittest import os import ngsutils.bam import ngsutils.bam.extract from ngsutils.bam.t import MockBam, _matches testbam1 = MockBam(['chr1']) testbam1.add_read('foo1', tid=0, pos=100, aend=150, cigar='50M') # too early testbam1.add_read('foo2', tid=0, pos=200, aend=250, cigar='50M') # in range, + strand testbam1.add_read('foo3', tid=0, pos=1000, aend=1050, cigar='50M') # too late testbam1.add_read('foo4', tid=0, pos=200, aend=350, cigar='25M100N25M') # in range, start, + strand testbam1.add_read('foo5', tid=0, pos=200, aend=250, cigar='50M', is_reverse=True) # in range, start, - strand testbam1.add_read('foo6', tid=0, pos=2000, aend=3050, cigar='20M500N20M500M10M') # touches (2000,2020), (2520, 2540), (3040, 3050) class ExtractTest(unittest.TestCase): def setUp(self): self.fname1 = os.path.join(os.path.dirname(__file__), 'testbam1') with open(self.fname1, 'w') as f: f.write('chr1\t200\t250\tfoo\t1\t+\n') f.write('chr1\t125\t170\tfoo\t1\t+\n') self.fname2 = os.path.join(os.path.dirname(__file__), 'testbam2') with open(self.fname2, 'w') as f:
#!/usr/bin/env python ''' Tests for bamutils count ''' import unittest import StringIO import ngsutils.bam import ngsutils.bam.count import ngsutils.bam.count.models from ngsutils.bam.t import MockBam testbam1 = MockBam(['chr1']) testbam1.add_read('foo1', 'A' * 50, tid=0, pos=100, cigar='50M', tags=[('IH', 2)]) testbam1.add_read('foo1', 'A' * 50, tid=0, pos=1000, cigar='50M', tags=[('IH', 2)]) testbam1.add_read('foo2', 'A' * 50, tid=0, pos=101, cigar='50M') testbam1.add_read('foo3', 'A' * 50, tid=0, pos=101, cigar='50M') testbam1.add_read('foo4', 'A' * 50, tid=0, pos=103, cigar='50M') testbam1.add_read('foo5', 'A' * 50, tid=0, pos=200, cigar='25M')
#!/usr/bin/env python ''' Tests for bamutils extract ''' import unittest import os import ngsutils.bam import ngsutils.bam.extract from ngsutils.bam.t import MockBam, _matches testbam1 = MockBam(['chr1']) testbam1.add_read('foo1', tid=0, pos=100, aend=150, cigar='50M') # too early testbam1.add_read('foo2', tid=0, pos=200, aend=250, cigar='50M') # in range, + strand testbam1.add_read('foo3', tid=0, pos=1000, aend=1050, cigar='50M') # too late testbam1.add_read('foo4', tid=0, pos=200, aend=350, cigar='25M100N25M') # in range, start, + strand testbam1.add_read('foo5', tid=0, pos=200, aend=250, cigar='50M', is_reverse=True) # in range, start, - strand testbam1.add_read('foo6', tid=0, pos=2000, aend=3050, cigar='20M500N20M500M10M'