Ejemplo n.º 1
0
    def _get_nmer(self, n):
        """Get the string of the last N bases on the path stack.
        
        n: aninteger or integer-castable value; nonnegative
        
        Returns None if n is greater than the number of searchnodes on the
            path stack.  Otherwise, returns a string of the values of those
            nodes, in the order they were pushed on the stack
        """
        
        nmer = []
        result = None
        n = makeNonnegInt(n)
        
        #only try to get the nmer if there are at least n items on stack
        if n <= len(self._path_stack):
            #for n to 0 -- start with the furthest-back entry
            for temp_length in xrange(n, 0, -1):
                #Note that we add one bc temp length is base 1, while
                #top index isn't.  Ex: a stack with length 5 has top
                #index 4.  If you want the last 4, you want to start
                #at index 1.  Thus, 4 - 4 + 1 = 1
                temp_index = self._top_index - temp_length +1

                #get value of node at that index and add to list
                nmer.append(self._path_stack[temp_index].Value)
            #next intermediate length

            #join list to get current nmer and return
            result = "".join(nmer)
        #end if

        return result
Ejemplo n.º 2
0
 def _get_alphabet(self, position):
     """Return the alphabet for input position. If none, return default
     
     position: a nonnegative integer or something castable to it.  
         Positions are assumed to be ZERO based.
     """
     
     position = makeNonnegInt(position)
     
     if position in self._alphabets:
         result = self._alphabets[position]
     else:
         result = self._alphabets[self.DEFAULT_KEY]
     #end if
     
     return result
Ejemplo n.º 3
0
 def generate(self, path_length):
     """Generate a valid path of required length and return its value.
     
     Returns None if no path is possible.
     
     path_length: a nonnegative integer or castable to it.  Indicates
         length of desired valid path.
     """
     
     path_length = makeNonnegInt(path_length)
     
     #while length of path stack < path_length
     while len(self._path_stack) < path_length:
         #always have to get the alphabet based on the current 
         #top index plus 1.  This is because, if we have to remove
         #a node because it contributes to a forbidden sequence,
         #we need to make sure that the position used to generate
         #the next node is the same
         position = self._top_index
         if position is None: position = -1
         position += 1
         #get the alphabet for the next node
         curr_alphabet = self._get_alphabet(position)
     
         #make new SearchNode and push it onto path
         new_node = SearchNode(curr_alphabet)
         self._add_node(new_node)
         
         #select the next available allowed option
         option_exists = self.findAllowedOption()
         
         #if no more options, no valid searchpath is possible
         #break out and return None
         if not option_exists: return None
     #end while
     
     #return path as string
     return self.Value