Skip to main content

extended_htslib/
errors.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4/// Generic result type for functions in this crate with
5/// a global error class.
6pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8#[derive(Error, Debug, PartialEq)]
9pub enum Error {
10    // General errors
11    #[error("file not found: {path}")]
12    FileNotFound { path: PathBuf },
13    #[error("file could not be opened: {path}")]
14    FileOpen { path: String },
15    #[error("invalid (non-unicode) characters in path")]
16    NonUnicodePath,
17    #[error("failed to fetch region")]
18    Fetch,
19    #[error("error seeking to file offset")]
20    FileSeek,
21    #[error("error seeking to {contig:?}:{start} in indexed file")]
22    GenomicSeek { contig: String, start: u64 },
23    #[error("sequence {sequence} not found in index")]
24    UnknownSequence { sequence: String },
25    #[error("error setting threads for file reading")]
26    SetThreads,
27    #[error("failed to create htslib thread pool")]
28    ThreadPool,
29
30    #[error("failed to write BAM/BCF record (out of disk space?)")]
31    WriteRecord,
32
33    // Errors for faidx
34    #[error("The given position is too large to be converted to i64")]
35    FaidxPositionTooLarge,
36    #[error("bad conversion of sequence name")]
37    FaidxBadSeqName,
38    #[error("failed to build index for fasta file {path:?}")]
39    FaidxBuildFailed { path: std::path::PathBuf },
40
41    // Errors for Tbx
42    #[error("previous iterator generation failed")]
43    TabixNoIter,
44    #[error("truncated tabix record")]
45    TabixTruncatedRecord,
46    #[error("invalid tabix index")]
47    TabixInvalidIndex,
48
49    // Errors for BAM
50    #[error("error parsing CIGAR string: {msg}")]
51    BamParseCigar { msg: String },
52    #[error("unexpected CIGAR operation: {msg}")]
53    BamUnexpectedCigarOperation { msg: String },
54    #[error("error parsing SAM record: {rec}")]
55    BamParseSAM { rec: String },
56    #[error("error parsing CS record: {rec}")]
57    BamParseCS { rec: String },
58    #[error("error parsing MD record: {rec}")]
59    BamParseMD { rec: String },
60    #[error("invalid path to CRAM-reference {path}")]
61    BamInvalidReferencePath { path: PathBuf },
62    #[error("invalid compression level {level}")]
63    BamInvalidCompressionLevel { level: u32 },
64    #[error("unable to open SAM/BAM/CRAM file at {target}")]
65    BamOpen { target: String },
66    #[error("unable to open SAM/BAM/CRAM index for {target}; please create an index")]
67    BamInvalidIndex { target: String },
68    #[error("invalid record in SAM/BAM/CRAM file")]
69    BamInvalidRecord,
70    #[error("truncated record in SAM/BAM/CRAM file")]
71    BamTruncatedRecord,
72    #[error(
73        "format not indexable by htslib (format is detected as something else than SAM/BAM/CRAM)"
74    )]
75    BamNotIndexable,
76    #[error("failed to write BAM/CRAM index (out of disk space?)")]
77    BamWriteIndex,
78    #[error("failed to build BAM/CRAM index")]
79    BamBuildIndex,
80    #[error("failed to create SAM/BAM/CRAM pileup")]
81    BamPileup,
82    #[error("file is not sorted by position")]
83    BamUnsorted,
84
85    // Errors for BAM auxiliary fields
86    #[error("failed to add aux field (out of memory?)")]
87    BamAux,
88    #[error("provided string contains internal 0 byte(s)")]
89    BamAuxStringError,
90    #[error("failed to parse aux data")]
91    BamAuxParsingError,
92    #[error("the specified tag does could not be found")]
93    BamAuxTagNotFound,
94    #[error("data type of aux field is not known")]
95    BamAuxUnknownType,
96    #[error("failed to add aux field, tag is already present")]
97    BamAuxTagAlreadyPresent,
98    #[error("updating the aux field for this datatype is not supported")]
99    BamAuxTagUpdatingNotSupported,
100
101    // Errors for base modification fields
102    #[error("no base modification tag found for record")]
103    BamBaseModificationTagNotFound,
104    #[error("no base modification with the specified code found in record")]
105    BamBaseModificationTypeNotFound,
106    #[error("base modification iteration failed")]
107    BamBaseModificationIterationFailed,
108    #[error("base modification found too many modifications")]
109    BamBaseModificationTooManyMods,
110
111    // Errors for BCF
112    #[error("error allocating internal data structure for BCF/VCF reader (out of memory?)")]
113    BcfAllocationError,
114    #[error("failed to open BCF/VCF from {target:?}")]
115    BcfOpen { target: String },
116    #[error("invalid record in BCF/VCF file")]
117    BcfInvalidRecord,
118    #[error("tag {tag} undefined in BCF/VCF header")]
119    BcfUndefinedTag { tag: String },
120    #[error("unexpected type for tag {tag} in BCF/VCF file")]
121    BcfUnexpectedType { tag: String },
122    #[error("tag {tag} missing from record {record} in BCF/VCF file")]
123    BcfMissingTag { tag: String, record: String },
124    #[error("error setting tag {tag} in BCF/VCF record (out of memory?)")]
125    BcfSetTag { tag: String },
126    #[error("ID {rid} not found in BCF/VCF header")]
127    BcfUnknownRID { rid: u32 },
128    #[error("contig {contig} not found in BCF/VCF header")]
129    BcfUnknownContig { contig: String },
130    #[error("ID {id} not found in BCF/VCF header")]
131    BcfUnknownID { id: String },
132    #[error("sample {name} not found in BCF/VCF header")]
133    BcfUnknownSample { name: String },
134    #[error("duplicate sample names given for subsetting BCF/VCF")]
135    BcfDuplicateSampleNames,
136    #[error("failed to set values in BCF/VCF record (out of memory?)")]
137    BcfSetValues,
138    #[error("failed to remove alleles in BCF/VCF record")]
139    BcfRemoveAlleles,
140    #[error("failed to render BCF record as string")]
141    BcfToString,
142    #[error("failed to translate BCF/VCF record")]
143    BcfTranslate,
144
145    #[error("invalid compression level {level}")]
146    BgzfInvalidCompressionLevel { level: i8 },
147    #[error("failed setting hts reading options")]
148    HtsSetOpt,
149    #[error("failed calculating slow index statistics")]
150    SlowIdxStats,
151    #[error("invalid tid {tid}")]
152    InvalidTid { tid: i32 },
153    #[error("No sequences in the reference")]
154    NoSequencesInReference,
155}