On Wed, Oct 12, 2011 at 8:50 PM, Andreas Perstinger < andreas.perstin...@gmx.net> wrote:
> On 2011-10-12 10:27, lina wrote: > >> $ 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. >> > > That's fine, because "mapping" is a dictionary which has no order. From the > tutorial (http://docs.python.org/py3k/**tutorial/datastructures.html#** > dictionaries<http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries> > ): > "It is best to think of a dictionary as an unordered set of key: value > pairs, with the requirement that the keys are unique (within one > dictionary)." > > What you want (as far as I understand it) is sorting the lines in > "pdbone.pdb" based on the positions in file "itpone.itp". The connection > between both files is the column with the values "O4", "C19", "C21", ... (= > your keys). You've already succesfully built a dictionary in which you saved > the position for every key. > > For the sorting you could now build a list of tuples of all lines in > "pdbone.pdb" you want to sort where the first element in the tuple is the > position and the second the line itself. Then you can easily sort this > temporary list and write the new ordered lines back to the file: > > def sortoneblock(cID): > text = fetchonefiledata(INFILENAME) > temp = [] # create an empty temporary list > > 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)) # add a tuple to the > list which has the following format: (position from the dictionary, complete > line) > > # the following line just shows you, what we have done so far. You can > delete it without consequences. > > for line in temp: print(line) > > temp.sort() # this sorts the list based on the position > > # the following line prints the sorted list (just the original line > without the position elements). If you want to write the result back to the > file you have to exchange "print()" > 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. > > 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. 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. > > > 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