void ComposedElementTest::testOperatorNotEqual() { // initializes alphabet Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); // initializes elements Element elementH(*hydrogen); Element elementO(*oxygen); Element elementC(*carbon); elements_container elements; elements[elementH] = 4; elements[elementO] = 2; elements[elementC] = 2; // checks case with different elements composed_element_type molecule1(elements); elements[elementH] = 5; composed_element_type molecule1_not_equal(elements); CPPUNIT_ASSERT(molecule1 != molecule1_not_equal); // checks case with different sequences composed_element_type molecule3("C2H4O2", cho), molecule3_not_equal("C2H3O2", cho); CPPUNIT_ASSERT(molecule3 != molecule3_not_equal); }
void ComposedElementTest::testUpdateIsotopeDistribution() { Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); std::vector<unsigned int> decomposition; decomposition.push_back(static_cast<unsigned int>(4)); decomposition.push_back(static_cast<unsigned int>(2)); decomposition.push_back(static_cast<unsigned int>(2)); // initializes molecule without setting isotope distribution and sequence composed_element_type molecule(decomposition, cho); molecule.updateIsotopeDistribution(); isotopes_type isodistr = molecule.getIsotopeDistribution(); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getMass(0), static_cast<mass_type>(60.02113), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getMass(1), static_cast<mass_type>(61.0245862), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getMass(2), static_cast<mass_type>(62.0254827), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getMass(3), static_cast<mass_type>(63.0288285), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getAbundance(0), static_cast<abundance_type>(0.972690002), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getAbundance(1), static_cast<abundance_type>(0.0231608083), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getAbundance(2), static_cast<abundance_type>(0.00405304857), 1.0e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(isodistr.getAbundance(3), static_cast<abundance_type>(9.1561898e-05), 1.0e-6); }
void ComposedElementTest::testOperatorEqual() { // checks for type of constructors with sequence Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); composed_element_type molecule1("H4C2O2", cho); composed_element_type molecule1_equal("H4C2O2", cho); CPPUNIT_ASSERT(molecule1 == molecule1_equal); // checks for type of constructors with elements Element elementH(*hydrogen); Element elementO(*oxygen); Element elementC(*carbon); elements_container elements; elements[elementH] = 4; elements[elementO] = 2; elements[elementC] = 2; // checks for type of constructors with elements and random sequence order composed_element_type molecule2(elements); composed_element_type molecule2_equal(elements); CPPUNIT_ASSERT(molecule2 == molecule2_equal); }
// ---------------------------------------------- // PLAIN DECODER // ---------------------------------------------- SegmentDecoder::SegmentDecoder( Model *model, int agenda) { this->m_Model = model; this->m_Agenda = agenda; Alphabet *labelAlpha = model->getAlphabet("LABELS"); this->m_NumLabels = labelAlpha->size(); m_Legal = new int *[m_NumLabels + 1]; for (int i = 0; i <= m_NumLabels; ++ i) { char prev = 'X'; if (i < m_NumLabels) prev = labelAlpha->rlookup(i)[0]; m_Legal[i] = new int[m_NumLabels]; for (int j = 0; j < m_NumLabels; ++ j) { char curr = labelAlpha->rlookup(j)[0]; m_Legal[i][j] = 0; if ((prev == 'X' || prev == 'S' || prev == 'E') && (curr == 'S' || curr == 'B')) m_Legal[i][j] = 1; if ((prev == 'M' || prev == 'B') && (curr == 'M' || curr == 'E')) m_Legal[i][j] = 1; } } }
void ComposedElementTest::testConstructorDecompositionAlphabet() { Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); std::vector<unsigned int> decomposition; decomposition.push_back(static_cast<unsigned int>(3)); decomposition.push_back(static_cast<unsigned int>(2)); decomposition.push_back(static_cast<unsigned int>(1)); composed_element_type molecule(decomposition, cho); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("C"), static_cast<elements_container::mapped_type>(2)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("H"), static_cast<elements_container::mapped_type>(3)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("O"), static_cast<elements_container::mapped_type>(1)); // checks if isotope distribution is not yet initialized CPPUNIT_ASSERT(molecule.getIsotopeDistribution().empty()); // checks if sequence is not yet initialized CPPUNIT_ASSERT(molecule.getSequence().empty()); }
void LZ78::encoding(Stream &stream, Alphabet &alpha, File &file) { LzNode *curr = root; char c; // Para cada caractere de entrada while(stream.hasNext()) { c = stream.next(); // Se já existe um nó com o caractere c' if(curr->child.count(c)) { curr = curr->child[c]; // Não existe a substring procurada, então .. } else { // printf(" (%2d, %c)", curr->id, c); serialize_int(file, curr->id); serialize_int(file, alpha.getIndex(c)); // Criar um novo nó LzNode *new_node = new LzNode(nextId++); curr->child.insert(std::make_pair(c, new_node)); curr = root; } } // Terminal // printf(" (%2d, 0)\n", curr->id); serialize_int(file, curr->id); serialize_int(file, alpha.getIndex('\0')); }
void Alphabets::LoadTxt(lem::Iridium::Macro_Parser &txt, Dictionary &dict) { lem::UCString name(txt.read().string()); if (Find(name) != UNKNOWN) { dict.GetIO().merr().printf( "Alphabet [%us] is already declared\n" , name.c_str() ); throw E_ParserError(); } const int id = storage->AddAlphabet(name); Alphabet *a = new Alphabet(id, name); a->LoadTxt(txt, dict); storage->StoreAlphabet(*a); name2id.insert(std::make_pair(name, id)); id2alphabet.insert(std::make_pair(id, a)); alphabets.push_back(a); return; }
bool TableTest_resolveNotClosed() { // Table is not closed Alphabet a; a.addSymbol('0'); a.addSymbol('1'); // Figure 2 ObservationTable table1; table1.addEntry("", "", true); table1.addEntry("0", "", false); table1.addEntry("1", "", false); table1.addStringToS(""); table1.addStringToE(""); if (table1.getS().count("0") != 0) { return false; } table1.resolveNotClosed(a); if (table1.getS().count("0") != 1) { return false; } return true; }
void State::build_word(bool allowed, string* word, size_t len) { if (len == 0) { return; } if (this->error_state) { if (allowed) { return; } } if (this->get_end_state() && allowed) { cout << "Accepting word: " << *word << endl; } else if (!this->get_end_state() && !allowed) { cout << "Rejecting word: " << *word << endl; } Alphabet* alpha = Alphabet::get_alphabet(); std::string* alpha_str = alpha->get_string(); for (size_t i = 0; i < alpha->get_size(); i++) { string tmp_string = *word; char c = (*alpha_str)[i]; tmp_string.push_back(c); this->map_transitions[c]->build_word(allowed, &tmp_string, len - 1); } }
void ComposedElementTest::testOperatorMinus() { // initializes alphabet Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); composed_element_type molecule1("H10O6", cho), molecule2("O4H4", cho); molecule1 -= molecule2; CPPUNIT_ASSERT_EQUAL(molecule1.getElements().size(), static_cast<elements_container::size_type>(2)); CPPUNIT_ASSERT_EQUAL(molecule1.getElementAbundance("O"), static_cast<elements_container::mapped_type>(2)); CPPUNIT_ASSERT_EQUAL(molecule1.getElementAbundance("H"), static_cast<elements_container::mapped_type>(6)); composed_element_type molecule3("O8H2", cho), molecule4("O6H7C5", cho); molecule3 -= molecule4; CPPUNIT_ASSERT_EQUAL(molecule3.getElements().size(), static_cast<elements_container::size_type>(1)); CPPUNIT_ASSERT_EQUAL(molecule3.getElementAbundance("O"), static_cast<elements_container::mapped_type>(2)); }
int main() { Alphabet *alphabet = new Alphabet(); vector<int> key; key.push_back(2); key.push_back(3); key.push_back(1); key.push_back(4); isValidKey(key, alphabet->getLength()); string message = "breathjavaeatcpp"; cout << "message = [" << message << "]" << endl; string encrypted = encrypt(key, alphabet, message); cout << "encrypted = [" << encrypted << "]" << endl; string decrypted = decrypt(key, alphabet, encrypted); cout << "decrypted = [" << decrypted << "]" << endl; delete alphabet; return 0; }
void State::reset_error() { Alphabet* alpha = Alphabet::get_alphabet(); for (int idx = 0; idx < alpha->get_string()->size(); idx++) { char c = alpha->get_string()->at(idx); this->map_transitions[c] = this->get_error(); } }
bool Base58::decode (const char* psz, Blob& vchRet, Alphabet const& alphabet) { CAutoBN_CTX pctx; vchRet.clear (); CBigNum bn58 = 58; CBigNum bn = 0; CBigNum bnChar; while (isspace (*psz)) psz++; // Convert big endian string to bignum for (const char* p = psz; *p; p++) { // VFALCO TODO Make this use the inverse table! // Or better yet ditch this and call raw_decode // const char* p1 = strchr (alphabet.chars(), *p); if (p1 == nullptr) { while (isspace (*p)) p++; if (*p != '\0') return false; break; } bnChar.setuint (p1 - alphabet.chars()); if (!BN_mul (&bn, &bn, &bn58, pctx)) throw bignum_error ("DecodeBase58 : BN_mul failed"); bn += bnChar; } // Get bignum as little endian data Blob vchTmp = bn.getvch (); // Trim off sign byte if present if (vchTmp.size () >= 2 && vchTmp.end ()[-1] == 0 && vchTmp.end ()[-2] >= 0x80) vchTmp.erase (vchTmp.end () - 1); // Restore leading zeros int nLeadingZeros = 0; for (const char* p = psz; *p == alphabet.chars()[0]; p++) nLeadingZeros++; vchRet.assign (nLeadingZeros + vchTmp.size (), 0); // Convert little endian data to big endian std::reverse_copy (vchTmp.begin (), vchTmp.end (), vchRet.end () - vchTmp.size ()); return true; }
MayaNumber(const Alphabet& alphabet, const string& lines) : alphabet_(alphabet) { size_t n = alphabet.height() * alphabet.width(); for (int i = 0; i < lines.length(); i += n) { symbols_.push_back(alphabet.fromValue(lines.substr(i, n))); } }
bool TableTest_resolveNotConsistent() { // Table is not consistent Alphabet a; a.addSymbol('0'); a.addSymbol('1'); // Figure 8 ObservationTable table1; table1.addStringToS(""); table1.addStringToS("0"); table1.addStringToS("1"); table1.addStringToS("11"); table1.addStringToS("01"); table1.addStringToS("011"); table1.addStringToE(""); table1.addStringToE("0"); table1.addEntry("", "", true); table1.addEntry("", "0", false); table1.addEntry("0", "", false); table1.addEntry("0", "0", true); table1.addEntry("1", "", false); table1.addEntry("1", "0", false); table1.addEntry("11", "", true); table1.addEntry("11", "0", false); table1.addEntry("01", "", false); table1.addEntry("01", "0", false); table1.addEntry("011", "", false); table1.addEntry("011", "0", true); table1.addEntry("00", "", true); table1.addEntry("00", "0", false); table1.addEntry("10", "", false); table1.addEntry("10", "0", false); table1.addEntry("110", "", false); table1.addEntry("110", "0", true); table1.addEntry("111", "", false); table1.addEntry("111", "0", false); table1.addEntry("010", "", false); table1.addEntry("010", "0", false); table1.addEntry("0110", "", true); table1.addEntry("0110", "0", false); table1.addEntry("0111", "", false); table1.addEntry("0111", "0", false); if (table1.getE().count("1") != 0) { return false; } table1.resolveNotConsistent(a); if (table1.getE().count("1") != 1) { return false; } return true; }
bool AlphabetTools::checkAlphabetCodingSize(const Alphabet& alphabet) throw (AlphabetException) { if (alphabet.getNumberOfChars() == 0) return true; // Will this really happen? size_t size = alphabet.intToChar(0).size(); for (int i = 1; i < static_cast<int>(alphabet.getNumberOfTypes()); ++i) { if (alphabet.intToChar(i).size() != size) return false; } return true; }
MayaNumber(const Alphabet& alphabet, const int value) : alphabet_(alphabet) { size_t tempVal = value; while (tempVal >= alphabet.num()) { size_t d = tempVal / alphabet.num(); symbols_.push_back(alphabet.fromValue(d)); tempVal %= alphabet.num(); } symbols_.push_back(alphabet.fromValue(tempVal)); }
MatrixXf SubstitutionMatrix::get_array(const Alphabet& abc) const { size_t n = abc.get_canonical_size(); MatrixXf m(n, n); int i, j; for (std::map<SymbolPair, double>::const_iterator pos = score.begin(); pos != score.end(); ++pos) { i = abc.get_idx((pos->first).first); j = abc.get_idx((pos->first).second); m(i, j) = pos->second; m(j, i) = pos->second; } return m; }
/** Prints one decomposition to stdout */ void printDecomposition(const Alphabet& alphabet, const decomposition_t& decomposition) { assert(alphabet.size() == decomposition.size()); bool first = true; for (Alphabet::size_type i = 0; i < alphabet.size(); ++i) { if (decomposition[i] > 0) { if (!first) { cout << " "; } else { first = false; } cout << alphabet.getName(i) << decomposition[i]; } } }
otws_t OTWS_Load(const char *cfg_file) { // allocate config parser and set default config. ltp_configure *cfg = new ltp_configure(); cfg->set_cfg("target", "test"); cfg->set_cfg("agenda", "1"); cfg->set_cfg("model", "./"); cfg->set_cfg("dict", "./"); // load config. if (-1 == cfg->load_cfg(cfg_file)) { WARNING_LOG("Failed to load config file"); delete cfg; return NULL; } OTWS_Engine *engine = new OTWS_Engine(); // allocate alphabet Alphabet *features = new HashDict(); Alphabet *labels = new HashDict(); Alphabet *words = new HashDict(); Parameter *param = new CParameter(0, 0); // load model. Model *model = new Model(); model->registAlphabet("FEATURES", features); model->registAlphabet("LABELS", labels); model->registParameter("PARAMETER", param); model->loadModel(cfg->config("model").c_str()); TRACE_LOG("Loading model is done."); TRACE_LOG("Num Features: %d", features->size()); TRACE_LOG("Num Labels: %d", labels->size()); Extractor *extractor = new SegmentExtractor( cfg->config("dict").c_str(), features, labels, words); Decoder *decoder = new SegmentDecoder(model, 1); engine->model = model; engine->extractor = extractor; engine->decoder = decoder; return reinterpret_cast<otws_t>(engine); }
CategoricalVariable(std::string value, Alphabet<std::string>& alphabet) : Variable<CategoricalVariable<counts_type>, counts_type >(true), HasValue<int>(alphabet.get_index(value)), Categorical<std::string>(alphabet), _lower(-1), _upper(-1){ }
IMM::IMM(BOOM::Vector<TrainingSequence*> &v,int order, int minSampleSize,int phase,ContentType contentType, Strand strand) : N(order), alphabetSize(alphabet.getNumElements()), phase(phase), revComp(NULL), models(new BOOM::Vector<BOOM::StringMap<double>*>) { setContentType(contentType); if(strand==EITHER_STRAND) strand=::getStrand(contentType); setStrand(strand); buildModels(v,minSampleSize); if(strand==FORWARD_STRAND) { BOOM::Vector<TrainingSequence*> rcSeqs; revCompSeqs(v,rcSeqs); revComp=new IMM(rcSeqs,order,minSampleSize,phase, ::reverseComplement(contentType), REVERSE_STRAND); revComp->revComp=this; } }
void printHeader(const Alphabet& alphabet, const Weights& weights, Options::Mode mode) { cout << "# " PROGRAM_NAME " " PROGRAM_VERSION "\n" "# Copyright 2006 Informatics for Mass Spectrometry group\n" "# at Bielefeld University\n" "#\n" "# http://BiBiServ.TechFak.Uni-Bielefeld.DE/decomp/\n" "#\n" "# alphabet (character, weight):\n"; for (Weights::size_type i = 0; i < weights.size(); ++i) { cout << "#\t" << alphabet.getName(i) << "\t" << weights[i] << '\n'; } cout << "#\n# Problem to solve: "; switch (mode) { case Options::GETNUMBER: cout << "Get number of decompositions"; break; case Options::FINDALL: cout << "Find all decompositions"; break; case Options::FINDONE: cout << "Find one decomposition"; break; case Options::ISDECOMPOSABLE: cout << "Is decomposable"; break; } cout << "\n#\n"; }
void LZ78::decoding(File &file, Alphabet &alpha, Stream &stream, int textSize) { lista.clear(); lista.reserve(101 + textSize / 5); char c; while(! file.terminated) { int pos = deserialize_int(file); int idx = deserialize_int(file); // Se há algo a ser adicionado (ou seja, se não é a raiz) if(pos) push_recursive(stream, pos - 1); c = alpha.getChar(idx); // Terminal if(c == '\0') break; // Atualiza a lista lista.push_back(make_pair(pos, c)); // Escreve o caractere stream.write(c); } }
bool Base58::raw_decode (char const* first, char const* last, void* dest, std::size_t size, bool checked, Alphabet const& alphabet) { CAutoBN_CTX pctx; CBigNum bn58 = 58; CBigNum bn = 0; CBigNum bnChar; // Convert big endian string to bignum for (char const* p = first; p != last; ++p) { int i (alphabet.from_char (*p)); if (i == -1) return false; bnChar.setuint ((unsigned int) i); int const success (BN_mul (&bn, &bn, &bn58, pctx)); assert (success); (void) success; bn += bnChar; } // Get bignum as little endian data Blob vchTmp = bn.getvch (); // Trim off sign byte if present if (vchTmp.size () >= 2 && vchTmp.end ()[-1] == 0 && vchTmp.end ()[-2] >= 0x80) vchTmp.erase (vchTmp.end () - 1); char* const out (static_cast <char*> (dest)); // Count leading zeros int nLeadingZeros = 0; for (char const* p = first; p!=last && *p==alphabet[0]; p++) nLeadingZeros++; // Verify that the size is correct if (vchTmp.size() + nLeadingZeros != size) return false; // Fill the leading zeros memset (out, 0, nLeadingZeros); // Copy little endian data to big endian std::reverse_copy (vchTmp.begin (), vchTmp.end (), out + nLeadingZeros); if (checked) { char hash4 [4]; fourbyte_hash256 (hash4, out, size - 4); if (memcmp (hash4, out + size - 4, 4) != 0) return false; } return true; }
void ComposedElementTest::testConstructorTexNotationSequenceAlphabet() { Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); composed_element_type molecule("C_{2}H_{4}O_{2}", cho, composed_element_type::TEX_NOTATION_MOLECULE_SEQUENCE_TYPE); CPPUNIT_ASSERT_EQUAL(molecule.getSequence(), static_cast<name_type>("C_{2}H_{4}O_{2}")); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("C"), static_cast<elements_container::mapped_type>(2)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("H"), static_cast<elements_container::mapped_type>(4)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("O"), static_cast<elements_container::mapped_type>(2)); // checks if isotope distribution is not yet initialized CPPUNIT_ASSERT(molecule.getIsotopeDistribution().empty()); }
ProfileFelsenstein::ProfileFelsenstein(const MultSeqAlignment &A, const Alphabet &alphabet, const AlphabetMap &alphabetMap) : alignment(A), alphabet(alphabet), numAlpha(alphabet.size()), alphabetMap(alphabetMap) { // ctor }
bool Application::match(const String &pattern,EmissionSequence &S, int begin,int len) { int end=begin+len; for(int i=begin ; i<end ; ++i) if(S[i].getDiscrete(0)!=alphabet.lookup(pattern[i-begin])) return false; return true; }
int Application::main(int argc,char *argv[]) { // Process command line cmd=new CommandLine(argc,argv,""); if(cmd->numArgs()!=2) throw String("\ndelete-target-gaps <in.maf> <target-name>\n"); String mafFile=cmd->arg(0); String targetName=cmd->arg(1); // Setting up alphabet alphabet=&DnaDashDotAlphabet::global(); gapSymbols.addMember(alphabet->lookup('-')); gapSymbols.addMember(alphabet->lookup('.')); gapSymbols.addMember(alphabet->lookup('N')); alphabetMap=new DropGapMapping(*alphabet,PureDnaAlphabet::global()); // Load the alignments //int rootID=phylogeny->getRoot()->getID(); alignment=NULL; ifstream is(mafFile.c_str()); if(!is.good()) throw String("Can't open file ")+mafFile; while(!is.eof()) { MultiAlignment *a=new MultiAlignment; a->loadMAF(is); if(a->getNumTracks()>0) { a->toupper(); MultSeqAlignment *m=new MultSeqAlignment(*a,*alphabet,gapSymbols); int rootID=m->getTrackByName(targetName).getID(); m->deleteTargetGaps(rootID); if(!alignment) alignment=m; else { alignment->append(*m); delete m; } } delete a; } is.close(); // Emit the processed alignment alignment->printSlice(cout,0,alignment->getLength(),'+',60); return 0; }
void ComposedElementTest::testConstructorSequenceAlphabet() { Alphabet cho; cho.push_back(*hydrogen); cho.push_back(*carbon); cho.push_back(*oxygen); composed_element_type molecule("C2H4O2", cho); CPPUNIT_ASSERT_EQUAL(molecule.getSequence(), static_cast<name_type>("C2H4O2")); CPPUNIT_ASSERT_EQUAL(molecule.getElements().size(), static_cast<elements_container::size_type>(3)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("C"), static_cast<elements_container::mapped_type>(2)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("H"), static_cast<elements_container::mapped_type>(4)); CPPUNIT_ASSERT_EQUAL(molecule.getElementAbundance("O"), static_cast<elements_container::mapped_type>(2)); // checks if isotope distribution is not yet initialized CPPUNIT_ASSERT(molecule.getIsotopeDistribution().empty()); }