def test_children_len(): x = Locus('1',1,2) y = Locus('1',3,4,name='sublocus1') z = Locus('1',3,4,name='sublocus2') x.children = (y,z) assert len(x.children) == 2
def test_combine(): x = Locus('1',1,2) y = Locus('1',3,4) z = x.combine(y) assert z.start == 1 assert z.end == 4 assert len(z.children) == 2
def test_add_duplicate_locus(): t = Term('test') assert len(t) == 0 t.add_locus(Locus('1', 1, 100)) assert len(t) == 1 # Add duplicate which wont count t.add_locus(Locus('1', 1, 100)) assert len(t) == 1
def simpleRefLoci(): # Create a Locus a = Locus(1,100,150, id='gene_a') # Create a couple more! b = Locus(1,160,175, id='gene_b') c = Locus(1,180,200, id='gene_c') d = Locus(1,210,300, id='gene_d') e = Locus(2,100,150, id='gene_e') x = RefLoci('simpleRefLoci') x.add_loci([a,b,c,d,e]) return x
def t(): a = Locus('1', 1, 100) b = Locus('2', 200, 300) c = Locus('3', 300, 400) t = Term('test', desc='test term', loci=[a, b, c], attrs={ 'foo': 'bar', 'baz': 'bat' }) return t
def testRefGen(): # Create a Locus a = Locus(8,100,150, name='gene_a') # Createa couple more! b = Locus(8,160,175, name='gene_b') c = Locus(8,180,200, name='gene_c') d = Locus(8,210,300, name='gene_d') e = Locus(9,100,150, name='gene_e') x = Loci() x.append([a,b,c,d,e]) return x
def test_nuke_tables(): 'add a locus to an empty refloci db and then retrieve it' if m80.Tools.available('RefLoci', 'empty'): m80.Tools.delete('RefLoci', 'empty', force=True) empty = RefLoci('empty') assert len(empty) == 0 x = Locus('1', 1, 1, feature_type='gene', attrs={'foo': 'bar'}) y = Locus('1', 2, 2, feature_type='exon', attrs={'baz': 'bat'}) x.add_sublocus(y) LID = empty.add_locus(x) assert len(empty) == 1 empty._nuke_tables() assert len(empty) == 0 m80.Tools.delete('RefLoci', 'empty', force=True)
def test_add_locus_with_subloci(): 'add a locus to an empty refloci db and then retrieve it' if m80.Tools.available('RefLoci', 'empty'): m80.Tools.delete('RefLoci', 'empty', force=True) empty = RefLoci('empty') assert len(empty) == 0 x = Locus('1', 1, 1, feature_type='gene', attrs={'foo': 'bar'}) y = Locus('1', 2, 2, feature_type='exon', attrs={'baz': 'bat'}) x.add_sublocus(y) LID = empty.add_locus(x) assert len(empty) == 1 l = empty._get_locus_by_LID(LID) assert l['foo'] == 'bar' assert len(l.subloci) == 1 m80.Tools.delete('RefLoci', 'empty', force=True)
def test_loci_within(testRefGen): random_locus = testRefGen.random_locus() bigger_locus = Locus(random_locus.chrom, start=random_locus.start - 100, end=random_locus.end + 100) genes = testRefGen.genes_within(bigger_locus) assert random_locus in genes
def test_get_attrs(simpleRefLoci): nLocus = Locus(1, 2, 3, 'nLocus', foo="bar", baz="bat") simpleRefLoci.add_locus(nLocus) del nLocus nLocus = simpleRefLoci['nLocus'] assert nLocus['foo'] == 'bar' assert nLocus['baz'] == 'bat'
def test_removie_locus(simpleRefLoci): new_locus = Locus(1, 100, 300, id='new_locus') if new_locus not in simpleRefLoci: simpleRefLoci.add_locus(new_locus) assert new_locus in simpleRefLoci simpleRefLoci.remove_locus(new_locus.id) assert new_locus not in simpleRefLoci
def test_within_bad_strand(testRefGen): 'simple within to get chromosomal segment' with pytest.raises(StrandError): assert len( list( testRefGen.within(Locus('1', 1, 139000, strand='='), partial=False))) == 4
def test_within_error_on_both_same_strand_and_ignore_strand(testRefGen): try: testRefGen.within(Locus('1', 1, 139000, strand='-'), ignore_strand=True, same_strand=True) except ValueError as e: assert True
def test_upstream_plus_strand(testRefGen): # Below is GRMZM2G093399, but on the minus strand x = Locus('1', 136307, 138929) l = [x.name for x in testRefGen.upstream_loci(x, n=3)] assert l[0] == 'GRMZM2G093344' assert l[1] == 'GRMZM5G888250' assert l[2] == 'GRMZM2G059865'
def test_within_same_strand_minus(testRefGen): 'test fetching loci only on the same strand' assert len( list( testRefGen.within(Locus('1', 1, 139000, strand='-'), partial=True, same_strand=True))) == 3
def test_within_same_strand_and_ignore_strand(testRefGen): 'test fetching loci only on the same strand' with pytest.raises(ValueError): list( testRefGen.within(Locus('1', 1, 139000, strand='+'), ignore_strand=True, same_strand=True))
def test_downstream(testRefGen): l = [ x.name for x in testRefGen.downstream_loci(Locus('1', 4854, 9652), n=3) ] assert l[0] == 'GRMZM5G888250' assert l[1] == 'GRMZM2G093344' assert l[2] == 'GRMZM2G093399'
def test_downstream_same_strand_limit_n(testRefGen): l = [ x.name for x in testRefGen.downstream_loci( Locus('1', 4854, 9652), n=1, same_strand=True) ] assert len(l) == 1 # This skips GRMZM5G888250 and GRMZM2G093344 since they are on the - strand assert l[0] == 'GRMZM2G093399'
def test_add_locus(): 'add a locus to an empty refloci db and then retrieve it' if m80.Tools.available('RefLoci', 'empty'): m80.Tools.delete('RefLoci', 'empty', force=True) empty = RefLoci('empty') assert len(empty) == 0 empty.add_locus(Locus('1', 1, 1, feature_type='gene', attrs={'foo': 'bar'})) assert len(empty) == 1 m80.Tools.delete('RefLoci', 'empty', force=True)
def test_flank_limit_for_candidate_loci_from_SNP(testRefGen): random_locus = testRefGen.random_locus() downstream = testRefGen.downstream_loci(random_locus, locus_limit=10, window_size=50e6) test_snp = Locus(downstream[5].chrom, downstream[5].start, window=50e6) # Create a Locus that is on gene 5 candidates = testRefGen.candidate_loci(test_snp, flank_limit=5, window_size=50e6) assert len(candidates) == 11
def test_candidate_loci_from_SNP(testRefGen): random_locus = testRefGen.random_locus() # grab a bunch of downstream genes down1, down2 = testRefGen.downstream_loci(random_locus, locus_limit=2, window_size=50e6) # Create a Locus containing both down1 and down2 test_snp = Locus(down1.chrom, down1.start - 50, end=down2.end + 50, window=50e6) candidates = testRefGen.candidate_loci(test_snp, flank_limit=5, chain=False) assert len(candidates) == 12
def test_children_detach_in_children_setter(): x = Locus('1',1,2) y = Locus('1',3,4,name='sublocus1') z = Locus('1',3,4,name='sublocus2') x.children = [y] # Now trigger a detach for current children # when assigning new children x.children = [z] assert len(x.children) == 1
def __getitem__(self, item): ''' A convenience method to extract loci by their name attribute e.g. # Create a locus object >>> x = Locus('chr1',1,100,name='locus1') >>> y = Loci['locus1'] >>> x == y True ''' try: LID, = self._db.cursor().execute( ''' SELECT LID FROM loci WHERE name = ? ''', (item, )).fetchone() except TypeError: raise ValueError(f'{item} not in Loci') from locuspocus import Locus return Locus.from_LID(LID)
def _get_locus_by_LID(self, LID: int): ''' Get a locus by its LID Parameters ---------- LID : int A Locus ID. These are assigned to Locus objects when they are added to the Loci database. Returns ------- The Locus. Raises ------ `MissingLocusError` if there is no Locus in the database with that LID. ''' lid_exists, = self._db.cursor().execute( 'SELECT COUNT(*) FROM loci WHERE LID = ? ', (LID, )).fetchone() if lid_exists == 0: raise MissingLocusError from locuspocus import Locus return Locus(LID, self)
def test_from_record(simple_Locus): assert Locus.from_record( ('1', 100, 200, '<None>1:100-200', 0, '<None>1:100-200')) == simple_Locus
def test_get_LID_missing(testRefGen): 'Make sure that fetching a locus by LID returns the same locus' with pytest.raises(MissingLocusError): assert testRefGen._get_LID(Locus('na', 1, 1))
def test_encompassing_loci(testRefGen): x = Locus('1', 10000, 10000) loci = list(testRefGen.encompassing_loci(x)) assert loci[0].name == 'GRMZM5G888250'
def test_flank_loci_limited_n(testRefGen): x = Locus('1', 10500, 10500) up, down = testRefGen.flanking_loci(x, n=2) assert len(list(up)) == 2 assert len(list(down)) == 2
def test_within_strand_order_minus(testRefGen): # should return fourth gene first loci = list(testRefGen.within(Locus('1', 1, 139000, strand='-'))) assert loci[0].start == 136307
def test_downstream_limit_n(testRefGen): l = [ x.name for x in testRefGen.downstream_loci(Locus('1', 4854, 9652), n=1) ] assert l[0] == 'GRMZM5G888250' assert len(l) == 1
def test_downstream_accepts_loci(testRefGen): x = Locus('1', 4854, 9652) loci = [x, x] l1, l2 = map(list, testRefGen.downstream_loci(loci, n=2)) assert len(l1) == 2 assert len(l2) == 2