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

2011-10-13 Thread lina
snip

 I think your final version of sortfile() might look something like:

 def sortfile(infilename=**INFILENAME, outfilename=OUTFILENAME):
infile = open(infilename, r)
intext = infile.readlines()
outfile = open(OUTFILENAME, w)
for chainid in CHAINID:
print(chain id = ,chainid)
 sortoneblock(chainid, intext, outfile)
infile.close()
outfile.close()


$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
C
Traceback (most recent call last):
  File map-to-itp.py, line 55, in module
sortfile()
  File map-to-itp.py, line 17, in sortfile
sortoneblock(chainid,intext,OUTFILENAME)
  File map-to-itp.py, line 29, in sortoneblock
f.write(line[1].strip() for line in temp)
TypeError: must be str, not generator


I don't know how to fix the writing issue.

can I write the different chainID one into the same OUTFILE?

Thanks, I attached the code I used below:

 #!/usr/bin/python3

import os.path

LINESTOSKIP=0
CHAINID=CDEFGHI
INFILENAME=pdbone.pdb
OUTFILENAME=sortedone.pdb
DICTIONARYFILE=itpone.itp
mapping={}
valuefromdict={}

def sortfile():
intext=fetchonefiledata(INFILENAME)
for chainid in CHAINID:
print(chainid)
sortoneblock(chainid,intext,OUTFILENAME)



def sortoneblock(cID,TEXT,OUTFILE):
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()
with open(OUTFILE,w) as f:
f.write(line[1].strip() for line in temp)




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:]
infilename.close()


if __name__==__main__:
generatedictionary(DICTIONARYFILE)
sortfile()





 --

 DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-13 Thread Andreas Perstinger

On 2011-10-13 15:09, lina wrote:

$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
C
Traceback (most recent call last):
   File map-to-itp.py, line 55, inmodule
 sortfile()
   File map-to-itp.py, line 17, in sortfile
 sortoneblock(chainid,intext,OUTFILENAME)
   File map-to-itp.py, line 29, in sortoneblock
 f.write(line[1].strip() for line in temp)
TypeError: must be str, not generator

I don't know how to fix the writing issue.


You should start to learn how to read the error messages :-).
write just writes strings into files (must be str) but you are 
calling it with an generator. I guess you wanted to use a list 
comprehension, but this is surrounded by square brackets:


f.write([line[1].strip() for line in temp])

But list comprehensions create lists so you would have to convert the 
list to a string:


f.write(str([line[1].strip() for line in temp]))

But this would convert the whole list into one single string which you 
probably don't want (try it for yourself).


IMHO it would be easier to iterate through temp and write each line 
separately:


for line in temp:
f.write(line[1])

line[1] is already a string including the newline (\n), so str() and 
strip() aren't necessary (remeber: write writes without automatic 
newlines).


Is that what you want?


can I write the different chainID one into the same OUTFILE?


I'm not sure what you mean. Do you want something like:
C



D
xxx
xxx
...
(xxx meaning the different lines)?

Then you just have to write the corresponding chainID before the for-loop:

f.write(cID + \n)

And you have to open the file in mode a (to append to an existing 
file) because otherwise you will overwrite the file with every new 
chainID you are processing:


with open(OUTFILE, a) as f:


def sortoneblock(cID,TEXT,OUTFILE):

   
Just a stylistic remark: It's better to use just lowercase for variable 
names and parameters. Uppercase names are usually just used for 
constants. Thus it's easier to distinguish them while reading the code.


Bye, Andreas
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-13 Thread Dave Angel

On 10/13/2011 09:09 AM, lina wrote:

snip


I think your final version of sortfile() might look something like:

def sortfile(infilename=**INFILENAME, outfilename=OUTFILENAME):
infile = open(infilename, r)
intext = infile.readlines()
outfile = open(OUTFILENAME, w)
for chainid in CHAINID:
print(chain id = ,chainid)
 sortoneblock(chainid, intext, outfile)
infile.close()
outfile.close()



$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
C
Traceback (most recent call last):
   File map-to-itp.py, line 55, inmodule
 sortfile()
   File map-to-itp.py, line 17, in sortfile
 sortoneblock(chainid,intext,OUTFILENAME)
   File map-to-itp.py, line 29, in sortoneblock
 f.write(line[1].strip() for line in temp)
TypeError: must be str, not generator




When you see an error message that describes a generator, it means you 
usually have a for-expression used as a value.


At your stage of learning you probably be ignoring generators and list 
comprehensions, and just write simple for loops.  So you should replace

the f.write with a loop.


for item in temp:
f.write(something + \n)

One advantage is that you can easily stuff print() functions into the 
loop, to debug what's really happening.  After you're sure it's right, 
it might be appropriate to use either a generator or a list comprehension.



I don't know how to fix the writing issue.

can I write the different chainID one into the same OUTFILE?

Thanks, I attached the code I used below:

  #!/usr/bin/python3

import os.path

LINESTOSKIP=0
CHAINID=CDEFGHI
INFILENAME=pdbone.pdb
OUTFILENAME=sortedone.pdb
DICTIONARYFILE=itpone.itp
mapping={}
valuefromdict={}

def sortfile():
 intext=fetchonefiledata(INFILENAME)
 for chainid in CHAINID:
 print(chainid)
 sortoneblock(chainid,intext,OUTFILENAME)

One way to get all the output into one file is to create the file in 
sortfile(), and pass the file object.  Look again at what I suggested 
for sortfile().  If you can open the file once, here, you won't have the 
overhead of constantly opening the same file that nobody closed, and 
you'll have the side benefit that the old contents of the file will be 
overwritten.


Andreas' suggestion of using append would make more sense if you wanted 
the output to accumulate over multiple runs of the program.  If you 
don't want the output file to be the history of all the runs, then 
you'll need to do one open(name, w), probably in sortfile(), and then 
you might as well pass the file object as I suggested.






def sortoneblock(cID,TEXT,OUTFILE):


If you followed my suggestions for sortfile(), then the last paramter to 
this function would be outfile., and you could use outfile.write().

As Andreas says, don't use uppercase for non-constants.


 temp = []


#this writes the cID to the output file, once per cID
outfile.write(cID + \n)


 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():


  if (len(blocks)== 11 and  blocks[3] == CUR
and blocks[4] == cID and blocks[2] in mapping ):

Having the .keys() in that test is redundant and slows execution down 
quite a bit.  in already knows how to look things up efficiently in a 
dictionary, so there's no use in converting to a slow list before doing 
the slow lookup.

Also, if you put parentheses around the whole if clause, you can span it
across multiple lines without doing anything special.



 temp.append((mapping[blocks[2]],line))
 temp.sort()
 with open(OUTFILE,w) as f:
 f.write(line[1].strip() for line in temp)

See comment above for splitting this write into a loop.  You also are 
going to have to decide what to write, as you have tuple containing both 
an index number and a string in each item of temp.  Probably you want to 
write the second item of the tuple. Combining these changes, you

would have
   for index, line in temp:
   outfile.write(line + \n)

Note that the following are equivalent:
   for item in temp:
index, line = item
outfile.write(line + \n)

   for item in temp:
outfile.write(item[1] + \n)

But I like the first form, since it makes it clear what's been stored in 
temp.  That sort of thing is important if you ever change it.




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:]
 infilename.close()


if __name__==__main__:
 generatedictionary(DICTIONARYFILE)
 

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

2011-10-12 Thread Andreas Perstinger

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:


#!/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


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

2011-10-12 Thread lina
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=0B93SVRfpVVg3NzkyOGU2ZTUtZTFjNC00ZjE4LThhNmQtOWY1YWFkOWI0NWEwhl=en_GB
https://docs.google.com/leaf?id=0B93SVRfpVVg3YTEwZjhiOTItN2I2Yi00NTEyLTljODAtYTc2ODI4Njk1YzZlhl=en_GB
https://docs.google.com/leaf?id=0B93SVRfpVVg3M2Y1MWZiMmEtOTE2Mi00M2VjLTljNjAtYWNlMjhiNzEyODY1hl=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/tutorhttp://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-12 Thread Andreas Perstinger

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):
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()


for line in temp: print(line[1])

Bye, Andreas
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-12 Thread lina
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#**
 dictionarieshttp://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
ATOM832  C21 CUR C  85  32.823  27.366   0.801  1.00
0.00
ATOM831  O4  CUR C  85  31.865  28.248   0.183  1.00
0.00
ATOM827  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/tutorhttp://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-12 Thread Dave Angel

On 10/12/2011 09:57 AM, lina wrote:


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.

In what sense do you mean stopped?  There are no records in pdbone.pdb 
that have column 4 equal to any of the remaining CID values, D, E, F, etc.


So they don't print anything.  You can see that 3 of them matched cID of C


for line in temp: print(line[1])

Thanks.

$ python3 map-to-itp.py
{'O4': '2', 'C19': '3', 'C21': '1'}
C
ATOM832  C21 CUR C  85  32.823  27.366   0.801  1.00
0.00
ATOM831  O4  CUR C  85  31.865  28.248   0.183  1.00
0.00
ATOM827  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.
Simple.  Move this line into sortfile (before the for loop), and pass 
text as an argument when it calls sortoneblock(cID, text)
Naturally, you'd then add the parameter to sortoneblock(cID, text).  
Once you try to create the output file, you'll also be adding the open() 
call for that into sortfile(), and passing its file object into 
sortoneblock(cID, text, outfile)

 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.



I think your final version of sortfile() might look something like:

def sortfile(infilename=INFILENAME, outfilename=OUTFILENAME):
infile = open(infilename, r)
intext = infile.readlines()
outfile = open(OUTFILENAME, w)
for chainid in CHAINID:
print(chain id = ,chainid)
 sortoneblock(chainid, intext, outfile)
infile.close()
outfile.close()


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-11 Thread lina
I tried to write one (not working one) as below, so many problems here.

#!/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:
valuefromdict[blocks[2]]=mapping[block[2]]
return


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__:
sortfiles()


The representative lines from itpone.itp is:
 1   CH3 1  CUR C21 10.200  15.0350
 2OA 1  CUR  O4 1   -0.400  15.9994
 3 C 1  CUR C19 10.200  12.0110

The representative lines from pdbone.pdb is:

ATOM827  C19 CUR C  85  31.891  29.624   0.280  1.00
0.00
...
ATOM831  O4  CUR C  85  31.865  28.248   0.183  1.00
0.00
ATOM832  C21 CUR C  85  32.823  27.366   0.801  1.00  0.00

The expected results of pdbone.pdb will be:

ATOM832  C21 CUR C  85  32.823  27.366   0.801  1.00  0.00
ATOM831  O4  CUR C  85  31.865  28.248   0.183  1.00  0.00
ATOM827  C19 CUR C  85  31.891  29.624   0.280  1.00  0.00

Thanks for telling me the existing problems.

Best regards,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-06 Thread Prasad, Ramit
I attached the two files.

Although I will help you, I will not do the work for you. If you are still 
having errors, please include your script, python version, and the problem (if 
you are getting an error include the full error).

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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-06 Thread lina
On Thu, Oct 6, 2011 at 10:48 PM, Prasad, Ramit ramit.pra...@jpmorgan.comwrote:

 I attached the two files.

 Although I will help you, I will not do the work for you. If you are still
 having errors, please include your script, python version, and the problem
 (if you are getting an error include the full error).


Thanks for your help.

Actually this one was solved by a clumsy bash script long time ago,

attached the clumsy script just to prove what I said is true. and at present
I want to push myself on the way of using python. so I can actually learn
something.

For this one, the python programming I will come back later, just put it
aside for a short while.

#for i in 3 4 5 6 7 8 9 10
#2
#do
#  sed '
 #/H01/ d
 #/H02/ d
 #/H03/ d
 #/H04/ d'   proAB_processed_$i.pdb  proAB_processed.pdb
#awk '{
#if($4 == CUR)
#a[i++] = $0
#}
#END{
#print a[6]
#print a[7]
#print a[8]
#print a[9]
#print a[5]
#print a[13]
#print a[33]
#print a[34]
#print a[12]
#print a[2]
#print a[11]
#print a[1]
#print a[10]
#print a[14]
#print a[15]
#print a[16]
#print a[17]
#print a[18]
#print a[19]
#print a[20]
#print a[21]
#print a[22]
#print a[23]
#print a[24]
#print a[4]
#print a[28]
#print a[0]
#print a[27]
#print a[3]
#print a[26]
#print a[31]
#print a[32]
#print a[25]
#print a[29]
#print a[30]
#}' proAB_processed.pdb  try.pdb




 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.
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-10-06 Thread Prasad, Ramit
attached the clumsy script just to prove what I said is true. and at present I 
want to push myself on the way of using python. so I can actually learn 
something.  

I think you were close to the solution. It seemed like all you needed to do was 
write the output to file correctly (which you are learning in one of your other 
threads)

For this one, the python programming I will come back later, just put it aside 
for a short while.
Just do not forget to come back to it. Coming back often gets forgotten ;)

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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-29 Thread Prasad, Ramit
From: tutor-bounces+ramit.prasad=jpmorgan@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of lina
Sent: Tuesday, September 27, 2011 10:35 PM
Cc: tutor
Subject: Re: [Tutor] map one file and print it out following the sequence

Hi,

Thanks for both of your reply.

File 1 is:

 3 C 1  CUR C19 10.200  12.0110   
 4   CR1 1  CUR C20 1   -0.060  12.0110   
 5HC 1  CUR H20 10.060   1.0080   

File 2 is:
ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00 
ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00  0.00
ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00 

I wish to get:

ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00
ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00  0.00
ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00   

The dictionary is C19 C20 H20 sequence read from file 1 field 5.
rearrange the file 2 field 3 following the sequence of C19, C20, H20.

Thanks.

===

This is something I wrote *really* quick and is untested. Hopefully someone on 
this list can spot any error I made.

mapping={}
with open(cur.itp) as f:
for line in f.readlines():
parts=line.strip().split()
if len(parts)==8:
   mapping[parts[4]]=parts[0]
with open(processedpdb) as f:
proccessed = [ line.split() for line in f.readlines() ]
processed.sort( key=lambda x: mapping.get( x[2], 0 ) ) # use 0 to put items 
without a mapping at the 
   # top of the file because 
they are probably an error
For line in array:
print ' '.join( line )




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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-29 Thread lina
On Thu, Sep 29, 2011 at 10:57 PM, Prasad, Ramit
ramit.pra...@jpmorgan.comwrote:

 From: tutor-bounces+ramit.prasad=jpmorgan@python.org [mailto:
 tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of lina
 Sent: Tuesday, September 27, 2011 10:35 PM
 Cc: tutor
 Subject: Re: [Tutor] map one file and print it out following the sequence

 Hi,

 Thanks for both of your reply.

 File 1 is:

 3 C 1  CUR C19 10.200  12.0110
 4   CR1 1  CUR C20 1   -0.060  12.0110
 5HC 1  CUR H20 10.060   1.0080

 File 2 is:
 ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00
 ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00  0.00
 ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00

 I wish to get:

 ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00
 ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00  0.00
 ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00

 The dictionary is C19 C20 H20 sequence read from file 1 field 5.
 rearrange the file 2 field 3 following the sequence of C19, C20, H20.

 Thanks.

 ===

 This is something I wrote *really* quick and is untested. Hopefully someone
 on this list can spot any error I made.

 mapping={}
 with open(cur.itp) as f:
for line in f.readlines():
 parts=line.strip().split()
if len(parts)==8:
   mapping[parts[4]]=parts[0]
 with open(processedpdb) as f:
proccessed = [ line.split() for line in f.readlines() ]
 processed.sort( key=lambda x: mapping.get( x[2], 0 ) ) # use 0 to put items
 without a mapping at the
   # top of the file because
 they are probably an error
 For line in array:
print ' '.join( line )


I checked,

::
proAB_processed.pdb
::
ATOM  1  H52 CUR 1  33.502  30.958  -9.831 -0.71 -0.23
H

the output added:

::
proAB_processed.pdb
::

without truly sorting,

but I do think you understand correctly.

Thanks,




 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.
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-29 Thread Prasad, Ramit
For line in array:
   print ' '.join( line )

Sorry this it should be:
for line in processed:
   print ' '.join( line )

This worked for me. 
 pprint.pprint( processed) # Note in the email I have changed the format but 
 not the data
[['ATOM',  '2',  'H20',  'CUR',  '1',  '30.338',  '28.778',  '-6.812',  '1.00', 
 '0.00'], 
 ['ATOM',  '4',  'C20',  'CUR',  '1',  '31.394',  '28.922',  '-7.039',  '1.00', 
 '0.00'],
 ['ATOM',  '5',  'C19',  'CUR',  '1',  '31.790',  '29.357',  '-8.323',  '1.00', 
 '0.00']]
 pprint.pprint( sorted( processed, key= lambda x: mapping.get( x[2], 0 ) ) ) 
# I use sorted instead of processed in case it did not sort correctly (that way 
I still have the original list)
[['ATOM',  '5',  'C19',  'CUR',  '1',  '31.790',  '29.357',  '-8.323',  '1.00', 
 '0.00'],
 ['ATOM',  '4',  'C20',  'CUR',  '1',  '31.394',  '28.922',  '-7.039',  '1.00', 
 '0.00'],
 ['ATOM',  '2',  'H20',  'CUR',  '1',  '30.338',  '28.778',  '-6.812',  '1.00', 
 '0.00']]

You should note, that I just used print I did not write to file. You will 
need to actually create/open a file, run through the list (e.g. processed) and 
write to the file, and then finally close it.

Ramit


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


From: tutor-bounces+ramit.prasad=jpmorgan@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of lina
Sent: Thursday, September 29, 2011 11:39 AM
To: tutor
Subject: Re: [Tutor] map one file and print it out following the sequence



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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-28 Thread Alan Gauld

On 28/09/11 04:34, lina wrote:


File 1 is:

  3 C 1  CUR C19 10.200  12.0110
  4   CR1 1  CUR C20 1   -0.060  12.0110
  5HC 1  CUR H20 10.060   1.0080

File 2 is:
ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00
ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00  0.00
ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00

I wish to get:

ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00
ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00  0.00
ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00

The dictionary is C19 C20 H20 sequence read from file 1 field 5.
rearrange the file 2 field 3 following the sequence of C19, C20, H20.


OK, so just to confirm: you want to sort file 2 based on the order of 
field 5 in file 1.


So read file2 into a dictionary keyed by field 3
Then read file 1 and output the dictionary entry associated with field 5

So in pseudo code:

d = dict()
for line in file1:
d[ line.split()[2] ] = line

for line in file1:
print d[ line.split()[4] ]

Is that what you want?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-27 Thread Alan Gauld

On 27/09/11 14:45, lina wrote:

Hi,

I have file 1: cur.itp

  1   CH3 1  CUR  C1 10.066  15.0350
  2OA 1  CUR  O1 1   -0.183  15.9994
  3 C 1  CUR  C2 10.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
ATOM  3  H22 CUR 1  31.110  22.839   1.886 -0.18
0.12   H



While it's good to show us sample data please keep it to the minimum 
needed to illustrate the point.




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


Sorry, I have no idea what you mean by that.
Can you explain again in more detail please?



hope someone can give me some advice,


We probably can once we understand what you are trying to do.
Or maybe its just me being slow...


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.


That makes two of us!

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-27 Thread Prasad, Ramit
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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2011-09-27 Thread lina
Hi,

Thanks for both of your reply.

File 1 is:

 3 C 1  CUR C19 10.200  12.0110
 4   CR1 1  CUR C20 1   -0.060  12.0110
 5HC 1  CUR H20 10.060   1.0080

File 2 is:
ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00

ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00
0.00
ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00  0.00

I wish to get:

ATOM  5  C19 CUR 1  31.790  29.357  -8.323  1.00
0.00
ATOM  4  C20 CUR 1  31.394  28.922  -7.039  1.00
0.00
ATOM  2  H20 CUR 1  30.338  28.778  -6.812  1.00  0.00


The dictionary is C19 C20 H20 sequence read from file 1 field 5.
rearrange the file 2 field 3 following the sequence of C19, C20, H20.

Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor