On 10/12/2011 09:57 AM, lina wrote:
I do have problems to write each blocks (differentiated by chainID) back
one by one, but this will leave it at the end. at present I still have
following problems
Q1: why the D E F G H I stopped being processed.
In what sense do you mean stopped? There are no records in pdbone.pdb
that have column 4 equal to any of the remaining CID values, D, E, F, etc.
So they don't print anything. You can see that 3 of them matched cID of "C"
for line in temp: print(line[1])
Thanks.
$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
C
ATOM 832 C21 CUR C 85 32.823 27.366 0.801 1.00
0.00
ATOM 831 O4 CUR C 85 31.865 28.248 0.183 1.00
0.00
ATOM 827 C19 CUR C 85 31.891 29.624 0.280 1.00
0.00
D
E
F
G
H
I
#!/usr/bin/python3
import os.path
LINESTOSKIP=0
CHAINID="CDEFGHI"
INFILENAME="pdbone.pdb"
DICTIONARYFILE="itpone.itp"
mapping={}
valuefromdict={}
def sortfile():
for chainid in CHAINID:
print(chainid)
sortoneblock(chainid)
def sortoneblock(cID):
text=fetchonefiledata(INFILENAME) ## Q2: How to avoid read this file
every time. actually it only need read once.
Simple. Move this line into sortfile (before the for loop), and pass
text as an argument when it calls sortoneblock(cID, text)
Naturally, you'd then add the parameter to sortoneblock(cID, text).
Once you try to create the output file, you'll also be adding the open()
call for that into sortfile(), and passing its file object into
sortoneblock(cID, text, outfile)
temp = []
for line in text:
blocks=line.strip().split()
if len(blocks)== 11 and blocks[3] == "CUR" and blocks[4] == cID and
blocks[2] in mapping.keys():
temp.append((mapping[blocks[2]],line))
temp.sort()
for line in temp:
print(line[1].strip())
def generatedictionary(dictfilename):
text=fetchonefiledata(DICTIONARYFILE)
for line in text:
parts=line.strip().split()
if len(parts)==8:
mapping[parts[4]]=parts[0]
print(mapping)
def fetchonefiledata(infilename):
text=open(infilename).readlines()
if os.path.splitext(infilename)[1]==".itp":
return text
if os.path.splitext(infilename)[1]==".pdb":
return text[LINESTOSKIP:]
if __name__=="__main__":
generatedictionary(DICTIONARYFILE)
sortfile()
Thanks.
I think your final version of sortfile() might look something like:
def sortfile(infilename=INFILENAME, outfilename=OUTFILENAME):
infile = open(infilename, "r")
intext = infile.readlines()
outfile = open(OUTFILENAME, "w")
for chainid in CHAINID:
print("chain id = ",chainid)
sortoneblock(chainid, intext, outfile)
infile.close()
outfile.close()
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor