Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scRNA-seq-simulation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
zavolan_group
pipelines
scRNA-seq-simulation
Commits
cbb3f78d
Commit
cbb3f78d
authored
3 years ago
by
Reto Tschannen
Browse files
Options
Downloads
Patches
Plain Diff
chore: removed second copy of module and optimized the function
parent
467c45a1
Branches
Branches containing commit
No related tags found
1 merge request
!13
feat: add function to calculate mean and variance
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/mean-variance-function.py
+0
-74
0 additions, 74 deletions
src/mean-variance-function.py
src/meanvariancefunction.py
+19
-21
19 additions, 21 deletions
src/meanvariancefunction.py
with
19 additions
and
95 deletions
src/mean-variance-function.py
deleted
100644 → 0
+
0
−
74
View file @
467c45a1
import
glob
,
io
import
matplotlib.pyplot
as
plt
import
csv
import
os
def
mean_variance
(
filepath
):
"""
Given the counts observed for a given gene across all cells, calculate the mean and variance.
Input:
directory with files of gene expression counts in individual cells
Output:
1. Path to Csv-formatted table with GeneID, Mean, Variance of the counts
2. Scatterplot of mean vs variance for all genes
"""
# Open each file in the input directory, raises error if no file is fund
files
=
[
file
for
file
in
glob
.
glob
(
filepath
)]
if
len
(
files
)
==
0
:
raise
ValueError
(
'
No files in directory:
'
,
filepath
)
# Creates all required dictionaries to cinstruct the mean, variance
gene_counts
=
{}
occurence
=
{}
individual_values
=
{}
mean
=
{}
variance
=
{}
# Added together all gene counts in gene_counts, and occurence in occurence
for
file_name
in
files
:
with
io
.
open
(
file_name
,
'
r
'
)
as
fh
:
for
line
in
fh
:
geneid
,
copies
=
str
(
line
.
split
()[
0
]),
int
(
line
.
split
()[
1
])
if
geneid
not
in
gene_counts
:
gene_counts
[
geneid
]
=
copies
occurence
[
geneid
]
=
1
individual_values
[
geneid
]
=
[
copies
]
else
:
gene_counts
[
geneid
]
+=
copies
occurence
[
geneid
]
+=
1
individual_values
[
geneid
]
+=
[
copies
]
# Calculate mean of each gene
for
i
in
gene_counts
:
mean
[
i
]
=
gene_counts
[
i
]
/
occurence
[
i
]
# Calculate the variance
for
i
in
individual_values
:
for
j
in
range
(
0
,
len
(
individual_values
[
i
])):
variance
[
i
]
=
(
individual_values
[
i
][
j
]
-
mean
[
i
])
**
2
/
occurence
[
i
]
# Plot mean against variance
plt
.
scatter
(
mean
.
values
(),
variance
.
values
())
for
value
in
list
(
mean
.
keys
()):
plt
.
text
(
mean
[
value
],
variance
[
value
],
value
)
plt
.
xlabel
(
'
mean
'
)
plt
.
ylabel
(
'
variance
'
)
plt
.
title
(
'
Mean gene expression vs. variance
'
)
plt
.
show
()
# Constructs csv file and saves it in the users directory
with
open
(
os
.
path
.
expanduser
(
"
~
"
)
+
'
/results_mean_var_function.csv
'
,
'
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
os
.
path
.
expanduser
(
"
~
"
)
+
'
/results_mean_var_function.csv
'
This diff is collapsed.
Click to expand it.
src/meanvariancefunction.py
+
19
−
21
View file @
cbb3f78d
import
glob
import
io
import
matplotlib.pyplot
as
plt
import
csv
from
glob
import
glob
import
io
import
os
import
matplotlib.pyplot
as
plt
def
mean_variance
(
filepath
)
:
def
mean_variance
(
filepath
:
str
)
->
str
:
"""
Given the counts observed for a given gene across all cells,
calculate the mean and variance.
Input
:
directory with files of gene expression counts in individual cells
Output
:
1. Path to Csv-formatted table with GeneID, Mean, Variance of the count
2. Scatterplot of mean vs variance for all genes
Args
:
directory with 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
.
Raises:
ValueError: If there are no files in diretory
ValueError: If there are no files in dire
c
tory
"""
# Open each file in the input directory, raises error if no file is fund
files
=
[
file
for
file
in
glob
.
glob
(
filepath
)]
# Open each file in the input directory, raises error if no file is f
o
und
files
=
[
file
for
file
in
glob
(
filepath
)]
if
len
(
files
)
==
0
:
raise
ValueError
(
'
No files in directory:
'
,
filepath
)
...
...
@@ -27,7 +28,6 @@ def mean_variance(filepath):
# Creates all required dictionaries to construct the mean and variance
gene_counts
=
{}
occurence
=
{}
individual_values
=
{}
mean
=
{}
variance
=
{}
...
...
@@ -37,21 +37,19 @@ def mean_variance(filepath):
for
line
in
fh
:
geneid
,
copies
=
str
(
line
.
split
()[
0
]),
int
(
line
.
split
()[
1
])
if
geneid
not
in
gene_counts
:
gene_counts
[
geneid
]
=
copies
gene_counts
[
geneid
]
=
[
copies
]
occurence
[
geneid
]
=
1
individual_values
[
geneid
]
=
[
copies
]
else
:
gene_counts
[
geneid
]
+=
copies
gene_counts
[
geneid
]
+=
[
copies
]
occurence
[
geneid
]
+=
1
individual_values
[
geneid
]
+=
[
copies
]
# Calculates mean of each gene
for
i
in
gene_counts
:
mean
[
i
]
=
gene_counts
[
i
]
/
occurence
[
i
]
mean
[
i
]
=
sum
(
gene_counts
[
i
]
)
/
occurence
[
i
]
# Calculates the variance
for
i
in
individual_value
s
:
for
j
in
range
(
0
,
len
(
individual_value
s
[
i
])):
variance
[
i
]
=
(
individual_value
s
[
i
][
j
]
-
mean
[
i
])
**
2
/
occurence
[
i
]
for
i
in
gene_count
s
:
for
j
in
range
(
0
,
len
(
gene_count
s
[
i
])):
variance
[
i
]
=
(
gene_count
s
[
i
][
j
]
-
mean
[
i
])
**
2
/
occurence
[
i
]
# Plots mean against variance
plt
.
scatter
(
mean
.
values
(),
variance
.
values
())
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment