|
| 1 | +#!/usr/bin/env python |
| 2 | +#========================================================================= |
| 3 | +# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public |
| 4 | +# License (GPL) version 3, as described at www.opensource.org. |
| 5 | +# Copyright (C)2017 William H. Majoros (martiandna@gmail.com). |
| 6 | +#========================================================================= |
| 7 | +from __future__ import (absolute_import, division, print_function, |
| 8 | + unicode_literals, generators, nested_scopes, with_statement) |
| 9 | +from builtins import (bytes, dict, int, list, object, range, str, ascii, |
| 10 | + chr, hex, input, next, oct, open, pow, round, super, filter, map, zip) |
| 11 | +# The above imports should allow this program to run in both Python 2 and |
| 12 | +# Python 3. You might need to update your version of module "future". |
| 13 | + |
| 14 | +###################################################################### |
| 15 | +# |
| 16 | +# NgramIterator.py bmajoros |
| 17 | +# |
| 18 | +# Attributes: |
| 19 | +# array ngram : array of integer indices into alphabet string |
| 20 | +# string alphabet |
| 21 | +# Methods: |
| 22 | +# ngramIterator=NgramIterator("ATCG",N) |
| 23 | +# string=ngramIterator.nextString() # returns None if no more |
| 24 | +# ngramIterator.reset() |
| 25 | +# Private methods: |
| 26 | +# self.ngramToString() |
| 27 | +###################################################################### |
| 28 | + |
| 29 | +class NgramIterator: |
| 30 | +#--------------------------------------------------------------------- |
| 31 | +# PUBLIC METHODS |
| 32 | +#--------------------------------------------------------------------- |
| 33 | +# ngramIterator=NgramIterator("ATCG",N) |
| 34 | + def __init__(self,alphabet,N): |
| 35 | + ngram=[] |
| 36 | + alphaSize=len(alphabet) |
| 37 | + for i in range(N-1): ngram.append(0) |
| 38 | + if(N>0): ngram.append(-1) |
| 39 | + self.alphabet=alphabet |
| 40 | + self.ngram=ngram |
| 41 | +#--------------------------------------------------------------------- |
| 42 | +# ngramIterator.reset() |
| 43 | + def reset(self): |
| 44 | + ngram=self.ngram |
| 45 | + L=len(ngram) |
| 46 | + for i in range(L-1): ngram[i]=0 |
| 47 | + ngram[L-1]=-1 |
| 48 | +#--------------------------------------------------------------------- |
| 49 | +# string=ngramIterator.nextString() # returns undef if no more |
| 50 | + def nextString(self): |
| 51 | + alphabet=self.alphabet |
| 52 | + ngram=self.ngram |
| 53 | + if(ngram is None): return None |
| 54 | + L=len(ngram) |
| 55 | + if(L==0): |
| 56 | + self.ngram=None |
| 57 | + return "" |
| 58 | + alphaSize=len(alphabet) |
| 59 | + for i in range(L-1,-1,-1): # for($i=$len-1 ; $i>=0 ; --$i) |
| 60 | + ngram[i]+=1 |
| 61 | + index=ngram[i] |
| 62 | + if(index<alphaSize): return self.ngramToString() |
| 63 | + ngram[i]=0 |
| 64 | + return None |
| 65 | +#--------------------------------------------------------------------- |
| 66 | +#--------------------------------------------------------------------- |
| 67 | +#--------------------------------------------------------------------- |
| 68 | +#--------------------------------------------------------------------- |
| 69 | +#--------------------------------------------------------------------- |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +#--------------------------------------------------------------------- |
| 77 | +# PRIVATE METHODS |
| 78 | +#--------------------------------------------------------------------- |
| 79 | +# self.ngramToString() |
| 80 | + def ngramToString(self): |
| 81 | + ngram=self.ngram |
| 82 | + alphabet=self.alphabet |
| 83 | + length=len(ngram) |
| 84 | + string="" |
| 85 | + for i in range(length): |
| 86 | + index=ngram[i] |
| 87 | + string+=alphabet[index:index+1] |
| 88 | + return string |
| 89 | + |
| 90 | + |
0 commit comments