Changeset 151


Ignore:
Timestamp:
02/19/10 19:08:58 (7 months ago)
Author:
reed
Message:

added several distance matrix output formats; removed identiy out in aln and fasta formats

Location:
current/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • current/src/align.cpp

    r142 r151  
    2727using namespace std; 
    2828 
    29 #ifdef min 
    30 #       undef min 
    31 #endif 
    32  
    3329template<class T> 
    3430inline T min3(T a, T b, T c) 
    3531{ 
    36         return min(a, min(b,c)); 
     32        return (std::min)(a, (std::min)(b,c)); 
    3733} 
    3834 
     
    6763         
    6864        // Allocate travel table to maximum possible size 
    69         size_t szA = std::min(szMa,seqA.size())+1; 
     65        size_t szA = (std::min)(szMa,seqA.size())+1; 
    7066        tabTravel.resize(szA); 
    71         size_t szB = std::min(szMb,seqB.size())+1; 
     67        size_t szB = (std::min)(szMb,seqB.size())+1; 
    7268        for(travel_table::iterator it = tabTravel.begin(); 
    7369            it != tabTravel.end(); ++it) 
     
    582578} 
    583579 
     580double alignment::identity() const { 
     581        aln_data::size_type matches = 0, mismatches = 0; 
     582        std::string::const_iterator ait = seqA.dna.begin(), bit = seqB.dna.begin(); 
     583        for(aln_data::const_iterator cit = data.begin(); cit != data.end(); ++cit) { 
     584                if(*cit == 0) { 
     585                        if(*ait == *bit) 
     586                                ++matches; 
     587                        else 
     588                                ++mismatches; 
     589                        ++ait; 
     590                        ++bit; 
     591                } else if(*cit > 0) 
     592                        bit+=*cit; 
     593                else 
     594                        ait-=*cit; 
     595        } 
     596        return (1.0*matches)/(matches+mismatches); 
     597} 
     598 
  • current/src/align.h

    r146 r151  
    4242        inline void print(OS &os, int format=0, double cost=0.0, int dir=0, bool swapped=false) const; 
    4343         
     44        double identity() const; 
     45         
    4446protected: 
    4547        const seq_data &seqA, &seqB; 
     
    5052}; 
    5153 
    52 class aligner 
    53 { 
     54class aligner { 
    5455public: 
    55         struct indel 
    56         { 
     56        struct indel { 
    5757                indel() { } 
    5858                indel(size_t pp, size_t xx, double dd) 
     
    6262                double d; // Score 
    6363        }; 
    64         struct min_mid_cost 
    65         { 
     64        struct min_mid_cost { 
    6665                double c; // Minimum cost 
    6766                size_t s; // minimum position in s-vector 
     
    141140 
    142141        std::string::const_iterator ait = seqA.dna.begin(), bit = seqB.dna.begin(); 
    143         unsigned int matches = 0, mismatches = 0; 
    144142        for(aln_data::const_iterator cit = data.begin(); cit != data.end(); ++cit) { 
    145143                if(*cit == 0) { 
    146                         if(*ait == *bit) { 
     144                        if(*ait == *bit) 
    147145                                strC.append(1, '*'); 
    148                                 ++matches; 
    149                         } else { 
     146                        else 
    150147                                strC.append(1, ' '); 
    151                                 ++mismatches; 
    152                         } 
    153148                        strA.append(ait, ait+1); ++ait; 
    154149                        strB.append(bit, bit+1); ++bit; 
     
    173168        size_t sz = strA.size();         
    174169        std::string ss; 
    175         double identity = double(matches)/(matches+mismatches); 
    176170        if(format == 0) { 
    177171                os << "CLUSTAL multiple sequence alignment (Created by " << PACKAGE_STRING 
    178                    << "; Cost = " << std::setprecision(10) << cost  << " Identity = " << identity 
     172                   << "; Cost = " << std::setprecision(10) << cost 
    179173                   << ")" << std::endl << std::endl; 
    180174 
     
    199193                os << ">" << nameA << " "  
    200194                   << "(Created by " << PACKAGE_STRING 
    201                    << "; Cost = " << std::setprecision(10) << cost  << " Identity = " << identity 
     195                   << "; Cost = " << std::setprecision(10) << cost 
    202196                   << ")" << std::endl; 
    203197                for(size_t u = 0; u < sz; u += 80) 
  • current/src/ngila.cpp

    r150 r151  
    2020#include <iomanip> 
    2121#include <iostream> 
     22#include <limits> 
    2223 
    2324#include <boost/preprocessor.hpp> 
    2425#include <boost/foreach.hpp> 
    2526#include <boost/config.hpp> 
     27//BOOST_DISABLE_ASSERTS 
    2628#include <boost/multi_array.hpp> 
     29#include <boost/algorithm/string/predicate.hpp> 
    2730 
    2831#include "ngila_app.h" 
     
    9699} 
    97100 
    98 template<size_t _N> 
    99 size_t key_switch(const std::string &ss, const std::string (&key)[_N]) { 
    100         for(size_t i=0;i<_N;++i) { 
    101                 if(key[i].find(ss) == 0) 
     101template<std::size_t _N> 
     102std::size_t key_switch(const std::string &ss, const std::string (&key)[_N]) { 
     103        using boost::algorithm::starts_with; 
     104        for(std::size_t i=0;i<_N;++i) { 
     105                if(starts_with(key[i], ss)) 
    102106                        return i; 
    103107        } 
    104         return (size_t)-1; 
     108        return (std::size_t)-1; 
    105109} 
    106110 
     
    169173        string pairs_keys[] = { string("first"), string("all"), string("each") }; 
    170174        seq_db::size_type table_size = mydb.size(); 
    171         switch(key_switch(arg.pairs, pairs_keys)) 
    172         { 
     175         
     176        switch(key_switch(arg.pairs, pairs_keys)) { 
    173177        case 0: 
    174178                pvec.push_back(make_pair(0,1)); 
     
    189193        }; 
    190194         
    191         string format_keys[] = { string("aln"), string("fasta"), string("smat") /*,string("imat")*/ }; 
     195        string format_keys[] = { 
     196                string("aln"), string("fasta"), 
     197                string("dist"), string("dist-c"), 
     198                string("dist-d"), string("dist-i") 
     199        }; 
    192200        int out_format = 0; 
    193201        if(!arg.output.empty()) { 
     
    223231                } 
    224232        } 
    225          
    226         typedef boost::multi_array<float, 2> tabmat; 
    227         tabmat cost_table(boost::extents[table_size][table_size]); 
    228  
    229         for(pair_vec::const_iterator cit = pvec.begin(); cit != pvec.end(); ++cit) 
    230         { 
    231                 if(cit != pvec.begin()) 
    232                         cout << "//" << endl; 
     233        ostream &myout = fout.is_open() ? static_cast<ostream&>(fout) 
     234                                        : static_cast<ostream&>(cout); 
     235        typedef boost::multi_array<float, 2> dist_mat; 
     236        dist_mat dist_table; 
     237        const bool do_dist = (out_format >= 2); 
     238        if(do_dist) { 
     239                dist_table.resize(boost::extents[table_size][table_size]); 
     240                fill(dist_table.data(), dist_table.data()+dist_table.num_elements(), numeric_limits<float>::quiet_NaN()); 
     241                float diag = (out_format == 5) ? 1.0f : 0.0f; 
     242                for(seq_db::size_type i = 0; i < table_size; ++i) 
     243                        dist_table[i][i] = diag; 
     244        } 
     245         
     246        for(pair_vec::const_iterator cit = pvec.begin(); cit != pvec.end(); ++cit) { 
     247                if(!do_dist && cit != pvec.begin()) 
     248                        myout << "//" << endl; 
    233249                // swap a and b so that a's hashed position is lower 
    234250                size_t a = cit->first; 
     
    243259                alignment aln(mydb[a], mydb[b]); 
    244260                double dcost = alner.align(aln); 
    245                         dcost += pmod->offset(mydb[a].dna, 
    246                                       mydb[b].dna); 
     261                dcost += pmod->offset(mydb[a].dna, mydb[b].dna); 
     262                double dident = aln.identity(); 
    247263                 
    248                 if(fout.is_open()) { 
    249                         aln.print(fout, out_format, dcost, 
     264                if(!do_dist) { 
     265                        aln.print(myout, out_format, dcost, 
    250266                                ((arg.const_align & 16) ? 0 : direction), swapped); 
    251267                } else { 
    252                         aln.print(cout, out_format, dcost, 
    253                                 ((arg.const_align & 16) ? 0 : direction), swapped); 
     268                        double upper,lower; 
     269                        if(a > b) 
     270                                swap(a,b); 
     271                        switch(out_format) { 
     272                        default: 
     273                        case 2: //dist 
     274                                lower = 1.0-dident; 
     275                                upper = dcost; 
     276                                break; 
     277                        case 3: //dist-c 
     278                                upper = lower = dcost; 
     279                                break; 
     280                        case 4: //dist-d 
     281                                upper = lower = 1.0-dident; 
     282                                break; 
     283                        case 5: //dist-i 
     284                                upper = lower = dident; 
     285                                break; 
     286                        } 
     287                        dist_table[a][b] = (float)upper; 
     288                        dist_table[b][a] = (float)lower; 
     289                } 
     290        } 
     291        if(do_dist) { 
     292                myout << setprecision(10); 
     293                for(seq_db::size_type i = 0; i < table_size; ++i) { 
     294                        for(seq_db::size_type j = 0; j < table_size-1; ++j) 
     295                                myout << dist_table[i][j] << "\t"; 
     296                        myout << dist_table[i][table_size-1] << endl; 
    254297                } 
    255298        } 
  • current/src/sort.h

    • Property svn:eol-style set to native
  • current/src/xm.h

    • Property svn:eol-style set to native
Note: See TracChangeset for help on using the changeset viewer.