@@ -2,13 +2,35 @@ import strutils
22import strformat
33import argparse
44import pedfile
5- import sequtils
6- import algorithm
5+
6+ proc formatFloatClean * (f: float32 ): string =
7+ # # Format a float32 as string, removing trailing zeros after decimal point
8+ # # but keeping at least one zero if the number is whole (e.g., "1.0" not "1.")
9+ # Use precision=6 to avoid floating-point precision artifacts
10+ let formatted = formatFloat (f, ffDecimal, precision= 6 )
11+
12+ # Find the decimal point
13+ let decimalPos = formatted.find ('.' )
14+ if decimalPos == - 1 :
15+ return formatted # No decimal point, return as is
16+
17+ # Remove trailing zeros from the decimal part
18+ var cleaned = formatted
19+ while cleaned.len > decimalPos + 1 and cleaned[^ 1 ] == '0' :
20+ cleaned = cleaned[0 ..^ 2 ] # Remove last character
21+
22+ # If we removed all decimal digits, add one back to keep "1.0" format
23+ if cleaned[^ 1 ] == '.' :
24+ cleaned &= " 0"
25+
26+ return cleaned
727
828proc pedrel_main * () =
929 var argv = commandLineParams ()
1030 if argv[0 ] == " pedrel" :
1131 argv = argv[1 .. argv.high]
32+ if len (argv) == 0 :
33+ argv = @ [" -h" ]
1234
1335 var p = newParser (" somalier pedrel" ):
1436 help (" report pairwise relationships from pedigree file" )
@@ -47,13 +69,14 @@ proc pedrel_main*() =
4769 let sampleB = samples[j]
4870 let rel = sampleA.relatedness (sampleB)
4971
50- if rel > min_rel:
72+ if rel >= min_rel:
5173 # Ensure consistent ordering (alphabetical by sample ID)
5274 let (sample1, sample2) = if sampleA.id < sampleB.id:
5375 (sampleA.id, sampleB.id)
5476 else :
5577 (sampleB.id, sampleA.id)
56- output_file.write_line (& " { sample1} \t { sample2} \t { rel:.6f } " )
78+ let relf = formatFloatClean (rel)
79+ output_file.write_line (& " { sample1} \t { sample2} \t { relf} " )
5780
5881 if opts.output != " stdout" :
5982 output_file.close ()
0 commit comments