Skip to content
Snippets Groups Projects
Commit 3ab82ba2 authored by Reto Tschannen's avatar Reto Tschannen
Browse files

chore: added requirements, added output to function and misc stuff, added output to parser

parent cbb3f78d
No related branches found
No related tags found
1 merge request!13feat: add function to calculate mean and variance
Pipeline #13846 failed
......@@ -6,15 +6,18 @@ import os
import matplotlib.pyplot as plt
def mean_variance(filepath: str) -> str:
def mean_variance(filepath: str, output_dir: str = os.getcwd()+'/') -> str:
"""Given the counts observed for a given gene across all cells,
calculate the mean and variance.
! At the moment the function does not check the import files format,
be careful, and only add text files in the format geneid number_of_transcipts !
Args:
directory with files of gene expression counts in individual cells.
directory with text files of gene expression counts in individual cells.
Returns:
1. Path to Csv-formatted table with GeneID, Mean, Variance of the count.
2. Scatterplot of mean vs variance for all genes.
1. Path to Csv-formatted table with GeneID, Mean, Variance of the count, and
Scatterplot of mean vs variance for all genes.
Raises:
ValueError: If there are no files in directory
......@@ -58,13 +61,14 @@ def mean_variance(filepath: str) -> str:
plt.xlabel('mean')
plt.ylabel('variance')
plt.title('Mean gene expression vs. variance')
plt.savefig(output_dir+'/meanvarianceplot.png')
plt.show()
# Constructs csv file and saves it in the users directory
path = os.path.expanduser("~")+'/results_mean_var_function.csv'
path = output_dir+'/results_mean_var_function.csv'
with open(path, 'w') as csv_file:
filewriter = csv.writer(csv_file, delimiter=',', quotechar='|',
quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['geneid', 'mean', 'variance'])
for id in gene_counts.keys():
filewriter.writerow([id, mean[id], variance[id]])
return path
return output_dir
import argparse
from meanvariancefunction import mean_variance as mv
parser = argparse.ArgumentParser(
description=('Calculates mean and variance for all gene entries' +
'and plots mean against variance'),
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-i', '--input', required=True, type=str,
help='input file path')
parser.add_argument('-o', '--output', required=True, type=str,
help='output file path')
parser.add_argument("-h", "--help", action="help",
help="show this help message and exit")
args = parser.parse_args()
input_path = args.input
output_path = args.output
meanvarf = mv(input_path, output_path)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment