Skip to content

Commit

Permalink
replace 1: with seq() or seq_along()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmitchell81 committed Apr 18, 2024
1 parent a6cace2 commit fb72994
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions R/import_fxns.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ create_rl_map_cellphonedb <- function(
}
# Step through the interactions and build rl connections.
rl_map <- NULL
for (i in 1:nrow(interactions)) {
for (i in seq(nrow(interactions))) {

This comment has been minimized.

Copy link
@snag-gh

snag-gh Apr 23, 2024

Collaborator

I would recommend using seq_len rather than seq. the behavior of seq_len when the argument is 0 is more what you would expect.

seq_len(0)
integer(0)
seq(0)
[1] 1 0

inter <- interactions[i, ]
partner_a <- inter[["partner_a"]]
partner_b <- inter[["partner_b"]]
Expand Down Expand Up @@ -338,7 +338,7 @@ create_domino <- function(
}
# Get genes for receptors
rl_reading <- NULL
for (i in 1:nrow(rl_map)) {
for (i in seq(nrow(rl_map))) {
rl <- list()
inter <- rl_map[i, ]
p <- ifelse(inter[["type_A"]] == "R", "A", "B")
Expand Down Expand Up @@ -366,7 +366,7 @@ create_domino <- function(
dom@linkages$complexes <- NULL
if (use_complexes) {
complex_list <- list()
for (i in 1:nrow(rl_reading)) {
for (i in seq(nrow(rl_reading))) {
inter <- rl_reading[i, ]
if (grepl("\\,", inter[["L.gene"]])) {
complex_list[[inter[["L.name"]]]] <- unlist(strsplit(inter[["L.gene"]], split = "\\,"))
Expand Down Expand Up @@ -502,7 +502,7 @@ create_domino <- function(
dom@misc$rec_cor <- rho
# assess correlation among genes in the same receptor complex
cor_list <- list()
for (i in 1:length(names(dom@linkages$rec_lig))) {
for (i in seq_along(names(dom@linkages$rec_lig))) {
r <- names(dom@linkages$rec_lig)[i]
if (r %in% names(dom@linkages$complexes)) {
r_genes <- dom@linkages$complexes[[r]]
Expand Down Expand Up @@ -623,7 +623,7 @@ add_rl_column <- function(map, map_ref, conv, new_name) {
not_in_ref_map <- c()
}
new_map <- c()
for (r_id in 1:nrow(map)) {
for (r_id in seq(nrow(map))) {
row <- map[r_id, ]
conv_ids <- which(conv[, 1] == row[[map_ref]])
for (id in conv_ids) {
Expand Down
14 changes: 7 additions & 7 deletions R/plot_fxns.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ signaling_network <- function(
}
# Get vert angle for labeling circos plot
if (layout == "circle") {
v_angles <- 1:length(igraph::V(graph))
v_angles <- seq(length(igraph::V(graph)))
v_angles <- -2 * pi * (v_angles - 1) / length(v_angles)
igraph::V(graph)$label.degree <- v_angles
}
Expand Down Expand Up @@ -465,9 +465,9 @@ gene_network <- function(dom, clust = NULL, OutgoingSignalingClust = NULL,
l[all_ligs, 1] <- -0.75
l[all_recs, 1] <- 0
l[all_tfs, 1] <- 0.75
l[all_ligs, 2] <- (1:length(all_ligs) / mean(1:length(all_ligs)) - 1) * 2
l[all_recs, 2] <- (1:length(all_recs) / mean(1:length(all_recs)) - 1) * 2
l[all_tfs, 2] <- (1:length(all_tfs) / mean(1:length(all_tfs)) - 1) * 2
l[all_ligs, 2] <- (seq_along(all_ligs) / mean(seq_along(all_ligs)) - 1) * 2
l[all_recs, 2] <- (seq_along(all_recs) / mean(seq_along(all_recs)) - 1) * 2
l[all_tfs, 2] <- (seq_along(all_tfs) / mean(seq_along(all_tfs)) - 1) * 2
rownames(l) <- c()
} else if (layout == "random") {
l <- igraph::layout_randomly(graph)
Expand Down Expand Up @@ -814,7 +814,7 @@ circos_ligand_receptor <- function(
names(cell_colors) <- cell_idents
}
grid_col <- c("#FFFFFF") # hide the arc corresponding to the receptor by coloring white
for (i in 1:length(ligands)) {
for (i in seq_along(ligands)) {
grid_col <- c(grid_col, rep(lig_colors[i], length(cell_idents)))
}
names(grid_col) <- c(receptor, signaling_df$origin)
Expand Down Expand Up @@ -955,7 +955,7 @@ plot_differential_linkages <- function(
group_palette <- ggplot_col_gen(length(g_names))
names(group_palette) <- g_names
}
for (i in 1:length(g_names)) {
for (i in seq_along(g_names)) {
g <- g_names[i]
g_count <- paste0(g, "_count")
g_n <- paste0(g, "_n")
Expand Down Expand Up @@ -1003,5 +1003,5 @@ do_norm <- function(mat, dir) {
#'
ggplot_col_gen <- function(n) {
hues <- seq(15, 375, length = n + 1)
return(grDevices::hcl(h = hues, l = 65, c = 100)[1:n])
return(grDevices::hcl(h = hues, l = 65, c = 100)[seq(n)])
}
8 changes: 4 additions & 4 deletions R/processing_fxns.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ build_domino <- function(
fcs <- c(fcs, fc)
}
names(fcs) <- zeros
sorted <- sort(fcs, decreasing = TRUE)[1:max_tf_per_clust]
sorted <- sort(fcs, decreasing = TRUE)[seq(max_tf_per_clust)]
} else {
sorted <- ordered[which(ordered < min_tf_pval)]
}
if (length(sorted) > max_tf_per_clust) {
sorted <- sorted[1:max_tf_per_clust]
sorted <- sorted[seq(max_tf_per_clust)]
}
clust_tf[[clust]] <- names(sorted)
}
Expand All @@ -62,7 +62,7 @@ build_domino <- function(
ordered <- sort(dom@cor[, tf], decreasing = TRUE)
filtered <- ordered[which(ordered > rec_tf_cor_threshold)]
if (length(filtered) > max_rec_per_tf) {
top_receptors <- names(filtered)[1:max_rec_per_tf]
top_receptors <- names(filtered)[seq(max_rec_per_tf)]
} else {
top_receptors <- names(filtered)
}
Expand Down Expand Up @@ -192,7 +192,7 @@ build_domino <- function(
ordered <- sort(dom@cor[, tf], decreasing = TRUE)
filtered <- ordered[which(ordered > rec_tf_cor_threshold)]
if (length(filtered) > max_rec_per_tf) {
top_receptors <- names(filtered)[1:max_rec_per_tf]
top_receptors <- names(filtered)[seq(max_rec_per_tf)]
} else {
top_receptors <- names(filtered)
}
Expand Down

1 comment on commit fb72994

@snag-gh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use seq_along and seq_len mostly. Aside from my thoughts on seq vs seq_len, I think the changes look good

Please sign in to comment.