[Tutor] map one file and print it out following the sequence

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Sep 27 23:18:16 CEST 2011


>I have file 1: cur.itp
>
>     1       CH3     1  CUR      C1     1    0.066  15.0350   
>     2        OA     1  CUR      O1     1   -0.183  15.9994   
>     3         C     1  CUR      C2     1    0.126  12.0110   

>and file 2: procesed.pdb
>ATOM      1  H52 CUR     1      33.502  30.958  -9.831 -0.71 -0.23           H  
>ATOM      2  H32 CUR     1      34.440  28.421  -3.916  0.00  0.09           H 

>Now I wish the file 2 filed 3 output as the order of the file 1 field 2,
>namely will be,

>ATOM     10  C21 CUR     1      30.599  28.677 -10.410 -0.06 -0.05           C  
>ATOM     11  O4  CUR     1      30.948  29.625  -9.382 -0.04  0.04           O 

>mapping={}
>for line in open("cur.itp").readlines():

I would also suggest changing the line above to the following lines below. 
with open("cur.itp") as f:
    for line in f.readlines():
        # do task here.

I would do this as it will close the file automatically (starting with Python 2.6?). Not a big deal in this sample program, but I think this is considered a good practice (I am sure people will correct me if I am wrong).

>    parts=line.strip().split()
>    if len(parts)==8:
>           mapping[parts[4]]=parts[0]

This will create a dictionary of where the key is C2 or O1 or C1 and the value is the line number (or what I assume to be the line number). 

>origs=open("processedpdb").read().split()
>print " ".join([mapping[orig] for orig in origs])
>
> the last sentence I even don't know what I am doing. sorry.

I can help with that.
" " is a string and the join function takes an iterable (like an array or set) and then creates one string with all the elements of the iterable in it but separated by a space.  You can use any string for the separator even multi-character ones. 
>>> print " ".join( [ 'blah' , 'hum', 'bug' , 'see' ] )
blah hum bug see
>>> print ",".join( [ 'blah' , 'hum', 'bug' , 'see' ] )
blah,hum,bug,see
>>> print ", ".join( [ 'blah' , 'hum', 'bug' , 'see' ] )
blah, hum, bug, see
>>> print "a_-_a".join( [ 'blah' , 'hum', 'bug' , 'see' ] )
blaha_-_ahuma_-_abuga_-_asee


In your situation this will probably print nothing (actually it should raise a KeyError).

>origs=open("processedpdb").read().split()
>print " ".join([mapping[orig] for orig in origs])

The reason I do not think this will print anything is because you are searching for the incorrect information in the mapping dictionary. First it will read the entire file and then split the file by lines. So origs will be a list of lines.

[mapping[orig] for orig in origs]
If done correctly this will generate a list with the results of mapping[orig]. The problem is that you are looking for a key that is "ATOM      1  H52 CUR     1      33.502  30.958  -9.831 -0.71 -0.23           H" but all the keys are in the format "C1" or "C2" or "O1" as mentioned before. Accessing the dictionary in this manner will raise a key error when trying to access a value for which there is no key.
>>> d = {}
>>> d['blayh']

KeyError: 'blayh'


> namely rearrange it.

I am not sure what you are trying to do. It would help a lot if you could explain a bit more detail about what you are trying to accomplish. From what I see your output looks to be the same as your file2. Without knowing exactly what you are trying to achieve I cannot really help to fix this, except for pointing out some of the mistakes.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list