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
283577f8
Commit
283577f8
authored
3 years ago
by
Reto Tschannen
Committed by
Reto Tschannen
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
chore: cleaned up function and added correct path to usr directory
parent
37aaef90
No related branches found
No related tags found
1 merge request
!13
feat: add function to calculate mean and variance
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/mean-variance-function.py
+18
-17
18 additions, 17 deletions
src/mean-variance-function.py
with
18 additions
and
17 deletions
src/mean-variance-function.py
+
18
−
17
View file @
283577f8
import
glob
,
io
import
glob
,
io
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
import
csv
import
csv
import
os
...
@@ -12,62 +13,62 @@ def mean_variance(filepath):
...
@@ -12,62 +13,62 @@ def mean_variance(filepath):
directory with files of gene expression counts in individual cells
directory with files of gene expression counts in individual cells
Output:
Output:
1. Csv-formatted table with GeneID, Mean, Variance of the counts
1.
Path to
Csv-formatted table with GeneID, Mean, Variance of the counts
2. Scatterplot of mean vs variance for all genes
2. Scatterplot of mean vs variance for all genes
"""
"""
# Open each file in the input directory, raises error if no file is fund
# Open each file in the input directory, raises error if no file is fund
files
=
[
file
for
file
in
glob
.
glob
(
filepath
)]
files
=
[
file
for
file
in
glob
.
glob
(
filepath
)]
if
len
(
files
)
==
0
:
if
len
(
files
)
==
0
:
raise
ValueError
(
'
No files in directory:
'
,
filepath
)
raise
ValueError
(
'
No files in directory:
'
,
filepath
)
# Creates all required dictionaries to cinstruct the mean, variance
nog
=
{}
nog
=
{}
count
=
{}
occurence
=
{}
t
es
t
=
{}
individual_valu
es
=
{}
mean
=
{}
mean
=
{}
variance
=
{}
variance
=
{}
# Added together all gene counts in nog, and occurence in occurence
for
file_name
in
files
:
for
file_name
in
files
:
with
io
.
open
(
file_name
,
'
r
'
)
as
fh
:
with
io
.
open
(
file_name
,
'
r
'
)
as
fh
:
for
line
in
fh
:
for
line
in
fh
:
geneid
,
copies
=
str
(
line
.
split
()[
0
]),
int
(
line
.
split
()[
1
])
geneid
,
copies
=
str
(
line
.
split
()[
0
]),
int
(
line
.
split
()[
1
])
if
geneid
not
in
nog
:
if
geneid
not
in
nog
:
nog
[
geneid
]
=
copies
nog
[
geneid
]
=
copies
count
[
geneid
]
=
1
occurence
[
geneid
]
=
1
t
es
t
[
geneid
]
=
[
copies
]
individual_valu
es
[
geneid
]
=
[
copies
]
else
:
else
:
nog
[
geneid
]
+=
copies
nog
[
geneid
]
+=
copies
count
[
geneid
]
+=
1
occurence
[
geneid
]
+=
1
t
es
t
[
geneid
]
+=
[
copies
]
individual_valu
es
[
geneid
]
+=
[
copies
]
# Calculate mean of each gene
# Calculate mean of each gene
for
i
in
nog
:
for
i
in
nog
:
mean
[
i
]
=
nog
[
i
]
/
count
[
i
]
mean
[
i
]
=
nog
[
i
]
/
occurence
[
i
]
# Calculate the variance
# Calculate the variance
for
i
in
t
es
t
:
for
i
in
individual_valu
es
:
for
j
in
range
(
0
,
len
(
t
es
t
[
i
])):
for
j
in
range
(
0
,
len
(
individual_valu
es
[
i
])):
variance
[
i
]
=
(
t
es
t
[
i
][
j
]
-
mean
[
i
])
**
2
/
count
[
i
]
variance
[
i
]
=
(
individual_valu
es
[
i
][
j
]
-
mean
[
i
])
**
2
/
occurence
[
i
]
# Plot mean against variance
# Plot mean against variance
plt
.
scatter
(
mean
.
values
(),
variance
.
values
())
plt
.
scatter
(
mean
.
values
(),
variance
.
values
())
for
value
in
list
(
mean
.
keys
()):
for
value
in
list
(
mean
.
keys
()):
plt
.
text
(
mean
[
value
],
variance
[
value
],
value
)
plt
.
text
(
mean
[
value
],
variance
[
value
],
value
)
#plt.annotate(list(mean.keys()), mean.values(), variance.values())
plt
.
xlabel
(
'
mean
'
)
plt
.
xlabel
(
'
mean
'
)
plt
.
ylabel
(
'
variance
'
)
plt
.
ylabel
(
'
variance
'
)
plt
.
title
(
'
Mean gene expression vs. variance
'
)
plt
.
title
(
'
Mean gene expression vs. variance
'
)
plt
.
show
()
plt
.
show
()
with
open
(
'
/home/reto/results_mean_var_function.csv
'
,
'
w
'
)
as
csv_file
:
# 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
=
csv
.
writer
(
csv_file
,
delimiter
=
'
,
'
,
quotechar
=
'
|
'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
filewriter
.
writerow
([
'
geneid
'
,
'
mean
'
,
'
variance
'
])
filewriter
.
writerow
([
'
geneid
'
,
'
mean
'
,
'
variance
'
])
for
id
in
nog
.
keys
():
for
id
in
nog
.
keys
():
filewriter
.
writerow
([
id
,
mean
[
id
],
variance
[
id
]])
filewriter
.
writerow
([
id
,
mean
[
id
],
variance
[
id
]])
return
files
,
nog
,
count
,
mean
,
'
var
'
,
variance
,
test
,
list
(
mean
.
keys
())
return
os
.
path
.
expanduser
(
"
~
"
)
+
'
/results_mean_var_function.csv
'
print
(
mean_variance
(
'
/home/reto/2021_project_folder/2021_test/*
'
))
print
(
mean_variance
(
'
/home/reto/2021_project_folder/2021_test/*
'
))
...
...
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