[Tutor] map one file and print it out following the sequence
lina
lina.lastname at gmail.com
Thu Oct 13 15:09:17 CEST 2011
<snip>
> 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()
>
$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
C
Traceback (most recent call last):
File "map-to-itp.py", line 55, in <module>
sortfile()
File "map-to-itp.py", line 17, in sortfile
sortoneblock(chainid,intext,OUTFILENAME)
File "map-to-itp.py", line 29, in sortoneblock
f.write(line[1].strip() for line in temp)
TypeError: must be str, not generator
I don't know how to fix the writing issue.
can I write the different chainID one into the same OUTFILE?
Thanks, I attached the code I used below:
#!/usr/bin/python3
import os.path
LINESTOSKIP=0
CHAINID="CDEFGHI"
INFILENAME="pdbone.pdb"
OUTFILENAME="sortedone.pdb"
DICTIONARYFILE="itpone.itp"
mapping={}
valuefromdict={}
def sortfile():
intext=fetchonefiledata(INFILENAME)
for chainid in CHAINID:
print(chainid)
sortoneblock(chainid,intext,OUTFILENAME)
def 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()
with open(OUTFILE,"w") as f:
f.write(line[1].strip() for line in temp)
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:]
infilename.close()
if __name__=="__main__":
generatedictionary(DICTIONARYFILE)
sortfile()
>
>
> --
>
> DaveA
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111013/eb6ebf0d/attachment.html>
More information about the Tutor
mailing list