def normalize_jxns(chrom1, chrom2, pos1, pos2, strand1, strand2, repleft, repright, order):
'''Choose one representation for DNA breakpoints'''
flipstr = maketrans("-+", "+-")
if order == 2:
if strand1 == "-":
new_pos1 = str(chrom1) + ":" + str(pos1 - int(repright)) + ":" + strand1.translate(flipstr)
else:
new_pos1 = str(chrom1) + ":" + str(pos1 + int(repright)) + ":" + strand1.translate(flipstr)
if strand2 == "-":
new_pos2 = str(chrom2) + ":" + str(pos2 - int(repright)) + ":" + strand2.translate(flipstr)
else:
new_pos2 = str(chrom2) + ":" + str(pos2 + int(repright)) + ":" + strand2.translate(flipstr)
newid = new_pos2 + ":" + new_pos1 + ":" + str(repleft) + ":" + str(repright)
elif order == 1:
new_pos1 = str(chrom1) + ":" + str(pos1) + ":" + strand1
new_pos2 = str(chrom2) + ":" + str(pos2) + ":" + strand2
newid = new_pos1 + ":" + new_pos2 + ":" + str(repleft) + ":" + str(repright)
return newid
python类maketrans()的实例源码
def flip_jxn(jxn, gs1):
'''Flip jxn orientation for RNA Fusion that is inverse according to strand info'''
chrom1, pos1, str1, chrom2, pos2, str2, repleft, repright = re.split(':', jxn)
chrom1 = str(chrom1)
chrom2 = str(chrom2)
pos1 = int(pos1)
pos2 = int(pos2)
if str1 != gs1[0]:
flip = 1
flipstr = maketrans("-+", "+-")
if str1 == "-":
new_pos1 = chrom1 + ":" + str(pos1) + ":" + str1.translate(flipstr)
else:
new_pos1 = chrom1 + ":" + str(pos1) + ":" + str1.translate(flipstr)
if str2 == "-":
new_pos2 = chrom2 + ":" + str(pos2) + ":" + str2.translate(flipstr)
else:
new_pos2 = chrom2 + ":" + str(pos2) + ":" + str2.translate(flipstr)
newid = new_pos2 + ":" + new_pos1 + ":" + str(repright) + ":" + str(repleft)
else:
newid = jxn
flip = 0
return (newid, flip)
def isIsomorphic(self, s, t):
if len(s) != len(t):
return False
for i in range(len(s)):
if occurence(s, s[i]) != occurence(t, t[i]):
return False
return True
# def isIsomorphic(self, s, t):
# if len(s) != len(t):
# return False
# lens = len(s)
# s_dict = ''
# t_dict = ''
# for idx, ele in enumerate(s):
# if ele not in s_dict:
# s_dict += ele
# t_dict += t[idx]
# translated = s.translate(maketrans(s_dict, t_dict))
# print translated, t
# if translated == t:
# return True
# return False
def b64decode(s, altchars=None):
"""Decode a Base64 encoded string.
s is the string to decode. Optional altchars must be a string of at least
length 2 (additional characters are ignored) which specifies the
alternative alphabet used instead of the '+' and '/' characters.
The decoded string is returned. A TypeError is raised if s were
incorrectly padded or if there are non-alphabet characters present in the
string.
"""
if altchars is not None:
s = s.translate(string.maketrans(altchars[:2], '+/'))
try:
return binascii.a2b_base64(s)
except binascii.Error, msg:
# Transform this exception for consistency
raise TypeError(msg)
def _ebcdic_to_ascii(s):
global _ebcdic_to_ascii_map
if not _ebcdic_to_ascii_map:
emap = (
0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15,
16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31,
128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7,
144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26,
32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33,
38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94,
45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63,
186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34,
195,97,98,99,100,101,102,103,104,105,196,197,198,199,200,201,
202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
123,65,66,67,68,69,70,71,72,73,232,233,234,235,236,237,
125,74,75,76,77,78,79,80,81,82,238,239,240,241,242,243,
92,159,83,84,85,86,87,88,89,90,244,245,246,247,248,249,
48,49,50,51,52,53,54,55,56,57,250,251,252,253,254,255
)
import string
_ebcdic_to_ascii_map = string.maketrans( \
''.join(map(chr, range(256))), ''.join(map(chr, emap)))
return s.translate(_ebcdic_to_ascii_map)
def _ebcdic_to_ascii(self, s):
c = self.__class__
if not c.EBCDIC_TO_ASCII_MAP:
emap = (0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15,
16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31,
128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7,
144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26,
32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33,
38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94,
45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63,
186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34,
195,97,98,99,100,101,102,103,104,105,196,197,198,199,200,
201,202,106,107,108,109,110,111,112,113,114,203,204,205,
206,207,208,209,126,115,116,117,118,119,120,121,122,210,
211,212,213,214,215,216,217,218,219,220,221,222,223,224,
225,226,227,228,229,230,231,123,65,66,67,68,69,70,71,72,
73,232,233,234,235,236,237,125,74,75,76,77,78,79,80,81,
82,238,239,240,241,242,243,92,159,83,84,85,86,87,88,89,
90,244,245,246,247,248,249,48,49,50,51,52,53,54,55,56,57,
250,251,252,253,254,255)
import string
c.EBCDIC_TO_ASCII_MAP = string.maketrans( \
''.join(map(chr, xrange(256))), ''.join(map(chr, emap)))
return s.translate(c.EBCDIC_TO_ASCII_MAP)
def test_trivial(self):
# A couple trivial tests
self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')
# XXX Name hacking to get this to work on Windows.
fname = os.path.abspath(urllib2.__file__).replace('\\', '/')
# And more hacking to get it to work on MacOS. This assumes
# urllib.pathname2url works, unfortunately...
if os.name == 'riscos':
import string
fname = os.expand(fname)
fname = fname.translate(string.maketrans("/.", "./"))
if os.name == 'nt':
file_url = "file:///%s" % fname
else:
file_url = "file://%s" % fname
f = urllib2.urlopen(file_url)
buf = f.read()
f.close()
def get_ext_fullpath(self, ext_name):
"""Returns the path of the filename for a given extension.
The file is located in `build_lib` or directly in the package
(inplace option).
"""
# makes sure the extension name is only using dots
all_dots = string.maketrans('/'+os.sep, '..')
ext_name = ext_name.translate(all_dots)
fullname = self.get_ext_fullname(ext_name)
modpath = fullname.split('.')
filename = self.get_ext_filename(ext_name)
filename = os.path.split(filename)[-1]
if not self.inplace:
# no further work needed
# returning :
# build_dir/package/path/filename
filename = os.path.join(*modpath[:-1]+[filename])
return os.path.join(self.build_lib, filename)
# the inplace option requires to find the package directory
# using the build_py command for that
package = '.'.join(modpath[0:-1])
build_py = self.get_finalized_command('build_py')
package_dir = os.path.abspath(build_py.get_package_dir(package))
# returning
# package_dir/filename
return os.path.join(package_dir, filename)
def test(args=[]):
if not args:
args = [
'/etc/passwd',
'file:/etc/passwd',
'file://localhost/etc/passwd',
'ftp://ftp.gnu.org/pub/README',
'http://www.python.org/index.html',
]
if hasattr(URLopener, "open_https"):
args.append('https://synergy.as.cmu.edu/~geek/')
try:
for url in args:
print '-'*10, url, '-'*10
fn, h = urlretrieve(url, None, reporthook)
print fn
if h:
print '======'
for k in h.keys(): print k + ':', h[k]
print '======'
with open(fn, 'rb') as fp:
data = fp.read()
if '\r' in data:
table = string.maketrans("", "")
data = data.translate(table, "\r")
print data
fn, h = None, None
print '-'*40
finally:
urlcleanup()
def __caesar(self, plaintext, shift):
lower = string.ascii_lowercase
lower_trans = lower[shift:] + lower[:shift]
alphabet = lower + lower.upper()
shifted = lower_trans + lower_trans.upper()
return plaintext.translate(string.maketrans(alphabet, shifted))
def _ConvertToValidName(name):
"""Converts to name that we can use as a kubernetes job prefix.
Args:
name: benchmark name.
Returns:
Benchmark name that can be used as a kubernetes job prefix.
"""
return name.translate(maketrans('/:_', '---'))
def emit_text(self, text):
"""Emit straight text to the file"""
if self.timeout is not None:
if select.select([],[self.handle],[], self.timeout) == ([],[],[]):
raise IOError("Timed out waiting for write")
self.handle.write(
"\x1e%s\n" % (text.translate(string.maketrans('', ''), "\n"))
)
self.handle.flush()
def encode(plaintext, code):
"Encodes text, using a code which is a permutation of the alphabet."
from string import maketrans
trans = maketrans(alphabet + alphabet.upper(), code + code.upper())
return plaintext.translate(trans)
def rev_comp(string):
"""Outputs reverse complement of a nucleotide sequence"""
if sys.version_info.major == 2:
import string as st
complement = string.translate(st.maketrans('ACGT', 'TGCA'))
else:
complement = string.translate(str.maketrans('ACGT', 'TGCA'))
return complement[::-1]
def _ebcdic_to_ascii(self, s):
c = self.__class__
if not c.EBCDIC_TO_ASCII_MAP:
emap = (0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15,
16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31,
128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7,
144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26,
32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33,
38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94,
45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63,
186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34,
195,97,98,99,100,101,102,103,104,105,196,197,198,199,200,
201,202,106,107,108,109,110,111,112,113,114,203,204,205,
206,207,208,209,126,115,116,117,118,119,120,121,122,210,
211,212,213,214,215,216,217,218,219,220,221,222,223,224,
225,226,227,228,229,230,231,123,65,66,67,68,69,70,71,72,
73,232,233,234,235,236,237,125,74,75,76,77,78,79,80,81,
82,238,239,240,241,242,243,92,159,83,84,85,86,87,88,89,
90,244,245,246,247,248,249,48,49,50,51,52,53,54,55,56,57,
250,251,252,253,254,255)
import string
c.EBCDIC_TO_ASCII_MAP = string.maketrans(
''.join(map(chr, list(range(256)))), ''.join(map(chr, emap)))
return s.translate(c.EBCDIC_TO_ASCII_MAP)
# A partial mapping of ISO-Latin-1 to HTML entities/XML numeric entities.
def fix_ugly_names(opts, uglydir):
"""Rename ugly file names to a beautified shell-correct names.
Collect ugly file names, perform beautification, rename.
"""
# Shell-unfriendly characters made into a string. The user-provided
# FROMCHARS and TOCHARS must be removed from the UGLYCHARS so that
# they can be transformed instead of removed.
delchars = opts.delchars + ''.join(set(UGLYCHARS)
- set(opts.tochars+opts.fromchars))
# Table for later translation (removal of `delchars`).
table = string.maketrans(opts.fromchars, opts.tochars)
uglyroot = os.path.expanduser(uglydir)
# Build list of name-change candidates.
if opts.recurse:
uglies = [f
for root , _, _ in os.walk(uglyroot, topdown=False)
for f in glob.glob(os.path.join(root, opts.limitglob))]
else:
uglies = glob.glob(os.path.join(uglyroot, opts.limitglob))
pretties = [beautify(ugly, table, delchars, opts.stringfunc)
for ugly in uglies]
# Do the renaming.
for ugly, pretty in zip(uglies, pretties):
if ugly != pretty:
if not opts.silent: print ugly, '-->', pretty
if not opts.dryrun: os.rename(ugly, pretty)
def __init__(self, frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)
self.trans = string.maketrans(frm, to)
if keep is None:
self.delete = delete
else:
self.delete = self.allchars.translate(self.allchars, keep.translate(self.allchars, delete))