-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
50 lines (45 loc) · 1.31 KB
/
Copy pathlib.rs
File metadata and controls
50 lines (45 loc) · 1.31 KB
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
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq)]
pub struct Dna {
coding_strand:String
}
#[derive(Debug, PartialEq, Eq)]
pub struct Rna {
coding_strand:String
}
impl Dna {
pub fn new(dna: &str) -> Result<Dna, usize> {
let nucleotides = ['A', 'T', 'C', 'G'];
let mut seq = String::new();
for (index, ch) in dna.to_uppercase().chars().enumerate() {
if nucleotides.contains(&ch) {
seq.push(ch);
}else {
return Err(index)
}
}
Ok(Self { coding_strand: seq })
}
pub fn into_rna(self) -> Rna {
let pair = HashMap::from([('G','C'),('C','G'), ('T','A'), ('A','U')]);
let mut trans_seq = String::new();
for ch in self.coding_strand.chars() {
trans_seq.push(pair.get(&ch).unwrap().to_owned())
}
Rna { coding_strand: trans_seq }
}
}
impl Rna {
pub fn new(rna: &str) -> Result<Rna, usize> {
let nucleotides = ['A', 'C', 'G', 'U'];
let mut seq = String::new();
for (index, ch) in rna.to_uppercase().chars().enumerate() {
if nucleotides.contains(&ch) {
seq.push(ch);
}else {
return Err(index)
}
}
Ok(Self { coding_strand: seq })
}
}