void SoyRef::Increment() { auto AlphabetIndexes = ToAlphabetIndexes( mRef ); auto& Alphabet = GetAlphabet(); // start at the back and increment auto MaxIndex = Alphabet.GetSize()-1; for ( ssize_t i=AlphabetIndexes.GetSize()-1; i>=0; i-- ) { // increment index AlphabetIndexes[i]++; if ( AlphabetIndexes[i] <= MaxIndex ) break; // rolled over... AlphabetIndexes[i] = 0; // ... let i-- and we increment previous char } // if the "final integer" is reached, we'll end up with 000000000 (ie, invalid). // if this is the case, we need to increment the alphabet, or start packing. // gr: actually, this will never happen. 0000000 is _________ (not invalid!) assert( IsValid() ); // convert back mRef = FromAlphabetIndexes( AlphabetIndexes ); }
//--------------РОЗШИФРУВАННЯ------------------------------------------------ void __fastcall TForm1::Button2Click(TObject *Sender) { // зчитування введених даних std::string txt = Memo1->Text.c_str(); std::string pr_key = Edit2->Text.c_str(); std:string res = Decipherment(txt,pr_key,GetAlphabet()); //виведення результату Form2->Memo1->Text = res.c_str(); Form2->ShowModal(); }
uint64 FromAlphabetIndexes(const BufferArray<size_t,SoyRef::MaxStringLength>& Indexes) { // turn back to chars and cram into a u64 uint64Chars Ref64Chars; assert( Indexes.GetSize() == SoyRef::MaxStringLength ); auto& Alphabet = GetAlphabet(); for ( int i=0; i<Indexes.GetSize(); i++ ) { auto Index = Indexes[i]; Ref64Chars.mChars[i] = Alphabet[Index]; } return Ref64Chars.m64; }
uint64 SoyRef::FromString(const std::string& String) { // make up indexes uint64Chars Ref64Chars; //auto& AlphabetLookup = GetAlphabetLookup(); GetAlphabetLookup(); auto& Alphabet = GetAlphabet(); for ( int i=0; i<SoyRef::MaxStringLength; i++ ) { char StringChar = i < String.length() ? String[i] : Alphabet[0]; CorrectAlphabetChar( StringChar ); Ref64Chars.mChars[i] = StringChar; } return Ref64Chars.m64; }
std::string SoyRef::ToString() const { // turn integer into alphabet indexes auto AlphabetIndexes = ToAlphabetIndexes( mRef ); auto& Alphabet = GetAlphabet(); // concat alphabet chars std::string String; for ( int i=0; i<AlphabetIndexes.GetSize(); i++ ) { auto Index = AlphabetIndexes[i]; char Char = Alphabet[Index]; String += Char; } return String; }
const BufferArray<size_t,256>& GetAlphabetLookup() { auto& AlphabetLookup = Private::gAlphabetLookup; // generate if ( AlphabetLookup.IsEmpty() ) { auto& Alphabet = GetAlphabet(); AlphabetLookup.SetSize( 256 ); for ( int i=0; i<256; i++ ) { auto AlphabetIndex = Alphabet.FindIndex( static_cast<char>(i) ); // non-alphabet characters turn into #0 (default char) AlphabetLookup[i] = (AlphabetIndex < 0) ? 0 : AlphabetIndex; } } return AlphabetLookup; }
void CorrectAlphabetChar(char& Char) { auto& AlphabetLookup = GetAlphabetLookup(); auto& Alphabet = GetAlphabet(); Char = Alphabet[ AlphabetLookup[Char] ]; }
//Function to initialize the Felsenstein Pruning Tables on the Inner Nodes of the Tree //This is not done simulataneously with the tree creation because the Substitution Matrices are needed for the //Felsenstein Probablilities estimation, and for these, the corresponding branch lengths are required (i.e. the tree has to be fully created first) void PairInitializer::InitTreeFPTables() { //Nodes A, B, C and D { for (int k=0;k<4;k++){ FTree->Get_TreeNodes()[k].N1.FPTable.resize(4, DoubleVec(Seqs->Get_BareSeqs()[k].size(),0)); for (int i=0; i<Seqs->Get_BareSeqs()[k].size(); i++) { FTree->Get_TreeNodes()[k].N1.FPTable[GetAlphabet(Seqs->Get_BareSeqs()[k][i])][i]= 1.0; } FTree->Get_TreeNodes()[k].N2.FPTable.resize(4, DoubleVec(Seqs->Get_BareSeqs()[k].size(),0)); FTree->Get_TreeNodes()[k].N3.FPTable.resize(4, DoubleVec(Seqs->Get_BareSeqs()[k].size(),0)); } } //Node E { //InnerNode N1 conditioned on Nodes A and B double len1 = FTree->Get_TreeNodes()[4].N2.Length; int idx1 = GetPosOfElement(OverallSubMatsLengths, len1, 0); double len2 = FTree->Get_TreeNodes()[4].N3.Length; int idx2 = GetPosOfElement(OverallSubMatsLengths, len2, 0); FTree->Get_TreeNodes()[4].N1.FPTable.resize(4, DoubleVec(0,0)); for (int j=0; j<AlignmentHomologies[4].size(); j++) { if (AlignmentHomologies[4][j]==-1) { continue; } for (int i=0; i<4; i++) { //For each possibility, meaning A,C,G or T double prob = 1; if (AlignmentHomologies[0][j]!=-1) { prob *= OverallSubMats[idx1][GetAlphabetPair(i, Seqs->Get_BareSeqs()[0][AlignmentHomologies[0][j]-1])]; } if (AlignmentHomologies[1][j]!=-1) { prob *= OverallSubMats[idx2][GetAlphabetPair(i, Seqs->Get_BareSeqs()[1][AlignmentHomologies[1][j]-1])]; } FTree->Get_TreeNodes()[4].N1.FPTable[i].push_back(prob); } } } //Node 6 { //InnerNode N1 conditioned on Nodes C and D double len1 = FTree->Get_TreeNodes()[5].N2.Length; int idx1 = GetPosOfElement(OverallSubMatsLengths, len1, 0); double len2 = FTree->Get_TreeNodes()[5].N3.Length; int idx2 = GetPosOfElement(OverallSubMatsLengths, len2, 0); FTree->Get_TreeNodes()[5].N1.FPTable.resize(4, DoubleVec(0,0)); for (int j=0; j<AlignmentHomologies[5].size(); j++) { if (AlignmentHomologies[5][j]==-1) { continue; } for (int i=0; i<4; i++) { //For each possibility, meaning A,C,G or T double prob = 1; if (AlignmentHomologies[2][j]!=-1) { prob *= OverallSubMats[idx1][GetAlphabetPair(i, Seqs->Get_BareSeqs()[2][AlignmentHomologies[2][j]-1])]; } if (AlignmentHomologies[3][j]!=-1) { prob *= OverallSubMats[idx2][GetAlphabetPair(i, Seqs->Get_BareSeqs()[3][AlignmentHomologies[3][j]-1])]; } FTree->Get_TreeNodes()[5].N1.FPTable[i].push_back(prob); } } } //Node 5 - InnerNodes N2 and N3 { //InnerNode N2 conditioned on Nodes B, C and D { double len1 = FTree->Get_TreeNodes()[4].N3.Length; int idx1 = GetPosOfElement(OverallSubMatsLengths, len1, 0); double len2 = FTree->Get_TreeNodes()[4].N1.Length; int idx2 = GetPosOfElement(OverallSubMatsLengths, len2, 0); FTree->Get_TreeNodes()[4].N2.FPTable.resize(4, DoubleVec(0,0)); for (int j=0; j<AlignmentHomologies[4].size(); j++) { if (AlignmentHomologies[4][j]==-1) { continue; } for (int i=0; i<4; i++) { //For each possibility, meaning A,C,G or T double prob = 1; if (AlignmentHomologies[1][j]!=-1) { prob *= OverallSubMats[idx1][GetAlphabetPair(i, Seqs->Get_BareSeqs()[1][AlignmentHomologies[1][j]-1])]; } //if (FTree->Get_TreeNodes()[4]->Get_Homologies()[5][j]!=-1) { if (AlignmentHomologies[5][j]!=-1) { double sum = 0; for (int k=0; k<4; k++) { sum += OverallSubMats[idx2][GetAlphabetPair(i, k)]* (FTree->Get_TreeNodes()[5].N1.FPTable[k][AlignmentHomologies[5][j]-1]); } prob *= sum; } FTree->Get_TreeNodes()[4].N2.FPTable[i].push_back(prob); } } } //InnerNode N3 conditioned on Nodes A, C and D { double len1 = FTree->Get_TreeNodes()[4].N2.Length; int idx1 = GetPosOfElement(OverallSubMatsLengths, len1, 0); double len2 = FTree->Get_TreeNodes()[4].N1.Length; int idx2 = GetPosOfElement(OverallSubMatsLengths, len2, 0); FTree->Get_TreeNodes()[4].N3.FPTable.resize(4, DoubleVec(0,0)); for (int j=0; j<AlignmentHomologies[4].size(); j++) { if (AlignmentHomologies[4][j]==-1) { continue; } for (int i=0; i<4; i++) { //For each possibility, meaning A,C,G or T double prob = 1; if (AlignmentHomologies[0][j]!=-1) { prob *= OverallSubMats[idx1][GetAlphabetPair(i, Seqs->Get_BareSeqs()[0][AlignmentHomologies[0][j]-1])]; } if (AlignmentHomologies[5][j]!=-1) { double sum = 0; for (int k=0; k<4; k++) { sum += OverallSubMats[idx2][GetAlphabetPair(i, k)]* (FTree->Get_TreeNodes()[5].N1.FPTable[k][AlignmentHomologies[5][j]-1]); } prob *= sum; } FTree->Get_TreeNodes()[4].N3.FPTable[i].push_back(prob); } } } } //Node 6 - InnerNodes N2 and N3 { //InnerNode N2 conditioned on Nodes A, B and D { double len1 = FTree->Get_TreeNodes()[5].N3.Length; int idx1 = GetPosOfElement(OverallSubMatsLengths, len1, 0); double len2 = FTree->Get_TreeNodes()[5].N1.Length; int idx2 = GetPosOfElement(OverallSubMatsLengths, len2, 0); FTree->Get_TreeNodes()[5].N2.FPTable.resize(4, DoubleVec(0,0)); for (int j=0; j<AlignmentHomologies[5].size(); j++) { if (AlignmentHomologies[5][j]==-1) { continue; } for (int i=0; i<4; i++) { //For each possibility, meaning A,C,G or T double prob = 1; if (AlignmentHomologies[3][j]!=-1) { prob *= OverallSubMats[idx1][GetAlphabetPair(i, Seqs->Get_BareSeqs()[3][AlignmentHomologies[3][j]-1])]; } if (AlignmentHomologies[4][j]!=-1) { double sum = 0; for (int k=0; k<4; k++) { sum += OverallSubMats[idx2][GetAlphabetPair(i, k)]* (FTree->Get_TreeNodes()[4].N1.FPTable[k][AlignmentHomologies[4][j]-1]); } prob *= sum; } FTree->Get_TreeNodes()[5].N2.FPTable[i].push_back(prob); } } } //InnerNode N3 conditioned on Nodes A, B and C { double len1 = FTree->Get_TreeNodes()[5].N2.Length; int idx1 = GetPosOfElement(OverallSubMatsLengths, len1, 0); double len2 = FTree->Get_TreeNodes()[5].N1.Length; int idx2 = GetPosOfElement(OverallSubMatsLengths, len2, 0); FTree->Get_TreeNodes()[5].N3.FPTable.resize(4, DoubleVec(0,0)); for (int j=0; j<AlignmentHomologies[5].size(); j++) { if (AlignmentHomologies[5][j]==-1) { continue; } for (int i=0; i<4; i++) { //For each possibility, meaning A,C,G or T double prob = 1; if (AlignmentHomologies[2][j]!=-1) { prob *= OverallSubMats[idx1][GetAlphabetPair(i, Seqs->Get_BareSeqs()[2][AlignmentHomologies[2][j]-1])]; } if (AlignmentHomologies[4][j]!=-1) { double sum = 0; for (int k=0; k<4; k++) { sum += OverallSubMats[idx2][GetAlphabetPair(i, k)]* (FTree->Get_TreeNodes()[4].N1.FPTable[k][AlignmentHomologies[4][j]-1]); } prob *= sum; } FTree->Get_TreeNodes()[5].N3.FPTable[i].push_back(prob); } } } } }