Skip to content

Commit 76085a3

Browse files
committed
Add option for output rounding digits #271
1 parent ef61eab commit 76085a3

13 files changed

Lines changed: 85 additions & 52 deletions

docs/source/usage.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ Output options
196196
-out_id_only Suppress output for individuals not present in
197197
the file specified with -out_id_order. It also suppresses
198198
"dummy" individuals.
199-
199+
-out_digits Specify the number of digits to round the outputs.
200+
Does not apply to outputs from ``alt_allele_prob``,
201+
``geno_error_prob``, ``seq_error_prob``,
202+
and ``pheno_penetrance``.
203+
Default: 4.
200204
|Software| by default produces a :ref:`dosage file <dosage_file_format>`.
201205
Additional individual-level outputs can be requested with the options described above.
202206

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies = [
2222
"numpy>=1.19",
2323
"numba>=0.50.0"
2424
]
25+
requires-python = ">= 3.6"
2526

2627
[project.scripts]
2728
# correspond to entry_points

src/tinypeel/Peeling/PeelingIO.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,20 @@ def writeOutParamaters(peelingInfo):
9292
:return: None. Writes to files specified in the InputOutput.args.
9393
"""
9494
args = InputOutput.args
95-
9695
if args.est_geno_error_prob:
9796
np.savetxt(
98-
args.out_file + ".geno_error_prob.txt", peelingInfo.genoError, fmt="%f"
97+
args.out_file + ".geno_error_prob.txt",
98+
peelingInfo.genoError,
9999
)
100100
if args.est_seq_error_prob:
101101
np.savetxt(
102-
args.out_file + ".seq_error_prob.txt", peelingInfo.seqError, fmt="%f"
102+
args.out_file + ".seq_error_prob.txt",
103+
peelingInfo.seqError,
103104
)
104105
if args.rec_prob:
105106
np.savetxt(
106-
args.out_file + ".rec_prob.txt", np.empty((1, 1)), fmt="%f"
107+
args.out_file + ".rec_prob.txt",
108+
np.empty((1, 1)),
107109
) # not implemented atm, just as a placeholder
108110
# np.savetxt(args.out_file + ".trans", peelingInfo.transmissionRate, fmt = "%f")
109111

@@ -134,7 +136,6 @@ def sort_key(mf_key):
134136
args.out_file + ".alt_allele_prob.txt",
135137
combined_AAP,
136138
delimiter="\t",
137-
fmt="%.2f",
138139
header="\t".join(sorted_MF),
139140
comments="",
140141
)
@@ -149,7 +150,8 @@ def writePhenoPenetrance(pedigree):
149150
"""
150151
args = InputOutput.args
151152
np.savetxt(
152-
args.out_file + ".pheno_penetrance.txt", pedigree.phenoPenetrance, fmt="%.2f"
153+
args.out_file + ".pheno_penetrance.txt",
154+
pedigree.phenoPenetrance,
153155
)
154156

155157

@@ -249,13 +251,17 @@ def writePhasedGenoProbs(pedigree, genoProbFunc, outputFile):
249251
:type outputFile: str
250252
:return: None. Writes to the specified output file.
251253
"""
254+
args = InputOutput.args
252255
with open(outputFile, "w+") as f:
253256
for idx, ind in pedigree.writeOrder():
254257
matrix = genoProbFunc(ind.idn, ind.sex)
255258
f.write("\n")
256259
for i in range(matrix.shape[0]):
257260
f.write(
258-
ind.idx + " " + " ".join(map("{:.4f}".format, matrix[i, :])) + "\n"
261+
ind.idx
262+
+ " "
263+
+ " ".join(map(f"{{:.{args.out_digits}f}}".format, matrix[i, :]))
264+
+ "\n"
259265
)
260266

261267

@@ -270,6 +276,7 @@ def writeGenoProbs(pedigree, genoProbFunc, outputFile):
270276
:type outputFile: str
271277
:return: None. Writes to the specified output file.
272278
"""
279+
args = InputOutput.args
273280
with open(outputFile, "w+") as f:
274281
for idx, ind in pedigree.writeOrder():
275282
matrix = genoProbFunc(ind.idn, ind.sex)
@@ -279,15 +286,20 @@ def writeGenoProbs(pedigree, genoProbFunc, outputFile):
279286
ind.idx
280287
+ " "
281288
+ " ".join(
282-
map("{:.4f}".format, matrix[i, :] + matrix[i + 1, :])
289+
map(
290+
f"{{:.{args.out_digits}f}}".format,
291+
matrix[i, :] + matrix[i + 1, :],
292+
)
283293
)
284294
+ "\n"
285295
)
286296
elif i != 2: # Print probabilities for aa and AA
287297
f.write(
288298
ind.idx
289299
+ " "
290-
+ " ".join(map("{:.4f}".format, matrix[i, :]))
300+
+ " ".join(
301+
map(f"{{:.{args.out_digits}f}}".format, matrix[i, :])
302+
)
291303
+ "\n"
292304
)
293305

@@ -308,7 +320,10 @@ def writePhenoProbs(pedigree, phenoProbFunc):
308320
f.write("\n")
309321
for i in range(matrix.shape[0]):
310322
f.write(
311-
ind.idx + " " + " ".join(map("{:.4f}".format, matrix[i, :])) + "\n"
323+
ind.idx
324+
+ " "
325+
+ " ".join(map(f"{{:.{args.out_digits}f}}".format, matrix[i, :]))
326+
+ "\n"
312327
)
313328

314329

@@ -325,14 +340,20 @@ def writeDosages(pedigree, genoProbFunc, isXChr, outputFile):
325340
:type outputFile: str
326341
:return: None. Writes to the specified output file.
327342
"""
343+
args = InputOutput.args
328344
with open(outputFile, "w+") as f:
329345
for idx, ind in pedigree.writeOrder():
330346
if isXChr and ind.sex == 0:
331347
tmp = np.array([0, 0, 0, 1])
332348
else:
333349
tmp = np.array([0, 1, 1, 2])
334350
matrix = np.dot(tmp, genoProbFunc(ind.idn, ind.sex))
335-
f.write(ind.idx + " " + " ".join(map("{:.4f}".format, matrix)) + "\n")
351+
f.write(
352+
ind.idx
353+
+ " "
354+
+ " ".join(map(f"{{:.{args.out_digits}f}}".format, matrix))
355+
+ "\n"
356+
)
336357

337358

338359
def writeCalledGenotypes(pedigree, genoProbFunc, isXChr, outputFile, thresh):

src/tinypeel/tinypeel.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ def get_output_options():
545545
required=False,
546546
help='Suppress output for individuals not present in the file specified with -out_id_order. It also suppresses "dummy" individuals.',
547547
)
548+
parse_dictionary["out_digits"] = lambda parser: parser.add_argument(
549+
"-out_digits",
550+
default=4,
551+
type=int,
552+
required=False,
553+
help="Specify the number of digits to round the outputs. Does not apply to outputs from ``alt_allele_prob``, ``geno_error_prob``, ``seq_error_prob``, and ``pheno_penetrance``. Default: 4.",
554+
)
548555

549556
return parse_dictionary
550557

@@ -712,7 +719,7 @@ def getArgs():
712719
InputOutput.add_arguments_from_dictionary(
713720
output_parser,
714721
get_output_options(),
715-
options=["writekey", "onlykeyed"],
722+
options=["writekey", "onlykeyed", "out_digits"],
716723
)
717724

718725
# Multithreading

tests/functional_tests/run_func_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ def test_alt_allele_prob(self):
695695

696696
self.input_files = ["geno_file", "ped_file"]
697697
self.input_file_depend_on_test_cases = self.input_files
698-
self.arguments = {"method": "multi", "out_id_only": None}
698+
self.arguments = {"method": "multi", "out_id_only": None, "out_digits": 2}
699699

700700
for self.test_cases in [
701701
"default",
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MF_1
2-
0.50
3-
0.50
4-
0.50
5-
0.50
6-
0.50
2+
5.000000000000000000e-01
3+
5.000000000000000000e-01
4+
5.000000000000000000e-01
5+
5.000000000000000000e-01
6+
5.000000000000000000e-01
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MF_1 MF_2
2-
0.50 0.50
3-
0.50 0.50
4-
0.50 0.50
5-
0.50 0.50
6-
0.50 0.50
2+
5.000000000000000000e-01 5.000000000000000000e-01
3+
5.000000000000000000e-01 5.000000000000000000e-01
4+
5.000000000000000000e-01 5.000000000000000000e-01
5+
5.000000000000000000e-01 5.000000000000000000e-01
6+
5.000000000000000000e-01 5.000000000000000000e-01
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MF_1 MF_2
2-
0.01 0.99
3-
0.01 0.99
4-
0.01 0.99
5-
0.01 0.99
6-
0.01 0.99
2+
9.999999776482582092e-03 9.900000095367431641e-01
3+
9.999999776482582092e-03 9.900000095367431641e-01
4+
9.999999776482582092e-03 9.900000095367431641e-01
5+
9.999999776482582092e-03 9.900000095367431641e-01
6+
9.999999776482582092e-03 9.900000095367431641e-01
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MF_1 MF_2
2-
0.01 0.99
3-
0.01 0.99
4-
0.50 0.50
5-
0.01 0.99
6-
0.01 0.99
2+
9.999999776482582092e-03 9.900000095367431641e-01
3+
9.999999776482582092e-03 9.900000095367431641e-01
4+
5.000083446502685547e-01 4.999749958515167236e-01
5+
9.999999776482582092e-03 9.900000095367431641e-01
6+
9.999999776482582092e-03 9.900000095367431641e-01
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MF_1
2-
0.01
3-
0.01
4-
0.01
5-
0.01
6-
0.01
2+
9.999999776482582092e-03
3+
9.999999776482582092e-03
4+
9.999999776482582092e-03
5+
9.999999776482582092e-03
6+
9.999999776482582092e-03

0 commit comments

Comments
 (0)