pdbget/
uri_types.rs

1use std::io;
2
3#[derive(Debug)]
4pub enum UriType {
5    Fasta,
6    Pdb,
7    PdbGz,
8    Cif,
9    CifGz,
10    Xml,
11    XmlGz,
12}
13
14#[derive(Debug)]
15pub enum Server {
16    PDBE,
17    RCSB,
18}
19
20impl Server {
21    pub fn get_server(s: &str) -> Result<Server, io::Error> {
22        match s.to_uppercase().as_ref() {
23            "RCSB" => Ok(Server::RCSB),
24            "PDBE" => Ok(Server::PDBE),
25            _ => Err(io::Error::new(
26                io::ErrorKind::InvalidInput,
27                format!(
28                    "[Error] '{}' is not an accepted server. Use -h to see available severs",
29                    s
30                ),
31            )),
32        }
33    }
34}
35
36impl UriType {
37    pub fn get_type(t: &str) -> Result<UriType, io::Error> {
38        let t = String::from(t);
39        match t.to_uppercase().as_ref() {
40            "FASTA" => Ok(UriType::Fasta),
41            "PDB" => Ok(UriType::Pdb),
42            "PDBGZ" => Ok(UriType::PdbGz),
43            "CIF" => Ok(UriType::Cif),
44            "CIFGZ" => Ok(UriType::CifGz),
45            "XMLGZ" => Ok(UriType::XmlGz),
46            "XML" => Ok(UriType::Xml),
47            _ => Err(io::Error::new(
48                io::ErrorKind::InvalidInput,
49                format!(
50                    "[Error] '{}' is not an accepted format. Use -h to see available types",
51                    t
52                ),
53            )),
54        }
55    }
56
57    pub fn generate_uri(&self, id: &str, server: &Server) -> Result<String, io::Error> {
58        match server {
59            Server::RCSB =>
60                match self {
61                    UriType::Fasta => Ok(format!("https://www.rcsb.org/pdb/download/downloadFastaFiles.do?structureIdList={}&compressionType=uncompressed", id)),
62                    UriType::Pdb => Ok(format!("https://files.rcsb.org/download/{}.pdb", id)),
63                    UriType::PdbGz => Ok(format!("https://files.rcsb.org/download/{}.pdb.gz", id)),
64                    UriType::Cif => Ok(format!("https://files.rcsb.org/download/{}.cif", id)),
65                    UriType::CifGz => Ok(format!("https://files.rcsb.org/download/{}.cif.gz", id)),
66                    UriType::XmlGz => Ok(format!("https://files.rcsb.org/download/{}.xml.gz", id)),
67                    _ => Err(
68                        io::Error::new(
69                            io::ErrorKind::InvalidData,
70                            format!(
71                                "[Error] '{:?}' format is not supported on {:?} server",
72                                self,
73                                server
74                            )
75                        )
76                    ),
77                },
78            Server::PDBE =>
79                match self {
80                    UriType::Fasta => Ok(format!("http://www.ebi.ac.uk/pdbe/entry/pdb/{}/fasta", id)),
81                    UriType::Pdb => Ok(format!("http://www.ebi.ac.uk/pdbe/entry-files/download/pdb{}.ent", id)),
82                    UriType::PdbGz => Ok(format!("http://ftp.ebi.ac.uk/pub/databases/rcsb/pdb-remediated/data/structures/divided/pdb/ci/pdb{}.ent.gz", id)),
83                    UriType::Cif => Ok(format!("http://www.ebi.ac.uk/pdbe/entry-files/download/{}_updated.cif", id)),
84                    UriType::Xml => Ok(format!("https://www.ebi.ac.uk/pdbe/entry-files/download/{}_validation.xml", id)),
85                    _ => Err(
86                        io::Error::new(
87                            io::ErrorKind::InvalidData,
88                            format!(
89                                "[Error] '{:?}' format is not supported on {:?} server",
90                                self,
91                                server
92                            )
93                        )
94                    ),
95                },
96        }
97    }
98
99    pub fn get_extension(&self) -> &str {
100        match self {
101            UriType::Fasta => ".fasta",
102            UriType::Pdb => ".pdb",
103            UriType::Cif => ".cif",
104            UriType::PdbGz => ".pdb.gz",
105            UriType::CifGz => ".cif.gz",
106            UriType::XmlGz => ".xml.gz",
107            UriType::Xml => ".xml",
108        }
109    }
110}