Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Robin Erich Muench
metaSNV
Commits
53beda6e
Commit
53beda6e
authored
Mar 15, 2017
by
Luis Pedro Coelho
Browse files
Refactor to make code simpler
This code has fewer repetitions and is more readable
parent
70ce9fa7
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/computeGenomeCoverage.py
View file @
53beda6e
import
sys
#We assume that contings are sorted within the genome, so that a contig won't
show up
#randomly in the file, but together with its friends.
#
We assume that contings are sorted within the genome, so that a contig won't
#
show up
randomly in the file, but together with its friends.
cov
=
open
(
sys
.
argv
[
1
],
'r'
)
xcov
=
open
(
sys
.
argv
[
2
],
'r'
)
...
...
@@ -15,42 +15,39 @@ while True:
xcovL
=
xcov
.
readline
()
if
not
xcovL
:
break
name
=
covL
.
split
(
'
\t
'
)[
0
]
namex
=
xcovL
.
split
(
'
\t
'
)[
0
]
covL
=
covL
.
split
(
'
\t
'
)
xcovL
=
xcovL
.
split
(
'
\t
'
)
name
=
covL
[
0
]
namex
=
xcovL
[
0
]
if
name
!=
namex
:
print
name
print
namex
print
'oups'
print
(
"Mismatch in names {} != {}"
.
format
(
name
,
namex
))
taxId
=
name
.
split
(
'.'
)[
0
]
if
taxId
not
in
genomeMap
:
genomeMap
[
taxId
]
=
[
0.0
,
0.0
,
0.0
,
0.0
]
#why not [0,0.0,0.0,0.0] since 1st is int?
genomeMap
[
taxId
]
=
[
0.0
,
0.0
,
0.0
,
0.0
]
#Add the length
genomeMap
[
taxId
][
0
]
+=
int
(
covL
.
split
(
'
\t
'
)[
1
])
#genomeMap[taxId][0] += 1
#Add the average coverage weighted with the length
genomeMap
[
taxId
][
1
]
+=
float
(
covL
.
split
(
'
\t
'
)[
2
])
*
int
(
covL
.
split
(
'
\t
'
)[
1
])
#genomeMap[taxId][1] += int(float(covL.split('\t')[2]))
#Add the number of bases covered at 1x
genomeMap
[
taxId
][
2
]
+=
int
(
xcovL
.
split
(
'
\t
'
)[
2
])
#Add the number of bases covered at 2x
genomeMap
[
taxId
][
3
]
+=
int
(
xcovL
.
split
(
'
\t
'
)[
3
])
genomeMap
[
taxId
][
0
]
+=
int
(
covL
[
1
])
outf
=
open
(
sys
.
argv
[
3
],
'w'
)
#Write header
outf
.
write
(
'TaxId
\t
Average_cov
\t
Percentage_1x
\t
Percentage_2x
\n
'
)
#Add the average coverage weighted with the length
genomeMap
[
taxId
][
1
]
+=
float
(
covL
[
2
])
*
int
(
covL
[
1
])
moreThan10x
=
0
#Print the average coverage over the taxId's
for
k
in
genomeMap
.
keys
():
if
genomeMap
[
k
][
1
]
/
genomeMap
[
k
][
0
]
>
10
:
moreThan10x
+=
1
outf
.
write
(
'%s
\t
%f
\t
%f
\t
%f
\n
'
%
(
k
,
genomeMap
[
k
][
1
]
/
genomeMap
[
k
][
0
],
genomeMap
[
k
][
2
]
/
genomeMap
[
k
][
0
]
*
100
,
genomeMap
[
k
][
3
]
/
genomeMap
[
k
][
0
]
*
100
))
#Add the number of bases covered at 1x
genomeMap
[
taxId
][
2
]
+=
int
(
xcovL
[
2
])
outf
.
close
()
#Add the number of bases covered at 2x
genomeMap
[
taxId
][
3
]
+=
int
(
xcovL
[
3
])
cov
.
close
()
xcov
.
close
()
#print moreThan10x
print
'Done'
with
open
(
sys
.
argv
[
3
],
'w'
)
as
outf
:
#Write header
outf
.
write
(
'TaxId
\t
Average_cov
\t
Percentage_1x
\t
Percentage_2x
\n
'
)
#Print the average coverage over the taxId's
for
k
in
genomeMap
:
outf
.
write
(
'%s
\t
%f
\t
%f
\t
%f
\n
'
%
(
k
,
genomeMap
[
k
][
1
]
/
genomeMap
[
k
][
0
],
genomeMap
[
k
][
2
]
/
genomeMap
[
k
][
0
]
*
100
,
genomeMap
[
k
][
3
]
/
genomeMap
[
k
][
0
]
*
100
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment