2

Python beginner here. I am iterating through a text file, column by column.

for line in Input:
    line = line.rstrip() 
    fields = line.split("\t")   
    for col in fields[5:]:

How would I keep track of which column I am in as I iterate through the columns? I want to print the current relevant column in the output so when I have my results I know which column the results refer to. It seems like something for which there is an obvious answer, but I don't know it!

Thanks in advance for the help. I find the helpfulness of people here overwhelming.

some input/output examples:

lines from input file :

 chr20   1032    .       A     1/1 1/1 1/1 0/1 0/0
 chr20   1326    .       T     1/1 1/1 1/1 0/1 0/0
 chr20   1388    .       C     0/0 1/1 ./. 0/1 0/0

Then I go through these lines looking at column 5 eg 1/1, 1/1, 0/0 and calculating some output statistics. My output file contains the statistics for every x number of rows calculated using column 5. I'd like my output file to also include the results from column 6, 7 etc and for the rows in the output file to tell me which column the data was calculated from. In essence to save me rerunning the script seperately for each column.

output example:

5   chr1    230344070   231345012   1000942 200720  66560   119841  7160    5.63775088385   94.3622491161
5   chr1    231345012   232345029   1000017 167920  55040   106160  3360    3.06793279766   96.9320672023
5   chr1    232345029   233345195   1000166 179280  64841   96079   9180    8.72134449311   91.2786555069

With the first column here referring the fact it was calculated based on the values in column 5

for index, col in enumerate(fields[5:]):

Best,

Rubal

1
  • Since you are looking at chromosomes, you might be interested by the package Biopython
    – Zenon
    Jun 3, 2012 at 14:36

2 Answers 2

4

You could try something like this

for i,col in enumerate(fields[5:], 5):
    ....

enumerate() will generate an index value for you, by default it starts with 0 unless a starting value is specified as 2nd parameter to enumerate() as shown above with 5.

Variable i will start with the value 5 and allow you to track the current column you are working on and col (as before) the value of the field in that column.

Alternatively, just for convenience and easier modification, you could use a variable:

start_col = 5
for i,col in enumerate(fields[start_col:], start_col):
    ....

--- UPDATE in reply to comments below:

I am still not quite sure I understand your comment, but if the loop you posted is inside a bigger loop you could to keep track of your current columns like this:

cur_column = 5
for line in Input:
    line = line.rstrip() 
    fields = line.split("\t")   
    for col in fields[cur_colum:]:
       ...
       ...

cur_column += 1 # done processing current column, increment value to next column

Posting some simple input/output examples would help if your code is too big to post. Hard to really know how to help without more information. I hope this is helpful.

9
  • These work for labelling the output of the first column, But the output is only from the first column. I am hoping for an outputfile that contains the results from all the columns. I am outputting with 'print >> output, str(i)'
    – user964689
    Jun 3, 2012 at 13:46
  • @user964689 I am not sure I understand, perhaps you could give some input/output examples? Don't you take a complete line and break it into fields based on \t? This will allow you to get (and label if you like) each of the fields in your line.
    – Levon
    Jun 3, 2012 at 13:49
  • My code is very long so I don't think I can print it out. I will try and explain concisely: I split text file into tab delimited fields. Then for a specific column I run through all the rows in the file and caculate some statistics based on the cumulative values of several rows. Now I want the script to do the same for other columns in the text file. Then output the results to one large output file with the associated information about which column the results refer to. So currently the output if only for column 5. I'd like it to iterate through all the columns
    – user964689
    Jun 3, 2012 at 13:52
  • perhaps i should put the for col in fields loop above the for line in input loop?
    – user964689
    Jun 3, 2012 at 14:00
  • I think it will work if I can just get it to loop back through once the cur_column has been updated to +=1. To do this do I have to indent it within a loop? Currently I think it reads through and updates the column to 6, then ends.
    – user964689
    Jun 3, 2012 at 14:30
1

You can use the Build-in function enumerate

for index, col in enumerate(fields[5:]):
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.