src/objtools/blast_format/showdefline.cpp

Go to the documentation of this file.
00001 /*  $Id: showdefline.cpp 148871 2009-01-05 16:51:12Z camacho $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Author:  Jian Ye
00027  *
00028  * File Description:
00029  *   Display blast defline
00030  *
00031  */
00032 
00033 #ifndef SKIP_DOXYGEN_PROCESSING
00034 static char const rcsid[] = "$Id: showdefline.cpp 148871 2009-01-05 16:51:12Z camacho $";
00035 #endif /* SKIP_DOXYGEN_PROCESSING */
00036 
00037 #include <ncbi_pch.hpp>
00038 #include <corelib/ncbiexpt.hpp>
00039 #include <corelib/ncbiutil.hpp>
00040 #include <corelib/ncbistre.hpp>
00041 #include <corelib/ncbireg.hpp>
00042 #include <serial/objostrasnb.hpp> 
00043 #include <serial/objistrasnb.hpp> 
00044 #include <connect/ncbi_conn_stream.hpp>
00045 
00046 #include <objects/general/Object_id.hpp>
00047 #include <objects/general/User_object.hpp>
00048 #include <objects/general/User_field.hpp>
00049 #include <objects/general/Dbtag.hpp>
00050 
00051 #include <serial/iterator.hpp>
00052 #include <serial/objistr.hpp>
00053 #include <serial/objostr.hpp>
00054 #include <serial/serial.hpp>
00055 
00056 #include <objmgr/object_manager.hpp>
00057 #include <objmgr/scope.hpp>
00058 #include <objmgr/util/sequence.hpp>
00059 #include <objects/seq/Bioseq.hpp>
00060 #include <objects/seq/Seq_descr.hpp>
00061 #include <objects/seq/Seqdesc.hpp>
00062 #include <objects/seqloc/Seq_id.hpp>
00063 
00064 #include <objects/seqalign/Seq_align_set.hpp>
00065 #include <objects/seqalign/Score.hpp>
00066 #include <objects/seqalign/Std_seg.hpp>
00067 #include <objects/seqalign/Dense_diag.hpp>
00068 #include <objects/seqalign/Dense_seg.hpp>
00069 #include <objects/blastdb/Blast_def_line.hpp>
00070 #include <objects/blastdb/Blast_def_line_set.hpp>
00071 #include <objects/blastdb/defline_extra.hpp>
00072 #include <objtools/blast_format/showdefline.hpp>
00073 #include <objtools/blast_format/blastfmtutil.hpp>
00074 
00075 #include <stdio.h>
00076 #include <html/htmlhelper.hpp>
00077 
00078 
00079 BEGIN_NCBI_SCOPE
00080 USING_SCOPE(objects);
00081 USING_SCOPE(sequence);
00082 
00083 //margins constant
00084 
00085 static string kOneSpaceMargin = " ";
00086 static string kTwoSpaceMargin = "  ";
00087 
00088 //const strings
00089 static const string kHeader = "Sequences producing significant alignments:";
00090 static const string kScore = "Score";
00091 static const string kE = "E";
00092 static const string kBits = 
00093     (getenv("CTOOLKIT_COMPATIBLE") ? "(bits)" : "(Bits)");
00094 static const string kEvalue = "E value";
00095 static const string kValue = "Value";
00096 static const string kN = "N";
00097 static const string kRepeatHeader = "Sequences used in model and found again:";
00098 static const string kNewSeqHeader = "Sequences not found previously or not pr\
00099 eviously below threshold:";
00100 static const string kMaxScore = "Max score";
00101 static const string kTotalScore = "Total score";
00102 static const string kTotal = "Total";
00103 static const string kIdentity = "Max ident";
00104 static const string kPercent = "Percent";
00105 static const string kHighest = "Highest";
00106 static const string kQuery = "Query";
00107 static const string kCoverage = "Query coverage";
00108 static const string kEllipsis = "...";
00109 
00110 //psiblast related
00111 static const string kPsiblastNewSeqGif = "<IMG SRC=\"images/new.gif\" \
00112 WIDTH=30 HEIGHT=15 ALT=\"New sequence mark\">";
00113 
00114 static const string kPsiblastNewSeqBackgroundGif = "<IMG SRC=\"images/\
00115 bg.gif\" WIDTH=30 HEIGHT=15 ALT=\" \">";
00116 
00117 static const string kPsiblastCheckedBackgroundGif = "<IMG SRC=\"images\
00118 /bg.gif\" WIDTH=15 HEIGHT=15 ALT=\" \">";
00119 
00120 static const string kPsiblastCheckedGif = "<IMG SRC=\"images/checked.g\
00121 if\" WIDTH=15 HEIGHT=15 ALT=\"Checked mark\">";
00122 
00123 static const string kPsiblastEvalueLink = "<a name = Evalue></a>";
00124 
00125 static const string  kPsiblastCheckboxChecked = "<INPUT TYPE=\"checkbox\" NAME\
00126 =\"checked_GI\" VALUE=\"%d\" CHECKED>  <INPUT TYPE=\"hidden\" NAME =\"good_G\
00127 I\" VALUE = \"%d\">";
00128 
00129 static const string  kPsiblastCheckbox =  "<INPUT TYPE=\"checkbox\" NAME=\"ch\
00130 ecked_GI\" VALUE=\"%d\">  ";
00131 
00132 
00133 ///Return url for seqid
00134 ///@param ids: input seqid list
00135 ///@param gi: gi to use
00136 ///@param user_url: use this user specified url if it's non-empty
00137 ///@param is_db_na: is the database nucleotide or not
00138 ///@param db_name: name of the database
00139 ///@param open_new_window: click the url to open a new window?
00140 ///@param rid: RID
00141 ///@param query_number: the query number
00142 ///@param cur_align: index of the current alignment
00143 ///
00144 static string s_GetIdUrl(const CBioseq::TId& ids, int gi, string& user_url,
00145                          bool is_db_na, string& db_name, bool open_new_window, 
00146                          string& rid, int query_number, int taxid, int linkout,
00147                          int cur_align)
00148 {
00149     string url_link = NcbiEmptyString;
00150     CConstRef<CSeq_id> wid = FindBestChoice(ids, CSeq_id::WorstRank);
00151     char dopt[32], db[32];
00152     char logstr_moltype[32], logstr_location[32];
00153  
00154     bool hit_not_in_mapviewer = !(linkout & eHitInMapviewer);
00155     
00156     if (user_url != NcbiEmptyString && 
00157         !((user_url.find("dumpgnl.cgi") != string::npos && gi > 0) || 
00158           (user_url.find("maps.cgi") != string::npos && hit_not_in_mapviewer))) {
00159         
00160         string url_with_parameters = 
00161             CBlastFormatUtil::BuildUserUrl(ids, taxid, user_url,
00162                                            db_name,
00163                                            is_db_na, rid,
00164                                            query_number,
00165                                            false);
00166         if (url_with_parameters != NcbiEmptyString) {
00167             url_link += "<a href=\"";
00168             url_link += url_with_parameters;
00169             url_link += "\">";
00170         }
00171     } else { 
00172         //use entrez or dbtag specified     
00173         char url_buf[2048];
00174         if (gi > 0) {
00175             if(is_db_na) {
00176                 strcpy(dopt, "GenBank");
00177                 strcpy(db, "Nucleotide");
00178                 strcpy(logstr_moltype, "nucl");
00179             } else {
00180                 strcpy(dopt, "GenPept");
00181                 strcpy(db, "Protein");
00182                 strcpy(logstr_moltype, "prot");
00183             }    
00184             strcpy(logstr_location, "top");
00185 
00186             string l_EntrezUrl = CBlastFormatUtil::GetURLFromRegistry("ENTREZ");            
00187             sprintf(url_buf, l_EntrezUrl.c_str(), "", db, gi, dopt, rid.c_str(),
00188                     logstr_moltype, logstr_location, cur_align,
00189                     open_new_window ? "TARGET=\"EntrezView\"" : "");
00190             url_link = url_buf;
00191         } else {//seqid general, dbtag specified
00192             if(wid->Which() == CSeq_id::e_General){
00193                 const CDbtag& dtg = wid->GetGeneral();
00194                 const string& dbname = dtg.GetDb();
00195                 if(NStr::CompareNocase(dbname, "TI") == 0){
00196                     string actual_id;
00197                     wid->GetLabel(&actual_id, CSeq_id::eContent);
00198                     sprintf(url_buf, kTraceUrl.c_str(), "", actual_id.c_str(),
00199                             rid.c_str());
00200                     url_link = url_buf;
00201                 }
00202             }
00203         }
00204     }
00205     
00206     return url_link;
00207 }
00208 
00209 
00210 string 
00211 CShowBlastDefline::GetSeqIdListString(const list<CRef<objects::CSeq_id> >& id,
00212                                       bool show_gi)
00213 {
00214     string id_str = NcbiEmptyString;
00215 
00216     ITERATE(list<CRef<CSeq_id> >, itr, id) {
00217         string id_token;
00218         // For local ids, make sure the "lcl|" part is not printed
00219         if ((*itr)->IsLocal())
00220             (*itr)->GetLabel(&id_token, CSeq_id::eContent, 0);
00221         else if (show_gi || !(*itr)->IsGi())
00222             id_token = (*itr)->AsFastaString();
00223         if (id_token != NcbiEmptyString)
00224             id_str += id_token + "|";
00225     }
00226     if (id_str.size() > 0)
00227         id_str.erase(id_str.size() - 1);
00228 
00229     return id_str;
00230 }
00231 
00232 void 
00233 CShowBlastDefline::GetSeqIdList(const objects::CBioseq_Handle& bh,
00234                                 list<CRef<objects::CSeq_id> >& ids)
00235 {
00236     ids.clear();
00237 
00238     // Check for ids of type "gnl|BL_ORD_ID". These are the artificial ids
00239     // created in a BLAST database when it is formatted without indexing.
00240     // For such ids, create new fake local Seq-ids, saving the first token of 
00241     // the Bioseq's title, if it's available.
00242     ITERATE(CBioseq_Handle::TId, itr, bh.GetId()) {
00243         CRef<CSeq_id> next_seqid(new CSeq_id());
00244         string id_token = NcbiEmptyString;
00245         
00246         if (next_seqid->IsGeneral() &&
00247             next_seqid->AsFastaString().find("gnl|BL_ORD_ID") 
00248             != string::npos) {
00249             vector<string> title_tokens;
00250             id_token = 
00251                 NStr::Tokenize(sequence::GetTitle(bh), " ", title_tokens)[0];
00252         }
00253         if (id_token != NcbiEmptyString) {
00254             // Create a new local id with a label containing the extracted
00255             // token and save it in the next_seqid instead of the original 
00256             // id.
00257             CObject_id* obj_id = new CObject_id();
00258             obj_id->SetStr(id_token);
00259             next_seqid->SetLocal(*obj_id);
00260         } else {
00261                 next_seqid->Assign(*itr->GetSeqId());
00262         }
00263         ids.push_back(next_seqid);
00264     }
00265 }
00266 
00267 void
00268 CShowBlastDefline::GetBioseqHandleDeflineAndId(const CBioseq_Handle& handle,
00269                                                list<int>& use_this_gi,
00270                                                string& seqid, string& defline, 
00271                                                bool show_gi /* = true */,
00272                                                int this_gi_first /* = -1 */)
00273 {
00274     // Retrieve the CBlast_def_line_set object and save in a CRef, preventing
00275     // its destruction; then extract the list of CBlast_def_line objects.
00276     CRef<CBlast_def_line_set> bdlRef = 
00277         CBlastFormatUtil::GetBlastDefline(handle);
00278     bdlRef->PutTargetGiFirst(this_gi_first);
00279     const list< CRef< CBlast_def_line > >& bdl = bdlRef->Get();
00280 
00281     if (bdl.empty()){
00282         list<CRef<objects::CSeq_id> > ids;
00283         GetSeqIdList(handle, ids);
00284         seqid = GetSeqIdListString(ids, show_gi);
00285         defline = GetTitle(handle);
00286     } else { 
00287         bool is_first = true;
00288         ITERATE(list<CRef<CBlast_def_line> >, iter, bdl) {
00289             const CBioseq::TId& cur_id = (*iter)->GetSeqid();
00290             int cur_gi =  FindGi(cur_id);
00291             int gi_in_use_this_gi = 0;
00292             ITERATE(list<int>, iter_gi, use_this_gi){
00293                 if(cur_gi == *iter_gi){
00294                     gi_in_use_this_gi = *iter_gi;                 
00295                     break;
00296                 }
00297             }
00298             if(use_this_gi.empty() || gi_in_use_this_gi > 0) {
00299                 if (is_first)
00300                     seqid = GetSeqIdListString(cur_id, show_gi);
00301 
00302                 if((*iter)->IsSetTitle()){
00303                     if(is_first){
00304                         defline = (*iter)->GetTitle();
00305                     } else {
00306                         string concat_acc;
00307                         CConstRef<CSeq_id> wid = 
00308                             FindBestChoice(cur_id, CSeq_id::WorstRank);
00309                         wid->GetLabel(&concat_acc, CSeq_id::eFasta, 0);
00310                         if( show_gi && cur_gi > 0){
00311                             defline = defline + " >" + "gi|" + 
00312                                 NStr::IntToString(cur_gi) + "|" + 
00313                                 concat_acc + " " + (*iter)->GetTitle();
00314                         } else {
00315                             defline = defline + " >" + concat_acc + " " + 
00316                                 (*iter)->GetTitle();
00317                         }
00318                     }
00319                     is_first = false;
00320                 }
00321             }
00322         }
00323     }
00324 }
00325 
00326 void CShowBlastDefline::x_FillDeflineAndId(const CBioseq_Handle& handle,
00327                                            const CSeq_id& aln_id,
00328                                            list<int>& use_this_gi,
00329                                            SDeflineInfo* sdl,
00330                                            int blast_rank)
00331 {
00332 
00333     const CRef<CBlast_def_line_set> bdlRef = CBlastFormatUtil::GetBlastDefline(handle);
00334     const list< CRef< CBlast_def_line > >& bdl = bdlRef->Get();
00335     const CBioseq::TId* ids = &handle.GetBioseqCore()->GetId();
00336     CRef<CSeq_id> wid;
00337     sdl->defline = NcbiEmptyString;
00338  
00339     sdl->gi = 0;
00340     sdl->id_url = NcbiEmptyString;
00341     sdl->score_url = NcbiEmptyString;
00342     sdl->linkout = 0;
00343     sdl->is_new = false;
00344     sdl->was_checked = false;
00345 
00346     //get psiblast stuff
00347     
00348     if(m_SeqStatus){
00349         string aln_id_str;
00350         aln_id.GetLabel(&aln_id_str, CSeq_id::eContent);
00351         PsiblastSeqStatus seq_status = eUnknown;
00352         
00353         TIdString2SeqStatus::const_iterator itr = m_SeqStatus->find(aln_id_str);
00354         if ( itr != m_SeqStatus->end() ){
00355             seq_status = itr->second;
00356         }
00357         if((m_PsiblastStatus == eFirstPass) ||
00358            ((m_PsiblastStatus == eRepeatPass) && (seq_status & eRepeatSeq))
00359            || ((m_PsiblastStatus == eNewPass) && (seq_status & eRepeatSeq))){
00360             if(!(seq_status & eGoodSeq)){
00361                 sdl->is_new = true;
00362             }
00363             if(seq_status & eCheckedSeq){
00364                 sdl->was_checked = true;
00365             }   
00366         }
00367     }
00368         
00369     //get id
00370     if(bdl.empty()){
00371         wid = FindBestChoice(*ids, CSeq_id::WorstRank);
00372         sdl->id = wid;
00373         sdl->gi = FindGi(*ids);    
00374     } else {        
00375         bool found = false;
00376         for(list< CRef< CBlast_def_line > >::const_iterator iter = bdl.begin();
00377             iter != bdl.end(); iter++){
00378             const CBioseq::TId* cur_id = &((*iter)->GetSeqid());
00379             int cur_gi =  FindGi(*cur_id);
00380             wid = FindBestChoice(*cur_id, CSeq_id::WorstRank);
00381             if (!use_this_gi.empty()) {
00382                 ITERATE(list<int>, iter_gi, use_this_gi){
00383                     if(cur_gi == *iter_gi){
00384                         found = true;
00385                         break;
00386                     }
00387                 }
00388             } else {
00389                 ITERATE(CBioseq::TId, iter_id, *cur_id) {
00390                     if ((*iter_id)->Match(aln_id)) {
00391                         found = true;
00392                     }
00393                 }
00394             }
00395             if(found){
00396                 sdl->id = wid;
00397                 sdl->gi = cur_gi;
00398                 ids = cur_id;
00399                 break;
00400             }
00401         }
00402     }
00403 
00404     //get linkout
00405     if((m_Option & eLinkout)){
00406         bool linkout_not_found = true;
00407         for(list< CRef< CBlast_def_line > >::const_iterator iter = bdl.begin();
00408             iter != bdl.end(); iter++){
00409             const CBioseq::TId& cur_id = (*iter)->GetSeqid();
00410             int cur_gi =  FindGi(cur_id);            
00411             if(use_this_gi.empty()){
00412                 if(sdl->gi == cur_gi){                 
00413                     sdl->linkout = CBlastFormatUtil::GetLinkout((**iter));
00414                     sdl->linkout_list =
00415                         CBlastFormatUtil::GetLinkoutUrl(sdl->linkout,
00416                                            cur_id, m_Rid, 
00417                                            m_CddRid, 
00418                                            m_EntrezTerm, 
00419                                            handle.GetBioseqCore()->IsNa(),
00420                                            0, true, false,
00421                                            blast_rank);
00422                     break;
00423                 }
00424             } else {
00425                 ITERATE(list<int>, iter_gi, use_this_gi){
00426                     if(cur_gi == *iter_gi){                     
00427                         sdl->linkout = CBlastFormatUtil::GetLinkout((**iter));
00428                         sdl->linkout_list = 
00429                            CBlastFormatUtil::GetLinkoutUrl(sdl->linkout,
00430                                            cur_id, m_Rid, 
00431                                            m_CddRid, 
00432                                            m_EntrezTerm, 
00433                                            handle.GetBioseqCore()->IsNa(),
00434                                            0, true, false,
00435                                            blast_rank);
00436                         linkout_not_found = false;
00437                         break;
00438                     }
00439                 }
00440                 if(!linkout_not_found){
00441                     break;
00442                 } 
00443             }
00444             
00445         }
00446     }
00447     //get score and id url
00448     if(m_Option & eHtml){
00449         string accession;
00450         sdl->id->GetLabel(&accession, CSeq_id::eContent);
00451         sdl->score_url = "<a href=#";
00452         sdl->score_url += sdl->gi == 0 ? accession : 
00453             NStr::IntToString(sdl->gi);
00454         sdl->score_url += ">";
00455 
00456         string user_url = m_Reg->Get(m_BlastType, "TOOL_URL");
00457         string type_temp = m_BlastType;
00458         type_temp = NStr::TruncateSpaces(NStr::ToLower(type_temp));
00459         int taxid = 0;
00460         if (type_temp == "mapview" || type_temp == "mapview_prev" || 
00461             type_temp == "gsfasta") {
00462             taxid = 
00463                 CBlastFormatUtil::GetTaxidForSeqid(aln_id, *m_ScopeRef);
00464         }
00465            
00466         sdl->id_url = s_GetIdUrl(*ids, sdl->gi, user_url,
00467                                  m_IsDbNa, m_Database, 
00468                                  (m_Option & eNewTargetWindow) ? true : false,
00469                                  m_Rid, m_QueryNumber, taxid, sdl->linkout,
00470                                  blast_rank);
00471     }
00472 
00473   
00474     sdl->defline = GetTitle(m_ScopeRef->GetBioseqHandle(*(sdl->id)));
00475     if (!(bdl.empty())) { 
00476         for(list< CRef< CBlast_def_line > >::const_iterator iter = bdl.begin();
00477             iter != bdl.end(); iter++){
00478             const CBioseq::TId& cur_id = (*iter)->GetSeqid();
00479             int cur_gi =  FindGi(cur_id);
00480             int gi_in_use_this_gi = 0;
00481             ITERATE(list<int>, iter_gi, use_this_gi){
00482                 if(cur_gi == *iter_gi){
00483                     gi_in_use_this_gi = *iter_gi;                 
00484                     break;
00485                 }
00486             }
00487             if(use_this_gi.empty() || gi_in_use_this_gi > 0) {
00488                 
00489                 if((*iter)->IsSetTitle()){
00490                     bool id_used_already = false;
00491                     ITERATE(CBioseq::TId, iter_id, cur_id) {
00492                         if ((*iter_id)->Match(*(sdl->id))) {
00493                             id_used_already = true;
00494                             break;
00495                         }
00496                     }
00497                     if (!id_used_already) {
00498                         string concat_acc;
00499                         wid = FindBestChoice(cur_id, CSeq_id::WorstRank);
00500                         wid->GetLabel(&concat_acc, CSeq_id::eFasta, 0);
00501                         if( (m_Option & eShowGi) && cur_gi > 0){
00502                             sdl->defline =  sdl->defline + " >" + "gi|" + 
00503                                 NStr::IntToString(cur_gi) + "|" + 
00504                                 concat_acc + " " + (*iter)->GetTitle();
00505                         } else {
00506                             sdl->defline = sdl->defline + " >" + concat_acc +
00507                                 " " + 
00508                                 (*iter)->GetTitle();
00509                         }
00510                     }
00511                 }
00512             }
00513         }
00514     }
00515 }
00516 
00517 
00518 //Constructor
00519 CShowBlastDefline::CShowBlastDefline(const CSeq_align_set& seqalign,
00520                                      CScope& scope,
00521                                      size_t line_length,
00522                                      size_t num_defline_to_show,
00523                                      bool translated_nuc_alignment,
00524                                      CRange<TSeqPos>* master_range):
00525     m_AlnSetRef(&seqalign), 
00526     m_ScopeRef(&scope),
00527     m_LineLen(line_length),
00528     m_NumToShow(num_defline_to_show),
00529     m_TranslatedNucAlignment(translated_nuc_alignment),
00530     m_MasterRange(master_range)
00531 {
00532     
00533     m_Option = 0;
00534     m_EntrezTerm = NcbiEmptyString;
00535     m_QueryNumber = 0;
00536     m_Rid = NcbiEmptyString;
00537     m_CddRid = NcbiEmptyString;
00538     m_IsDbNa = true;
00539     m_BlastType = NcbiEmptyString;
00540     m_PsiblastStatus = eFirstPass;
00541     m_SeqStatus = NULL;
00542     m_Ctx = NULL;
00543     m_StructureLinkout = false;
00544     if(m_MasterRange) {
00545         if(m_MasterRange->GetFrom() >= m_MasterRange->GetTo()) {
00546             m_MasterRange = NULL;
00547         }
00548     }
00549 }
00550 
00551 CShowBlastDefline::~CShowBlastDefline()
00552 {
00553    
00554     ITERATE(list<SScoreInfo*>, iter, m_ScoreList){
00555         delete *iter;
00556     }
00557 }
00558 
00559 
00560 void CShowBlastDefline::Init(void)
00561 {
00562     string descrTableFormat = m_Ctx->GetRequestValue("NEW_VIEW").GetValue();
00563     descrTableFormat = NStr::ToLower(descrTableFormat);
00564     bool formatAsTable = (descrTableFormat == "on" || descrTableFormat == "true" || descrTableFormat == "yes") ? true : false;        
00565     if (formatAsTable) {
00566         x_InitDeflineTable();        
00567     }
00568     else {        
00569         x_InitDefline();
00570     }
00571 }
00572 
00573 
00574 void CShowBlastDefline::Display(CNcbiOstream & out)
00575 {
00576     string descrTableFormat = m_Ctx->GetRequestValue("NEW_VIEW").GetValue();
00577     descrTableFormat = NStr::ToLower(descrTableFormat);
00578     bool formatAsTable = (descrTableFormat == "on" || descrTableFormat == "true" || descrTableFormat == "yes") ? true : false;    
00579     if (formatAsTable) {        
00580         x_DisplayDeflineTable(out);        
00581     }
00582     else {        
00583         x_DisplayDefline(out);
00584     }
00585 }
00586 
00587 bool CShowBlastDefline::x_CheckForStructureLink()
00588 {
00589       bool struct_linkout = false;
00590       int count = 0;
00591       const int k_CountMax = 200; // Max sequences to check.
00592 
00593       ITERATE(list<SScoreInfo*>, iter, m_ScoreList) {
00594           const CBioseq_Handle& handle = m_ScopeRef->GetBioseqHandle(*(*iter)->id);
00595           const CRef<CBlast_def_line_set> bdlRef = CBlastFormatUtil::GetBlastDefline(handle);
00596           const list< CRef< CBlast_def_line > >& bdl = bdlRef->Get();
00597           for(list< CRef< CBlast_def_line > >::const_iterator bdl_iter = bdl.begin();
00598               bdl_iter != bdl.end() && struct_linkout == false; bdl_iter++){
00599               if ((*bdl_iter)->IsSetLinks())
00600               {
00601                  for (list< int >::const_iterator link_iter = (*bdl_iter)->GetLinks().begin();
00602                       link_iter != (*bdl_iter)->GetLinks().end(); link_iter++)
00603                  {
00604                       if (*link_iter & eStructure) {
00605                          struct_linkout = true;
00606                          break;
00607                       }
00608                  }
00609               }
00610           }
00611           if (struct_linkout == true || count > k_CountMax)
00612             break;
00613           count++;
00614       }
00615       return struct_linkout;
00616 }
00617 
00618 //size_t max_score_len = kBits.size(), max_evalue_len = kValue.size();
00619 //size_t max_sum_n_len =1;
00620 //size_t m_MaxScoreLen , m_MaxEvalueLen,m_MaxSumNLen;
00621 //bool m_StructureLinkout
00622 void CShowBlastDefline::x_InitDefline(void)
00623 {
00624     /*Note we can't just show each alnment as we go because we will 
00625       need to show defline only once for all hsp's with the same id*/
00626     
00627     bool is_first_aln = true;
00628     size_t num_align = 0;
00629     CConstRef<CSeq_id> previous_id, subid;
00630 
00631     m_MaxScoreLen = kBits.size();
00632     m_MaxEvalueLen = kValue.size();
00633     m_MaxSumNLen =1;
00634     
00635 
00636     if(m_Option & eHtml){
00637         m_ConfigFile.reset(new CNcbiIfstream(".ncbirc"));
00638         m_Reg.reset(new CNcbiRegistry(*m_ConfigFile));  
00639     }
00640     bool master_is_na = false;
00641     //prepare defline
00642 
00643     for (CSeq_align_set::Tdata::const_iterator 
00644              iter = m_AlnSetRef->Get().begin(); 
00645          iter != m_AlnSetRef->Get().end() && num_align < m_NumToShow; 
00646          iter++){
00647         if (is_first_aln) {
00648             master_is_na = m_ScopeRef->GetBioseqHandle((*iter)->GetSeq_id(0)).
00649                 GetBioseqCore()->IsNa();
00650         }
00651         subid = &((*iter)->GetSeq_id(1));
00652         if(is_first_aln || (!is_first_aln && !subid->Match(*previous_id))) {
00653             SScoreInfo* sci = x_GetScoreInfo(**iter, num_align);
00654             if(sci){
00655                 m_ScoreList.push_back(sci);
00656                 if(m_MaxScoreLen < sci->bit_string.size()){
00657                     m_MaxScoreLen = sci->bit_string.size();
00658                 }
00659                 if(m_MaxEvalueLen < sci->evalue_string.size()){
00660                     m_MaxEvalueLen = sci->evalue_string.size();
00661                 }
00662 
00663                 if( m_MaxSumNLen < NStr::IntToString(sci->sum_n).size()){
00664                     m_MaxSumNLen = NStr::IntToString(sci->sum_n).size();
00665                 }
00666             }
00667             num_align++;
00668         }
00669         is_first_aln = false;
00670         previous_id = subid;
00671       
00672     }
00673 
00674     
00675     if((m_Option & eLinkout) && (m_Option & eHtml) && !m_IsDbNa && !master_is_na)
00676         m_StructureLinkout = x_CheckForStructureLink();
00677 }
00678 
00679 
00680 
00681 void CShowBlastDefline::x_DisplayDefline(CNcbiOstream & out)
00682 {
00683     if(!(m_Option & eNoShowHeader)) {
00684         if((m_PsiblastStatus == eFirstPass) ||
00685            (m_PsiblastStatus == eRepeatPass)){
00686             CBlastFormatUtil::AddSpace(out, m_LineLen + kTwoSpaceMargin.size());
00687             if(m_Option & eHtml){
00688                 if((m_Option & eShowNewSeqGif)){
00689                     out << kPsiblastNewSeqBackgroundGif;
00690                     out << kPsiblastCheckedBackgroundGif;
00691                 }
00692                 if (m_Option & eCheckbox) {
00693                     out << kPsiblastNewSeqBackgroundGif;
00694                     out << kPsiblastCheckedBackgroundGif;
00695                 }
00696             }
00697             out << kScore;
00698             CBlastFormatUtil::AddSpace(out, m_MaxScoreLen - kScore.size());
00699             CBlastFormatUtil::AddSpace(out, kTwoSpaceMargin.size());
00700             CBlastFormatUtil::AddSpace(out, 2); //E align to l of value
00701             out << kE;
00702             out << "\n";
00703             out << kHeader;
00704             if(m_Option & eHtml){
00705                 if((m_Option & eShowNewSeqGif)){
00706                     out << kPsiblastNewSeqBackgroundGif;
00707                     out << kPsiblastCheckedBackgroundGif;
00708                 }
00709                 if (m_Option & eCheckbox) {
00710                     out << kPsiblastNewSeqBackgroundGif;
00711                     out << kPsiblastCheckedBackgroundGif;
00712                 }
00713             }
00714             CBlastFormatUtil::AddSpace(out, m_LineLen - kHeader.size());
00715             CBlastFormatUtil::AddSpace(out, kOneSpaceMargin.size());
00716             out << kBits;
00717             //in case m_MaxScoreLen > kBits.size()
00718             CBlastFormatUtil::AddSpace(out, m_MaxScoreLen - kBits.size()); 
00719             CBlastFormatUtil::AddSpace(out, kTwoSpaceMargin.size());
00720             out << kValue;
00721             if(m_Option & eShowSumN){
00722                 CBlastFormatUtil::AddSpace(out, m_MaxEvalueLen - kValue.size()); 
00723                 CBlastFormatUtil::AddSpace(out, kTwoSpaceMargin.size());
00724                 out << kN;
00725             }
00726             out << "\n";
00727         }
00728         if(m_PsiblastStatus == eRepeatPass){
00729             out << kRepeatHeader << "\n";
00730         }
00731         if(m_PsiblastStatus == eNewPass){
00732             out << kNewSeqHeader << "\n";
00733         }
00734         out << "\n";
00735     }
00736     
00737     bool first_new =true;
00738     ITERATE(list<SScoreInfo*>, iter, m_ScoreList){
00739         SDeflineInfo* sdl = x_GetDeflineInfo((*iter)->id, (*iter)->use_this_gi, (*iter)->blast_rank);
00740         size_t line_length = 0;
00741         string line_component;
00742         if ((m_Option & eHtml) && (sdl->gi > 0)){
00743             if((m_Option & eShowNewSeqGif)) { 
00744                 if (sdl->is_new) {
00745                     if (first_new) {
00746                         first_new = false;
00747                         out << kPsiblastEvalueLink;
00748                     }
00749                     out << kPsiblastNewSeqGif;
00750                 
00751                 } else {
00752                     out << kPsiblastNewSeqBackgroundGif;
00753                 }
00754                 if (sdl->was_checked) {
00755                     out << kPsiblastCheckedGif;
00756                     
00757                 } else {
00758                     out << kPsiblastCheckedBackgroundGif;
00759                 }
00760             }
00761             char buf[256];
00762             if((m_Option & eCheckboxChecked)){
00763                 sprintf(buf, kPsiblastCheckboxChecked.c_str(), sdl->gi, 
00764                         sdl->gi);
00765                 out << buf;
00766             } else if (m_Option & eCheckbox) {
00767                 sprintf(buf, kPsiblastCheckbox.c_str(), sdl->gi);
00768                 out << buf;
00769             }
00770         }
00771         
00772         
00773         if((m_Option & eHtml) && (sdl->id_url != NcbiEmptyString)) {
00774             out << sdl->id_url;
00775         }
00776         if(m_Option & eShowGi){
00777             if(sdl->gi > 0){
00778                 line_component = "gi|" + NStr::IntToString(sdl->gi) + "|";
00779                 out << line_component;
00780                 line_length += line_component.size();
00781             }
00782         }
00783         if(!sdl->id.Empty()){
00784             if(!(sdl->id->AsFastaString().find("gnl|BL_ORD_ID") 
00785                  != string::npos)){
00786                 out << sdl->id->AsFastaString();
00787                 line_length += sdl->id->AsFastaString().size();
00788             }
00789         }
00790         if((m_Option & eHtml) && (sdl->id_url != NcbiEmptyString)) {
00791             out << "</a>";
00792         }        
00793         line_component = "  " + sdl->defline; 
00794         string actual_line_component;
00795         if(line_component.size()+line_length > m_LineLen){
00796             actual_line_component = line_component.substr(0, m_LineLen - 
00797                                                           line_length - 3);
00798             actual_line_component += kEllipsis;
00799         } else {
00800             actual_line_component = line_component.substr(0, m_LineLen - 
00801                                                           line_length);
00802         }
00803         if (m_Option & eHtml) {
00804             out << CHTMLHelper::HTMLEncode(actual_line_component);
00805         } else {
00806             out << actual_line_component; 
00807         }
00808         line_length += actual_line_component.size();
00809         //pad the short lines
00810         CBlastFormatUtil::AddSpace(out, m_LineLen - line_length);
00811         out << kTwoSpaceMargin;
00812        
00813         if((m_Option & eHtml) && (sdl->score_url != NcbiEmptyString)) {
00814             out << sdl->score_url;
00815         }
00816         out << (*iter)->bit_string;
00817         if((m_Option & eHtml) && (sdl->score_url != NcbiEmptyString)) {
00818             out << "</a>";
00819         }   
00820         CBlastFormatUtil::AddSpace(out, m_MaxScoreLen - (*iter)->bit_string.size());
00821         out << kTwoSpaceMargin << (*iter)->evalue_string;
00822         CBlastFormatUtil::AddSpace(out, m_MaxEvalueLen - (*iter)->evalue_string.size());
00823         if(m_Option & eShowSumN){ 
00824             out << kTwoSpaceMargin << (*iter)->sum_n;   
00825             CBlastFormatUtil::AddSpace(out, m_MaxSumNLen - 
00826                      NStr::IntToString((*iter)->sum_n).size());
00827         }
00828         if((m_Option & eLinkout) && (m_Option & eHtml)){
00829             bool is_first = true;
00830             ITERATE(list<string>, iter_linkout, sdl->linkout_list){
00831                 if(is_first){
00832                     out << kOneSpaceMargin;
00833                     is_first = false;
00834                 }
00835                 out << *iter_linkout;
00836             }
00837         }
00838         out <<"\n";
00839         delete sdl;
00840     }
00841 }
00842 
00843 void CShowBlastDefline::DisplayBlastDefline(CNcbiOstream & out)
00844 {
00845     x_InitDefline();
00846     if(m_StructureLinkout){        
00847         char buf[512];
00848         sprintf(buf, kStructure_Overview.c_str(), m_Rid.c_str(),
00849                         0, 0, m_CddRid.c_str(), "overview",
00850                         m_EntrezTerm == NcbiEmptyString ?
00851                         m_EntrezTerm.c_str() : "none");
00852         out << buf <<"\n\n";  
00853     }
00854     x_DisplayDefline(out);
00855 }
00856 
00857 static void s_DisplayDescrColumnHeader(CNcbiOstream & out,
00858                                        int currDisplaySort,
00859                                        string query_buf,  
00860                                        int columnDisplSort,
00861                                        int columnHspSort,
00862                                        string columnText,
00863                                        int max_data_len,
00864                                        bool html)
00865                                        
00866 
00867 {
00868     if (html) {             
00869         if(currDisplaySort == columnDisplSort) {
00870             out << "<th class=\"sel\">";        
00871         }
00872         else {
00873             out << "<th>";
00874         }   
00875         
00876         out << "<a href=\"Blast.cgi?"
00877             << "CMD=Get&" << query_buf 
00878             << "&DISPLAY_SORT=" << columnDisplSort
00879             << "&HSP_SORT=" << columnHspSort
00880             << "#sort_mark\">";
00881         
00882     }
00883     out << columnText;
00884     if (html) {        
00885         out << "</a></th>\n";
00886     }
00887     else {
00888         CBlastFormatUtil::AddSpace(out, max_data_len - columnText.size());
00889         CBlastFormatUtil::AddSpace(out, kTwoSpaceMargin.size());
00890     }
00891     
00892 }
00893 
00894 void CShowBlastDefline::x_InitDeflineTable(void)
00895 {
00896     /*Note we can't just show each alnment as we go because we will 
00897       need to show defline only once for all hsp's with the same id*/
00898     
00899     bool is_first_aln = true;
00900     size_t num_align = 0;
00901     CConstRef<CSeq_id> previous_id, subid;
00902     m_MaxScoreLen = kMaxScore.size();
00903     m_MaxEvalueLen = kValue.size();
00904     m_MaxSumNLen =1;
00905     m_MaxTotalScoreLen = kTotal.size();
00906     m_MaxPercentIdentityLen = kIdentity.size();
00907     int percent_identity = 0;
00908     m_MaxQueryCoverLen = kCoverage.size();
00909 
00910     if(m_Option & eHtml){
00911         m_ConfigFile.reset(new CNcbiIfstream(".ncbirc"));
00912         m_Reg.reset(new CNcbiRegistry(*m_ConfigFile));  
00913     }
00914 
00915     CSeq_align_set hit;
00916     m_QueryLength = 1;
00917     bool master_is_na = false;
00918     //prepare defline
00919 
00920     for (CSeq_align_set::Tdata::const_iterator 
00921              iter = m_AlnSetRef->Get().begin(); 
00922          iter != m_AlnSetRef->Get().end() && num_align < m_NumToShow; 
00923          iter++){
00924 
00925         if (is_first_aln) {
00926             m_QueryLength = m_MasterRange ? 
00927                 m_MasterRange->GetLength() :
00928                 m_ScopeRef->GetBioseqHandle((*iter)->GetSeq_id(0)).GetBioseqLength();
00929             master_is_na = m_ScopeRef->GetBioseqHandle((*iter)->GetSeq_id(0)).
00930                 GetBioseqCore()->IsNa();
00931         }
00932 
00933         subid = &((*iter)->GetSeq_id(1));
00934       
00935         // This if statement is working on the last CSeq_align_set, stored in "hit"
00936         // This is confusing and the loop should probably be restructured at some point.
00937         if(!is_first_aln && !(subid->Match(*previous_id))) {
00938             SScoreInfo* sci = x_GetScoreInfoForTable(hit, num_align);
00939             if(sci){
00940                 m_ScoreList.push_back(sci);
00941                 if(m_MaxScoreLen < sci->bit_string.size()){
00942                     m_MaxScoreLen = sci->bit_string.size();
00943                 }
00944                 if(m_MaxTotalScoreLen < sci->total_bit_string.size()){
00945                     m_MaxTotalScoreLen = sci->total_bit_string.size();
00946                 }
00947                 percent_identity = 100*sci->match/sci->align_length;
00948                 if(m_MaxPercentIdentityLen < NStr::IntToString(percent_identity).size()) {
00949                     m_MaxPercentIdentityLen = NStr::IntToString(percent_identity).size();
00950                 }
00951                 if(m_MaxEvalueLen < sci->evalue_string.size()){
00952                     m_MaxEvalueLen = sci->evalue_string.size();
00953                 }
00954                 
00955                 if( m_MaxSumNLen < NStr::IntToString(sci->sum_n).size()){
00956                     m_MaxSumNLen = NStr::IntToString(sci->sum_n).size();
00957                 }
00958                 hit.Set().clear();
00959             }
00960           
00961             num_align++; // Only increment if new subject ID found.
00962         }
00963         if (num_align < m_NumToShow) { //no adding if number to show already reached
00964             hit.Set().push_back(*iter);
00965         }
00966         is_first_aln = false;
00967         previous_id = subid;
00968     }
00969 
00970     //the last hit
00971     SScoreInfo* sci = x_GetScoreInfoForTable(hit, num_align);
00972     if(sci){
00973          m_ScoreList.push_back(sci);
00974         if(m_MaxScoreLen < sci->bit_string.size()){
00975             m_MaxScoreLen = sci->bit_string.size();
00976         }
00977         if(m_MaxTotalScoreLen < sci->total_bit_string.size()){
00978             m_MaxScoreLen = sci->total_bit_string.size();
00979         }
00980         percent_identity = 100*sci->match/sci->align_length;
00981         if(m_MaxPercentIdentityLen < NStr::IntToString(percent_identity).size()) {
00982             m_MaxPercentIdentityLen =  NStr::IntToString(percent_identity).size();
00983         }
00984         if(m_MaxEvalueLen < sci->evalue_string.size()){
00985             m_MaxEvalueLen = sci->evalue_string.size();
00986         }
00987         
00988         if( m_MaxSumNLen < NStr::IntToString(sci->sum_n).size()){
00989             m_MaxSumNLen = NStr::IntToString(sci->sum_n).size();
00990         }
00991         hit.Set().clear();
00992     }
00993 
00994     if((m_Option & eLinkout) && (m_Option & eHtml) && !m_IsDbNa && !master_is_na)
00995         m_StructureLinkout = x_CheckForStructureLink();
00996  
00997 }
00998 
00999 void CShowBlastDefline::x_DisplayDeflineTable(CNcbiOstream & out)
01000 {
01001     int percent_identity = 0;
01002     //This is max number of columns in the table - later should be probably put in enum DisplayOption
01003     int tableColNumber = 9;
01004     //if(!(m_Option & eNoShowHeader)) {
01005         if((m_PsiblastStatus == eFirstPass) ||
01006            (m_PsiblastStatus == eRepeatPass)){
01007             //CBlastFormatUtil::AddSpace(out, m_LineLen + 1);
01008             ///???
01009             if(m_Option & eHtml){
01010                 if((m_Option & eShowNewSeqGif)){
01011                     out << kPsiblastNewSeqBackgroundGif;
01012                     out << kPsiblastCheckedBackgroundGif;
01013                 }
01014                 if (m_Option & eCheckbox) {
01015                     out << kPsiblastNewSeqBackgroundGif;
01016                     out << kPsiblastCheckedBackgroundGif;
01017                 }
01018             }
01019             //This is done instead of code displaying titles            
01020             if(!(m_Option & eNoShowHeader)) {
01021                 
01022                 if(m_Option & eHtml){
01023                 
01024                     out << "<b>";
01025                 }
01026                 out << kHeader << "\n";
01027                 if(m_Option & eHtml){
01028                     out << "</b>";          
01029                     out << "(Click headers to sort columns)\n";
01030                 }
01031             }
01032             if(m_Option & eHtml){
01033                 out << "<div id=\"desctbl\">" << "<table id=\"descs\">" << "\n" << "<thead>" << "\n";
01034                 out << "<tr class=\"first\">" << "\n" << "<th>Accession</th>" << "\n" << "<th>Description</th>" << "\n";            
01035             }
01036             /*
01037             if(m_Option & eHtml){
01038                 if((m_Option & eShowNewSeqGif)){
01039                     out << kPsiblastNewSeqBackgroundGif;
01040                     out << kPsiblastCheckedBackgroundGif;
01041                 }
01042                 if (m_Option & eCheckbox) {
01043                     out << kPsiblastNewSeqBackgroundGif;
01044                     out << kPsiblastCheckedBackgroundGif;
01045                 }
01046             }
01047             
01048             
01049             CBlastFormatUtil::AddSpace(out, m_LineLen - kHeader.size());
01050             CBlastFormatUtil::AddSpace(out, kTwoSpaceMargin.size());
01051             */
01052 
01053             string query_buf; 
01054             map< string, string> parameters_to_change;
01055             parameters_to_change.insert(map<string, string>::
01056                                         value_type("DISPLAY_SORT", ""));
01057             parameters_to_change.insert(map<string, string>::
01058                                         value_type("HSP_SORT", ""));
01059             CBlastFormatUtil::BuildFormatQueryString(*m_Ctx, 
01060                                                      parameters_to_change,
01061                                                      query_buf);
01062         
01063             parameters_to_change.clear();
01064 
01065             string display_sort_value = m_Ctx->GetRequestValue("DISPLAY_SORT").
01066                 GetValue();
01067             int display_sort = display_sort_value == NcbiEmptyString ? 
01068                 CBlastFormatUtil::eEvalue : NStr::StringToInt(display_sort_value);
01069             
01070             s_DisplayDescrColumnHeader(out,display_sort,query_buf,CBlastFormatUtil::eHighestScore,CBlastFormatUtil::eScore,kMaxScore,m_MaxScoreLen,m_Option & eHtml);
01071 
01072             s_DisplayDescrColumnHeader(out,display_sort,query_buf,CBlastFormatUtil::eTotalScore,CBlastFormatUtil::eScore,kTotalScore,m_MaxTotalScoreLen,m_Option & eHtml);
01073             s_DisplayDescrColumnHeader(out,display_sort,query_buf,CBlastFormatUtil::eQueryCoverage,CBlastFormatUtil::eHspEvalue,kCoverage,m_MaxQueryCoverLen,m_Option & eHtml);
01074             s_DisplayDescrColumnHeader(out,display_sort,query_buf,CBlastFormatUtil::eEvalue,CBlastFormatUtil::eHspEvalue,kEvalue,m_MaxEvalueLen,m_Option & eHtml);
01075             if(m_Option & eShowPercentIdent){
01076                 s_DisplayDescrColumnHeader(out,display_sort,query_buf,CBlastFormatUtil::ePercentIdentity,CBlastFormatUtil::eHspPercentIdentity,kIdentity,m_MaxPercentIdentityLen,m_Option & eHtml);
01077             }else {
01078                 tableColNumber--;
01079             }
01080            
01081             if(m_Option & eShowSumN){
01082                 // CBlastFormatUtil::AddSpace(out, kTwoSpaceMargin.size());
01083                 out << "<th>" << kN << "</th>" << "\n";
01084                 
01085             }
01086             //out << "\n\n";
01087             if (m_Option & eLinkout) {
01088                 out << "<th>Links</th>\n";
01089                 out << "</tr>\n";
01090                 out << "</thead>\n";
01091             }
01092         }
01093         //????
01094         /*      if(m_PsiblastStatus == eRepeatPass){
01095             out << kRepeatHeader <<"\n";
01096         }
01097         if(m_PsiblastStatus == eNewPass){
01098             out << kNewSeqHeader <<"\n";
01099             }*/
01100     //}
01101     
01102     bool first_new =true;
01103     int prev_database_type = 0, cur_database_type = 0;
01104     bool is_first = true;
01105     // Mixed db is genomic + transcript and this does not apply to proteins.
01106     bool is_mixed_database = false;
01107     if (m_IsDbNa == true)
01108        is_mixed_database = CBlastFormatUtil::IsMixedDatabase(*m_AlnSetRef, *m_ScopeRef);
01109 
01110     map< string, string> parameters_to_change;
01111     string query_buf;
01112     if (is_mixed_database && m_Option & eHtml) {
01113         parameters_to_change.insert(map<string, string>::
01114                                     value_type("DATABASE_SORT", ""));
01115         CBlastFormatUtil::BuildFormatQueryString(*m_Ctx, 
01116                                                  parameters_to_change,
01117                                                  query_buf);
01118     }
01119     if (m_Option & eHtml) {
01120         out << "<tbody>\n";
01121     }
01122     ITERATE(list<SScoreInfo*>, iter, m_ScoreList){
01123         SDeflineInfo* sdl = x_GetDeflineInfo((*iter)->id, (*iter)->use_this_gi, (*iter)->blast_rank);
01124         size_t line_length = 0;
01125         string line_component;
01126         cur_database_type = (sdl->linkout & eGenomicSeq);
01127         if (is_mixed_database) {
01128             if (is_first) {
01129                 if (m_Option & eHtml) {
01130                     out << "<tr>\n<th colspan=\"" << tableColNumber<< "\" class=\"l sp\">";
01131                 }
01132                 if (cur_database_type) {
01133                     out << "Genomic sequences";                    
01134                 } else {
01135                     out << "Transcripts";                    
01136                 }
01137                 if (!(m_Option & eHtml)) {                  
01138                     out << ":\n";
01139                 }
01140                 if (m_Option & eHtml) {
01141                     out << "</th></tr>\n";
01142                 }
01143             } else if (prev_database_type != cur_database_type) {               
01144                 if (m_Option & eHtml) {
01145                     out << "<tr>\n<th colspan=\"" << tableColNumber<< "\" class=\"l sp\">";
01146                 }
01147                 if (cur_database_type) {
01148                     out << "Genomic sequences";                 
01149                 } else {
01150                     out << "Transcripts";
01151                 }                
01152                 if (m_Option & eHtml) {
01153                     out << "<span class=\"slink\">"
01154                         << " [<a href=\"Blast.cgi?CMD=Get&"
01155                         << query_buf 
01156                         << "&DATABASE_SORT=";                   
01157                     if (cur_database_type) {
01158                         out << CBlastFormatUtil::eGenomicFirst;             
01159                     } else {
01160                         out << CBlastFormatUtil::eNonGenomicFirst;
01161                     }
01162                     out << "#sort_mark\">show first</a>]</span>";
01163                 }
01164                 else {
01165                     out << ":\n";
01166                 }               
01167                 if (m_Option & eHtml) {
01168                     out << "</th></tr>\n";
01169                 }
01170             }           
01171         }
01172         prev_database_type = cur_database_type;
01173         is_first = false;
01174         if (m_Option & eHtml) {
01175             out << "<tr>\n";
01176             out << "<td class=\"l\">\n";
01177         }
01178         if ((m_Option & eHtml) && (sdl->gi > 0)){
01179             if((m_Option & eShowNewSeqGif)) { 
01180                 if (sdl->is_new) {
01181                     if (first_new) {
01182                         first_new = false;
01183                         out << kPsiblastEvalueLink;
01184                     }
01185                     out << kPsiblastNewSeqGif;
01186                 
01187                 } else {
01188                     out << kPsiblastNewSeqBackgroundGif;
01189                 }
01190                 if (sdl->was_checked) {
01191                     out << kPsiblastCheckedGif;
01192                     
01193                 } else {
01194                     out << kPsiblastCheckedBackgroundGif;
01195                 }
01196             }
01197             char buf[256];
01198             if((m_Option & eCheckboxChecked)){
01199                 sprintf(buf, kPsiblastCheckboxChecked.c_str(), sdl->gi, 
01200                         sdl->gi);
01201                 out << buf;
01202             } else if (m_Option & eCheckbox) {
01203                 sprintf(buf, kPsiblastCheckbox.c_str(), sdl->gi);
01204                 out << buf;
01205             }
01206         }
01207         
01208         
01209         if((m_Option & eHtml) && (sdl->id_url != NcbiEmptyString)) {
01210             out << sdl->id_url;
01211         }
01212         if(m_Option & eShowGi){
01213             if(sdl->gi > 0){
01214                 line_component = "gi|" + NStr::IntToString(sdl->gi) + "|";
01215                 out << line_component;
01216                 line_length += line_component.size();
01217             }
01218         }
01219         if(!sdl->id.Empty()){
01220             if(!(sdl->id->AsFastaString().find("gnl|BL_ORD_ID") 
01221                  != string::npos)){
01222                 string id_str;
01223                 sdl->id->GetLabel(&id_str, CSeq_id::eContent);
01224                 out << id_str;
01225                 line_length += id_str.size();
01226             }
01227         }
01228         if((m_Option & eHtml) && (sdl->id_url != NcbiEmptyString)) {
01229             out << "</a>";
01230         }
01231         if (m_Option & eHtml) {
01232             out << "</td><td class=\"lim l\"><div class=\"lim\">";
01233         }
01234         line_component = "  " + sdl->defline; 
01235         string actual_line_component;
01236         actual_line_component = line_component;
01237         /*
01238         if(line_component.size() > m_LineLen){
01239             actual_line_component = line_component.substr(0, m_LineLen - 
01240                                                           line_length - 3);
01241             actual_line_component += kEllipsis;
01242         } else {
01243             actual_line_component = line_component.substr(0, m_LineLen - 
01244                                                           line_length);
01245         }
01246         */
01247         if (m_Option & eHtml) {
01248             out << CHTMLHelper::HTMLEncode(actual_line_component);
01249             out << "</div></td><td>";
01250         } else {
01251             out << actual_line_component; 
01252         }
01253         /*
01254         line_length += actual_line_component.size();
01255         //pad the short lines
01256         if (!(m_Option & eHtml)) {
01257             CBlastFormatUtil::AddSpace(out, m_LineLen - line_length);
01258             out << kTwoSpaceMargin;
01259         }
01260         */
01261         if((m_Option & eHtml) && (sdl->score_url != NcbiEmptyString)) {
01262             out << sdl->score_url;
01263         }
01264         out << (*iter)->bit_string;
01265         if((m_Option & eHtml) && (sdl->score_url != NcbiEmptyString)) {
01266             out << "</a>";
01267         }   
01268         if(m_Option & eHtml) {
01269             out << "</td>";
01270             out << "<td>" << (*iter)->total_bit_string << "</td>";
01271         }
01272         if (!(m_Option & eHtml)) {
01273             CBlastFormatUtil::AddSpace(out, m_MaxScoreLen - (*iter)->bit_string.size());
01274 
01275             out << kTwoSpaceMargin << kOneSpaceMargin << (*iter)->total_bit_string;
01276             CBlastFormatUtil::AddSpace(out, m_MaxTotalScoreLen - 
01277                                    (*iter)->total_bit_string.size());
01278         }
01279         
01280                 int percent_coverage = 100*(*iter)->master_covered_length/m_QueryLength;
01281         if (m_Option & eHtml) {
01282             out << "<td>" << percent_coverage << "%</td>";
01283         }
01284         else {
01285             out << kTwoSpaceMargin << percent_coverage << "%";
01286         
01287             //minus one due to % sign
01288             CBlastFormatUtil::AddSpace(out, m_MaxQueryCoverLen - 
01289                                        NStr::IntToString(percent_coverage).size() - 1);
01290         }
01291 
01292         if (m_Option & eHtml) {        
01293             out << "<td>" << (*iter)->evalue_string << "</td>";
01294         }
01295         else {
01296             out << kTwoSpaceMargin << (*iter)->evalue_string;
01297             CBlastFormatUtil::AddSpace(out, m_MaxEvalueLen - (*iter)->evalue_string.size());
01298         }
01299         if(m_Option & eShowPercentIdent){
01300             percent_identity = 100*(*iter)->match/(*iter)->align_length;
01301             if (m_Option & eHtml) {        
01302                 out << "<td>" << percent_identity << "%</td>";
01303             }
01304             else {
01305                 out << kTwoSpaceMargin << percent_identity <<"%";
01306                 
01307                 CBlastFormatUtil::AddSpace(out, m_MaxPercentIdentityLen - 
01308                                            NStr::IntToString(percent_identity).size());
01309             }
01310         }
01311         //???
01312         if(m_Option & eShowSumN){ 
01313             if (m_Option & eHtml) {
01314                 out << "<td>";
01315             }
01316             out << kTwoSpaceMargin << (*iter)->sum_n;   
01317             if (m_Option & eHtml) {
01318                 out << "</td>";
01319             } else {
01320                 CBlastFormatUtil::AddSpace(out, m_MaxSumNLen - 
01321                                            NStr::IntToString((*iter)->sum_n).size());
01322             }
01323         }
01324     
01325         if((m_Option & eLinkout) && (m_Option & eHtml)){
01326                
01327             out << "<td>";
01328             bool first_time = true;
01329             ITERATE(list<string>, iter_linkout, sdl->linkout_list){
01330                 if(first_time){
01331                     out << kOneSpaceMargin;
01332                     first_time = false;
01333                 }
01334                 out << *iter_linkout;
01335             }
01336             out << "</td>";
01337         }
01338         if (m_Option & eHtml) {
01339             out << "</tr>";
01340         }
01341         if (!(m_Option & eHtml)) {        
01342             out <<"\n";
01343         }
01344         delete sdl;
01345     }
01346     if (m_Option & eHtml) {
01347     out << "</tbody>\n</table></div>\n";
01348     }
01349 }
01350 
01351 void CShowBlastDefline::DisplayBlastDeflineTable(CNcbiOstream & out)
01352 {
01353     x_InitDeflineTable();
01354     if(m_StructureLinkout){        
01355         char buf[512];
01356         sprintf(buf, kStructure_Overview.c_str(), m_Rid.c_str(),
01357                         0, 0, m_CddRid.c_str(), "overview",
01358                         m_EntrezTerm == NcbiEmptyString ?
01359                         m_EntrezTerm.c_str() : "none");
01360         out << buf <<"\n\n";        
01361     }    
01362     x_DisplayDeflineTable(out);
01363 }
01364 
01365 CShowBlastDefline::SScoreInfo* 
01366 CShowBlastDefline::x_GetScoreInfo(const CSeq_align& aln, int blast_rank)
01367 {
01368     string evalue_buf, bit_score_buf, total_bit_score_buf;
01369     int score = 0;
01370     double bits = 0;
01371     double evalue = 0;
01372     int sum_n = 0;
01373     int num_ident = 0;
01374     list<int> use_this_gi; 
01375 
01376     use_this_gi.clear();
01377 
01378     CBlastFormatUtil::GetAlnScores(aln, score, bits, evalue, sum_n, 
01379                                        num_ident, use_this_gi);
01380 
01381     CBlastFormatUtil::GetScoreString(evalue, bits, 0, 
01382                               evalue_buf, bit_score_buf, total_bit_score_buf);
01383 
01384     SScoreInfo* score_info = new SScoreInfo;
01385     score_info->sum_n = sum_n == -1 ? 1:sum_n ;
01386     score_info->id = &(aln.GetSeq_id(1));
01387 
01388     score_info->use_this_gi = use_this_gi;
01389 
01390     score_info->bit_string = bit_score_buf;
01391     score_info->evalue_string = evalue_buf;
01392     score_info->id = &(aln.GetSeq_id(1));
01393     score_info->blast_rank = blast_rank+1;
01394 
01395     return score_info;
01396 }
01397 
01398 CShowBlastDefline::SScoreInfo* 
01399 CShowBlastDefline::x_GetScoreInfoForTable(const CSeq_align_set& aln, int blast_rank)
01400 {
01401     string evalue_buf, bit_score_buf, total_bit_score_buf;
01402     int score = 0;
01403     double bits = 0;
01404     double evalue = 0;
01405     int sum_n = 0;
01406     int num_ident = 0;
01407     SScoreInfo* score_info = NULL;
01408 
01409     if(aln.Get().empty())
01410         return score_info;
01411 
01412     score_info = x_GetScoreInfo(*(aln.Get().front()), blast_rank); 
01413 
01414     double total_bits = 0;
01415     double highest_bits = 0;
01416     double lowest_evalue = 0;
01417     int highest_length = 1;
01418     int highest_ident = 0;
01419     int highest_identity = 0;
01420     list<int> use_this_gi;   // Not used here, but needed for GetAlnScores.
01421     score_info->master_covered_length =  CBlastFormatUtil::GetMasterCoverage(aln);
01422     ITERATE(CSeq_align_set::Tdata, iter, aln.Get()) {
01423         int align_length = CBlastFormatUtil::GetAlignmentLength(**iter, 
01424                                                         m_TranslatedNucAlignment);
01425         CBlastFormatUtil::GetAlnScores(**iter, score, bits, evalue, sum_n, 
01426                                    num_ident, use_this_gi);  
01427         use_this_gi.clear();
01428     
01429         total_bits += bits;
01430     
01431         if (100*num_ident/align_length > highest_identity) {
01432             highest_length = align_length;
01433             highest_ident = num_ident;
01434             highest_identity = 100*num_ident/align_length;
01435         }
01436     
01437         if (bits > highest_bits) {
01438             highest_bits = bits;
01439             lowest_evalue = evalue;
01440         }       
01441     }
01442     score_info->match = highest_ident;      
01443     score_info->align_length = highest_length;
01444 
01445     // Really need to call this twice??
01446     CBlastFormatUtil::GetScoreString(lowest_evalue, highest_bits, total_bits, 
01447                                      evalue_buf, bit_score_buf, total_bit_score_buf);
01448 
01449     score_info->total_bit_string = total_bit_score_buf; 
01450     score_info->bit_string = bit_score_buf;
01451     score_info->evalue_string = evalue_buf;
01452 
01453     return score_info;
01454 }
01455 
01456 vector <CShowBlastDefline::SDeflineInfo*> 
01457 CShowBlastDefline::GetDeflineInfo(vector< CConstRef<CSeq_id> > &seqIds)
01458 {
01459     vector <CShowBlastDefline::SDeflineInfo*>  sdlVec;
01460     for(int i = 0; i < seqIds.size(); i++) {
01461         list<int> use_this_gi;
01462         CShowBlastDefline::SDeflineInfo* sdl = x_GetDeflineInfo(seqIds[i], use_this_gi, i + 1 );
01463         sdlVec.push_back(sdl);        
01464     }
01465     return sdlVec;
01466 }
01467 
01468 
01469 
01470 CShowBlastDefline::SDeflineInfo* 
01471 CShowBlastDefline::x_GetDeflineInfo(CConstRef<CSeq_id> id, list<int>& use_this_gi, int blast_rank)
01472 {
01473     SDeflineInfo* sdl = NULL;
01474       
01475     try{
01476         const CBioseq_Handle& handle = m_ScopeRef->GetBioseqHandle(*id);
01477         sdl = new SDeflineInfo;
01478         x_FillDeflineAndId(handle, *id, use_this_gi, sdl, blast_rank);
01479     } catch (const CException&){
01480         sdl = new SDeflineInfo;
01481         sdl->defline = "Unknown";
01482         sdl->is_new = false;
01483         sdl->was_checked = false;
01484         sdl->linkout = 0;
01485         
01486         if((*id).Which() == CSeq_id::e_Gi){
01487             sdl->gi = (*id).GetGi();
01488         } else {            
01489             sdl->id = id;
01490             sdl->gi = 0;
01491         }
01492         if(m_Option & eHtml){
01493             string user_url= m_Reg->Get(m_BlastType, "TOOL_URL");
01494             CBioseq::TId ids;
01495             CRef<CSeq_id> id_ref;
01496             id_ref = &(const_cast<CSeq_id&>(*id));
01497             ids.push_back(id_ref);
01498             sdl->id_url = s_GetIdUrl(ids, sdl->gi, user_url,
01499                                      m_IsDbNa, m_Database, 
01500                                      (m_Option & eNewTargetWindow) ? 
01501                                      true : false, m_Rid, m_QueryNumber, 0,
01502                                      0, blast_rank);
01503             sdl->score_url = NcbiEmptyString;
01504         }
01505     }
01506     
01507     return sdl;
01508 }
01509 
01510 END_NCBI_SCOPE
01511 
01512 

Generated on Sun Feb 8 22:05:48 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Feb 09 14:47:20 2009 by modify_doxy.py rev. 117643