Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new user needs help!

0 views
Skip to first unread message

drjekil

unread,
Apr 8, 2008, 3:54:47 PM4/8/08
to pytho...@python.org

I am totally new in biopython and its my first program.so may be i am asking
stupid question.
I am working with a text filelooks like this:
#NAME AA TOPO ACCESS DSSP STRIDE Z-COORD
1lghB A i 79.8 H H -24.58
1lghB V i 79.6 H H -22.06
1lghB H i 71.9 H H -19.94
i need to compare those lines which has a value between 10 to 22 and
presents in the following way
True/false A C D E F G H I K L M N P Q R S T V X Y W(here alfabets
represents amino acids)
1 1:1 2:0 3:0 and so on for rest of the amino acids.
IF PRESENT IN THAT RANGE
IF not PRESENT IN THAT RANGE then
-1 1:0 2:0 so on,it should be 1 instead of 0 when it will find corresponding
amino acid for that line.say here,for 2nd line it will be 16:1.
true will represent 1,false -1.
i have to cheek all the lins in the file and print it.
u have to tell simply otherwise i cant understand even,so stupid am i!
I will be really greatful!Thanks in advance
--
View this message in context: http://www.nabble.com/new--user-needs-help%21-tp16571784p16571784.html
Sent from the Python - python-list mailing list archive at Nabble.com.

drjekil

unread,
Apr 8, 2008, 3:55:33 PM4/8/08
to pytho...@python.org

I am totally new in biopython and its my first program.so may be i am asking
stupid question.
I am working with a text filelooks like this:
#NAME AA TOPO ACCESS DSSP STRIDE Z-COORD
1lghB A i 79.8 H H -24.58
1lghB V i 79.6 H H -22.06
1lghB H i 71.9 H H -19.94
i need to compare those lines which has a Z-COORED value between 10 to 22

and presents in the following way
True/false A C D E F G H I K L M N P Q R S T V X Y W(here alfabets
represents amino acids)
1 1:1 2:0 3:0 and so on for rest of the amino acids.
IF PRESENT IN THAT RANGE
IF not PRESENT IN THAT RANGE then
-1 1:0 2:0 so on,it should be 1 instead of 0 when it will find corresponding
amino acid for that line.say here,for 2nd line it will be 16:1.
true will represent 1,false -1.
i have to cheek all the lins in the file and print it.
u have to tell simply otherwise i cant understand even,so stupid am i!
I will be really greatful!Thanks in advance
--
View this message in context: http://www.nabble.com/new--user-needs-help%21-tp16571823p16571823.html

Steve Holden

unread,
Apr 8, 2008, 4:38:06 PM4/8/08
to pytho...@python.org
drjekil wrote:
> I am totally new in biopython and its my first program.so may be i am asking
> stupid question.

New? Most questions are sensible.

Let's suppose that the four lines you give below are stored in a text
file called "/tmp/data.txt".

> I am working with a text filelooks like this:
> #NAME AA TOPO ACCESS DSSP STRIDE Z-COORD
> 1lghB A i 79.8 H H -24.58
> 1lghB V i 79.6 H H -22.06
> 1lghB H i 71.9 H H -19.94

f = open("/tmp/data.txt", 'w')

will open that file.

You can throw the first line away with

headings = f.next()

Then you can loop over the rest with

for name, aa, topo, access, dssp, stride, z in file:
#
# Then process each line here
#
if 10.0 <= z <= 22.0:
#
# select on other criteria here
#

> i need to compare those lines which has a Z-COORED value between 10 to 22
> and presents in the following way
> True/false A C D E F G H I K L M N P Q R S T V X Y W(here alfabets
> represents amino acids)
> 1 1:1 2:0 3:0 and so on for rest of the amino acids.
> IF PRESENT IN THAT RANGE
> IF not PRESENT IN THAT RANGE then
> -1 1:0 2:0 so on,it should be 1 instead of 0 when it will find corresponding
> amino acid for that line.say here,for 2nd line it will be 16:1.
> true will represent 1,false -1.

I am afraid I am having trouble understanding that last bit. Perhaps you
could find some other way to say the same thing? Sometimes my
understanding is not too good.

> i have to cheek all the lins in the file and print it.
> u have to tell simply otherwise i cant understand even,so stupid am i!

"Poor at English" != "Stupid"

> I will be really greatful!Thanks in advance

That's the first step. Now, what's next?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Mike Driscoll

unread,
Apr 8, 2008, 4:41:35 PM4/8/08
to

To read the file, do something like this:

<code>

f = open('path/to/filename')
for line in f.readlines():
# do something
print line

</code>

See http://docs.python.org/lib/bltin-file-objects.html for more
information.

You'll want to look at using "if" statements to do the various
conditions and you'll probably need to use string slicing/splitting to
get the correct part of the line to do the comparison.

So, something like

<code>

f = open('path/to/filename')
for line in f.readlines():
parts = line.split()
zcoord = parts[-1]
if zcoord > 10 and zcoord < 22:
# do something
pass
print line

</code>

Resources:

http://docs.python.org/ref/if.html
http://www.ibiblio.org/g2swap/byteofpython/read/if-statement.html
http://docs.python.org/lib/string-methods.html
http://www.diveintopython.org/native_data_types/joining_lists.html

Mike

Mike Driscoll

unread,
Apr 8, 2008, 4:44:14 PM4/8/08
to
On Apr 8, 3:38 pm, Steve Holden <st...@holdenweb.com> wrote:
> drjekil wrote:
> > I am totally new in biopython and its my first program.so may be i am asking
> > stupid question.
>
> New? Most questions are sensible.
>
> Let's suppose that the four lines you give below are stored in a text
> file called "/tmp/data.txt".
>
> > I am working with a text filelooks like this:
> > #NAME AA TOPO ACCESS DSSP STRIDE Z-COORD
> > 1lghB A i 79.8 H H -24.58
> > 1lghB V i 79.6 H H -22.06
> > 1lghB H i 71.9 H H -19.94
>
> f = open("/tmp/data.txt", 'w')
>
> will open that file.


Don't do that! You will overwrite the original!


>
> You can throw the first line away with
>
> headings = f.next()


I forgot to throw away the first line in my code though...so mine has
errors too.

<snip>

>
> regards
> Steve
> --
> Steve Holden +1 571 484 6266 +1 800 494 3119
> Holden Web LLC http://www.holdenweb.com/

Mike

Tim Chase

unread,
Apr 8, 2008, 4:49:59 PM4/8/08
to Steve Holden, pytho...@python.org
> f = open("/tmp/data.txt", 'w')
>
> will open that file.
>
> You can throw the first line away with
>
> headings = f.next()
>
> Then you can loop over the rest with
>
> for name, aa, topo, access, dssp, stride, z in file:
> #
> # Then process each line here


Small caveat here...Steve changed file-variables on you here, and
assumed a tuple-unpacking that won't work. You'll want something
like

for line in f:
(name, aa, topo, access, dssp, stride, z) = (
line.rstrip('\n').split('\t')
# process the line here

I don't know how your fields are delimited, whether by spaces or
tabs. In the above code, I split by tabs, but you can just use
.split() if it should be split on any white-space. It's a bit
more complex if you need to split by column-offsets.

-tkc


Steve Holden

unread,
Apr 8, 2008, 5:33:45 PM4/8/08
to Tim Chase, pytho...@python.org
Hmm, perhaps I shoudl wait until I get my brain fixed before I post
again! Sorry, Tim, I got distracted and shouldn't have tried to dash off
the post before coping with the distraction. Just a minor flood in the
basement ...

Steve Holden

unread,
Apr 8, 2008, 8:57:47 PM4/8/08
to drje...@gmail.com, pytho...@python.org
drje...@gmail.com wrote:
> thanks!

Please keep all replies on the list: somebody else may also wish to help
(and they will also pick up mistakes I make ;-)

> I am working with a text filelooks like this:
> #NAME AA TOPO ACCESS DSSP STRIDE Z-COORD
> 1lghB A i 79.8 H H -24.58
> 1lghB V i 79.6 H H -22.06
> 1lghB H i 71.9 H H -19.94

> i need to compare those lines which has a value between 10 to 22 and presents in the following way


> True/false A C D E F G H I K L M N P Q R S T V X Y W(here alfabets represents amino acids)
> 1 1:1 2:0 3:0 and so on for rest of the amino acids.
>
>

> I have given 2 examples below to make it clear a bit:
>
> ex.1:


> #NAME AA TOPO ACCESS DSSP STRIDE Z-COORD

> 1lghB A i 79.8 H H -24.58 #for that line amino acid is A and z-COORED value is more than 22,so output should be look like


>
> True/false A C D E F G H I K L M N P Q R S T V X Y W(here alfabets represents amino acids)

> -1 1:1 2:0 3:o 4:0 so on upto 20 bcz there r 20 amino acids.and A presents in the 1st position.every line represents one amino acid with value,so output will show in which position is it(here A is in 1st position thats why its value 1:1 and all the other position o like 2:0,3:0,4:0 and if its Z-COORED value between 10-22 then true false value 1,otherwise -1.
>
> another ex:
>
> 1lghB H i 71.9 H H -19.94 # for that line amino acid is H and it has value between 10-22.so output should looks like:
>
> True/false A C D E F G H I K L M N P Q R S T V X Y W(here alfabets represents 20 amino acids)
>
> 1 1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0,every position is zero up to 20 bcz H presents in the 7th position.so it will be 1.
>
> so for every line, output will in 21 coulum,1st coulum for true false value,others r for 20 amino acids.true false value will be either 1 or -1,and within other 20 coulum one value will be n:1,others will be n:0.n=0,1,2,3..20.
> its a bit tricky.
> thank u so much for ur help
> waiting for ur opinion.
>
So, you are using -1 for true and 1 for false? Can there be multiple
amino acids on each line?

Would this be a possible input:

1lghB AHG i 87.3 Q Q -23.45

If so, would this be the required output?

-1 1:1 2:0 3:0 4:0 5:0 6:0 7:1 8:1 9:0 10:0 ... 19:0 20:0

There is no point trying to write more code until we understand the
requirements properly.

drjekil

unread,
Apr 8, 2008, 10:17:57 PM4/8/08
to pytho...@python.org

u got it!
thats the thing i am trying to explain by my bad english!
thanks for the help.
--
View this message in context: http://www.nabble.com/new--user-needs-help%21-tp16571823p16578029.html
Message has been deleted

drjekil

unread,
Apr 9, 2008, 5:02:01 PM4/9/08
to pytho...@python.org

I have done something so far about that problem,but its not the good way to
do it

need ur comments about that....


from string import *;
import sys

myfile = open("/afs/pdc.kth.se/home/d/debnath/membrane/1a91A.txt")
a = myfile.readlines()
data = myfile.readlines()
for line in myfile.readlines():
fields = line.split('\t')

items=fields.strip()
list1.append(items[1])

for i in aminoacid:


if 10.0 <= z <= 22.0:

matches.append([1,i])
#here i am getting comment! bash-3.1$ python bb.py
File "bb.py", line 16
matches.append([1,i])
^
IndentationError: expected an indented block

else:
matches.append([-1,i])

print "#T/F A C D E F G H I K L M N P Q R S T V W X Y
Z"

for a in range(0,len(matches),1):

if matches[a][0]==1:

if matches[a][1]=='A':
print "1 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0
14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
elif matches[a][1]=='C':
print "1 1:0 2:1 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0
14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
elif matches[a][1]=='D':

print " 1 1:0 2:0 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
""" if matches[a][1]=='E' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='F' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:1 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='G' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='H' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='I' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:1 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='K' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:1 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='L' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:1 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='M' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:1 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='N' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:1
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='P' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:1 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='Q' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:1 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='R' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='S' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:1 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='T' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:1 18:0 19:0 20:0 21:0"
if matches[a][1]=='V' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:1 19:0 20:0 21:0"
if matches[a][1]=='X' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:1 20:0 21:0"
if matches[a][1]=='Y' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:1 21:0"
if matches[a][1]=='Z' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:1"

"""
else:
if matches[a][1]=='A':
print "-1 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0
14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
elif matches[a][1]=='C':
print "-1 1:0 2:1 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
elif matches[a][1]=='D':

print " 1 1:0 2:0 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
""" if matches[a][1]=='E' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='F' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:1 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='G' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='H' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='I' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:1 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='K' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:1 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='L' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:1 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='M' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:1 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='N' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:1
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='P' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:1 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='Q' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:1 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='R' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='S' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:1 17:0 18:0 19:0 20:0 21:0"
if matches[a][1]=='T' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:1 18:0 19:0 20:0 21:0"
if matches[a][1]=='V' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:1 19:0 20:0 21:0"
if matches[a][1]=='X' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:1 20:0 21:0"
if matches[a][1]=='Y' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:1 21:0"
if matches[a][1]=='Z' and matches[a][0]==1:

print " 1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:1"
waiting for ur opinion.
thanks

--
View this message in context: http://www.nabble.com/new--user-needs-help%21-tp16571823p16596608.html

Steve Holden

unread,
Apr 9, 2008, 10:08:01 PM4/9/08
to pytho...@python.org
drjekil wrote:
> I have done something so far about that problem,but its not the good way to
> do it
>
> need ur comments about that....
>
Well, at least you can see that your approach is not satisfactory, so
that means you have some sense of what's good and bad programming/

>
> from string import *;
> import sys
>
> myfile = open("/afs/pdc.kth.se/home/d/debnath/membrane/1a91A.txt")
> a = myfile.readlines()
> data = myfile.readlines()

The first readlines() exhausts the file.

> for line in myfile.readlines():

You should omit the assignments to a and to data, as they are using up
the contents of the file that you want this statrement to iterate over.

> fields = line.split('\t')
>
> items=fields.strip()
> list1.append(items[1])
>

Note that what you are doing here is building a list of the lines in the
file to iterate over later, when you could just iterate over the lines
in the file.

>
> for i in aminoacid:
> if 10.0 <= z <= 22.0:
> matches.append([1,i])
> #here i am getting comment! bash-3.1$ python bb.py
> File "bb.py", line 16
> matches.append([1,i])
> ^
> IndentationError: expected an indented block
>
> else:
> matches.append([-1,i])
>

The error message is literally correct. The statement(s) controlled by
an if must be indented relative to the if statement itself - remember,
Python uses indentation to indicate block structure.

Also, it seems a little unnecessary to build a list of all the matching
lines and then process them, when you could process them one at a time
as you read them from the file!

> print "#T/F A C D E F G H I K L M N P Q R S T V W X Y
> Z"
>

That looks fine!

I think you're right about the logic being unnecessarily complex :-)

The following is untested, but it will give you some idea of how you
might proceed.

print "#T/F A C D E F G H I K L M N P Q R S T V W X
Y Z"

for line in myfile.readlines():
fields = line.strip().split('\t')
if 10.0 <= float(fields[6]) <= 22.0: # Sets T/F flag
flag = 1
else:
flag = -1
pos = "ACDEFGHIKLMNPQRSTVWXYZ".index(fields[1])
m = []
for i in range(20):
if pos == i:
m.append("%d:1")
else:
m.append("%d:0")
print flag, " ".join(m)

This is not quite as compact as it could be. What you could do is try
running it with print statements inserted to show you what value
variables are taking in statements you don't understand.

Hope this helps!

Chris

unread,
Apr 10, 2008, 4:07:44 AM4/10/08
to
On Apr 9, 11:02 pm, drjekil <drjeki...@gmail.com> wrote:
> I have done something so far about that problem,but its not the good way to
> do it
>
> need ur comments about that....
>
> from string import  *;
> import sys
>
> myfile = open("/afs/pdc.kth.se/home/d/debnath/membrane/1a91A.txt")
> a = myfile.readlines()
> data = myfile.readlines()
> for line in myfile.readlines():
>        fields = line.split('\t')
>
>        items=fields.strip()
>        list1.append(items[1])
>
> for i in aminoacid:
>     if  10.0 <= z <= 22.0:
>     matches.append([1,i])
> #here i am getting comment!  bash-3.1$ python bb.py
>   File "bb.py", line 16
>     matches.append([1,i])
>           ^
> IndentationError: expected an indented block
>
>     else:
>     matches.append([-1,i])
>

You are getting the indentation error because you need to tab the
lines in after your 'if' and 'else' statements. After that is fixed
you should get an Attribute Error on the 'items=fields.strip()' as a
list doesn't have access to strip.

import string
myfile = open("/afs/pdc.kth.se/home/d/debnath/membrane/1a91A.txt",
'rb')

AMINO_ACIDS = ['A','B','C',......'X','Y','Z']

for line in myfile: # Files are iterable themselves.

try:
fields = map(string.strip, line.split('\t')) # This will
strip every column for you
name, aa, topo, access, tssp, stride, z = fields
except IndexError: # In case you don't have enough elements
print 'Not enough elements on the line, moving along'

if 10 <= z <= 22: # You only wanted between those Z-Coords ?
if aa in AMINO_ACIDS:
# Now actually process the data you want

Masses of print statements might work for debugging but you can get a
better structure through something else. If you have a specific
message for those you could always build a dictionary containing the
messages and access them like....

msg_dict = {'A':'Message for A', 'B':'Message for
B',......,'Z':'Message for Z'}

for part1, part2 in matches: # You have a 2 element structure,
(part1, part2) inside the list and lists are iterable
if part2 == 1: # Test the value
try:
print msg_dict[part1]
except KeyError:
print 'Amino Acid listed not on record' # or you can just
simply pass over and not log anything

Hope that helps.
Chris

Chris

unread,
Apr 10, 2008, 4:17:29 AM4/10/08
to
> msg_dict = {'A':'Message for A', 'B':'Message for...
>
> read more »

whoops, forgot the continue statement after the index error

for line in myfile: # Files are iterable themselves.

try:
fields = map(string.strip, line.split('\t')) # This will
strip every column for you
name, aa, topo, access, tssp, stride, z = fields
except IndexError: # In case you don't have enough elements
print 'Not enough elements on the line, moving along'

continue # This will take your code to the top of the loop
and start the next iteration instead of continuing through the code

0 new messages