Created
March 31, 2014 19:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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