Tips: [Python] Calculate the average value of the specified area with bedgraph

Suppose that the following bedgraph file is read and some processing is performed only in the specified area.

example.bedgraph


chr1	10084	10104	2
chr1	10104	10124	4
chr1	10124	10164	6
chr1	10164	10184	11
chr1	10184	10204	14
chr1	10204	10224	16
chr1	10224	10244	14
chr1	10244	10264	15
chr1	10264	10284	14
chr1	10284	10304	17

For example, to calculate the average value in the area, the following script is applied.

averageBedgraph.py (corrected version)


#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
Calculates the average value of the specified area from the Bedgraph file.
"""
__version__ = "1.00"
__date__ = "7 Jun 2017"

import sys

def averageBedgraph(filename, chromosome, start, end):
    """
    @function   averageBedgraph();
Calculates the average value of the specified area from the Bedgraph file.
    @param  {string} filename :Input file name
    @param  {string} chromosome :Chromosome number
    @param  {int}    start :Starting position
    @param  {int}    end :End position
    """

    total = 0
    with open(filename) as lines:
        for line in lines:
            c, s, e, v = line.split()
            if c != chromosome:
                continue
            s, e, v = int(s), int(e), int(v)
            if s <= start and end <= e:
                total += (end - start) * v
            elif s <= start < e:
                total += (e - start) * v
            elif start < s and e <= end:
                total += (e - s) * v
            elif s < end <= e:
                total += (end - s) * v
            elif end < s:
                break
    print('average : %s' % (total / (end - start)))
    print('done')

if __name__ == '__main__':
    argvs = sys.argv
    argc = len(argvs)

    if (argc != 5):   # Checking input
        print("USAGE : python3 averageBedgraph.py <INPUT_FILE> <CHROMOSOME> <START> <END>")
        quit()

    averageBedgraph(str(argvs[1]),str(argvs[2]),int(argvs[3]),int(argvs[4]))
quit()

Execution example

Calculate the average value of the specified area of bedfile


$ python3 averageBedgraph.py example.bedgraph chr1 10150 10250

Standard output


average : 12.74
done

It's a muddy method that doesn't need to be mentioned, but if you don't want to assemble it, please use it.

Also, if you have a tool that is good at processing such as bedgraph, I would appreciate it if you could teach me. There is no point in reinventing the wheel ... Thank you.

that's all. Thank you very much.

2017/06/09 postscript The code that was posted has been corrected. We would like to express our deep gratitude to shiracamus for making the correction.

Recommended Posts

Tips: [Python] Calculate the average value of the specified area with bedgraph
[Python] Calculate the average value of the pixel value RGB of the object
Calculate the total number of combinations with python
Calculate the regression coefficient of simple regression analysis with python
Compare the sum of each element in two lists with the specified value in Python
Calculate the square root of 2 in millions of digits with python
Check the existence of the file with python
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Test & Debug Tips: Create a file of the specified size in Python
Calculate the probability of being a squid coin with Bayes' theorem [python]
Get the average salary of a job with specified conditions from indeed.com
Get the return value of an external shell script (ls) with python3
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Check the date of the flag duty with Python
Find the divisor of the value entered in python
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
Extract the table of image files with OneDrive & Python
[Python Data Frame] When the value is empty, fill it with the value of another column.
Learn Nim with Python (from the beginning of the year).
Get the value of a specific key in a list from the dictionary type in the list with Python
Destroy the intermediate expression of the sweep method with Python
the zen of Python
Visualize the range of interpolation and extrapolation with python
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Summary of the basic flow of machine learning with Python
[Python & SQLite] I analyzed the expected value of a race with horses with a win of 1x ②
Get the operation status of JR West with Python
Extract the band information of raster data with python
Calculate the product of matrices with a character expression?
Get the value of a specific key up to the specified index in the dictionary list in Python
I tried to find the entropy of the image with python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
The story of implementing the popular Facebook Messenger Bot with python
I tried to find the average of the sequence with TensorFlow
Unify the environment of the Python development team starting with Poetry
Visualize the results of decision trees performed with Python scikit-learn
[Python] Heron's formula functionalization and calculation of the maximum area
Inherit the standard library to find the average value of Queue
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks
Run the intellisense of your own python library with VScode.
Your URL didn't respond with the value of the challenge parameter.
Issue WOL with Scapy of Python3 (LAN controller specified type)
I evaluated the strategy of stock system trading with Python.
Check the scope of local variables with the Python locals function.
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
[Python] Calculate the angle consisting of three points on the coordinates
The story of rubyist struggling with python :: Dict data with pycall
[Homology] Count the number of holes in data with Python
Rewrite the record addition node of SPSS Modeler with Python.
[Tips] Problems and solutions in the development of python + kivy
Estimate the attitude of AR markers with Python + OpenCV + drone
The true value of Terraform automation starting with Oracle Cloud
Play with the password mechanism of GitHub Webhook and Python
Get the source of the page to load infinitely with python.
Towards the retirement of Python2