-3
import re
import sys
import os
import xmltodict

if len(sys.argv) < 2:
    message = '\n Get 7 TM pdb files from a GPCR PDB. \n\n Usage: '+sys.argv[0] + ' [Input csv file with TM info] \n' + ' Example: ' + sys.argv[0] + '  pdbtms7.csv\n' + ' Output Files: pdb_2rh1A_tm1.pdb ... pdb_2rh1A_tm7.pdb'
    print (message)
    exit()
csvFile=sys.argv[1]
f = open (csvFile,'r')
k = 0
data = f.readlines()
for linedata in data:
    line=linedata.strip()
    letters = line.split(',')
    print (letters)
    pdbId=letters[0]
    chain=letters[1]
    numTMs=int(letters[3])
    for i in range(numTMs):
         j=((2*i)+4)
         k=((2*i)+5)
       #  print(i,j,k)
         print(i+1,letters[j],letters[k])
         dump=  'pdb_'+ pdbId + chain + '_tm' + str(i+1) +'.pdb'
         pdbFile=pdbId+'.pdb'
         wgetcom='wget https://files.rcsb.org/view/'+pdbFile+' -O '+pdbFile
         os.system(wgetcom)
         p = open (pdbFile,'r')
           data =p.readlines()
         g = open(dump,'w')
         for linedata in data:
             line=linedata.strip()
             words = line.split()
             if (words[0]=='ATOM'):
                 words[5]=int(line[22:26].strip())
                 if(words[5] in range(j,k+1)):
                     g.write(linedata)

g.close()
f.close()

this code is supposed to create 7 other files as an output; the code actually creates them, but it does not write anything inside the files, so is there any way to fix this issue.

Thanks very much in advance!

rafeed
  • 1

1 Answers1

1

You're doing one of 2 things, you're either overwriting your dump file with nothing at the end, OR you are not flushing the data. I highly doubt it's overwriting since you state that the code is creating the 7 individual files. However we don't have access to your cvs file you're using as an input. The likely answer is that after creating the file, no data is being written to the disk. Here is an example as to how to do that.

data =p.readlines()
with open(dump,'w') as g:
  for linedata in data:
    line=linedata.strip()
    words = line.split()
    if (words[0]=='ATOM'):
      words[5]=int(line[22:26].strip())
      if(words[5] in range(j,k+1)):
        g.write(linedata)
  g.flush()
  os.fsync()
f.close()
David Fisher
  • 282
  • 2
  • 13
  • can you please explain more – rafeed Oct 21 '18 at 02:13
  • add `g.flush()` after your `for linedata in data:` loop – David Fisher Oct 21 '18 at 02:14
  • https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file#3168436 Also not sure where exactly you placed g.flush() but if it's not working, you might want to change the way you open your g file. `with open(dump, "w") as g`, the file will automatically close at the end of the block. Making flushing a little easier to place. Also look at the comments about fsync. – David Fisher Oct 21 '18 at 02:21
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – jhpratt Oct 21 '18 at 02:30
  • @jhpratt, This is a duplicate question. Referenced in the comments to better question with better answer with more content from people far smarter than me. – David Fisher Oct 21 '18 at 02:33
  • @DavidFisher If it's a duplicate, flag it as such, don't answer it. – jhpratt Oct 21 '18 at 02:34
  • @jhpratt, new users that are also new programmers cannot gain any rep to actually use the site with that system. *points at my first question that was also a duplicate when I wrote it years ago* – David Fisher Oct 21 '18 at 02:41