Hi,
I am self-teaching myself bioinformatics. In one of the books I am reading, I came across the following code written in Python language. My Python knowledge is only at the beginner level.
from operator import itemgetter
input_file = open("PDBhaemoglobinReport.csv")
output_file = open("PDBhaemoglobinSorted.csv", "w")
table = []
header = input_file.readline()
for line in input_file:
col = line.split(',')
col[3] = float(col[3][1:-1])
col[4] = int(col[4][1:-2])
table.append(col)
table_sorted = sorted(table, key = itemgetter(3,4))
output_file.write(header + '\n')
for row in table_sorted:
row = [str(x) for x in row]
output_file.write('\t'.join(row) + '\n')
output_file.close()
The following are the first three lines of the file from which the data is read.
PDB ID,Chain ID,Exp. Method,Resolution,Chain Length
"1A4F","A","X-RAY DIFFRACTION","2.00","141"
"1C7C","A","X-RAY DIFFRACTION","1.80","283"
I am completely confused with the following two lines
col[3] = float(col[3][1:-1])
col[4] = int(col[4][1:-2])
When I tried col[3] = float(col[3]) or col[4] = int(col[4]) the script throws an error. For example col[1:-1] corresponds to
['"A"', '"X-RAY DIFFRACTION"', '"2.00"']. this list doesn't have a third element, so I am not sure how float(col[3][1:-1]) works.
Thanks
python