@@ -111,6 +111,47 @@ def test_migrate_old_data(tmp_path, monkeypatch):
111111 assert len (bt2_files ) == 6
112112
113113
114+ def test_migrate_auto_registers_species_when_config_empty (tmp_path , monkeypatch ):
115+ """Index files survive pip upgrade but old config was replaced with species: {}.
116+ Migration should auto-register species based on index directory names."""
117+ data_dir = tmp_path / "data"
118+ monkeypatch .setenv ("HCRPROBEDESIGN_DATA_DIR" , str (data_dir ))
119+
120+ # Set up old package dir with index files but EMPTY species config
121+ # (simulates pip replacing HCRconfig.yaml with the clean default)
122+ old_pkg = tmp_path / "old_pkg"
123+ old_indices = old_pkg / "indices" / "mm10"
124+ old_indices .mkdir (parents = True )
125+ for suffix in ["1.bt2" , "2.bt2" , "3.bt2" , "4.bt2" , "rev.1.bt2" , "rev.2.bt2" ]:
126+ (old_indices / f"mm10.{ suffix } " ).write_text ("fake" )
127+
128+ old_config_path = old_pkg / "HCRconfig.yaml"
129+ old_config = {
130+ "species" : {},
131+ "default_params" : {"tileSize" : 52 },
132+ }
133+ with open (old_config_path , "w" ) as fh :
134+ yaml .safe_dump (old_config , fh )
135+
136+ monkeypatch .setattr (_datadir , "_PACKAGE_DIRECTORY" , str (old_pkg ))
137+ monkeypatch .setattr (_datadir , "_SEED_CONFIG" , str (old_config_path ))
138+
139+ _datadir .ensure_data_dir ()
140+
141+ # Index files should be migrated
142+ new_index_dir = data_dir / "indices" / "mm10"
143+ assert new_index_dir .exists ()
144+ bt2_files = glob .glob (str (new_index_dir / "*.bt2*" ))
145+ assert len (bt2_files ) == 6
146+
147+ # Species should be auto-registered from the directory name
148+ new_config = yaml .safe_load ((data_dir / "HCRconfig.yaml" ).read_text ())
149+ assert "mm10" in new_config ["species" ]
150+ assert new_config ["species" ]["mm10" ]["bowtie2_index" ] == os .path .join (
151+ str (data_dir ), "indices" , "mm10" , "mm10"
152+ )
153+
154+
114155def test_has_old_data_empty (tmp_path , monkeypatch ):
115156 monkeypatch .setattr (_datadir , "_PACKAGE_DIRECTORY" , str (tmp_path ))
116157 # Write an empty config
0 commit comments