'Reactivating' a previous while statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nleake
    New Member
    • May 2010
    • 1

    'Reactivating' a previous while statement

    Hey,

    I've been trying to use Python to help me with some DNA sequencing work. I've figured out how to use Biopython for the importing work; however, I need some python code to help me remove a smaller regulatory sequence (a short string) from a larger DNA sequence (a longer string) recursively so that when one is removed and the subsequent joining creates a new regulatory sequence (a short string) this new regulatory sequence is removed as well. The following code shows this better.

    Before working with the large files, I made some test code to show this is a working program. Code is as follows:

    Code:
    >>> x = ("CCCGACTGATATCTACCCC") #My long string
    >>> y = ("GATA") #One of my short strings
    >>> z = ("CTTC") #Another short string
    
    >>>running = True #Bool to work with while statement
    >>>running_y = True #Bool to work with while statement
    >>>running_z = True #Bool to work with while statement
    
    >>>while running:  #While statment to remove short strings.
    	while running_y:
    		n = x.replace(y,'',1) #Finds 'y' and removes from 'x'
    		if x != n: #If something was removed
    			x = n #x now equals the removed 
    		else:
    			running_y = False #turns off the while #statement
    	while running_z: #similar to while y statement with z
    		n = x.replace(z,'',1)
    		if x != n:
    			x = n
    		else:
    			running_z = False
    			running = False
    >>> print x
    CCCGATACCCC
    #Next, I tried to 'reactivate' the while loop to remove the new 'y' string created #by the following code:
    Code:
    >>>r = x.find(y) 
    >>>if r == True:
    	running_y = True
    	running_z = True
    	running = True
    >>> print x
    CCCGATACCCC
    Unfortunetately , this code doesn't 'reactivate' the while statement because the 'y' string of 'GATA' is still in 'x'. So, how do I make this while statement to be recursive so that newly made 'y' sequences from the rejoining can be found and removed or is there a more logical way to remove newly created elements after the joining?

    I apologize for not knowing the lingo since I am still fairly new to Python.
    If my question is still confusing and needs further clarification, please let me know. Any advice/help will be greatly appreciated.

    Thanks,
    Nick
    Last edited by bvdet; May 5 '10, 10:46 AM. Reason: Add code tags
  • woooee
    New Member
    • Mar 2008
    • 43

    #2
    Pass the sequence to a function which returns the updated sequence. You can do this from the while loop and anywhere else in the program you want, providing the function is defined before the code calling it (the norm is to declare all functions first with the body of the code following).

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      nleake,

      I am not sure this does what you want, but the short strings are removed to leave "CCCCCCC".
      Code:
      def remove_ss(ls, *args):
          while True:
              for ss in args:
                  if ss in ls:
                      ls = ls.replace(ss, '', 1)
              if True not in [(item in ls) for item in args]:
                  return ls
      
      x = "CCCGACTGATATCTACCCC"
      y = "GATA"
      z = "CTTC"
      
      print remove_ss(x, y, z)

      Comment

      Working...