How can I calculate the average of column 1 and 2 and do the same for each line with python?
1 2
chrX 153706121 153706381 10 chrX 153706065 153706547 26 260 chrX 153705996 153706564 64 260
chrX 153993742 153993999 10 chrX 153993486 153994032 16 257 chrX 153993524 153994054 51 257
chrY 11920805 11921481 34 chrY 11920737 11921423 46 618 chrY 11920001 11921739 148 676
chrY 12379363 12379922 33 chrY 12379100 12379843 31 480 chrY 12379092 12380100 49 559
Thank you very much!
What I'm trying to do is calculate the average of columns 1 and 2 , 5 and 6 , 10 and 11 for each of the lines . Then use these averages for calculate the average of the three averages calculated and save the result in a bed file. This should be the center of the peaks or the region. So far I have this code but is not working , I think that just calculate the averages for the first line and no all the lines in the file. What do you think that is wrong?
n= open('overlap_peaks.bed', 'r')
for line in n:
Thanks!
I guess this isn't the 'complete' code? Are you using python interactively? What's going wrong?
For example if I do print average , it give me just one number. The program should give a list of all the average calculated for each line.
Also if I do average.saveas('center.bed') give me the error 'float' object has no attribute 'saveas'.
Okay, I'll take it you don't have a lot of experience with python then. You may want to adapt something like this:
And then let the loop execute. But more commonly, you would put this in a script (and not use python interactively). I suggest you first try to get yourself comfortable with python before using it for your research, otherwise mistakes will be made.
Yeah, I'm new with python. I'm having a hard time with it. I just tried your code but it give me:
outfile.write(average) expected a string or other character buffer object
Oh damn, my bad, should have used
A resource I found particularily useful to get started with python is Codecademy: https://www.codecademy.com/ If you work yourself through this interactive introduction things will get easier.
(You'll never stop making mistakes for the rest of your coding life, but they'll become easier to understand and solve.)
Thank you very much. Now it works !!!