NCBI C++ ToolKit
codons_stringlist_validator.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: codons_stringlist_validator.cpp 47479 2023-05-02 13:24:02Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Colleen Bollin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
37 
38 #include <serial/objectinfo.hpp>
39 
41 #include "string_list_ctrl.hpp"
42 
45 
46 /*
47  * CSerialStringListValidator
48  */
49 
50 
52  : m_Object(object)
53 {
54 }
55 
56 
58  : wxValidator(), m_Object(val.m_Object)
59 {
60 }
61 
62 
64 {
65  CStringListCtrl *control = (CStringListCtrl*)m_validatorWindow;
66  control->Clear();
67  size_t maxStringsCount = control->GetMaxStringsCount();
68 
69  const CTrna_ext& trna = dynamic_cast<const CTrna_ext&>(m_Object);
70  if (trna.IsSetCodon()) {
71  ITERATE( CTrna_ext::TCodon, iter, trna.GetCodon() ) {
72  if (*iter == 255 || *iter > 63) continue;
73 
74  string codon = CGen_code_table::IndexToCodon(*iter);
75  control->AddString(codon);
76  if (--maxStringsCount == 0)
77  break;
78  }
79  }
80 
81  if (maxStringsCount > 0)
82  control->AddString("");
83 
84  control->FitInside();
85 
86  return true;
87 }
88 
89 
90 static bool s_IsATGC(char ch)
91 {
92  if (ch == 'A' || ch == 'T' || ch == 'G' || ch == 'C' || ch == 'U') {
93  return true;
94  } else {
95  return false;
96  }
97 }
98 
99 
100 static const string kAmbiguities = "MRSVWYHKDBN";
101 static const string kReplacements[] = {
102  "AC", "AG", "CG", "ACG", "AT", "CT", "ACT", "GT", "AGT", "CGT", "ACGT" };
103 
104 static const string s_GetExpansion (const string& ch)
105 {
106  size_t pos = NStr::Find(kAmbiguities, ch);
107  if (pos != string::npos) {
108  return kReplacements[pos];
109  } else {
110  return ch;
111  }
112 }
113 
114 
115 static vector<string> ParseDegenerateCodons (string codon)
116 {
117  vector<string> replacements;
118 
119  if (codon.length() == 3 && s_IsATGC(codon.c_str()[0])) {
120  string this_codon = codon.substr(0, 1);
121  replacements.push_back(this_codon);
122 
123  for (int i = 1; i < 3; i++) {
124  string ch = s_GetExpansion (codon.substr(i, 1));
125  auto num_now = replacements.size();
126  // add copies for each expansion letter beyond the first
127  for (unsigned int j = 1; j < ch.length(); j++) {
128  for (auto k = 0; k < num_now; k++) {
129  string cpy = replacements[k];
130  replacements.push_back(cpy);
131  }
132  }
133  for (int k = 0; k < num_now; k++) {
134  for (unsigned int j = 0; j < ch.length(); j++) {
135  replacements[j * num_now + k].append(ch.substr(j, 1));
136  }
137  }
138  }
139  } else {
140  replacements.push_back(codon);
141  }
142  return replacements;
143 }
144 
145 
147 {
148  CTrna_ext& trna = dynamic_cast<CTrna_ext&>(m_Object);
149 
150  // reset existing codons
151  trna.ResetCodon();
152 
153  //now add new ones
154  CStringListCtrl *control = (CStringListCtrl *)m_validatorWindow;
155  CStringListCtrl::CConstIterator it(*control);
156 
157  if (it) {
158  for (; it; ++it) {
159  string codon = it.GetValue();
161  NStr::ToUpper(codon);
162  if (!codon.empty()) {
163  vector<string> codons = ParseDegenerateCodons(codon);
164  for (unsigned int j = 0; j < codons.size(); j++) {
166  if (val > -1) {
167  trna.SetCodon().push_back(val);
168  }
169  }
170  }
171  }
172  }
173  return true;
174 }
175 
User-defined methods of the data storage class.
CCodonsStringListValidator(CSerialObject &object)
static string IndexToCodon(int index)
static int CodonToIndex(char base1, char base2, char base3)
Base class for all serializable objects.
Definition: serialbase.hpp:150
bool AddString(const string &value)
size_t GetMaxStringsCount() const
USING_SCOPE(objects)
static bool s_IsATGC(char ch)
static vector< string > ParseDegenerateCodons(string codon)
static const string s_GetExpansion(const string &ch)
static const string kAmbiguities
static const string kReplacements[]
const TResidue codons[4][4]
Definition: gnomon_seq.cpp:76
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:3201
static SIZE_TYPE Find(const CTempString str, const CTempString pattern, ECase use_case=eCase, EDirection direction=eForwardSearch, SIZE_TYPE occurrence=0)
Find the pattern in the string.
Definition: ncbistr.cpp:2891
static string & ToUpper(string &str)
Convert string to upper case – string& version.
Definition: ncbistr.cpp:424
const TCodon & GetCodon(void) const
Get the Codon member data.
Definition: Trna_ext_.hpp:624
TCodon & SetCodon(void)
Assign a value to Codon data member.
Definition: Trna_ext_.hpp:630
bool IsSetCodon(void) const
codon(s) as in Genetic-code Check if a value has been assigned to Codon data member.
Definition: Trna_ext_.hpp:612
list< int > TCodon
Definition: Trna_ext_.hpp:306
void ResetCodon(void)
Reset Codon data member.
Definition: Trna_ext_.cpp:147
int i
Modified on Mon Apr 22 04:01:48 2024 by modify_doxy.py rev. 669887