Skip to content

Commit

Permalink
Merge compress-writeMixtureInfo-output into develop
Browse files Browse the repository at this point in the history
* compress-writeMixtureInfo-output:
  Updated docs
  Improved output of writeMixtureInfo()
  Auto-recognizing format of greedyMix() data
  Fixed output of greedyMix()
  • Loading branch information
wleoncio committed Sep 14, 2023
2 parents bde3be6 + 9ed39d4 commit 0f3c570
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rBAPS
Title: Bayesian Analysis of Population Structure
Version: 0.0.0.9023
Version: 0.0.0.9024
Date: 2020-11-09
Authors@R:
c(
Expand Down
4 changes: 2 additions & 2 deletions R/greedyMix.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' data <- system.file("extdata", "FASTA_clustering_haploid.fasta", package = "rBAPS")
#' greedyMix(data, "fasta")
greedyMix <- function(
data, format, partitionCompare = NULL, ninds = 1L, npops = 1L,
data, format = gsub("^.*\\.", "", data), partitionCompare = NULL, ninds = 1L, npops = 1L,
counts = NULL, sumcounts = NULL, max_iter = 100L, alleleCodes = NULL,
inp = NULL, popnames = NULL, fixedK = FALSE, verbose = FALSE
) {
Expand Down Expand Up @@ -73,5 +73,5 @@ greedyMix <- function(
)

# Updateing results ==========================================================
return(c(out, "changesInLogml" = changesInLogml))
return(c(out, list("changesInLogml" = changesInLogml)))
}
11 changes: 6 additions & 5 deletions R/kldiv2str.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
kldiv2str <- function(div) {
mjono <- " "
kldiv2str <- function(div, max_chars = 6L) {
if (max_chars > 6L) message("max_chars > 6L, truncating to 6L")
mjono <- rep(" ", max_chars)
if (abs(div) < 100) {
# Ei tarvita e-muotoa
mjono[6] <- as.character((floor(div * 1000)) %% 10)
mjono[5] <- as.character((floor(div * 100)) %% 10)
mjono[4] <- as.character((floor(div * 10)) %% 10)
if (max_chars >= 6) mjono[6] <- as.character((floor(div * 1000)) %% 10)
if (max_chars >= 5) mjono[5] <- as.character((floor(div * 100)) %% 10)
if (max_chars >= 4) mjono[4] <- as.character((floor(div * 10)) %% 10)
mjono[3] <- "."
mjono[2] <- as.character((floor(div)) %% 10)
arvo <- (floor(div / 10)) %% 10
Expand Down
11 changes: 7 additions & 4 deletions R/logml2String.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#' @title Logml to string
#' @description Returns a string representation of a logml
#' @param logml input Logml
#' @param
#' @param leading_zeros_replacement string to replace leading zeros with
#' @return String version of logml
logml2String <- function(logml) {
logml2String <- function(logml, leading_zeros_replacement = " ") {
mjono <- rep(" ", 7L)
# Palauttaa logml:n string-esityksen.
mjono <- " "

if (logml == -Inf) {
mjono[7] <- "-"
Expand All @@ -20,8 +22,9 @@ logml2String <- function(logml) {
mjono[3] <- palautaYks(abs(logml), 2)
mjono[2] <- palautaYks(abs(logml), 3)
pointer <- 2
while (mjono[pointer] == "0" & pointer < 7) {
mjono[pointer] <- " "
while (mjono[pointer] == "0" && pointer < 7) {
# Removes leading zeros
mjono[pointer] <- leading_zeros_replacement
pointer <- pointer + 1
}
if (logml < 0) {
Expand Down
3 changes: 1 addition & 2 deletions R/takeLine.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
takeLine <- function(description, width) {
# Returns one line from the description: line ends to the first
# space after width:th mark.
newLine <- description[1:width]
n <- width + 1
while ((description[n] != " ") & (n < length(description))) {
while (description[n] != "" && n < length(description)) {
n <- n + 1
}
newline <- description[1:n]
Expand Down
46 changes: 17 additions & 29 deletions R/writeMixtureInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ writeMixtureInfo <- function(
logml, rowsFromInd, data, adjprior, priorTerm, outPutFile, inputFile,
partitionSummary, popnames, fixedK, verbose
) {
changesInLogml <- list()
ninds <- size(data, 1) / rowsFromInd
npops <- size(COUNTS, 3)
# Check that the names refer to individuals
Expand Down Expand Up @@ -76,41 +75,34 @@ writeMixtureInfo <- function(
cluster_size <- length(indsInM)

if (names) {
text <- c(
"Cluster ",
as.character(m),
": {",
as.character(popnames[[indsInM[1]]])
)
text <- c("Cluster", m, ": {", popnames[[indsInM[1]]])
for (k in 2:cluster_size) {
text <- c(text, ", ", as.character(popnames[[indsInM[k]]]))
text <- c(text, ", ", popnames[[indsInM[k]]])
}
} else {
text <- c(
"Cluster ", as.character(m), ": {", as.character(indsInM[1])
)
text <- c("Cluster", m, ": {", indsInM[1])
for (k in 2:cluster_size) {
text <- c(text, ",", as.character(indsInM[k]))
text <- c(text, ", ", indsInM[k])
}
}
text <- c(text, "}\n")
while (length(text) > 58) {
# Take one line and display it.
# Take one line (new_line) and display it.
new_line <- takeLine(text, 58)
text <- (length(new_line) + 1):length(text)
if (verbose) cat(new_line)
text <- text[(length(new_line) + 1):length(text)]
if (verbose) cat(new_line, sep = "")
if (fid != -1) {
append(fid, new_line)
append(fid, "\n")
}
if (length(text) > 0) {
text <- c(blanks(length_of_beginning), text)
} else {
if (any(is.na(text))) {
text <- ""
} else {
text <- c(blanks(length_of_beginning), text)
}
}
if (any(text != "")) {
if (verbose) cat(text)
if (verbose) cat(text, sep = "")
if (fid != -1) {
append(fid, text)
append(fid, "\n")
Expand Down Expand Up @@ -169,30 +161,26 @@ writeMixtureInfo <- function(
changesInLogml <- t(LOGDIFF)
for (ind in 1:ninds) {
muutokset <- changesInLogml[, ind]

if (names) {
nimi <- as.character(popnames[ind])
rivi <- c(blanks(maxSize - length(nimi)), nimi, ":\n")
} else {
rivi <- c("\n", blanks(4 - floor(log10(ind))), ownNum2Str(ind), ":\n")
}
for (j in 1:npops) {
rivi <- c(rivi, " ", logml2String(omaRound(muutokset[j])))
rivi <- c(rivi, logml2String(omaRound(muutokset[j]), ""))
}
if (verbose) cat(rivi)
if (verbose) cat(rivi, sep = "")
if (fid != -1) {
append(fid, rivi)
append(fid, "\n")
}
}
if (verbose) cat("\n\nKL-divergence matrix in PHYLIP format:\n")
if (verbose) cat("\n\nKL-divergence matrix in PHYLIP format: ")

dist_mat <- zeros(npops, npops)
if (fid != -1) {
append(fid, " ")
append(fid, " ")
append(fid, "KL-divergence matrix in PHYLIP format:")
append(fid, "\n")
append(fid, " KL-divergence matrix in PHYLIP format: ")
}

COUNTS <- COUNTS[seq_len(nrow(adjprior)), seq_len(ncol(adjprior)), , drop = FALSE]
Expand Down Expand Up @@ -237,9 +225,9 @@ writeMixtureInfo <- function(
for (pop1 in 1:npops) {
rivi <- c("\nCluster_", as.character(pop1), "\n")
for (pop2 in 1:npops) {
rivi <- c(rivi, kldiv2str(dist_mat[pop1, pop2]))
rivi <- c(rivi, kldiv2str(dist_mat[pop1, pop2], 4L))
}
if (verbose) cat(rivi)
if (verbose) cat(rivi, sep = "")
if (fid != -1) {
append(fid, rivi)
append(fid, "\n")
Expand Down
2 changes: 1 addition & 1 deletion man/greedyMix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/logml2String.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f3c570

Please sign in to comment.