Skip to content

Commit 3fc35a1

Browse files
authored
Merge pull request #8 from wanglab-broad/dev
27. fix environment.yaml
2 parents 9c20dbf + 92e13b1 commit 3fc35a1

File tree

10 files changed

+786
-26
lines changed

10 files changed

+786
-26
lines changed

code-base/src/FilterReads.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
numel(barcodes));
2424
fprintf(s);
2525

26-
obj.signal.scores = [score_1];
26+
obj.signal.scores = [obj.signal.scores score_1];
2727

2828
for i=1:numel(end_base)
2929

code-base/src/FilterReadsMultiSegment.m

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737

3838
fprintf('Filtration Statistics:\n');
3939

40+
% filter reads based on codebook
41+
codebook_barcodes = obj.codebook.seqToGene.keys;
42+
barcodes_in_codebook = contains(color_seq, codebook_barcodes);
43+
44+
score_1 = sum(barcodes_in_codebook)/numel(color_seq);
45+
s = sprintf('%f [%d / %d] percent of good reads are in codebook\n',...
46+
sum(barcodes_in_codebook)/numel(color_seq),...
47+
sum(barcodes_in_codebook),...
48+
numel(color_seq));
49+
fprintf(s);
50+
51+
obj.signal.scores = [obj.signal.scores score_1];
52+
4053
for i=1:numel(end_base)
4154

4255
current_end_base_char = end_base_char(i, :);
@@ -50,20 +63,9 @@
5063
current_end_base_char(1),...
5164
current_end_base_char(2));
5265
fprintf(s);
53-
end
5466

55-
% filter reads based on codebook
56-
codebook_barcodes = obj.codebook.seqToGene.keys;
57-
barcodes_in_codebook = contains(color_seq, codebook_barcodes);
58-
59-
score_1 = sum(barcodes_in_codebook)/numel(color_seq);
60-
s = sprintf('%f [%d / %d] percent of good reads are in codebook\n',...
61-
sum(barcodes_in_codebook)/numel(color_seq),...
62-
sum(barcodes_in_codebook),...
63-
numel(color_seq));
64-
fprintf(s);
65-
66-
obj.signal.scores = [score_1];
67+
obj.signal.scores = [obj.signal.scores current_score];
68+
end
6769

6870
obj.signal.goodSpots = obj.signal.allSpots(barcodes_in_codebook, :);
6971
% obj.signal.goodSpots{:, "barcode"} = barcodes(barcodes_in_codebook);

code-base/src/MakeSubtileTable.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
output_table = table([],[],[],[],[],[],[],[],[],[],[],...
99
'VariableNames', column_headers);
10+
1011
sub_order = [];
1112
for i = 0:(sqrt_pieces-1)
1213
for j = 0:(sqrt_pieces-1)
@@ -25,15 +26,15 @@
2526
start_coords_y = tile_idx(2) * tile_size - overlap_half + 1;
2627
end_coords_y = (tile_idx(2) + 1) * tile_size + overlap_half;
2728

28-
%% compensate in edge
29+
% compensate in edge
2930
if tile_idx(1) == 0
3031
start_coords_x = start_coords_x + overlap_half;
3132
end
3233
if tile_idx(2) == 0
3334
start_coords_y = start_coords_y + overlap_half;
3435

3536
end
36-
%% compensate in edge
37+
% compensate in edge
3738
if tile_idx(1) == sqrt_pieces - 1
3839
end_coords_x = dims(1);
3940
end
@@ -46,7 +47,7 @@
4647

4748
dims_t = dims;
4849
dims_t(1:2) = [end_coords_x - start_coords_x + 1,end_coords_y - start_coords_y + 1];
49-
disp([tile_idx,start_coords_x,end_coords_x,start_coords_y,end_coords_y,upper_left(1:2),dims_t(1:2)]);
50+
% disp([tile_idx,start_coords_x,end_coords_x,start_coords_y,end_coords_y,upper_left(1:2),dims_t(1:2)]);
5051
output_table_t = table(t, tile_idx(1), tile_idx(2),...
5152
start_coords_x, start_coords_y, end_coords_x, end_coords_y,...
5253
upper_left(1), upper_left(2), dims_t(1), dims_t(2),...

code-base/src/STARMapDataset.m

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@
7373
obj.images = dictionary();
7474
obj.projections = dictionary();
7575
obj.metadata = dictionary();
76+
obj.subtile = struct();
7677
obj.layers = struct();
7778
obj.registration = dictionary();
7879
obj.signal = struct();
7980
obj.codebook = struct();
8081

82+
obj.subtile.index = 0;
8183
obj.layers.seq = [];
8284
obj.layers.other = [];
8385
obj.layers.ref = [];
@@ -720,12 +722,16 @@
720722
defaultSplitIndex = [5];
721723
addParameter(p, 'split_index', defaultSplitIndex);
722724

725+
defaultSaveScores = false;
726+
addParameter(p, 'save_scores', defaultSaveScores);
727+
723728
parse(p, varargin{:});
724729

725730
fprintf('====Reads Filtration====\n');
726731
end_base = string(p.Results.end_base);
727732

728733
% remove reads with incorrect colors
734+
n_spots = size(obj.signal.allSpots, 1);
729735
no_color_spots = contains(obj.signal.allSpots.color_seq, "N");
730736
multi_color_spots = contains(obj.signal.allSpots.color_seq, "M");
731737
to_keep = ~or(no_color_spots, multi_color_spots);
@@ -734,6 +740,7 @@
734740
fprintf(sprintf('Number of spots were dropped because of multi-max color: %d\n', sum(multi_color_spots)));
735741
fprintf('Comparing with codebook...\n');
736742
fprintf(sprintf('Number of barcode segments: %d\n', p.Results.n_barcode_segments));
743+
obj.signal.scores = [n_spots sum(no_color_spots) sum(multi_color_spots)];
737744

738745
if numel(end_base) > 1
739746
end_base_msg = strjoin(end_base, " or ");
@@ -748,6 +755,23 @@
748755
obj = FilterReadsMultiSegment(obj, end_base, p.Results.split_index);
749756
end
750757

758+
if p.Results.save_scores
759+
current_fname = fullfile(obj.outputPath, "log", "rsf_scores.csv");
760+
current_output_folder_msg = strrep(current_fname, '\', '\\');
761+
fprintf(sprintf('Saving scores to %s\n', current_output_folder_msg));
762+
if obj.subtile.index > 0
763+
scores_to_save = [string(obj.fovID) string(obj.subtile.index) string(obj.signal.scores)];
764+
else
765+
scores_to_save = [string(obj.fovID) string(obj.signal.scores)];
766+
end
767+
768+
if ~exist(current_fname, 'file')
769+
writematrix(scores_to_save, current_fname, 'Delimiter', ',');
770+
else
771+
writematrix(scores_to_save, current_fname, 'Delimiter', ',', 'WriteMode', 'append');
772+
end
773+
end
774+
751775
% change metadata
752776
obj.jobFinished.ReadsFiltration = true;
753777

@@ -847,13 +871,21 @@
847871

848872
switch p.Results.signal_slot
849873
case "goodSpots"
850-
current_fname = fullfile(p.Results.output_path, sprintf("%s_goodSpots.csv", obj.fovID));
874+
if obj.subtile.index > 0
875+
current_fname = fullfile(obj.outputPath, "output", "subtile", obj.fovID, sprintf("subtile_goodSpots_%d.csv", obj.subtile.index));
876+
else
877+
current_fname = fullfile(p.Results.output_path, sprintf("%s_goodSpots.csv", obj.fovID));
878+
end
851879
current_output_folder_msg = strrep(current_fname, '\', '\\');
852880
fprintf(sprintf('Saving goodSpots to %s\n', current_output_folder_msg));
853881
output_table = obj.signal.goodSpots(:, p.Results.field_to_keep);
854882
writetable(output_table, current_fname);
855883
case "allSpots"
856-
current_fname = fullfile(p.Results.output_path, sprintf("%s_allSpots.csv", obj.fovID));
884+
if obj.subtile.index > 0
885+
current_fname = fullfile(obj.outputPath, "output", "subtile", obj.fovID, sprintf("subtile_allSpots_%d.csv", obj.subtile.index));
886+
else
887+
current_fname = fullfile(p.Results.output_path, sprintf("%s_allSpots.csv", obj.fovID));
888+
end
857889
current_output_folder_msg = strrep(current_fname, '\', '\\');
858890
fprintf(sprintf('Saving allSpots to %s\n', current_output_folder_msg));
859891
output_table = obj.signal.allSpots(:, p.Results.field_to_keep);
@@ -879,7 +911,7 @@
879911
defaultSave = false;
880912
addOptional(p, 'save', defaultSave);
881913

882-
defaultOutputFolder = fullfile(obj.outputPath, "temp");
914+
defaultOutputFolder = fullfile(obj.outputPath, "output");
883915
addOptional(p, 'output_path', defaultOutputFolder);
884916

885917
defaultRefLayer = obj.layers.ref;
@@ -891,12 +923,41 @@
891923
mkdir(p.Results.output_path)
892924
end
893925

926+
current_subtile_folder = fullfile(p.Results.output_path, "subtile");
927+
if ~exist(current_subtile_folder, 'dir')
928+
mkdir(current_subtile_folder)
929+
end
930+
894931
current_metadata = obj.metadata{p.Results.ref_layer};
895-
current_metadata = current_metadata{1};
896-
obj.subtile = MakeSubtileTable(current_metadata.dims, p.Results.sqrt_pieces);
932+
obj.subtile.coords = MakeSubtileTable(current_metadata.dims, p.Results.sqrt_pieces);
897933

898934
if p.Results.save
899-
writetable(obj.subtile, fullfile(p.Results.output_path, sprintf("%s_subtile.csv", obj.fovID)), 'Delimiter',',', 'QuoteStrings',false);
935+
current_output_folder = fullfile(current_subtile_folder, obj.fovID);
936+
if ~exist(current_output_folder, 'dir')
937+
mkdir(current_output_folder)
938+
end
939+
% save the coords of subtiles
940+
writetable(obj.subtile.coords, fullfile(current_output_folder, "subtile_coords.csv"), 'Delimiter',',', 'QuoteStrings',false);
941+
942+
% save the object of subtiles
943+
for i=1:size(obj.subtile.coords)
944+
tile_idx = obj.subtile.coords.t(i);
945+
start_coords_x = obj.subtile.coords.scoords_x(i);
946+
start_coords_y = obj.subtile.coords.scoords_y(i);
947+
948+
end_coords_x = obj.subtile.coords.ecoords_x(i);
949+
end_coords_y = obj.subtile.coords.ecoords_y(i);
950+
951+
subtile_data = obj;
952+
for l=1:length(obj.layers.seq)
953+
current_layer = obj.layers.seq{l};
954+
subtile_data.images{current_layer} = subtile_data.images{current_layer}(start_coords_y:end_coords_y, start_coords_x:end_coords_x, :, :);
955+
end
956+
subtile_data.subtile.index = tile_idx;
957+
958+
save(fullfile(current_output_folder, sprintf('subtile_data_%d.mat', tile_idx)), "subtile_data");
959+
end
960+
900961
end
901962

902963
% change metadata

config/environment.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ dependencies:
375375
- tifffile==2023.4.12
376376
- tqdm==4.65.0
377377
- typeguard==4.1.5
378-
- typing-extensions==4.5.0
379378
- umap-learn==0.5.3
380379
- visions==0.7.5
381380
- werkzeug==2.2.3

0 commit comments

Comments
 (0)