-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathcallbarcodes.py
More file actions
914 lines (765 loc) · 35.4 KB
/
callbarcodes.py
File metadata and controls
914 lines (765 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#################################
#
# Imports from useful Python libraries
#
#################################
import csv
import numpy
import os
import re
import urllib.request, urllib.error, urllib.parse
try:
from io import StringIO
except ImportError:
from io import StringIO
#################################
#
# Imports from CellProfiler
#
##################################
from cellprofiler.modules import _help
import cellprofiler_core.image
import cellprofiler_core.module
import cellprofiler_core.measurement
import cellprofiler_core.object
import cellprofiler_core.setting
import cellprofiler_core.constants.setting
import cellprofiler_core.setting.text
import cellprofiler_core.setting.choice
import cellprofiler_core.setting.subscriber
import cellprofiler_core.utilities.image
import cellprofiler_core.preferences
import cellprofiler_core.constants.measurement
__doc__ = """\
CallBarcodes
============
**CallBarcodes** is used for assigning a barcode to an object based on the channel with the strongest intensity for a given number of cycles.
It is used for optical sequencing by synthesis (SBS).
What do I need as input?
^^^^^^^^^^^^^^^^^^^^^^^^
You need to input a .csv file that contains at least two columns.
One column contains the known barcodes that you will be matching against.
One column contains the corresponding gene/transcript names.
All other columns in the .csv will be ignored.
Before running this module in your pipeline, you need to identify the objects in which you will be calling your barcodes and you will need to have measured the intensities of each object in four channels corresponding to nucleotides A,C,T, and G.
Your images for each nucleotide channel per cycle must be named in a particular way for this module to work as intended. For example, 'Cycle01_A_BackSub' is a valid name. Begin with 'Cycle' then the cycle number '01, 02...', then the nucleotide and any other information can follow.
If the background intensities of your four channels are not very well matched, you might want to run the **CompensateColors** module before measuring the object intensities.
What do I get as output?
^^^^^^^^^^^^^^^^^^^^^^^^
Image- and object-level measurements about barcode calling and score. Optionally, images of objects color-coded by barcode call or matching score.
Measurements made by this module
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Within the InputObject.csv, this module outputs the following measurements:
- **BarcodeCalled** is the n-cycle string of the barcode sequence that was read by the module
- **MatchedTo_Barcode** is the known barcode that the module best matched to the called barcode
- **MatchedTo_ID** is an ID number assigned to each known barcode
- **MatchedTo_GeneCode** is the known gene/transcript name that corresponds to the known barcode
- **MatchedTo_Score** is the quality of the called barcode to known barcode match, reported as (matching nucleotides)/(total nucleotides) where 1 is a perfect match
Note that CellProfiler cannot create a per-parent mean measurement of a string.
References
^^^^^^^^^^
Optical Pooled Screens in Human Cells.
Feldman D, Singh A, Schmid-Burgk JL, Carlson RJ, Mezger A, Garrity AJ, Zhang F, Blainey PC.
Cell. 2019 Oct 17;179(3):787-799.e17. doi: 10.1016/j.cell.2019.09.016.
|
============ ============ ===============
Supports 2D? Supports 3D? Respects masks?
============ ============ ===============
YES YES YES
============ ============ ===============
"""
C_CALL_BARCODES = "Barcode"
ENCODING_TYPES = ["One Hot Exponentially Multiplexed (ie 4-color SBS/ISS)", "Exponentially Multiplexed (SBS/ISS only)"]
BASE_MEASUREMENT_DESCRIPTION = "Select the {YYY} measurement indicating that {ZZZ} is encoded"
BASE_BOOLEAN_DESCRIPTION = "Does a positive value here mean the base SHOULD be called {ZZZ}?"
BASE_BOOLEAN_LONG_DESCRIPTION = "Select *Yes* if this measurement is *inclusive* (ie if this measurement is above zero, this is all or part of the information that indicates the base SHOULD be called); Select *No* if this measurement is *exclusive* (if this measurement is above zero, this is all or part of the information that indicates the base SHOULD NOT be called)"
# The number of settings per metric
METRIC_SETTING_COUNT = 3
FIXED_SETTING_COUNT = 15
class CallBarcodes(cellprofiler_core.module.Module):
module_name = "CallBarcodes"
category = "Data Tools"
variable_revision_number = 3
def create_settings(self):
self.csv_directory = cellprofiler_core.setting.text.Directory(
"Input data file location",
allow_metadata=False,
doc="""\
Select the folder containing the CSV file to be loaded. {IO_FOLDER_CHOICE_HELP_TEXT}
""".format(
**{"IO_FOLDER_CHOICE_HELP_TEXT": _help.IO_FOLDER_CHOICE_HELP_TEXT}
),
)
def get_directory_fn():
"""Get the directory for the CSV file name"""
return self.csv_directory.get_absolute_path()
def set_directory_fn(path):
dir_choice, custom_path = self.csv_directory.get_parts_from_path(path)
self.csv_directory.join_parts(dir_choice, custom_path)
self.csv_file_name = cellprofiler_core.setting.text.Filename(
"Name of the file",
"None",
doc="""Provide the file name of the CSV file containing the data you want to load.""",
get_directory_fn=get_directory_fn,
set_directory_fn=set_directory_fn,
browse_msg="Choose CSV file",
exts=[("Data file (*.csv)", "*.csv"), ("All files (*.*)", "*.*")],
)
self.input_object_name = cellprofiler_core.setting.subscriber.LabelSubscriber(
text="Input object name",
doc="These are the objects that the module operates on.",
)
self.ncycles = cellprofiler_core.setting.text.Integer(
doc="""\
Enter the number of cycles present in the data.
""",
text="Number of cycles",
value=8,
)
self.cycle1measure = cellprofiler_core.setting.Measurement(
"Select one of the measures from Cycle 1 to use for calling",
self.input_object_name.get_value,
"AreaShape_Area",
doc="""\
This measurement should be an intensity measure that is measured for every cycle and is of a category that distinguishes different nucleotides. If named correctly (see main module input help), this module will gather the appropriate measurement for all nucleotides. For example, choosing the maximum intensity in the channel for nucleotide A will also use max intensity for C, T, G.""",
)
self.metadata_field_barcode = cellprofiler_core.setting.choice.Choice(
"Select the column containing barcodes to match against",
["No CSV file"],
choices_fn=self.get_choices,
doc="""\Select the column containing barcodes to match against.
""",
)
self.metadata_field_tag = cellprofiler_core.setting.choice.Choice(
"Select the column containing gene/transcript barcode names",
["No CSV file"],
choices_fn=self.get_choices,
doc="""\Select the column containing gene/transcript barcode names.
""",
)
self.wants_call_image = cellprofiler_core.setting.Binary(
"Retain an image of the barcodes color coded by call?",
False,
doc="""\
Select "*{YES}*" to retain the image of the objects color-coded
according to which line of the CSV their barcode call matches to,
for use later in the pipeline (for example, to be saved by a **SaveImages**
module).""".format(
**{"YES": "Yes"}
),
)
self.outimage_calls_name = cellprofiler_core.setting.text.ImageName(
"Enter the called barcode image name",
"None",
doc="""\
*(Used only if the called barcode image is to be retained for later use in the pipeline)*
Enter the name to be given to the called barcode image.""",
)
self.wants_score_image = cellprofiler_core.setting.Binary(
"Retain an image of the barcodes color coded by score match?",
False,
doc="""\
Select "*{YES}*" to retain the image of the objects where the intensity of the spot matches
indicates the match score between the called barcode and its closest match,
for use later in the pipeline (for example, to be saved by a **SaveImages**
module).""".format(
**{"YES": "Yes"}
),
)
self.outimage_score_name = cellprofiler_core.setting.text.ImageName(
"Enter the barcode score image name",
"None",
doc="""\
*(Used only if the barcode score image is to be retained for later use in the pipeline)*
Enter the name to be given to the barcode score image.""",
)
self.do_library_match = cellprofiler_core.setting.Binary(
"Do you want to match to an external library of barcodes?",
True,
doc="""\
Select "*{YES}*" to match the result read by CellProfiler to a provided barcode list.
Select "*{NO}*") to simply call bases, and do the matching downstream.""".format(
**{"YES": "Yes", "NO":"No"}
),
)
self.has_empty_vector_barcode = cellprofiler_core.setting.Binary(
"Do you have an empty vector barcode you would like to add to the barcode list?",
False,
doc="""\
Select "*{YES}*" to manually enter a sequence that should be added to the uploaded barcode
list with the gene name of "EmptyVector". This can be helpful when there is a consistent
backbone sequence to look out for in every barcoding set).""".format(
**{"YES": "Yes"}
),
)
self.empty_vector_barcode_sequence = cellprofiler_core.setting.text.Text(
"What is the empty vector sequence?",
"AAAAAAAAAAAAAAA",
doc="""\
Enter the sequence that represents barcoding reads of an empty vector""",
)
self.n_colors = cellprofiler_core.setting.choice.Choice(
"What kind of encoding is used here?",
value=ENCODING_TYPES[0],
choices=ENCODING_TYPES,
doc="""\
Select "*{4COLOR}*" if using a a code where each thing has its own single-letter code
and its own channel (such as in SBS 4 color chemistry kit where each channel has its own image).
For 2- or 3- color kits SBS/ISS kits, select "*{SBS_OTHER}*" and you will be directed to explain your channel mapping.
No other channel formats are available at this time, though you are free to open a GitHub issue or pull request.""".format(
**{"4COLOR": ENCODING_TYPES[0],"SBS_OTHER":ENCODING_TYPES[1]}
),
)
self.base_measurements = {"A":[], "C":[], "G":[], "T":[]}
self.add_measurement(base="A", removable=False)
self.add_button_a = cellprofiler_core.setting.do_something.DoSomething("", "Add another measurement for calling A", self.add_measurement,"A")
self.divider_1 = cellprofiler_core.setting.Divider(line=True)
self.add_measurement(base="C", removable=False)
self.add_button_c = cellprofiler_core.setting.do_something.DoSomething("", "Add another measurement for calling C", self.add_measurement,"C")
self.divider_2 = cellprofiler_core.setting.Divider(line=True)
self.add_measurement(base="G", removable=False)
self.add_button_g = cellprofiler_core.setting.do_something.DoSomething("", "Add another measurement for calling G", self.add_measurement,"G")
self.divider_3 = cellprofiler_core.setting.Divider(line=True)
self.add_measurement(base="T", removable=False)
self.add_button_t = cellprofiler_core.setting.do_something.DoSomething("", "Add another measurement for calling T", self.add_measurement,"T")
def add_measurement(self, base, removable=True):
group = cellprofiler_core.setting.SettingsGroup()
group.removable = removable
group.append("base",
cellprofiler_core.setting.choice.Choice(
'Base we are currently calling',
value = base,
choices= [base],
doc="Base we are currently calling"
)
)
if removable:
YYY="next"
else:
YYY="first"
group.append(
"measurement_name",
cellprofiler_core.setting.Measurement(
BASE_MEASUREMENT_DESCRIPTION.format(
**{"YYY":YYY,"ZZZ": base}
),
self.input_object_name.get_value,
"AreaShape_Area",
doc=BASE_MEASUREMENT_DESCRIPTION.format(
**{"YYY":YYY,"ZZZ": base}
),
)
)
group.append(
"base_boolean",
cellprofiler_core.setting.Binary(
BASE_BOOLEAN_DESCRIPTION.format(**{"ZZZ":base}),
True,
doc = BASE_BOOLEAN_LONG_DESCRIPTION
)
)
if removable:
group.append(
"remover",
cellprofiler_core.setting.do_something.RemoveSettingButton("", "Remove this image", self.base_measurements[base], group),
)
self.base_measurements[base].append(group)
def prepare_settings(self, setting_values):
value_count = len(setting_values)
assert (value_count - FIXED_SETTING_COUNT) % METRIC_SETTING_COUNT == 0
bases_encountered = []
for x in range(FIXED_SETTING_COUNT,value_count,METRIC_SETTING_COUNT):
if setting_values[x] not in bases_encountered:
#don't add an "extra" setting for the first one of each base, added in create_settings
bases_encountered.append(setting_values[x])
else:
self.add_measurement(base=setting_values[x])
def settings(self):
result = [
self.ncycles,
self.input_object_name,
self.cycle1measure,
self.csv_directory,
self.csv_file_name,
self.metadata_field_barcode,
self.metadata_field_tag,
self.wants_call_image,
self.outimage_calls_name,
self.wants_score_image,
self.outimage_score_name,
self.has_empty_vector_barcode,
self.empty_vector_barcode_sequence,
self.n_colors,
self.do_library_match,
]
for eachbase in self.base_measurements.values():
for measurement in eachbase:
result += [
measurement.base,
measurement.measurement_name,
measurement.base_boolean,
]
return result
def visible_settings(self):
result = [
self.n_colors,
self.ncycles,
self.input_object_name]
if self.n_colors.value == ENCODING_TYPES[0]:
result += [
self.cycle1measure
]
else:
add_buttons = {"A":[self.add_button_a, self.divider_1], "C":[self.add_button_c, self.divider_2],
"G":[self.add_button_g, self.divider_3], "T":[self.add_button_t]}
for base in self.base_measurements.keys():
for base_meas in self.base_measurements[base]:
result += [
base_meas.measurement_name,
base_meas.base_boolean
]
if base_meas.removable:
result += [base_meas.remover]
result += add_buttons[base]
if self.do_library_match:
result += [
self.csv_directory,
self.csv_file_name,
self.metadata_field_barcode,
self.metadata_field_tag,
self.has_empty_vector_barcode
]
if self.has_empty_vector_barcode:
result += [self.empty_vector_barcode_sequence]
result += [
self.wants_call_image,
self.wants_score_image,
]
if self.wants_call_image:
result += [self.outimage_calls_name]
if self.wants_score_image:
result += [self.outimage_score_name]
return result
def validate_module(self, pipeline):
csv_path = self.csv_path
if not os.path.isfile(csv_path):
raise cellprofiler_core.setting.ValidationError(
"No such CSV file: %s" % csv_path, self.csv_file_name
)
try:
self.open_csv()
except IOError as e:
import errno
if e.errno == errno.EWOULDBLOCK:
raise cellprofiler_core.setting.ValidationError(
"Another program (Excel?) is locking the CSV file %s."
% self.csv_path,
self.csv_file_name,
)
else:
raise cellprofiler_core.setting.ValidationError(
"Could not open CSV file %s (error: %s)" % (self.csv_path, e),
self.csv_file_name,
)
try:
self.get_header()
except Exception as e:
raise cellprofiler_core.setting.ValidationError(
"The CSV file, %s, is not in the proper format."
" See this module's help for details on CSV format. (error: %s)"
% (self.csv_path, e),
self.csv_file_name,
)
@property
def csv_path(self):
"""The path and file name of the CSV file to be loaded"""
path = self.csv_directory.get_absolute_path()
return os.path.join(path, self.csv_file_name.value)
def open_csv(self, do_not_cache=False):
"""Open the csv file or URL, returning a file descriptor"""
if cellprofiler_core.preferences.is_url_path(self.csv_path):
if self.csv_path not in self.header_cache:
self.header_cache[self.csv_path] = {}
entry = self.header_cache[self.csv_path]
if "URLEXCEPTION" in entry:
raise entry["URLEXCEPTION"]
if "URLDATA" in entry:
fd = StringIO(entry["URLDATA"])
else:
if do_not_cache:
raise RuntimeError("Need to fetch URL manually.")
try:
url = cellprofiler_core.utilities.image.generate_presigned_url(
self.csv_path
)
url_fd = urllib.request.urlopen(url)
except Exception as e:
entry["URLEXCEPTION"] = e
raise e
fd = StringIO()
while True:
text = url_fd.read()
if len(text) == 0:
break
fd.write(text)
fd.seek(0)
entry["URLDATA"] = fd.getvalue()
return fd
else:
return open(self.csv_path, "r")
def get_header(self, do_not_cache=False):
"""Read the header fields from the csv file
Open the csv file indicated by the settings and read the fields
of its first line. These should be the measurement columns.
"""
with open(self.csv_path, "r") as fp:
reader = csv.DictReader(fp)
return reader.fieldnames
def get_choices(self, pipeline):
choices = self.get_header()
if not choices:
choices = ["No CSV file"]
return choices
def run(self, workspace):
measurements = workspace.measurements
listofmeasurements = measurements.get_feature_names(
self.input_object_name.value
)
objectcount = len(
measurements.get_current_measurement(
self.input_object_name.value, listofmeasurements[0]
)
)
if self.n_colors.value == ENCODING_TYPES[0]:
measurements_for_calls = self.getallonehotbarcodemeasurements(
listofmeasurements, self.ncycles.value, self.cycle1measure.value
)
calledbarcodes, quality_scores = self.callonehotbarcode(
measurements_for_calls,
measurements,
self.input_object_name.value,
self.ncycles.value,
objectcount,
)
workspace.measurements.add_measurement(
self.input_object_name.value,
"_".join([C_CALL_BARCODES, "MeanQualityScore"]),
quality_scores,
)
imagemeanquality = numpy.mean(quality_scores)
workspace.measurements.add_measurement(
"Image", "_".join([C_CALL_BARCODES, "MeanQualityScore"]), imagemeanquality
)
else:
calledbarcodes = self.calloneexpISSbarcode(
self.base_measurements,
measurements,
self.input_object_name.value,
self.ncycles.value,
objectcount)
workspace.measurements.add_measurement(
self.input_object_name.value,
"_".join([C_CALL_BARCODES, "BarcodeCalled"]),
calledbarcodes,
)
if self.do_library_match.value:
barcodes = self.barcodeset(
self.metadata_field_barcode.value, self.metadata_field_tag.value
)
cropped_barcode_dict = {
y[: self.ncycles.value]: y for y in list(barcodes.keys())
}
scorelist = []
matchedbarcode = []
matchedbarcodecode = []
matchedbarcodeid = []
if self.wants_call_image or self.wants_score_image:
objects = workspace.object_set.get_objects(self.input_object_name.value)
labels = objects.segmented
pixel_data_call = objects.segmented
pixel_data_score = objects.segmented
count = 1
for eachbarcode in calledbarcodes:
eachscore, eachmatch = self.queryall(cropped_barcode_dict, eachbarcode)
scorelist.append(eachscore)
matchedbarcode.append(eachmatch)
m_id, m_code = barcodes[eachmatch]
matchedbarcodeid.append(m_id)
matchedbarcodecode.append(m_code)
if self.wants_call_image:
pixel_data_call = numpy.where(
labels == count, barcodes[eachmatch][0], pixel_data_call
)
if self.wants_score_image:
pixel_data_score = numpy.where(
labels == count, 65535 * eachscore, pixel_data_score
)
count += 1
imagemeanscore = numpy.mean(scorelist)
workspace.measurements.add_measurement(
"Image", "_".join([C_CALL_BARCODES, "MeanBarcodeScore"]), imagemeanscore
)
workspace.measurements.add_measurement(
self.input_object_name.value,
"_".join([C_CALL_BARCODES, "MatchedTo_Barcode"]),
matchedbarcode,
)
workspace.measurements.add_measurement(
self.input_object_name.value,
"_".join([C_CALL_BARCODES, "MatchedTo_ID"]),
matchedbarcodeid,
)
workspace.measurements.add_measurement(
self.input_object_name.value,
"_".join([C_CALL_BARCODES, "MatchedTo_GeneCode"]),
matchedbarcodecode,
)
workspace.measurements.add_measurement(
self.input_object_name.value,
"_".join([C_CALL_BARCODES, "MatchedTo_Score"]),
scorelist,
)
if self.wants_call_image:
workspace.image_set.add(
self.outimage_calls_name.value,
cellprofiler_core.image.Image(
pixel_data_call.astype("uint16"), convert=False
),
)
if self.wants_score_image:
workspace.image_set.add(
self.outimage_score_name.value,
cellprofiler_core.image.Image(
pixel_data_score.astype("uint16"), convert=False
),
)
if self.show_window:
workspace.display_data.col_labels = (
"Image Mean Score",
"Image Mean Quality Score",
)
workspace.display_data.statistics = [imagemeanscore, imagemeanquality]
else:
if self.show_window:
workspace.display_data.col_labels = (
"Number barcodes",
"Number of unique calls",
)
workspace.display_data.statistics = [len(calledbarcodes), len(list(set([calledbarcodes])))]
def display(self, workspace, figure):
statistics = workspace.display_data.statistics
figure.set_subplots((1, 1))
figure.subplot_table(0, 0, statistics)
def calloneexpISSbarcode(self, calling_setting_dict, measurements,
object_name, ncycles,objectcount):
def call_by_column(base_array):
base_order = ['A','C','G','T']
if sum(base_array) != 1:
return 'X'
else:
return base_order[base_array.argmax()]
call_bool_dict = {}
for base in calling_setting_dict.keys():
base_list = calling_setting_dict[base]
call_bool_dict[base]={}
for eachmeas in base_list:
all_cycle_measurements = self.getallcyclebarcodemeasurements(measurements,ncycles,eachmeas.measurement_name.value, object_name)
if list(all_cycle_measurements.keys()) != list(range(1,ncycles+1)):
raise RuntimeError(f"CellProfiler could not find all the cycle measurements required which should match {eachmeas.measurement_name}. Please check that these measurements exist and are named properly.")
if eachmeas.base_boolean.value not in call_bool_dict[base].keys():
call_bool_dict[base][eachmeas.base_boolean.value] = [all_cycle_measurements]
else:
call_bool_dict[base][eachmeas.base_boolean.value] += [all_cycle_measurements]
full_base_array = numpy.zeros(objectcount,dtype="str")
base_list = ["A", "C", "G", "T"]
for cycle in range(1,ncycles+1):
which_base_array = numpy.zeros([objectcount,4])
for base_order in range (4):
this_base_array = numpy.ones(objectcount)
base = call_bool_dict[base_list[base_order]]
if True in base.keys():
for eachmeas in base[True]:
this_base_array = this_base_array * (measurements.get_current_measurement(object_name,eachmeas[cycle]) > 0)
if False in base.keys():
for eachmeas in base[False]:
this_base_array = this_base_array * ~(measurements.get_current_measurement(object_name,eachmeas[cycle]) > 0)
which_base_array[:,base_order] = this_base_array
full_base_array = numpy.char.add(full_base_array,numpy.apply_along_axis(call_by_column,1,which_base_array))
return list(full_base_array)
def getallcyclebarcodemeasurements(self, measurements, ncycles, examplemeas, object_name):
measurementdict = {}
obj_measurement_columns = [x[1] for x in measurements.get_measurement_columns() if x[0]=='Foci']
cycle_string = re.search("Cycle.*[0-9]{1,2}",examplemeas).group()
for cycle in range(1,ncycles+1):
if cycle <10:
updated_cycle_string_with_pad = re.sub("[0-9]{1,2}",f"{cycle:02d}",cycle_string)
updated_full_measurement = examplemeas.replace(cycle_string,updated_cycle_string_with_pad)
if updated_full_measurement in obj_measurement_columns:
measurementdict[cycle] = updated_full_measurement
updated_cycle_string_no_pad = re.sub("[0-9]{1,2}",f"{cycle}",cycle_string)
updated_full_measurement = examplemeas.replace(cycle_string,updated_cycle_string_no_pad)
if updated_full_measurement in obj_measurement_columns:
measurementdict[cycle] = updated_full_measurement
return measurementdict
def getallonehotbarcodemeasurements(self, measurements, ncycles, examplemeas):
stem = re.split("Cycle", examplemeas)[0]
measurementdict = {}
for eachmeas in measurements:
if stem in eachmeas:
to_parse = re.split("Cycle", eachmeas)[1]
find_cycle = re.search("[0-9]{1,2}", to_parse)
parsed_cycle = int(find_cycle.group(0))
find_base = re.search("[A-Z]", to_parse)
parsed_base = find_base.group(0)
if parsed_cycle <= ncycles:
if parsed_cycle not in list(measurementdict.keys()):
measurementdict[parsed_cycle] = {eachmeas: parsed_base}
else:
measurementdict[parsed_cycle].update({eachmeas: parsed_base})
return measurementdict
def callonehotbarcode(
self, measurementdict, measurements, object_name, ncycles, objectcount
):
master_cycles = []
score_array = numpy.zeros([ncycles, objectcount])
for eachcycle in range(1, ncycles + 1):
cycles_measures_perobj = []
cyclecode = []
cycledict = measurementdict[eachcycle]
cyclemeasures = list(cycledict.keys())
for eachmeasure in cyclemeasures:
cycles_measures_perobj.append(
measurements.get_current_measurement(object_name, eachmeasure)
)
cyclecode.append(measurementdict[eachcycle][eachmeasure])
cycle_measures_perobj = numpy.transpose(numpy.array(cycles_measures_perobj))
argmax_per_obj = numpy.argmax(cycle_measures_perobj, 1)
max_per_obj = numpy.max(cycle_measures_perobj, 1)
sum_per_obj = numpy.sum(cycle_measures_perobj, 1)
score_per_obj = max_per_obj / sum_per_obj
argmax_per_obj = list(argmax_per_obj)
argmax_per_obj = [cyclecode[x] for x in argmax_per_obj]
master_cycles.append(list(argmax_per_obj))
score_array[eachcycle - 1] = score_per_obj
mean_per_object = score_array.mean(axis=0)
return list(map("".join, zip(*master_cycles))), mean_per_object
def barcodeset(self, barcodecol, genecol):
fd = self.open_csv()
reader = csv.DictReader(fd)
barcodeset = {}
count = 1
for row in reader:
if len(row[barcodecol]) != 0:
barcodeset[row[barcodecol]] = (count, row[genecol])
count += 1
fd.close()
if self.has_empty_vector_barcode:
barcodeset[self.empty_vector_barcode_sequence.value] = (
count,
"EmptyVector",
)
return barcodeset
def queryall(self, cropped_barcode_dict, query):
cropped_barcode_list = list(cropped_barcode_dict.keys())
if query in cropped_barcode_list:
# is a perfect match
return 1, cropped_barcode_dict[query]
else:
scoredict = {
sum([1 for x in range(len(query)) if query[x] == y[x]])
/ float(len(query)): y
for y in cropped_barcode_list
}
scores = list(scoredict.keys())
scores.sort(reverse=True)
return scores[0], cropped_barcode_dict[scoredict[scores[0]]]
def get_measurement_columns(self, pipeline):
input_object_name = self.input_object_name.value
result = [
(
input_object_name,
"_".join([C_CALL_BARCODES, "BarcodeCalled"]),
cellprofiler_core.constants.measurement.COLTYPE_VARCHAR,
),
]
if self.do_library_match.value:
result += [
(
"Image",
"_".join([C_CALL_BARCODES, "MeanBarcodeScore"]),
cellprofiler_core.constants.measurement.COLTYPE_FLOAT,
),
(
"Image",
"_".join([C_CALL_BARCODES, "MeanQualityScore"]),
cellprofiler_core.constants.measurement.COLTYPE_FLOAT,
),
]
result += [
(
input_object_name,
"_".join([C_CALL_BARCODES, "MatchedTo_Barcode"]),
cellprofiler_core.constants.measurement.COLTYPE_VARCHAR,
),
(
input_object_name,
"_".join([C_CALL_BARCODES, "MatchedTo_ID"]),
cellprofiler_core.constants.measurement.COLTYPE_INTEGER,
),
(
input_object_name,
"_".join([C_CALL_BARCODES, "MatchedTo_GeneCode"]),
cellprofiler_core.constants.measurement.COLTYPE_VARCHAR,
),
(
input_object_name,
"_".join([C_CALL_BARCODES, "MatchedTo_Score"]),
cellprofiler_core.constants.measurement.COLTYPE_FLOAT,
),
]
if self.n_colors.value == ENCODING_TYPES[0]:
result += [
(
input_object_name,
"_".join([C_CALL_BARCODES, "MeanQualityScore"]),
cellprofiler_core.constants.measurement.COLTYPE_FLOAT,
),
]
return result
def get_categories(self, pipeline, object_name):
if object_name == self.input_object_name or object_name == "Image":
return [C_CALL_BARCODES]
return []
def get_measurements(self, pipeline, object_name, category):
if object_name == self.input_object_name and category == C_CALL_BARCODES:
result = [
"BarcodeCalled",
]
if self.do_library_match.value:
result += [
"MatchedTo_Barcode",
"MatchedTo_ID",
"MatchedTo_GeneCode",
"MatchedTo_Score",
]
if self.n_colors.value == ENCODING_TYPES[0]:
result.append("MeanQualityScore")
return result
elif object_name == object_name == "Image":
if self.do_library_match.value:
return [
"MeanBarcodeScore",
"MeanQualityScore",
]
return []
def upgrade_settings(self, setting_values, variable_revision_number, module_name):
if variable_revision_number == 1:
setting_values += [ENCODING_TYPES[0]]
setting_values += ["A", "AreaShape_Area", True] #A
setting_values += ["C", "AreaShape_Area", True] #C
setting_values += ["G", "AreaShape_Area", True] #G
setting_values += ["T", "AreaShape_Area", True] #T
variable_revision_number = 2
if variable_revision_number == 2:
setting_values = setting_values[:14]+["True"]+setting_values[14:]
return setting_values, variable_revision_number