def testValidate(self): """Setting CDSPosition anchor offset should fail for invalid cases""" intron = CDSPosition("22+4") self.assertRaises(CDSPositionError, setattr, intron, "offset", 0) self.assertRaises(CDSPositionError, setattr, intron, "anchor", -5) exon = CDSPosition("60") self.assertRaises(CDSPositionError, setattr, exon, "anchor", None)
def p2c(self, ppos, dialect=None): """Convert integer from protein coordinate to CDS closed range Parameters ---------- ppos : int Protein position dialect : str Coordinate dialect (GenBank or HGVS, default None) Returns ------- CDSPosition """ try: ppos = int(ppos) except TypeError: return None if ppos < 0: raise ProteinPositionError("'%s' should not be negative") # FIXME is CDS guaranteed to have len % 3 == 0? first_base = (ppos) * 3 last_base = first_base + 2 if last_base > len(self.exons): raise ProteinPositionError("'%s' is too large") return (CDSPosition(first_base), CDSPosition(last_base))
def g2c(self, gpos, dialect=None): """Convert integer from genomic to CDS coordinates Parameters ---------- gpos : int Genomic position dialect : str Coordinate dialect (GenBank or HGVS, default None) Returns ------- CDSPosition """ gpos = int(gpos) fmts = CDSPosition.fmt_dict def _simple_g2c(g): return CDSPosition(self.exon_list.index(g)) # within exon if gpos in self.exons: return _simple_g2c(gpos) # before CDS if gpos < self.exons.start: return CDSPosition(fmts['post-CDS'] % (gpos - self.exons.start)) # after CDS if gpos >= self.exons.end: return CDSPosition(fmts['pre-CDS'] % (gpos - self.exons.end + 1)) # intron # set start of first intron prev_end = self.exons.parts[0].end for part in self.exons.parts[1:]: # not in this intron if gpos > part.start: prev_end = part.end continue len_intron = part.start - prev_end # second half (exclusive) of intron # offset > middle of intron if gpos - prev_end > floor(len_intron / 2.0): anchor = _simple_g2c(part.start) offset = gpos - part.start assert offset < 0 # first half (inclusive) of intron else: anchor = _simple_g2c(prev_end - 1) offset = gpos - prev_end + 1 assert offset > 0 assert self.check_intron(anchor, offset) return CDSPosition(fmts['intron'] % (anchor, offset)) assert False # function should return for every integer
def testGoodOutside(self): """g2c should work for outside positions (CDSPosition)""" for arg, tup in zip(g_outside[self.dialect], c_outside_tups): actual = cmap.g2c(arg) expect = CDSPosition.from_anchor(*tup) self.assertEqual(actual, expect) self.assertEqual(str(actual), str(expect))
def testGoodIntrons(self): """g2c should work for intron positions (CDSPosition)""" for arg, tup in zip(g_introns[self.dialect], c_intron_tups): actual = cmap.g2c(arg, self.dialect) expect = CDSPosition.from_anchor(*tup) self.assertEqual(actual, expect) self.assertEqual(str(actual), str(expect))
def _simple_g2c(g): return CDSPosition(self.exon_list.index(g))
def testGoodOutside(self): """CDSPosition should match good outside-CDS values""" for c_args, c in zip(c_outside_tups, c_outside[self.dialect]): actual = CDSPosition.from_anchor(*c_args).to(self.dialect) self.assertEqual(actual, c)
def testGoodIntron(self): """CDSPosition should match good intron values""" for c_args, c in zip(c_intron_tups, c_introns[self.dialect]): actual = CDSPosition.from_anchor(*c_args).to(self.dialect) self.assertEqual(actual, c)
def testGoodOutside(self): """c2g should work for outside (CDSPosition)""" for args, expect in zip(c_outside_tups, g_outside[self.dialect]): self.assertEqual(cmap.c2g(CDSPosition.from_anchor(*args)), expect)
def testGoodIntrons(self): """c2g should work for introns (CDSPosition)""" dia = self.dialect for c_args, g in zip(c_intron_tups, g_introns[dia]): actual = cmap.c2g(CDSPosition.from_anchor(*c_args), dia) self.assertEqual(actual.to(dia), g)
def testBadNotProtein(self): """c2p should fail for CDSPosition or str""" bad = ("string", CDSPosition.from_anchor(7, -5)) for arg in bad: self.assertRaises(ValueError, cmap.c2p, arg)