<br><br><div class="gmail_quote">On Wed, Oct 12, 2011 at 3:29 PM, Andreas Perstinger <span dir="ltr">&lt;<a href="mailto:andreas.perstinger@gmx.net">andreas.perstinger@gmx.net</a>&gt;</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 &quot;sort&quot; parts did not work.<br><br>#!/usr/bin/python3<br><br>import os.path<br><br>LINESTOSKIP=0<br>
CHAINID=&quot;CDEFGHI&quot;<br>INFILENAME=&quot;pdbone.pdb&quot;<br>DICTIONARYFILE=&quot;itpone.itp&quot;<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] == &quot;CUR&quot; 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]==&quot;.itp&quot;:<br>
        return text<br>    if os.path.splitext(infilename)[1]==&quot;.pdb&quot;:<br>        return text[LINESTOSKIP:]<br>            <br>         <br>if __name__==&quot;__main__&quot;:<br>    generatedictionary(DICTIONARYFILE)<br>
    sortfile()   <br><br>   The result is:<br><br> $ python3 map-to-itp.py <br>{&#39;O4&#39;: &#39;2&#39;, &#39;C19&#39;: &#39;3&#39;, &#39;C21&#39;: &#39;1&#39;}<br>{&#39;C&#39;: &#39;3&#39;}<br>{&#39;C&#39;: &#39;2&#39;}<br>
{&#39;C&#39;: &#39;1&#39;}<br><br>for print(mapping) part, {&#39;O4&#39;: &#39;2&#39;, &#39;C19&#39;: &#39;3&#39;, &#39;C21&#39;: &#39;1&#39;} the value doesn&#39;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&amp;hl=en_GB">https://docs.google.com/leaf?id=0B93SVRfpVVg3NzkyOGU2ZTUtZTFjNC00ZjE4LThhNmQtOWY1YWFkOWI0NWEw&amp;hl=en_GB</a><br>
<a href="https://docs.google.com/leaf?id=0B93SVRfpVVg3YTEwZjhiOTItN2I2Yi00NTEyLTljODAtYTc2ODI4Njk1YzZl&amp;hl=en_GB">https://docs.google.com/leaf?id=0B93SVRfpVVg3YTEwZjhiOTItN2I2Yi00NTEyLTljODAtYTc2ODI4Njk1YzZl&amp;hl=en_GB</a><br>
<a href="https://docs.google.com/leaf?id=0B93SVRfpVVg3M2Y1MWZiMmEtOTE2Mi00M2VjLTljNjAtYWNlMjhiNzEyODY1&amp;hl=en_GB">https://docs.google.com/leaf?id=0B93SVRfpVVg3M2Y1MWZiMmEtOTE2Mi00M2VjLTljNjAtYWNlMjhiNzEyODY1&amp;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=&quot;CDEFGHI&quot;<br>
INFILENAME=&quot;pdbone.pdb&quot;<br>
DICTIONARYFILE=&quot;itpone.itp&quot;<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 &quot;dictfilename&quot; but you&#39;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] == &quot;CUR&quot; and blocks[4] == &quot;cID&quot;:<br>
</blockquote>
<br>
<br></div>
&quot;cID&quot; is a string-variable but you compare block 4 to the literal string &quot;cID&quot;. In &quot;pdbone.pdb&quot; you will never find &quot;cID&quot; so this function will do nothing. You probably mean &quot;blocks[4] == cID&quot;.<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 &quot;mapping&quot; because you never call your &quot;generatedictionary&quot;-function. Therefore &quot;mapping&quot; 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 &quot;sortoneblock&quot; after the first processed line. Secondly, as you return nothing, you don&#39;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(&quot;infilename&quot;).<u></u>readlines()<br>
</blockquote>
<br></div>
Again, &quot;infilename&quot; 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]=&quot;.itp&quot;<br>
         return text<br>
     if os.path.splitext(infilename)[<u></u>1]=&quot;.pdb&quot;<br>
         return text[LINESTOSKIP:]<br>
<br>
<br>
if __name__==&quot;__main__&quot;:<br>
     sortfiles()<br>
</blockquote>
<br></div>
There is no &quot;sortfiles()&quot; in your script. The function you probably mean is called &quot;sortfile()&quot;<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>