Re: 'While' question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Keshet

    Re: 'While' question

    Thanks. I tried to use 'for' instead of 'while' as both of you
    suggested. It's running well as my previous version but breaks
    completely instead of just skipping the empty file. I suspect the
    reason is that this part is inside another 'for' so it stops
    everything. I just want to it to break only one 'for', that is go back
    to 5th line in the example code (directory C has one empty file):

    receptors = ['A', 'B', 'C']
    for x in receptors:
    print x
    for i in range(3):
    for r in (7, 9, 11, 13, 15, 17):
    f =
    open('c:/Linux/Dock_method_val idation/%s/validation/ligand_ran_line _%s_%sA_seconda ry_scored.mol2'
    %(x,i,r), 'r')
    line = f.readline()[:-1]
    out_file =
    open('c:/Linux/Dock_method_val idation/%s/validation/pockets.out' %(x),'a')
    out_file.write( '%s ' %i)
    out_file.write( '%s ' %r)
    # skip to scores
    j=0
    for line in f:
    line = line.rstrip()
    if "PRIMARY" not in line:
    j += 1
    if j == 20:
    break
    else:
    for line in f:
    if "TRIPOS" not in line:
    line = line.rstrip()
    out_file.write( line)
    else:
    break
    f.close()
    out_file.close( )

    Any suggestions as for how to control the "extent of break"? should I do
    something else instead? Thank you!
  • Wojtek Walczak

    #2
    Re: 'While' question

    On Fri, 22 Aug 2008 10:42:13 -0400, Ben Keshet wrote:
    Thanks. I tried to use 'for' instead of 'while' as both of you
    suggested. It's running well as my previous version but breaks
    completely instead of just skipping the empty file. I suspect the
    reason is that this part is inside another 'for' so it stops
    everything. I just want to it to break only one 'for', that is go back
    to 5th line in the example code (directory C has one empty file):
    for line in f:
    ^^^
    line = line.rstrip()
    if "PRIMARY" not in line:
    j += 1
    if j == 20:
    break
    else:
    for line in f:
    ^^^
    You're iterating through the same value in inner and outer loop.
    Don't do that. It's hard to predict the behavior of such a code.

    Regarding break statement, it breaks only the inner loop
    and returns to the outer loop/block.

    It would be great if you could reduce your code to a short piece
    that illustrates your problem and that we could all run.

    --
    Regards,
    Wojtek Walczak,

    Comment

    • Ben Keshet

      #3
      Re: 'While' question

      Wojtek Walczak wrote:
      On Fri, 22 Aug 2008 10:42:13 -0400, Ben Keshet wrote:
      >
      >Thanks. I tried to use 'for' instead of 'while' as both of you
      >suggested. It's running well as my previous version but breaks
      >completely instead of just skipping the empty file. I suspect the
      >reason is that this part is inside another 'for' so it stops
      >everything. I just want to it to break only one 'for', that is go back
      >to 5th line in the example code (directory C has one empty file):
      >>
      >
      >
      > for line in f:
      >>
      ^^^
      >
      > line = line.rstrip()
      > if "PRIMARY" not in line:
      > j += 1
      > if j == 20:
      > break
      > else:
      > for line in f:
      >>
      ^^^
      You're iterating through the same value in inner and outer loop.
      Don't do that. It's hard to predict the behavior of such a code.
      >
      Regarding break statement, it breaks only the inner loop
      and returns to the outer loop/block.
      >
      It would be great if you could reduce your code to a short piece
      that illustrates your problem and that we could all run.
      >
      >
      I ended up using another method as someone suggested to me. I am still
      not sure why the previous version got stuck on empty files, while this
      one doesn't:

      receptors = ['A' 'B']
      for x in receptors:
      # open out_file for appending for each 'x' in receptors, close at
      same level
      out_file =
      open('c:/Linux/Dock_method_val idation/%s/validation/pockets.out' %(x),'a')
      for i in range(10):
      for r in (7, 9, 11, 13, 15, 17):
      f =
      open('c:/Linux/Dock_method_val idation/%s/validation/ligand_ran_line _%s_%sA_seconda ry_scored.mol2'
      %(x,i,r), 'r')
      # assume 'PRIMARY' should be found first
      # set flag for string 'PRIMARY'
      primary = False
      # iterate on file object, empty files will be skipped
      for line in f:
      if 'PRIMARY' in line:
      primary = True
      out_file.write( line.strip())
      # copy scores
      elif 'TRIPOS' not in line and primary:
      out_file.write( line.strip())
      out_file.write( ' ')
      elif 'TRIPOS' in line and primary:
      break
      print
      out_file.write( '\n')
      f.close()
      out_file.close( )

      Comment

      • Scott David Daniels

        #4
        Re: 'While' question

        Ben Keshet wrote:
        ....
        I ended up using another method as someone suggested to me. I am still
        not sure why the previous version got stuck on empty files, while this
        one doesn't:
        >
        receptors = ['A' 'B']
        *** Alarm bells *** Do you mean ['AB'], or do you mean ['A', 'B']?
        ...(more code one way) ...
        Don't be afraid of defining functions, you are nested too deeply to
        easily understand, and hence likely to make mistakes. For similar
        reasons, I don't like names like x and i unless there are no better
        names. Also, since you don't seem to "really" need to write, I used
        print. The comments would be better if I knew the field a bit (or
        your code had had better names).


        Try something like (obviously I couldn't test it, so untested code):

        OUTPUT = 'c:/Linux/Dock_method_val idation/%s/validation/pockets.out'
        INPUT = ('c:/Linux/Dock_method_val idation/%s/validation/'
        'ligand_ran_lin e_%s_%sA_second ary_scored.mol2 ')

        def extract_dist(de st, receptor, line, ligand):
        '''Get distances after "PRIMARY" from the appropriate file
        '''
        source = open(INPUT % (receptor, line, ligand), 'r')
        gen = iter(source) # get a name for walking through the file.
        try:
        # Find the start
        for j, text in enumerate(gen):
        if 'PRIMARY' in text:
        print >>dest, text.strip(),
        break
        if j == 19: # Stop looking after 20 lines.
        return # nothing here, go on to the next one
        # copy scores up to TRIPOS
        for text in gen:
        if 'TRIPOS' in text:
        break
        print >>dest, text.strip(),
        print
        print >>dest
        finally:
        source.close()

        for receptor in 'A', 'B':
        # open out_file for appending per receptor, close at same level
        out_file = open(OUTPUT % receptor, 'a')
        for line in range(10):
        for ligand in (7, 9, 11, 13, 15, 17):
        extract_dist(ou t_file, receptor, line, ligand)
        out_file.close( )


        --Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        Working...