473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

new python user needs help!

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.
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 Z-CORRED:
if 10.0 <= z <= 22.0:
matches.append([1,i])



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: #here i have getting comments
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
Apr 10 '08 #1
3 1618
bvdet
2,851 Expert Mod 2GB
Please use code tags. The code is difficult to follow without them. You can accomplish your needs with much less code. Reading the file and splitting the data if fairly straightforward, and so is formatting the output if you use some of the tools Python has available. The following reads the data into two lists - header and acid data:
Expand|Select|Wrap|Line Numbers
  1. acids = 'ACDEFGHIKLMNPQRSTVXYW'
  2. num_range = [10,22]
  3. fn = r'H:\TEMP\temsys\amino_acids_data.txt'
  4. f = open(fn)
  5.  
  6. # save the header text list using a list comprehension
  7. headerList = [item.strip() for item in f.readline()[1:].split()]
  8. print headerList
  9.  
  10. # save stripped lines in a list using a list comprehension
  11. lineList = [item.strip() for item in f]
  12. f.close()
The next section formats the results for printing.
Expand|Select|Wrap|Line Numbers
  1. # iterate on list of lines without the list comprehensions
  2. for item in lineList:
  3.     itemList = item.split()
  4.     acid = itemList[1]
  5.     z_coord = float(itemList[-1])
  6.     print 'True/False A C D E F G H I K L M N P Q R S T V X Y W'
  7.     if num_range[0]<=abs(z_coord)<=num_range[1]:
  8.         print 1,
  9.     else:
  10.         print -1,
  11.     outputList = []
  12.     for letter in acids:
  13.         if acid == letter:
  14.             outputList.append('1:%d' % (acids.index(letter)+1))
  15.         else:
  16.             outputList.append('0:%d' % (acids.index(letter)+1))
  17.     print ' '.join(outputList)
The following does the same thing, but uses list comprehensions:
Expand|Select|Wrap|Line Numbers
  1. # iterate on list of lines
  2. for item in lineList:
  3.     itemList = item.split()
  4.     acid = itemList[1]
  5.     z_coord = float(itemList[-1])
  6.     print 'True/False A C D E F G H I K L M N P Q R S T V X Y W'
  7.     print '%d' % ([-1, 1][(num_range[0]<=abs(z_coord)<=num_range[1]) or 0]),
  8.     print ' '.join(['%d:%d' % ([0, 1][acid==letter or 0], acids.index(letter)+1) for letter in acids])
Sample output:
Expand|Select|Wrap|Line Numbers
  1. >>> True/False A C D E F G H I K L M N P Q R S T V X Y W
  2. -1 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
  3. True/False A C D E F G H I K L M N P Q R S T V X Y W
  4. -1 0: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
  5. True/False A C D E F G H I K L M N P Q R S T V X Y W
  6. 1 0: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
It looks like there are 21 acids.
Apr 11 '08 #2
Thanks for ur help!
sorry to answer u late.
still i have some problem...

python ffff.py
[]
True/False A C D E F G H I K L M N P Q R S T V X Y W
Traceback (most recent call last):
File "ffff.py", line 50, in <module>
if num_range[0]<=abs(zcoord)<=num_range[1]:
TypeError: bad operand type for abs(): 'list'
bash-3.1$

some times got comment like "zcoord" is not defined.If i creat a list named zcoord then got only one line output.and another thing is aminoacid position need to be printed first like 1:0,2:0,3:1,4:0 and so on.

waiting for ur help!
Thanks again!
Apr 23 '08 #3
jlm699
314 100+
TypeError: bad operand type for abs(): 'list'
This line is telling you that the abs() command is being passed a list object instead of a single element. Right before the line insert a print command to check what you're passing to this function and try and see if you can't realize your mistake then.
Apr 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Gary | last post by:
Hi- I've been searching the web for a while and I've been unable to find a way to access c data objects in python without using SWIG. I can do methods just fine but I can't access variables. ...
1
by: Henko Gouws (H) | last post by:
Hallo I am looking for good examples on how to call a python script from C, C++ and pass parameters (list of strings) to the python script. The aim is to ultimately allow the end-user to modify...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
12
by: Tony Belding | last post by:
I'm interested in using an off-the-shelf interpreted language as a user-accessible scripting language for a MUCK. I'm just not sure if I can find one that does everything I need. The MUCK must be...
32
by: siggi | last post by:
@Ben Sizer Hi Ben, in January I received your message re Pygame and Python 2.5: As a Python (and programming ) newbie allow me a - certainly naive - question:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.