Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ RUN apt-get update -y && apt-get install -y \
libpng-dev \
default-jdk \
libtiff-dev \
git \
&& rm -rf /var/lib/apt/lists/*

# rJava configuration
Expand Down
Binary file modified Python/.DS_Store
Binary file not shown.
37 changes: 28 additions & 9 deletions Python/FABgal.ipynb

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion Python/fabgal/calculate_CTF.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from pathlib import Path
import pandas as pd
from re import sub
from skimage import io
from skimage.color import label2rgb
import numpy as np
import logging
from .config import FABgalConfig
from .helpers import choose_threshold
Expand Down Expand Up @@ -39,12 +42,15 @@ def calculate_CTF(cfg: FABgalConfig):

# BiaPy results (prior or newly generated)
if cfg.Biapy_run is not None:
biapyout = Path(cfg.out_path) / f"Results_{cfg.experiment_name}" / cfg.Biapy_run / "BiaPy_output"
biapystatsfile = Path(cfg.out_path) / f"Results_{cfg.experiment_name}" / cfg.Biapy_run / "BiaPy_results.tsv"
else:
biapyout = results_dir / "BiaPy_output"
biapystatsfile = results_dir / "BiaPy_results.tsv"
else:
# Newly generated results for both if is_rerun is false
bgalquantfile = results_dir / "Raw_Bgal_results.tsv"
biapyout = results_dir / "BiaPy_output"
biapystatsfile = results_dir / "BiaPy_results.tsv"

### Check if B-gal and BiaPy results file exist ###
Expand Down Expand Up @@ -122,7 +128,7 @@ def calculate_CTF(cfg: FABgalConfig):
nucleidf = pd.read_table(biapystatsfile)
bgaldf = pd.read_table(bgalquantfile)

# Filter nuclei below area threshold
##### Filter nuclei below area threshold #####
nucleidf_pxarea = pd.merge(nucleidf,bgaldf[['File','PxArea']],how='inner',on='File')

## If cfg.nuclei_thr is None, then choose interactively the nuclei thr
Expand All @@ -133,6 +139,34 @@ def calculate_CTF(cfg: FABgalConfig):
else:
nucleidf_pxarea['nucl_thr_pixel'] = cfg.nuclei_thr / nucleidf_pxarea.PxArea
nucleidf_filt = nucleidf_pxarea[nucleidf_pxarea.area > nucleidf_pxarea.nucl_thr_pixel]
## If cfg.nuclei_thr is None, then choose interactively the nuclei thr
if cfg.nuclei_thr is None:
cfg.nuclei_thr = choose_threshold(nucleidf_pxarea)
nucleidf_pxarea['nucl_thr_pixel'] = cfg.nuclei_thr / nucleidf_pxarea.PxArea
nucleidf_filt = nucleidf_pxarea[nucleidf_pxarea.area > nucleidf_pxarea.nucl_thr_pixel]
else:
nucleidf_pxarea['nucl_thr_pixel'] = cfg.nuclei_thr / nucleidf_pxarea.PxArea
nucleidf_filt = nucleidf_pxarea[nucleidf_pxarea.area > nucleidf_pxarea.nucl_thr_pixel]

## Filter BiaPy mask results ##
original_masks = biapyout / "original_masks"

filtered_masks = results_dir / "filtered_masks"
filtered_masks.mkdir(exist_ok=True)

if cfg.keep_masks:
for inf in original_masks.glob("*.tif"):
img = io.imread(inf)
#### Filter by area ####
label, area = np.unique(img, return_counts = True)
labs_to_remove = label[area < cfg.nuclei_thr / nucleidf_pxarea['PxArea'].median()]
img[np.isin(img,labs_to_remove)] = 0

## Save mask as RGB ##
img = label2rgb(img)*255
img = img.astype(np.uint8)
io.imsave(filtered_masks / f"{inf.stem}_mask.png",img, check_contrast = False)


# Count nuclei per image file
nucleitot = nucleidf_filt['File'].value_counts()
Expand Down
22 changes: 10 additions & 12 deletions Python/fabgal/run_biapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import logging
from biapy import BiaPy
import pandas as pd
from skimage import io
from skimage.color import label2rgb
import numpy as np
import shutil
from .config import FABgalConfig
from .helpers import choose_threshold

#### Log options ####

Expand Down Expand Up @@ -54,7 +52,7 @@ def run_biapy(cfg: FABgalConfig):
## Define Nuclei stats output file
biapystatsfile = results_dir / "BiaPy_results.tsv"

#### Process nuclei stats ####
######## Process nuclei stats ########

# List of csv with filename column
biapy_csv = list(biapyout.glob("*full_stats.csv"))
Expand All @@ -68,23 +66,23 @@ def run_biapy(cfg: FABgalConfig):
# Concatenate dfs
result = pd.concat(dfs, ignore_index=True)
result = result.drop(columns=["conditions"])

# Save concat data
result.to_csv(str(biapystatsfile), sep="\t", encoding="utf-8")

# Delete original csv
for inf in biapy_csv:
inf.unlink()

########### Arrange intermediate files and images ###########
########## Arrange intermediate files and images ###########

#### Process output mask images ####
### Process output mask images ####
masks_out = results_dir / "BiaPy_output" / "original_masks"
masks_out.mkdir(exist_ok=True)

if cfg.keep_masks:
for inf in biapyout.glob("*.tif"):
img = io.imread(inf)
img = label2rgb(img)*255
img = img.astype(np.uint8)
io.imsave(results_dir / "BiaPy_output" / f"{inf.stem}_mask.png",img, check_contrast = False)
inf.unlink()
for file in biapyout.glob("*.tif"):
file.replace(masks_out / file.name)

#### Save subtracted images from BiaPy input (if generated) ####

Expand Down
Loading