Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////
- // RUST
- ////////////////////
- // use std::env;
- fn main() {
- if false {
- let args: Vec<String> = std::env::args().collect();
- println!("{:?}", args);
- if args.len() < 2 {
- println!("Not enough parameters.");
- } else {
- let aas = proceed(&args[1]);
- println!("{:?}", &aas);
- }
- } else {
- if let Some(dna) = std::env::args().nth(1) {
- let aas = proceed(&dna);
- println!("{:?}", &aas);
- } else {
- println!("Not enough parameters.");
- }
- }
- }
- fn proceed(dna: &str) -> String {
- // TODO: Check for potential unnecessary allocations.
- let bs: Vec<char> = dna.chars().collect();
- let codon_iter: std::slice::Chunks<char> = bs.chunks(3);
- let mut result = "".to_string();
- for codon in codon_iter.clone() {
- println!("{:?}", codon);
- result.push_str(aa_from_codon(codon));
- //result.push_str(aas_from_codon_0(codon.iter().cloned().collect::<String>().as_str())); // Maybe replacing push_str with something else would save this as_str() callβ¦
- }
- result
- }
- fn is_pyrimidine_base(b: char) -> bool {
- b == 'C' || b == 'T'
- }
- fn is_purine_base(b: char) -> bool {
- b == 'G' || b == 'A'
- }
- // #[allow(dead_code)]
- // fn aas_from_codon_0(codon: &str) -> &str {
- // if codon == "aaa" {"L"} else {"X"}
- // }
- fn aa_from_codon(codon: &[char]) -> &str {
- if codon.len() < 3 { "" }
- else {
- // let (i, m) = (codon[0], codon[1]);
- let f = codon[2];
- match (codon[0], codon[1]) {
- ('T', 'T') => if is_purine_base(f) { "Leu" } else { "Phe" },
- ('C', 'T') => "Leu",
- ('A', 'T') => if f == 'G' { "Met" } else { "Ile" },
- ('G', 'T') => "Val",
- ('T', 'C') => "Ser",
- ('C', 'C') => "Pro",
- ('A', 'C') => "Thr",
- ('G', 'C') => "Ala",
- ('T', 'A') => if among(f, vec!('T', 'C')) { "Tyr" } else { "Stp" },
- ('C', 'A') => if is_purine_base(f) { "Gln" } else { "His" },
- ('A', 'A') => if is_purine_base(f) { "Lys" } else { "Asn" },
- ('G', 'A') => if is_purine_base(f) { "Glu" } else { "Asp" },
- ('T', 'G') => if is_pyrimidine_base(f) { "Cys" }
- else if f == 'G' { "Trp" } else { "Stp" },
- ('C', 'G') => "Arg",
- ('A', 'G') => if is_purine_base(f) { "Arg" } else { "Ser" },
- ('G', 'G') => "Gly",
- (_, _) => "Xxx",
- }
- }
- }
- fn among<T: Eq>(x: T, v: Vec<T>) -> bool {
- for e in v {
- if x == e {
- return true
- }
- }
- false
- }
- ////////////////////
- // HASKELL
- ////////////////////
- import System.Environment
- import Data.List.Split
- main :: IO ()
- main = do
- args <- getArgs
- case nth 1 args of
- Nothing -> do putStrLn "No argument provided." -- TODO: delete this 'do'?
- Just x -> putStrLn ("Result: " ++ proceed x)
- putStrLn "End."
- nth :: Int -> [a] -> Maybe a
- nth _ [] = Nothing
- nth i (x:xs) = if i == 0
- then Just x
- else nth (i - 1) xs
- nth' :: Int -> [a] -> a
- nth' _ [] = error "Index out of range!"
- nth' i (x:xs) = if i == 0
- then x
- else nth' (i - 1) xs
- proceed :: String -> String
- proceed cs = joinStrList (aas_from_codons (chunksOf 3 cs))
- joinStrList :: [String] -> String
- joinStrList ss = foldr (++) "" ss
- aas_from_codons :: [String] -> [String]
- aas_from_codons = map aa_from_codon
- aa_from_codon :: String -> String
- aa_from_codon c =
- if length c < 3
- then ""
- else
- let head = (nth' 0 c, nth' 1 c)
- in case head of
- ('T', 'T') -> if is_purine_base f then "Leu" else "Phe"
- ('C', 'T') -> "Leu"
- ('A', 'T') -> if f == 'G' then "Met" else "Ile"
- ('G', 'T') -> "Val"
- ('T', 'C') -> "Ser"
- ('C', 'C') -> "Pro"
- ('A', 'C') -> "Thr"
- ('G', 'C') -> "Ala"
- ('T', 'A') -> if among f ['T', 'C'] then "Tyr" else "Stp"
- ('C', 'A') -> if is_purine_base f then "Gln" else "His"
- ('A', 'A') -> if is_purine_base f then "Lys" else "Asn"
- ('G', 'A') -> if is_purine_base f then "Glu" else "Asp"
- ('T', 'G') -> if is_pyrimidine_base f
- then "Cys"
- else if f == 'G' then "Trp" else "Stp"
- ('C', 'G') -> "Arg"
- ('A', 'G') -> if is_purine_base f then "Arg" else "Ser"
- ('G', 'G') -> "Gly"
- _ -> "Xxx"
- where
- f = nth' 2 c
- is_purine_base :: Char -> Bool
- is_purine_base b = (b == 'A' || b == 'G')
- is_pyrimidine_base :: Char -> Bool
- is_pyrimidine_base b = (b == 'T' || b == 'C')
- among :: (Eq a) => a -> [a] -> Bool
- among v [] = False
- among v (x:xs) = ((v == x) || (among v xs))
- ////////////////////
- // MERCURY
- ////////////////////
- :- module dna.
- :- interface.
- :- import_module io.
- :- pred main(io::di, io::uo) is det.
- :- implementation.
- :- import_module int, list, string, char, maybe.
- main(!IO) :-
- io.command_line_arguments(Args, !IO),
- % io.format("ArgNum: %d\n", [i(length(Args))], !IO),
- % foldl2(print_arg, Args, 1, _, !IO),
- NthArg = nth(0, Args),
- (
- NthArg = no,
- io.write_string("No argument provided.\n", !IO)
- ;
- NthArg = yes(A),
- io.format("Result: %s\n", [s(proceed(A))], !IO)
- ),
- io.write_string("End.\n\n", !IO).
- % :- pred print_arg(string::in, int::in, int::out, io::di, io::uo) is det.
- % print_arg(Arg, ArgNum, ArgNum + 1, !IO) :-
- % io.format("the argument #%d is %s\n", [i(ArgNum), s(Arg)], !IO).
- :- func nth(int, list(T)) = maybe(T).
- nth(_, []) = no.
- nth(I, [X | Xs]) = (
- if I = 0
- then yes(X)
- else nth(I - 1, Xs)
- ).
- :- pred is_nth(T::out, int::in, list(T)::in) is semidet.
- is_nth(E, I, [X | Xs]) :-
- if I = 0 then E = X
- else is_nth(E, I - 1, Xs).
- :- func proceed(string) = string.
- proceed(S) = joinStrList(aas_from_codons(stringChunksOf(3, S))).
- :- func joinStrList(list(string)) = string.
- joinStrList(Ss) = S :- foldr((pred(E::in, A::in, O::out) is det :- O = append(E, A)), Ss, "", S).
- :- func stringChunksOf(int, string) = list(string).
- stringChunksOf(L, S) = Cs :-
- if S \= ""
- then (split(S, L, SL, SR),
- stringChunksOf(L, SR) = Xs,
- Cs = [SL | Xs])
- else Cs = [].
- :- func aas_from_codons(list(string)) = list(string).
- aas_from_codons(Cs) = map(aa_from_codon, Cs).
- :- func aa_from_codon(string) = string.
- aa_from_codon(C) = AA :-
- if (
- length(C) >= 3,
- index(C, 0, I),
- index(C, 1, M),
- index(C, 2, F)
- ) then (
- if I = 'T', M = 'T'
- then (if is_purine_base(F) then AA = "Leu" else AA = "Phe")
- else if I = 'C', M = 'T'
- then AA = "Leu"
- else if I = 'A', M = 'T'
- then (if F = 'G' then AA = "Met" else AA = "Ile")
- else if I = 'G', M = 'T'
- then AA = "Val"
- else if I = 'T', M = 'C'
- then AA = "Ser"
- else if I = 'C', M = 'C'
- then AA = "Pro"
- else if I = 'A', M = 'C'
- then AA = "Ala"
- else if I = 'T', M = 'A'
- then (if is_pyrimidine_base(F) then AA = "Tyr" else AA = "Stp")
- else if I = 'C', M = 'A'
- then (if is_purine_base(F) then AA = "Gln" else AA = "His")
- else if I = 'A', M = 'A'
- then (if is_purine_base(F) then AA = "Lys" else AA = "Asn")
- else if I = 'G', M = 'A'
- then (if is_purine_base(F) then AA = "Glu" else AA = "Asp")
- else if I = 'T', M = 'G'
- then (if is_pyrimidine_base(F) then AA = "Cys" else if F = 'G' then AA = "Trp" else AA = "Stp")
- else if I = 'C', M = 'G'
- then AA = "Arg"
- else if I = 'A', M = 'G'
- then (if is_purine_base(F) then AA = "Arg" else AA = "Ser")
- else if I = 'G', M = 'G'
- then AA = "Gly"
- else AA = "Xxx"
- ) else (
- AA = ""
- ).
- :- pred is_purine_base(char::in) is semidet.
- is_purine_base(B) :- (B = 'A' ; B = 'G').
- :- pred is_pyrimidine_base(char::in) is semidet.
- is_pyrimidine_base(B) :- (B = 'T' ; B = 'C').
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement