bernardo-de-almeida commited on
Commit
3fb06c7
·
1 Parent(s): eac26d2

feat: improve option of coordinates vs input sequence

Browse files
Files changed (1) hide show
  1. app.py +149 -47
app.py CHANGED
@@ -391,7 +391,7 @@ def predict(
391
  chrom: str,
392
  start: int,
393
  end: int,
394
- use_coords: bool,
395
  bigwig_selected: list[str],
396
  bed_elements: list[str],
397
  ):
@@ -402,6 +402,9 @@ def predict(
402
  # Extract track IDs from display format if needed
403
  bigwig_selected = [_extract_track_id(tid) for tid in bigwig_selected]
404
 
 
 
 
405
  if use_coords:
406
  # Check if this species supports coordinate-based fetching
407
  if species not in SPECIES_WITH_COORDINATE_SUPPORT:
@@ -699,6 +702,61 @@ CSS = """
699
  font-size: 2.0rem !important;
700
  }
701
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
702
  """
703
 
704
  JS = """
@@ -825,7 +883,7 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
825
 
826
  <div class="intro-tip">
827
  <span class="intro-tip-icon">💡</span>
828
- <span><strong>Tip:</strong> The demo includes default settings that you can use to get started, taking ~ 1 minute to run.</span>
829
  </div>
830
 
831
  <div style="margin-top: 16px; padding: 12px; background: rgba(0,0,0,0.03); border-radius: 12px; font-size: 0.95rem;">
@@ -862,31 +920,44 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
862
 
863
  model_status = gr.Markdown("", visible=False)
864
 
865
- gr.Markdown("## Input sequence (Genomic coordinate or DNA sequence)\n"
866
- "Supported species for coordinate-based sequence fetching: " + ", ".join(sorted(SPECIES_WITH_COORDINATE_SUPPORT)) + "\n"
867
- )
868
 
869
  # Get all available species from the pipeline
870
  all_species = sorted(ASSEMBLY_TO_SPECIES.values())
871
 
872
- with gr.Row():
873
- species = gr.Dropdown(
874
- choices=all_species,
875
- value=DEFAULT_SPECIES,
876
- label="Species",
877
- )
878
- use_coords = gr.Checkbox(
879
- True,
880
- label="Use genome coordinate (only for supported species)",
881
- visible=DEFAULT_SPECIES in SPECIES_WITH_COORDINATE_SUPPORT
882
- )
883
-
884
- with gr.Row(visible=True) as coords_row:
885
- chrom = gr.Textbox(label="Chromosome", value=_default_coords["chrom"])
886
- start = gr.Number(label="Start", value=_default_coords["start"], precision=0)
887
- end = gr.Number(label="End", value=_default_coords["end"], precision=0)
888
-
889
- seq = gr.Textbox(lines=4, label="Input DNA sequence", placeholder="ACGT...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
890
 
891
  def change_model(display_name: str, species: str):
892
  """Reload pipeline with new model."""
@@ -967,8 +1038,8 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
967
  )
968
 
969
  with gr.Row(visible=True) as bigwig_buttons_row:
970
- bigwig_clear_btn = gr.Button("Clear results")
971
- bigwig_remove_btn = gr.Button("Remove checked from Selected")
972
 
973
  gr.Markdown("## Select genome annotation elements")
974
 
@@ -982,6 +1053,7 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
982
  btn = gr.Button("Predict", elem_id="predict_btn")
983
 
984
  gr.Markdown("## NTv3 predictions for selected tracks and elements")
 
985
 
986
  plot = gr.Plot(label="", elem_id="tracks_plot")
987
  export_png = gr.File(elem_id="export_png_hidden", interactive=False)
@@ -1152,13 +1224,15 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
1152
  )
1153
 
1154
  # Update coordinates visibility and values when species changes
1155
- def update_on_species_change(species: str, use_coords_val: bool):
1156
  """Update coordinates visibility and values when species changes."""
1157
  is_supported = species in SPECIES_WITH_COORDINATE_SUPPORT
1158
  has_bigwigs = _has_bigwigs(species)
1159
  coords = DEFAULT_COORDS.get(species, DEFAULT_COORDS["human"])
1160
- # Show coordinates only if species is supported AND use_coords is True
1161
- show_coords = is_supported and use_coords_val
 
 
1162
 
1163
  # Format available tracks for display if species has bigwigs
1164
  if has_bigwigs:
@@ -1183,8 +1257,9 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
1183
  gr.update(visible=show_coords, value=coords["chrom"]),
1184
  gr.update(visible=show_coords, value=coords["start"]),
1185
  gr.update(visible=show_coords, value=coords["end"]),
1186
- gr.update(value=is_supported, visible=is_supported), # Show/hide and enable use_coords only if supported
1187
- gr.update(visible=show_coords), # Show/hide the row
 
1188
  gr.update(visible=not has_bigwigs), # Show "no tracks" message if no bigwigs
1189
  gr.update(visible=show_selected_tracks, choices=formatted_tracks, value=default_formatted), # Show bigwig selection with defaults if available
1190
  gr.update(visible=has_bigwigs), # Show bigwig query if available
@@ -1192,36 +1267,62 @@ with gr.Blocks(title="NTv3 Tracks Demo") as demo:
1192
  gr.update(visible=has_bigwigs), # Show bigwig buttons if available
1193
  )
1194
 
1195
- # Update coordinates visibility when checkbox changes
1196
- def update_coords_visibility(use_coords_val: bool, species: str):
1197
- """Update coordinates visibility when checkbox changes."""
1198
  is_supported = species in SPECIES_WITH_COORDINATE_SUPPORT
1199
- show_coords = is_supported and use_coords_val
1200
- return (
1201
- gr.update(visible=show_coords),
1202
- gr.update(visible=show_coords),
1203
- gr.update(visible=show_coords),
1204
- gr.update(visible=show_coords), # Show/hide the row
1205
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1206
 
1207
  species.change(
1208
  fn=update_on_species_change,
1209
- inputs=[species, use_coords],
1210
  outputs=[
1211
- chrom, start, end, use_coords, coords_row,
1212
  bigwig_no_tracks_msg, bigwig_selected, bigwig_query, bigwig_results, bigwig_buttons_row
1213
  ],
1214
  )
1215
 
1216
- use_coords.change(
1217
- fn=update_coords_visibility,
1218
- inputs=[use_coords, species],
1219
- outputs=[chrom, start, end, coords_row],
1220
  )
1221
 
1222
  btn.click(
1223
  fn=predict,
1224
- inputs=[seq, species, chrom, start, end, use_coords, bigwig_selected, bed_elements],
1225
  outputs=[plot, export_png, meta, prediction_state, bigwig_selected_state, bed_elements_state],
1226
  api_name="predict",
1227
  )
@@ -1252,3 +1353,4 @@ if __name__ == "__main__":
1252
  css=CSS,
1253
  js=JS,
1254
  )
 
 
391
  chrom: str,
392
  start: int,
393
  end: int,
394
+ input_type: str,
395
  bigwig_selected: list[str],
396
  bed_elements: list[str],
397
  ):
 
402
  # Extract track IDs from display format if needed
403
  bigwig_selected = [_extract_track_id(tid) for tid in bigwig_selected]
404
 
405
+ # Determine if using coordinates based on input_type radio button
406
+ use_coords = input_type == "Use genomic coordinates"
407
+
408
  if use_coords:
409
  # Check if this species supports coordinate-based fetching
410
  if species not in SPECIES_WITH_COORDINATE_SUPPORT:
 
702
  font-size: 2.0rem !important;
703
  }
704
  }
705
+
706
+ /* Remove borders/lines from input sections */
707
+ .gr-group {
708
+ border: none !important;
709
+ box-shadow: none !important;
710
+ margin-bottom: 0 !important;
711
+ padding-bottom: 0 !important;
712
+ }
713
+
714
+ /* Remove any borders or separators from textbox components in input sections */
715
+ .gr-textbox {
716
+ border-top: none !important;
717
+ margin-top: 0 !important;
718
+ }
719
+
720
+ /* Remove borders from form sections */
721
+ .form {
722
+ border: none !important;
723
+ }
724
+
725
+ /* Remove any hr or separator lines */
726
+ hr {
727
+ display: none !important;
728
+ }
729
+
730
+ /* Remove line above DNA sequence input */
731
+ #dna_sequence_input {
732
+ border-top: none !important;
733
+ margin-top: 0 !important;
734
+ padding-top: 0 !important;
735
+ }
736
+
737
+ /* Remove any border or separator before the DNA sequence input */
738
+ #dna_sequence_input::before,
739
+ #dna_sequence_input .wrap::before {
740
+ display: none !important;
741
+ content: none !important;
742
+ }
743
+
744
+ /* Ensure coordinates group doesn't leave a border when hidden */
745
+ #coords_group[style*="display: none"],
746
+ #coords_group:not(:visible) {
747
+ display: none !important;
748
+ border: none !important;
749
+ margin: 0 !important;
750
+ padding: 0 !important;
751
+ }
752
+
753
+ /* Remove any separator between input sections */
754
+ #coords_group + *,
755
+ #coords_group ~ #dna_sequence_input {
756
+ border-top: none !important;
757
+ margin-top: 0 !important;
758
+ padding-top: 0 !important;
759
+ }
760
  """
761
 
762
  JS = """
 
883
 
884
  <div class="intro-tip">
885
  <span class="intro-tip-icon">💡</span>
886
+ <span><strong>Tip:</strong> The demo includes default settings that you can use to get started, taking ~ 10 seconds to run.</span>
887
  </div>
888
 
889
  <div style="margin-top: 16px; padding: 12px; background: rgba(0,0,0,0.03); border-radius: 12px; font-size: 0.95rem;">
 
920
 
921
  model_status = gr.Markdown("", visible=False)
922
 
923
+ gr.Markdown("## Input DNA sequence")
 
 
924
 
925
  # Get all available species from the pipeline
926
  all_species = sorted(ASSEMBLY_TO_SPECIES.values())
927
 
928
+ species = gr.Dropdown(
929
+ choices=all_species,
930
+ value=DEFAULT_SPECIES,
931
+ label="Species",
932
+ )
933
+
934
+ # Radio buttons for input type selection
935
+ is_supported_default = DEFAULT_SPECIES in SPECIES_WITH_COORDINATE_SUPPORT
936
+ initial_input_type = "Use genomic coordinates" if is_supported_default else "Enter DNA sequence"
937
+ input_type = gr.Radio(
938
+ choices=["Use genomic coordinates", "Enter DNA sequence"],
939
+ value=initial_input_type,
940
+ label="Input method",
941
+ visible=is_supported_default, # Only show if species supports coordinates
942
+ )
943
+
944
+ # Coordinates section - visible only when "Use genomic coordinates" is selected
945
+ with gr.Group(visible=is_supported_default and initial_input_type == "Use genomic coordinates", elem_id="coords_group") as coords_group:
946
+ gr.Markdown("**Genomic coordinates** (supported species: " + ", ".join(sorted(SPECIES_WITH_COORDINATE_SUPPORT)) + ")")
947
+ with gr.Row():
948
+ chrom = gr.Textbox(label="Chromosome", value=_default_coords["chrom"])
949
+ start = gr.Number(label="Start", value=_default_coords["start"], precision=0)
950
+ end = gr.Number(label="End", value=_default_coords["end"], precision=0)
951
+
952
+ # DNA sequence section - visible only when "Enter DNA sequence" is selected
953
+ # Using Textbox directly (not wrapped in Group) to avoid visual border/line
954
+ seq = gr.Textbox(
955
+ lines=4,
956
+ label="Input DNA sequence",
957
+ placeholder="ACGT...",
958
+ visible=initial_input_type == "Enter DNA sequence",
959
+ elem_id="dna_sequence_input"
960
+ )
961
 
962
  def change_model(display_name: str, species: str):
963
  """Reload pipeline with new model."""
 
1038
  )
1039
 
1040
  with gr.Row(visible=True) as bigwig_buttons_row:
1041
+ bigwig_clear_btn = gr.Button("Clear search results")
1042
+ bigwig_remove_btn = gr.Button("Remove all selected")
1043
 
1044
  gr.Markdown("## Select genome annotation elements")
1045
 
 
1053
  btn = gr.Button("Predict", elem_id="predict_btn")
1054
 
1055
  gr.Markdown("## NTv3 predictions for selected tracks and elements")
1056
+ gr.Markdown("Note: NTv3 predictions are for the 37.5% center of the input sequence.")
1057
 
1058
  plot = gr.Plot(label="", elem_id="tracks_plot")
1059
  export_png = gr.File(elem_id="export_png_hidden", interactive=False)
 
1224
  )
1225
 
1226
  # Update coordinates visibility and values when species changes
1227
+ def update_on_species_change(species: str, input_type_val: str):
1228
  """Update coordinates visibility and values when species changes."""
1229
  is_supported = species in SPECIES_WITH_COORDINATE_SUPPORT
1230
  has_bigwigs = _has_bigwigs(species)
1231
  coords = DEFAULT_COORDS.get(species, DEFAULT_COORDS["human"])
1232
+ # Show coordinates only if species is supported AND input type is coordinates
1233
+ use_coords = input_type_val == "Use genomic coordinates"
1234
+ show_coords = is_supported and use_coords
1235
+ show_seq = not show_coords
1236
 
1237
  # Format available tracks for display if species has bigwigs
1238
  if has_bigwigs:
 
1257
  gr.update(visible=show_coords, value=coords["chrom"]),
1258
  gr.update(visible=show_coords, value=coords["start"]),
1259
  gr.update(visible=show_coords, value=coords["end"]),
1260
+ gr.update(visible=is_supported, value="Use genomic coordinates" if is_supported else "Enter DNA sequence"), # Update input_type radio
1261
+ gr.update(visible=show_coords), # Show/hide coords_group
1262
+ gr.update(visible=show_seq), # Show/hide seq
1263
  gr.update(visible=not has_bigwigs), # Show "no tracks" message if no bigwigs
1264
  gr.update(visible=show_selected_tracks, choices=formatted_tracks, value=default_formatted), # Show bigwig selection with defaults if available
1265
  gr.update(visible=has_bigwigs), # Show bigwig query if available
 
1267
  gr.update(visible=has_bigwigs), # Show bigwig buttons if available
1268
  )
1269
 
1270
+ # Update input type radio visibility and value when species changes
1271
+ def update_input_type_on_species_change(species: str):
1272
+ """Update input type radio when species changes."""
1273
  is_supported = species in SPECIES_WITH_COORDINATE_SUPPORT
1274
+ # If species doesn't support coordinates, default to sequence input
1275
+ default_value = "Use genomic coordinates" if is_supported else "Enter DNA sequence"
1276
+ return gr.update(visible=is_supported, value=default_value)
1277
+
1278
+ # Update input visibility when radio button changes
1279
+ def update_input_visibility(input_type_val: str, species: str):
1280
+ """Update input visibility when radio button changes."""
1281
+ # Explicitly check the input type value
1282
+ if input_type_val == "Enter DNA sequence":
1283
+ # Hide coordinates, show sequence
1284
+ return (
1285
+ gr.update(visible=False), # coords_group - always hide when sequence is selected
1286
+ gr.update(visible=True), # seq - always show when sequence is selected
1287
+ )
1288
+ elif input_type_val == "Use genomic coordinates":
1289
+ # Show coordinates only if species supports it
1290
+ is_supported = species in SPECIES_WITH_COORDINATE_SUPPORT
1291
+ return (
1292
+ gr.update(visible=is_supported), # coords_group - show only if supported
1293
+ gr.update(visible=not is_supported), # seq - hide when coordinates are shown
1294
+ )
1295
+ else:
1296
+ # Fallback: hide both (shouldn't happen)
1297
+ return (
1298
+ gr.update(visible=False),
1299
+ gr.update(visible=False),
1300
+ )
1301
+
1302
+ species.change(
1303
+ fn=update_input_type_on_species_change,
1304
+ inputs=[species],
1305
+ outputs=[input_type],
1306
+ )
1307
 
1308
  species.change(
1309
  fn=update_on_species_change,
1310
+ inputs=[species, input_type],
1311
  outputs=[
1312
+ chrom, start, end, input_type, coords_group, seq,
1313
  bigwig_no_tracks_msg, bigwig_selected, bigwig_query, bigwig_results, bigwig_buttons_row
1314
  ],
1315
  )
1316
 
1317
+ input_type.change(
1318
+ fn=update_input_visibility,
1319
+ inputs=[input_type, species],
1320
+ outputs=[coords_group, seq],
1321
  )
1322
 
1323
  btn.click(
1324
  fn=predict,
1325
+ inputs=[seq, species, chrom, start, end, input_type, bigwig_selected, bed_elements],
1326
  outputs=[plot, export_png, meta, prediction_state, bigwig_selected_state, bed_elements_state],
1327
  api_name="predict",
1328
  )
 
1353
  css=CSS,
1354
  js=JS,
1355
  )
1356
+