Here's a Python script using pandas and matplotlib to plot the averages of genes and intergenic regions.
import pandas as pd
import matplotlib.pyplot as plt
# Load the csv file
def load_data(file_name):
try:
data = pd.read_csv(file_name, sep=';')
return data
except Exception as e:
print(f"Failed to load data: {e}")
return None
# Plot the data
def plot_data(data):
# Separate genes and intergenic regions
genes = data[data['Gene name'].str.contains('gene')]
intergenic = data[data['Gene name'] == 'INTERGENIC']
# Create the plot
plt.figure(figsize=(10, 6))
plt.bar(genes['starting bp'], genes['average'], width=(genes['ending bp'] - genes['starting bp']), color='red', label='Genes')
plt.bar(intergenic['starting bp'], intergenic['average'], width=(intergenic['ending bp'] - intergenic['starting bp']), color='blue', label='Intergenic regions')
plt.xlabel('Base Pair Position')
plt.ylabel('Average')
plt.title('Averages of Genes and Intergenic Regions')
plt.legend()
plt.show()
# Main function
def main():
file_name = 'genes_averages.csv'
data = load_data(file_name)
if data is not None:
plot_data(data)
if __name__ == "__main__":
main()
This script first loads the csv file into a pandas DataFrame. Then it separates the genes and intergenic regions into two separate DataFrames. Finally, it creates a bar plot where the x-axis represents the base pair position and the y-axis represents the average. The genes are plotted in red and the intergenic regions are plotted in blue. The width of each bar is the length of the gene or intergenic region.