notpron

Deutsche Lösungen zu Notpron
git clone git://xatko.vsos.ethz.ch/notpron.git
Log | Files | Refs

commit 20300a51328d8f8c0243090ceaff6a3972e45220
parent 6e88bc07a3085bd2460a0950a0868d6f9a189198
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Wed,  2 Sep 2015 20:44:51 +0200

D-Programm für DNS-Strang verbessert.

Diffstat:
Objects/Program/D/BasePair.d | 186+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 172 insertions(+), 14 deletions(-)

diff --git a/Objects/Program/D/BasePair.d b/Objects/Program/D/BasePair.d @@ -1,22 +1,70 @@ import std.stdio; import std.range; +import std.algorithm; +static immutable AminoAcid.Desc aD[]=[ + AminoAcid.Desc("Alanine", "Ala", 'A'), + AminoAcid.Desc("Cyxteine", "Cys", 'C'), + AminoAcid.Desc("Aspartic acid", "Asp", 'D'), + AminoAcid.Desc("Glutamic acid", "Glu", 'E'), + AminoAcid.Desc("Phenylalanine", "Phe", 'F'), + AminoAcid.Desc("Glycine", "Gly", 'G'), + AminoAcid.Desc("Histidine", "His", 'H'), + AminoAcid.Desc("Isoleucine", "Ile", 'I'), + AminoAcid.Desc("Lysine", "Lys", 'K'), + AminoAcid.Desc("Leucine", "Leu", 'L'), + AminoAcid.Desc("Methionine", "Met", 'M'), + AminoAcid.Desc("Asparagine", "Asn", 'N'), + AminoAcid.Desc("Proline", "Pro", 'P'), + AminoAcid.Desc("Glutamine", "Gln", 'Q'), + AminoAcid.Desc("Arginine", "Arg", 'R'), + AminoAcid.Desc("Serine", "Ser", 'S'), + AminoAcid.Desc("Threonine", "Thr", 'T'), + AminoAcid.Desc("Valine", "Val", 'V'), + AminoAcid.Desc("Tryptophan", "Trp", 'W'), + AminoAcid.Desc("Tyrosine", "Tyr", 'Y'), +]; +immutable(AminoAcid.Desc)* findAA(char shortcut){ + auto res=aD.assumeSorted.find!"a.shortcut==b"(shortcut); + if(res.empty){ + return null; + } + return &(res.front()); +} + struct AminoAcid{ - string name; + struct Desc{ + string longname; + string shortname; + char shortcut; + } + immutable(AminoAcid.Desc) *desc; bool start; bool stop; - this(string str, bool start=false, bool stop=false){ - name=str; - this.start=start; - this.stop=stop; - } this(char sname, bool start=false, bool stop=false){ - name=""~sname; + desc=findAA(sname); this.start=start; this.stop=stop; } + string toString(bool shrt=true){ + if(start){ + return "START"; + } + else if(stop){ + return "STOP"; + } + else if(desc==null) + return "*"; + return (shrt) ? desc.shortname : desc.longname; + } + char toChar(){ + if(desc==null) + return ' '; + return desc.shortcut; + } } + struct AminoAcidTable{ private immutable static char[] AminoAcids = "FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG"; private immutable static char[] Starts = "---M---------------M---------------M----------------------------"; @@ -35,16 +83,126 @@ struct AminoAcidTable{ table[am]=AminoAcid(AminoAcids[i],(Starts[i]!='-'),(AminoAcids[i]=='*')); } } + + AminoAcid* opIndex(in DNS.Codon c){ + return (c.c in table); + } + + AminoAcid* opIndex(in RNS.Codon c){ + return opIndex(c.dns); + } + + auto opCall(Range)(Range r) if(isInputRange!Range && (is(ElementType!Range == RNS.Codon) || is(ElementType!Range == DNS.Codon))){ + return r.map!(a=>*this[a]); + } + + auto opCall(in DNS d){ + return d.seq.map!(a=>*this[a]); + } + auto opCall(in RNS d){ + return d.seq.map!(a=>*this[a]); + } +} +template CodonSeq(){ + struct Codon{ + char[3]c; + this(char[3] c){ + this.c=c; + } + this(Range)(Range r){ + foreach(i; 0..3){ + c[i]=cast(char)r.front; + r.popFront(); + } + } + } +} +///Swaps A with B and C with A +void BaseSwap(ref char str[3], in char A, in char B, in char C){ + foreach(ref c; str){ + if(c==A){ + c=B; + } + else if(c==C){ + c=A; + } + } +} +///Swaps A with B and vice versa +void BaseSwap(ref char str[3], in char A, in char B){ + foreach(ref c; str){ + if(c==A){ + c=B; + } + else if(c==B){ + c=A; + } + } +} + +RNS.Codon rns(in DNS.Codon d){ + RNS.Codon c=RNS.Codon(d.c); + c.c.BaseSwap('A','U','T'); + c.c.BaseSwap('G','C'); + return c; +} +DNS.Codon dns(in RNS.Codon d){ + DNS.Codon c=DNS.Codon(d.c); + c.c.BaseSwap('A','T','U'); + c.c.BaseSwap('G','C'); + return c; +} + +struct DNS{ + mixin CodonSeq; + Codon seq[]; + this(Range)(Range r) if(isInputRange!Range && is(ElementType!Range==Codon)){ + r.copy(seq.appender); + } + this(Range)(Range r) if(isInputRange!Range && isSomeChar!(ElementType!Range)){ + foreach(s; r.chunks(3)){ + seq~=Codon(s); + } + } + auto rns(){ + return seq.map!(a=>a.rns); + } + auto aa(ref AminoAcidTable aat){ + return aat(seq); + } +} +struct RNS{ + mixin CodonSeq; + Codon seq[]; + this(Range)(Range r) if(isInputRange!Range && isSomeChar!(ElementType!Range)){ + foreach(s; r.chunks(3)){ + seq~=Codon(s); + } + } + this(Range)(Range r) if(isInputRange!Range && is(ElementType!Range==Codon)){ + auto app=appender(seq); + r.copy(app); + seq=app.data; + } + auto dns(){ + return seq.map!(a=>a.dns); + } + auto aa(ref AminoAcidTable aat){ + return aat(seq); + } } static AminoAcidTable aat; -void main(){ - string str="GTTGCTCTTGAAAATACTATTAATGAA"; +auto aaString(Range)(Range r){ + return r.map!(a=>a.toChar()); +} +auto aalString(Range)(Range r){ + return r.map!(a=>a.toString()).joiner(",\t"); +} + +void main(string args[]){ aat.initialize(); - writeln(aat.table.byValue()); - foreach(seq; str.chunks(3)){ - char[] d=seq.map!(a=>cast(char)a).array; - write(aat.table[d[0..3]].name); - } + DNS d=DNS(args[1]); + writeln(aat(d).aaString()); }