Skip to content

Commit 726e6cd

Browse files
Merge pull request #1583 from nextstrain/fix/empty-ref-crash
fix: handle empty reference sequence fasta
2 parents ea26209 + 3133a72 commit 726e6cd

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

packages/nextclade/src/io/fasta.rs

+14
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ pub fn read_one_fasta(filepath: impl AsRef<Path>) -> Result<FastaRecord, Report>
145145
let mut reader = FastaReader::from_path(filepath)?;
146146
let mut record = FastaRecord::default();
147147
reader.read(&mut record)?;
148+
if record.is_empty() {
149+
return make_error!("Expected exactly one FASTA record, but found none")
150+
.wrap_err_with(|| format!("When reading file {filepath:?}"));
151+
}
152+
if record.seq.is_empty() {
153+
return make_error!("Sequence is empty, but a non-empty sequence was expected")
154+
.wrap_err_with(|| format!("When reading file {filepath:?}"));
155+
}
148156
Ok(record)
149157
}
150158

@@ -168,6 +176,12 @@ pub fn read_one_fasta_str(contents: impl AsRef<str>) -> Result<FastaRecord, Repo
168176
let mut reader = FastaReader::from_str(&contents)?;
169177
let mut record = FastaRecord::default();
170178
reader.read(&mut record)?;
179+
if record.is_empty() {
180+
return make_error!("Expected exactly one FASTA record, but found none");
181+
}
182+
if record.seq.is_empty() {
183+
return make_error!("Sequence is empty, but a non-empty sequence was expected");
184+
}
171185
Ok(record)
172186
}
173187

0 commit comments

Comments
 (0)