Skip to content

Instantly share code, notes, and snippets.

@cathalgarvey
Created March 31, 2014 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathalgarvey/9899882 to your computer and use it in GitHub Desktop.
Save cathalgarvey/9899882 to your computer and use it in GitHub Desktop.
Features missing from Python's string/list types that are handy for bio-informatics
"Functions missing from Python's string/list types that are handy for bio-informatics."
def codonise(seq):
'''Returns a list of codons, not including trailing 1/2n.
To get codons starting from letter X, pass seq[X:].'''
mylist = []
for i in range(0, len(seq), 3):
this_codon = seq[i:i+3]
# This bit ensures that only whole codons,
# not trailing bits, are added:
if len(this_codon) is 3:
mylist.append( this_codon )
return mylist
def allindices(string, substring):
'Returns a list of ALL indices of a substring, not just first.'
listindex = []
i = string.find(substring)
while i >= 0: # Because find returns -1 when it finds nothing.
listindex.append(i)
i = string.find(substring, i + 1)
return listindex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment