1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! The `rosalind` crate provides fuctions to solve probles from [Rosalind](http://rosalind.info/) site.
//!
//! # Counting DNA Nucleotides
//! ## Examples
//! ```
//! use rosalind::RosalindError::UnknownNucleotide;
//! use rosalind::dna::*;
//!
//! let dna = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
//! let dna_nucleotides = DNANucleotides {A: 20, C: 12, G: 17, T: 21};
//! assert_eq!(count_dna_nucleotides(dna).unwrap(), dna_nucleotides);
//! assert_eq!(dna_nucleotides.to_string(), "20 12 17 21");
//! assert_eq!(count_dna_nucleotides("\n").unwrap(), DNANucleotides {A: 0, C: 0, G: 0, T: 0});
//! assert_eq!(count_dna_nucleotides("Z").unwrap_err(), UnknownNucleotide('Z'));
//! ```
//!
//! # Transcribing DNA into RNA
//! ## Examples
//! ```
//! use rosalind::RosalindError::UnknownNucleotide;
//! use rosalind::rna::*;
//!
//! let dna = "GATGGAACTTGACTACGTAAATT";
//! assert_eq!(transcribe_dna_into_rna(dna).unwrap(), "GAUGGAACUUGACUACGUAAAUU");
//! assert_eq!(transcribe_dna_into_rna("\n").unwrap(), "");
//! assert_eq!(transcribe_dna_into_rna("Z").unwrap_err(), UnknownNucleotide('Z'));
//! ```
//!
//! # Complementing a Strand of DNA
//! ## Examples
//! ```
//! use rosalind::RosalindError::UnknownNucleotide;
//! use rosalind::revc::*;
//!
//! let dna = "AAAACCCGGT";
//! assert_eq!(reverse_complement_dna(dna).unwrap(), "ACCGGGTTTT");
//! assert_eq!(reverse_complement_dna("\n").unwrap(), "");
//! assert_eq!(reverse_complement_dna("Z").unwrap_err(), UnknownNucleotide('Z'));
//! ```
//!
//! # Rabbits and Recurrence Relations
//! ## Examples
//! ```
//! # #[macro_use] extern crate num;
//! # #[macro_use] extern crate rosalind;
//! # fn main() {
//! use rosalind::fib::*;
//! use num::{BigUint};
//! use num::bigint::{ToBigUint};
//!
//! let mut expected_relation: BigUint = 19.to_biguint().unwrap();
//! assert_eq!(recurrence_relation(5, 3).unwrap(), expected_relation);
//! expected_relation = 4.to_biguint().unwrap();
//! assert_eq!(recurrence_relation_with_stop(6, 3).unwrap(), expected_relation);
//! # }
//! ```
//!
//! # Translating RNA into Protein, Inferring mRNA from Protein, Calculating Protein Mass
//! ## Examples
//! ```
//! use rosalind::RosalindError::{CodonParseError, UnknownCodon, UnknownAminoAcid};
//! use rosalind::prot::*;
//!
//! let rna = "AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA";
//! assert_eq!(translate_rna_into_protein(rna).unwrap(), "MAMAPRTEINSTRING");
//! assert_eq!(translate_rna_into_protein("AUGUGA\n").unwrap(), "M");
//! assert_eq!(translate_rna_into_protein("Z").unwrap_err(), CodonParseError);
//! assert_eq!(translate_rna_into_protein("ZZZ").unwrap_err(), UnknownCodon("ZZZ".to_string()));
//!
//! assert_eq!(get_number_of_rna_from_protein("MA").unwrap(), 12);
//! assert_eq!(get_number_of_rna_from_protein("").unwrap(), 0);
//! assert_eq!(get_number_of_rna_from_protein("\n").unwrap(), 3);
//! assert_eq!(get_number_of_rna_from_protein("B").unwrap_err(), UnknownAminoAcid('B'));
//!
//! assert_eq!(get_protein_mass("SKADYEK\n").unwrap(), 821.392f64);
//! assert_eq!(get_protein_mass("AB").unwrap_err(), UnknownAminoAcid('B'));
//! ```
//!
//! # Counting Point Mutations
//! ## Examples
//! ```
//! use rosalind::RosalindError::HammingStringsLengthError;
//! use rosalind::hamm::*;
//!
//! let s = "GAGCCTACTAACGGGAT";
//! let t = "CATCGTAATGACGGCCT";
//! assert_eq!(hamming_distance(s, t).unwrap(), 7);
//! assert_eq!(hamming_distance("G", "").unwrap_err(), HammingStringsLengthError);
//! ```
//!
//! # Finding a Motif in DNA
//! ## Examples
//! ```
//! use rosalind::RosalindError::MotifStringsLengthError;
//! use rosalind::subs::*;
//!
//! let s = "GATATATGCATATACTT";
//! let t = "ATAT";
//! assert_eq!(motif_lookup(s, t).unwrap(), vec![2, 4, 10]);
//! assert_eq!(motif_lookup(t, s).unwrap_err(), MotifStringsLengthError);
//! ```
//!
//! # Computing GC Content
//! ## Examples
//! ```
//! use rosalind::gc::*;
//!
//! assert_eq!(gc_content("").unwrap(), 0f32);
//! assert_eq!(gc_content("AGCTATAG").unwrap(), 37.5f32);
//!
//! let dataset = ">Rosalind_6404
//!   CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC
//!   TCCCACTAATAATTCTGAGG
//!   >Rosalind_5959
//!   CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCT
//!   ATATCCATTTGTCAGCAGACACGC
//!   >Rosalind_0808
//!   CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC
//!   TGGGAACCTGCGGGCAGTAGGTGGAAT";
//!
//! assert_eq!(best_gc_content_in_dataset(dataset).unwrap(),
//!   GCcontent {string_id: "Rosalind_0808".to_string(), gc_content: 60.919540f32});
//! ```
//!
//! # Mendel's First Law
//! ## Examples
//! ```
//! use rosalind::RosalindError::InvalidInputParameters;
//! use rosalind::iprb::*;
//!
//! assert_eq!(dominant_allele_probability(0, 1, 1).unwrap_err(), InvalidInputParameters);
//! assert_eq!(dominant_allele_probability(1, 0, 1).unwrap_err(), InvalidInputParameters);
//! assert_eq!(dominant_allele_probability(1, 1, 0).unwrap_err(), InvalidInputParameters);
//!
//! assert_eq!(dominant_allele_probability(2, 2, 2).unwrap(), 0.7833333);
//! ```
//!
//! # Consensus and Profile
//! ## Examples
//! ```
//! use rosalind::cons::*;
//!
//! let prof = Profile {
//!     A: vec![5, 1, 0, 0, 5, 5, 0, 0],
//!     C: vec![0, 0, 1, 4, 2, 0, 6, 1],
//!     G: vec![1, 1, 6, 3, 0, 1, 0, 0],
//!     T: vec![1, 5, 0, 0, 0, 1, 1, 6],
//! };
//!
//! assert_eq!(consensus(prof).unwrap(), "ATGCAACT");
//! ```
//!
//! # Utilities
//! ## Parse FASTA dataset into list of Strings
//! ```
//! use rosalind::utils::*;
//!
//! let fasta_dataset = ">Rosalind_1
//!     CCTGCGGAAG
//!     TCCCACTAAT
//!     >Rosalind_2
//!     CCATCGGTAG
//!     ATATCCATTT
//!     >Rosalind_3
//!     CCACCCTCGT
//!     TGGGAACCTG";
//!
//! let expected_dataset = vec![
//!     "CCTGCGGAAGTCCCACTAAT",
//!     "CCATCGGTAGATATCCATTT",
//!     "CCACCCTCGTTGGGAACCTG",
//! ];
//!
//! assert_eq!(parse_fasta_dataset(fasta_dataset).unwrap(), expected_dataset);
//! ```

extern crate num;

use std::error::Error;
use std::fmt;
use std::result;

use self::RosalindError::*;

#[derive(PartialEq, Debug)]
pub enum RosalindError {
  UnknownNucleotide(char),
  UnknownCodon(String),
  UnknownAminoAcid(char),
  CodonParseError,
  HammingStringsLengthError,
  MotifStringsLengthError,
  InvalidInputParameters,
}

impl fmt::Display for RosalindError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match *self {
      UnknownNucleotide(ref nucleotide) => write!(f, "{}: '{}'", self.description(), nucleotide),
      UnknownCodon(ref codon) => write!(f, "{}: '{}'", self.description(), codon),
      UnknownAminoAcid(ref amino_acid) => write!(f, "{}: '{}'", self.description(), amino_acid),
      _ => write!(f, "{}", self.description()),
    }
  }
}

impl Error for RosalindError {
  fn description(&self) -> &str {
    match *self {
      UnknownNucleotide(..) => "Unknown nucleotide",
      UnknownCodon(..) => "Unknown codon",
      UnknownAminoAcid(..) => "Unknown amino acid",
      CodonParseError => "Could not parse RNA string and group codons",
      HammingStringsLengthError => "Strings must have equal length",
      MotifStringsLengthError => "Substrig `t` must be no longer than `s`",
      InvalidInputParameters => "Invalid input parameters have been passed to the function"
    }
  }
}

/// Unified return type for all modules and methods of `rosalind` library
///
/// ## Examples
/// ```
/// use rosalind::RosalindResult;
/// use rosalind::RosalindError::UnknownNucleotide;
/// use rosalind::dna::count_dna_nucleotides;
/// use rosalind::rna::transcribe_dna_into_rna;
///
/// fn wrapper<T, U>(method: &Fn(U) -> RosalindResult<T>, args: U) -> RosalindResult<T> {
///   method(args)
/// }
///
/// let result = wrapper(&transcribe_dna_into_rna, "GATGGAACTTGACTACGTAAATT");
/// assert_eq!(result.unwrap(), "GAUGGAACUUGACUACGUAAAUU");
///
/// let result = wrapper(&count_dna_nucleotides, "Z");
/// assert_eq!(result.unwrap_err(), UnknownNucleotide('Z'));
/// ```
pub type RosalindResult<T> = result::Result<T, RosalindError>;

pub mod dna;
pub mod rna;
pub mod revc;
pub mod fib;
pub mod prot;
pub mod hamm;
pub mod subs;
pub mod gc;
pub mod iprb;
pub mod cons;
pub mod constants;
pub mod utils;

#[cfg(test)]
mod tests {
  use super::RosalindError;
  use super::RosalindError::CodonParseError;

  #[test]
  fn it_should_stringify_rosalind_error() {
    let error: RosalindError = CodonParseError;
    assert_eq!(error.to_string(), "Could not parse RNA string and group codons");
  }
}