00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "gn/gnFastTranslator.h"
00013 #include <iostream>
00014
00015
00016 const gnFastTranslator *gnFastTranslator::ProteinDNATranslator(){
00017 const static gnFastTranslator* t_trans = new gnFastTranslator(ProteinDNATranslatorType);
00018 return t_trans;
00019 }
00020 const gnFastTranslator *gnFastTranslator::DNAProteinTranslator(){
00021 const static gnFastTranslator* t_trans = new gnFastTranslator(DNAProteinTranslatorType);
00022 return t_trans;
00023 }
00024
00025
00026 gnFastTranslator::gnFastTranslator()
00027 {
00028 use_default = false;
00029 m_defaultChar = 0;
00030 }
00031
00032 gnFastTranslator::gnFastTranslator( const gnFastTranslator &sf )
00033 {
00034 m_name = sf.m_name;
00035 use_default = sf.use_default;
00036 m_defaultChar = sf.m_defaultChar;
00037 m_transCache = sf.m_transCache;
00038 }
00039 gnFastTranslator::gnFastTranslator( gnTranslatorType t_type )
00040 {
00041 use_default = false;
00042 m_defaultChar = 0;
00043 switch(t_type){
00044 case ProteinDNATranslatorType:
00045 CacheTranslator(gnTranslator::ProteinDNATranslator(), "FLIMVPTAY.HQNKDECGSR", 1);
00046 break;
00047 case DNAProteinTranslatorType:
00048 CacheTranslator(gnTranslator::DNAProteinTranslator(), "ACGTRYKMBVDHSWNX", 3);
00049 break;
00050 }
00051 }
00052
00053
00054 gnSeqC gnFastTranslator::Filter( const gnSeqC ch ) const{
00055
00056
00057
00058
00059
00060 return m_defaultChar;
00061 }
00062
00063 void gnFastTranslator::Filter( gnSeqC** seq, uint32& len ) const{
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 }
00093
00094 void gnFastTranslator::Filter( string &seq ) const{
00095 uint32 curpos = 0, outpos = 0;
00096 uint32 len = seq.length();
00097 uint32 width = m_transCache.begin()->first.length();
00098 uint32 out_width = m_transCache.begin()->second.length();
00099 uint32 out_size = (seq.length() / width) * out_width + seq.length() % width + 1;
00100 gnSeqC* output_array = new gnSeqC[out_size];
00101 output_array[out_size-1] = 0;
00102 string seq_upper;
00103 while(curpos < len){
00104
00105 seq_upper = seq.substr(curpos, width);
00106 for(uint32 i=0; i < seq_upper.size(); i++)
00107 seq_upper[i] = toupper(seq_upper[i]);
00108
00109 map<string, string>::const_iterator iter = m_transCache.find(seq_upper);
00110
00111 if(iter == m_transCache.end()){
00112
00113 if(use_default)
00114 output_array[curpos] = m_defaultChar;
00115 curpos++;
00116 }else{
00117 iter->second.copy(output_array + outpos, out_width);
00118 curpos += width;
00119 outpos += out_width;
00120 }
00121 }
00122 seq = output_array;
00123 }
00124
00125 void gnFastTranslator::CacheTranslator(const gnTranslator* tranny, string inputs, const gnSeqI input_width){
00126 string cur_input;
00127 string cur_trans;
00128 vector<gnSeqI> index;
00129 gnSeqI cur_index = input_width;
00130
00131
00132 for(gnSeqI curI = 0; curI < input_width; curI++)
00133 index.push_back(0);
00134
00135 while(true){
00136
00137 cur_index = input_width - 1;
00138 while(index[cur_index] == inputs.length()){
00139 if(cur_index == 0){
00140 return;
00141 }
00142 index[cur_index] = 0;
00143 cur_index--;
00144 index[cur_index]++;
00145 continue;
00146 }
00147
00148
00149 for(gnSeqI i = 0; i < input_width; i++){
00150 cur_input += inputs[index[i]];
00151 }
00152 cur_trans = cur_input;
00153 tranny->Filter(cur_trans);
00154 m_transCache[cur_input] = cur_trans;
00155
00156
00157 cur_input = "";
00158 index[input_width - 1]++;
00159 }
00160 }