On Wed, Oct 12, 2011 at 3:29 PM, Andreas Perstinger < andreas.perstin...@gmx.net> wrote:
> On 2011-10-12 05:31, lina wrote: > >> I tried to write one (not working one) as below, so many problems here. >> > > Just some quick remarks: Thanks, Now the newly-improved one as following: but still the "sort" parts did not work. #!/usr/bin/python3 import os.path LINESTOSKIP=0 CHAINID="CDEFGHI" INFILENAME="pdbone.pdb" DICTIONARYFILE="itpone.itp" mapping={} valuefromdict={} def sortfile(): for chainid in CHAINID: sortoneblock(chainid) 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 sortoneblock(cID): text=fetchonefiledata(INFILENAME) 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(): valuefromdict[cID]=mapping[blocks[2]] print(valuefromdict) 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() The result is: $ python3 map-to-itp.py {'O4': '2', 'C19': '3', 'C21': '1'} {'C': '3'} {'C': '2'} {'C': '1'} for print(mapping) part, {'O4': '2', 'C19': '3', 'C21': '1'} the value doesn't keep the 1, 2, 3 order any more. Thanks for further suggestions. The relevant files I put it here: https://docs.google.com/leaf?id=0B93SVRfpVVg3NzkyOGU2ZTUtZTFjNC00ZjE4LThhNmQtOWY1YWFkOWI0NWEw&hl=en_GB https://docs.google.com/leaf?id=0B93SVRfpVVg3YTEwZjhiOTItN2I2Yi00NTEyLTljODAtYTc2ODI4Njk1YzZl&hl=en_GB https://docs.google.com/leaf?id=0B93SVRfpVVg3M2Y1MWZiMmEtOTE2Mi00M2VjLTljNjAtYWNlMjhiNzEyODY1&hl=en_GB > > #!/usr/bin/python3 >> >> import os.path >> >> LINESTOSKIP=0 >> CHAINID="CDEFGHI" >> INFILENAME="pdbone.pdb" >> DICTIONARYFILE="itpone.itp" >> mapping={} >> valuefromdict={} >> >> def sortfile(): >> for chainid in CHAINID: >> sortoneblock(chainid) >> >> >> def generatedictionary(**dictfilename): >> > > You define the function with the parameter "dictfilename" but you'll never > use it. > > > text=fetchonefiledata(**DICTIONARYFILE) >> for line in text: >> parts=line.strip().split() >> if len(parts)==8: >> mapping[parts[4]]=parts[0] >> print(mapping) >> > > The if-branch is probably wrongly indented (should be inside the for-loop). > > > def sortoneblock(cID) >> text=fetchonefiledata(**INFILENAME) >> for line in text: >> blocks=line.strip().split() >> if len(blocks)== 11 and blocks[3] == "CUR" and blocks[4] == >> "cID": >> > > > "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". > > > valuefromdict[blocks[2]]=**mapping[block[2]] >> > > 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. > > return >> > > 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. > > > >> >> def fetchonefiledata(infilename): >> text=open("infilename").**readlines() >> > > Again, "infilename" is a variable, so no need for the quotes. > > > if os.path.splitext(infilename)[**1]=".itp" >> return text >> if os.path.splitext(infilename)[**1]=".pdb" >> return text[LINESTOSKIP:] >> >> >> if __name__=="__main__": >> sortfiles() >> > > There is no "sortfiles()" in your script. The function you probably mean is > called "sortfile()" > > Bye, Andreas > > ______________________________**_________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor> >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor