How to count total number of water ('HOH') molecules in each file ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iromi
    New Member
    • Mar 2017
    • 6

    How to count total number of water ('HOH') molecules in each file ?

    #!/usr/bin/python
    import os
    path=os.getcwd( )
    print path
    list_of_filenam es=os.listdir(p ath+'//newfiles')
    print list_of_filenam es
    residue=[]
    for f in list_of_filenam es:
    f1=open(path+'//newfiles//'+f).readlines( )
    for line in f1:
    if line.startswith ('HETATM'):
    res_number=line[22:26]
    if res_number not in residue and line[17:20]=='HOH':
    residue.append( res_number)
    else:
    continue
    else:
    continue
    print(len(resid ue))



    As the output of this script I got only one value which is the total number of 'HOH' molecules in all the files in that directory. But I need to count total number of 'HOH' molecules in each file one by one.
    So please can you help me to find out how this script should be changed according to my requirement?
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Code:
    for f in list_of_filenames:
        residue=[]  ## new list for each name
        f1=open(patf1=open(path+'//newfiles//'+f).readlines()
        for line in f1:
            ...
        print(len(residue))  ## for each name
    And note that else + continue does nothing. Try it for yourself and see what happens.

    Comment

    • iromi
      New Member
      • Mar 2017
      • 6

      #3
      @dwblas Thank you. I will try.

      Comment

      Working...