Skip to content

Commit

Permalink
Merge pull request #20 from bzhanglab/dev
Browse files Browse the repository at this point in the history
Fix Normalized enrichments scores and repo improvements
  • Loading branch information
iblacksand authored Jan 11, 2024
2 parents 473ec58 + 6474049 commit b7be835
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 15 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Rust
on:
push:
branches: [ "master" ]
tags:
- '*'
pull_request:
branches: [ "master" ]

Expand All @@ -20,3 +22,35 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --all --verbose

release:
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
needs: build
environment: release
permissions:
id-token: write
steps:
- name: Install cargo binstall
uses: cargo-bins/cargo-binstall@main
- name: cargo-release Cache
id: cargo_release_cache
uses: actions/cache@v3
with:
path: ~/.cargo/bin/cargo-release
key: ${{ runner.os }}-cargo-release
- name: Install cargo release
if: steps.cargo_release_cache.outputs.cache-hit != 'true'
run: cargo binstall cargo-release
- name: cargo login
run: cargo login
- name: "cargo release publish"
run: |-
cargo release \
publish \
--workspace \
--all-features \
--allow-branch HEAD \
--no-confirm \
--no-verify \
--execute
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "webgestalt"
version = "0.1.0"
version = "0.1.1"
authors = ["John Elizarraras"]
edition = "2021"
rust-version = "1.63.0"
Expand All @@ -16,7 +16,7 @@ repository = "https://github.com/bzhanglab/webgestalt_rust"
bincode = "1.3.3"
clap = { version = "4.4.15", features = ["derive"] }
owo-colors = { version = "4.0.0", features = ["supports-colors"] }
webgestalt_lib = { path = "webgestalt_lib" }
webgestalt_lib = { version = "0.1.0", path = "webgestalt_lib" }

[profile.release]
opt-level = 3
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ fn main() {
"webgestalt_lib/data/test.rnk".to_owned(),
);
let gmt = webgestalt_lib::readers::read_gmt_file(
"webgestalt_lib/data/test.gmt".to_owned(),
"webgestalt_lib/data/ktest.gmt".to_owned(),
);
let start = Instant::now();
webgestalt_lib::methods::gsea::gsea(
let res = webgestalt_lib::methods::gsea::gsea(
gene_list.unwrap(),
gmt.unwrap(),
GSEAConfig::default(),
Expand All @@ -137,7 +137,7 @@ fn main() {
}
Some(ExampleOptions::Ora) => {
let (gmt, gene_list, reference) = webgestalt_lib::readers::read_ora_files(
"webgestalt_lib/data/test.gmt".to_owned(),
"webgestalt_lib/data/ktest.gmt".to_owned(),
"webgestalt_lib/data/genelist.txt".to_owned(),
"webgestalt_lib/data/reference.txt".to_owned(),
);
Expand Down
2 changes: 1 addition & 1 deletion webgestalt_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webgestalt_lib"
version = "0.1.0"
version = "0.1.1"
authors = ["John Elizarraras"]
edition = "2021"
rust-version = "1.63.0"
Expand Down
23 changes: 19 additions & 4 deletions webgestalt_lib/src/methods/gsea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl RankListItem {
/// Run GSEA for one analyte set.
///
/// Returns a [`GSEAResult`], which does not have FDR.
///
/// # Parameters
///
/// - `analytes` - Vector containing the names of the analytes
Expand Down Expand Up @@ -195,18 +196,32 @@ fn analyte_set_p(
.collect();
let down: Vec<f64> = es_iter // down scores
.par_iter()
.filter(|&x| *x < 0_f64)
.filter(|&x| *x <= 0_f64)
.copied()
.collect();
let up_len = up.len();
let down_len = down.len();
let up_avg: f64 = up.par_iter().sum::<f64>() / (up_len as f64 + 0.000001) + 0.000001; // up average
let down_avg: f64 = down.par_iter().sum::<f64>() / (down_len as f64 + 0.000001) - 0.000001; // down average
let up_avg: f64 = if up.is_empty() {
0.000001
} else {
up.par_iter().sum::<f64>() / (up_len as f64 + 0.000001) + 0.000001
}; // up average
let down_avg: f64 = if down.is_empty() {
-0.000001
} else {
down.par_iter().sum::<f64>() / (down_len as f64 - 0.000001) - 0.000001
}; // down average
let mut nes_es: Vec<f64> = up.par_iter().map(|x| x / up_avg).collect(); // get all normalized scores for up
nes_es.extend(down.par_iter().map(|x| -x / down_avg).collect::<Vec<f64>>()); // extend with down scores
let norm_es: f64 = if real_es >= 0_f64 {
// get normalized score for the real run
real_es / up_avg
if up.is_empty() {
0.0
} else {
real_es / up_avg
}
} else if down.is_empty() {
0.0
} else {
-real_es / down_avg
};
Expand Down
19 changes: 16 additions & 3 deletions webgestalt_lib/src/readers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ use std::{
};
use utils::Item;

/// Read GMT file from specified path. For format description, see [broadinstitute.org](https://software.broadinstitute.org/cancer/software/gsea/wiki/index.php/Data_formats#GMT:_Gene_Matrix_Transposed_file_format_.28.2A.gmt.29)
///
/// # Parameters
///
/// - `path` - A [`String`] of the path of the GMT to read.
///
/// # Panics
///
/// Panics if there is not file at `path`.
///
/// # Returns
///
/// If result is `Ok`, returns a [`Vec<Item>`] containing the elements of the GMT
pub fn read_gmt_file(path: String) -> Result<Vec<Item>, Box<std::io::Error>> {
let file = File::open(path)?;
let mut rdr = csv::ReaderBuilder::new()
Expand All @@ -21,7 +34,7 @@ pub fn read_gmt_file(path: String) -> Result<Vec<Item>, Box<std::io::Error>> {
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
let id = result.get(0).unwrap().to_owned();
let id = result.first().unwrap().to_owned();
let url = result.get(1).unwrap().to_owned();
let parts = result[2..].to_vec();
let item = Item { id, url, parts };
Expand All @@ -44,7 +57,7 @@ pub fn read_rank_file(path: String) -> Result<Vec<RankListItem>, Box<std::io::Er
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
let phenotype = result.get(0).unwrap().to_owned();
let phenotype = result.first().unwrap().to_owned();
let rank = result.get(1).unwrap().to_owned().parse::<f64>().unwrap();
let item = RankListItem {
analyte: phenotype,
Expand Down Expand Up @@ -88,7 +101,7 @@ pub fn read_ora_files(
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
let id = result.get(0).unwrap().to_owned();
let id = result.first().unwrap().to_owned();
let url = result.get(1).unwrap().to_owned();
let parts = result[2..].to_vec();
for analyte in parts.clone().into_iter() {
Expand Down

0 comments on commit b7be835

Please sign in to comment.