<br><br><div class="gmail_quote">On Wed, Oct 12, 2011 at 3:29 PM, Andreas Perstinger <span dir="ltr"><<a href="mailto:andreas.perstinger@gmx.net">andreas.perstinger@gmx.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On 2011-10-12 05:31, lina wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I tried to write one (not working one) as below, so many problems here.<br>
</blockquote>
<br></div>
Just some quick remarks:</blockquote><div><br>Thanks,<br><br>Now the newly-improved one as following: but still the "sort" parts did not work.<br><br>#!/usr/bin/python3<br><br>import os.path<br><br>LINESTOSKIP=0<br>
CHAINID="CDEFGHI"<br>INFILENAME="pdbone.pdb"<br>DICTIONARYFILE="itpone.itp"<br>mapping={}<br>valuefromdict={}<br><br>def sortfile():<br> for chainid in CHAINID:<br> sortoneblock(chainid)<br>
<br><br>def generatedictionary(dictfilename):<br> text=fetchonefiledata(DICTIONARYFILE)<br> for line in text:<br> parts=line.strip().split()<br> if len(parts)==8:<br> mapping[parts[4]]=parts[0]<br>
print(mapping)<br> <br>def sortoneblock(cID):<br> text=fetchonefiledata(INFILENAME)<br> for line in text:<br> blocks=line.strip().split()<br> if len(blocks)== 11 and blocks[3] == "CUR" and blocks[4] == cID and blocks[2] in mapping.keys():<br>
valuefromdict[cID]=mapping[blocks[2]]<br> print(valuefromdict)<br> <br> <br> <br>def fetchonefiledata(infilename):<br> text=open(infilename).readlines()<br> if os.path.splitext(infilename)[1]==".itp":<br>
return text<br> if os.path.splitext(infilename)[1]==".pdb":<br> return text[LINESTOSKIP:]<br> <br> <br>if __name__=="__main__":<br> generatedictionary(DICTIONARYFILE)<br>
sortfile() <br><br> The result is:<br><br> $ python3 map-to-itp.py <br>{'O4': '2', 'C19': '3', 'C21': '1'}<br>{'C': '3'}<br>{'C': '2'}<br>
{'C': '1'}<br><br>for print(mapping) part, {'O4': '2', 'C19': '3', 'C21': '1'} the value doesn't keep the 1, 2, 3 order any more.<br><br>Thanks for further suggestions.<br>
<br>The relevant files I put it here:<br><br><a href="https://docs.google.com/leaf?id=0B93SVRfpVVg3NzkyOGU2ZTUtZTFjNC00ZjE4LThhNmQtOWY1YWFkOWI0NWEw&hl=en_GB">https://docs.google.com/leaf?id=0B93SVRfpVVg3NzkyOGU2ZTUtZTFjNC00ZjE4LThhNmQtOWY1YWFkOWI0NWEw&hl=en_GB</a><br>
<a href="https://docs.google.com/leaf?id=0B93SVRfpVVg3YTEwZjhiOTItN2I2Yi00NTEyLTljODAtYTc2ODI4Njk1YzZl&hl=en_GB">https://docs.google.com/leaf?id=0B93SVRfpVVg3YTEwZjhiOTItN2I2Yi00NTEyLTljODAtYTc2ODI4Njk1YzZl&hl=en_GB</a><br>
<a href="https://docs.google.com/leaf?id=0B93SVRfpVVg3M2Y1MWZiMmEtOTE2Mi00M2VjLTljNjAtYWNlMjhiNzEyODY1&hl=en_GB">https://docs.google.com/leaf?id=0B93SVRfpVVg3M2Y1MWZiMmEtOTE2Mi00M2VjLTljNjAtYWNlMjhiNzEyODY1&hl=en_GB</a><br>
<br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#!/usr/bin/python3<br>
<br>
import os.path<br>
<br>
LINESTOSKIP=0<br>
CHAINID="CDEFGHI"<br>
INFILENAME="pdbone.pdb"<br>
DICTIONARYFILE="itpone.itp"<br>
mapping={}<br>
valuefromdict={}<br>
<br>
def sortfile():<br>
for chainid in CHAINID:<br>
sortoneblock(chainid)<br>
<br>
<br>
def generatedictionary(<u></u>dictfilename):<br>
</blockquote>
<br></div>
You define the function with the parameter "dictfilename" but you'll never use it.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
text=fetchonefiledata(<u></u>DICTIONARYFILE)<br>
for line in text:<br>
parts=line.strip().split()<br>
if len(parts)==8:<br>
mapping[parts[4]]=parts[0]<br>
print(mapping)<br>
</blockquote>
<br></div>
The if-branch is probably wrongly indented (should be inside the for-loop).<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
def sortoneblock(cID)<br>
text=fetchonefiledata(<u></u>INFILENAME)<br>
for line in text:<br>
blocks=line.strip().split()<br>
if len(blocks)== 11 and blocks[3] == "CUR" and blocks[4] == "cID":<br>
</blockquote>
<br>
<br></div>
"cID" is a string-variable but you compare block 4 to the literal string "cID". In "pdbone.pdb" you will never find "cID" so this function will do nothing. You probably mean "blocks[4] == cID".<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
valuefromdict[blocks[2]]=<u></u>mapping[block[2]]<br>
</blockquote>
<br></div>
You never fill up "mapping" because you never call your "generatedictionary"-function. Therefore "mapping" is still an empty dictionary and this line will raise an exception.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
return<br>
</blockquote>
<br>
Firstly, the indentation is wrong because you would leave "sortoneblock" after the first processed line. Secondly, as you return nothing, you don't need this line because you will leave the function anyway.<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
def fetchonefiledata(infilename):<br>
text=open("infilename").<u></u>readlines()<br>
</blockquote>
<br></div>
Again, "infilename" is a variable, so no need for the quotes.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
if os.path.splitext(infilename)[<u></u>1]=".itp"<br>
return text<br>
if os.path.splitext(infilename)[<u></u>1]=".pdb"<br>
return text[LINESTOSKIP:]<br>
<br>
<br>
if __name__=="__main__":<br>
sortfiles()<br>
</blockquote>
<br></div>
There is no "sortfiles()" in your script. The function you probably mean is called "sortfile()"<br>
<br>
Bye, Andreas<div><div></div><div class="h5"><br>
______________________________<u></u>_________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br>