1 /*  $Id: vectorscreen.hpp 441136 2014-07-21 17:54:53Z jianye $
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  * Author:  Jian Ye
27  *
28  * @file vectorscreen.hpp
29  *   vector screen display (using HTML table)
30  *
31  */
32 
33 #ifndef OBJTOOLS_ALIGN_FORMAT___VECTORSCREEN_HPP
34 #define OBJTOOLS_ALIGN_FORMAT___VECTORSCREEN_HPP
35 
36 #include <html/html.hpp>
37 #include <corelib/ncbiobj.hpp>
38 #include <objects/seqalign/Seq_align.hpp>
39 #include <objects/seqalign/Seq_align_set.hpp>
40 #include <objmgr/scope.hpp>
41 #include <util/range.hpp>
42 #include <objtools/align_format/align_format_util.hpp>
43 
44 BEGIN_NCBI_SCOPE
45 BEGIN_SCOPE(align_format)
46 
47 
48 /**
49  * Example:
50  * @code
51  * CVecscreen vec(align_set, master_length);
52  * vec.SetImagePath("images/");
53  * CRef<objects::CSeq_align_set> temp_aln = vec.ProcessSeqAlign();
54  * vec.VecscreenPrint(out);
55  * @endcode
56  */
57 
58 class NCBI_ALIGN_FORMAT_EXPORT CVecscreen {
59 public:
60 
61     ///vector match defines
62     enum MatchType{
63         eStrong = 0,
64         eModerate,
65         eWeak,
66         eSuspect,
67         eNoMatch
68     };
69 
70     ///Match info
71     struct AlnInfo {
72         TSeqRange range;
73         MatchType type;
74 
AlnInfoCVecscreen::AlnInfo75         AlnInfo(TSeqRange r = TSeqRange::GetEmpty(), MatchType m = eNoMatch)
76             : range(r), type(m) {}
77 
78         /// to allow sorting in std::list
operator <CVecscreen::AlnInfo79         int operator<(const AlnInfo& rhs) const {
80             if (this == &rhs) {
81                 return 0;
82             }
83             int rv = static_cast<int>(this->type < rhs.type);
84             if (rv == 0) {
85                 // range is the tie-breaker
86                 rv = this->range < rhs.range;
87             }
88             return rv;
89         }
90     };
91 
92     ///Constructors
93     ///@param seqalign: alignment to show
94     ///@param master_length: master seq length
95     ///
96     CVecscreen(const objects::CSeq_align_set& seqalign, TSeqPos master_length);
97 
98     ///Destructor
99     ~CVecscreen();
100 
101     ///Set path to pre-made image gif files with different colors
102     ///@param path: the path.  i.e. "mypath/". Internal default "./"
103     ///
SetImagePath(string path)104     void SetImagePath(string path) {
105         m_ImagePath = path;
106     }
107 
108     ///provide url link to help docs.  Default is
109     ///"/VecScreen/VecScreen_docs.html"
110     ///@param url: the url
111     ///
SetHelpDocsUrl(string url)112     void SetHelpDocsUrl(string url) {
113         m_HelpDocsUrl = url;
114     }
115 
116     ///Do not show weak(eWeak) match
NoShowWeakMatch()117     void NoShowWeakMatch() {
118         m_ShowWeakMatch = false;
119     }
120 
121     ///Process alignment to show
122     ///@return: the processed seqalign ref
123     ///
124     CRef<objects::CSeq_align_set> ProcessSeqAlign(void);
125 
126     ///return alignment info list
127     ///@return: the info list
128     ///
GetAlnInfoList() const129     const list<AlnInfo*>* GetAlnInfoList() const {
130         return &m_AlnInfoList;
131     }
132 
133     ///show alignment graphic view
134     ///@param out: stream for display
135     ///
136     void VecscreenPrint(CNcbiOstream& out);
137 
138     ///Returns a string concerning the strength of the match for a given enum value
139     static const string& GetStrengthString(MatchType match_type);
140 
141 protected:
142 
143 
144     ///the current seqalign
145     CConstRef<objects::CSeq_align_set> m_SeqalignSetRef;
146     ///the processed seqalign
147     CRef<objects::CSeq_align_set> m_FinalSeqalign;
148     ///gif image file path
149     string m_ImagePath;
150     ///help url
151     string m_HelpDocsUrl;
152     ///master seq length
153     TSeqPos m_MasterLen;
154     ///internal match list
155     list<AlnInfo*> m_AlnInfoList;
156     ///Show weak match?
157     bool m_ShowWeakMatch;
158 
159     ///Sort on range from
160     ///@param info1: the first range
161     ///@param info2: the second range
162     ///
FromRangeAscendingSort(AlnInfo * const & info1,AlnInfo * const & info2)163     inline static bool FromRangeAscendingSort(AlnInfo* const& info1,
164                                               AlnInfo* const& info2)
165     {
166         if (info1->range.GetFrom() == info2->range.GetFrom()){
167             return info1->range.GetTo() < info2->range.GetTo();
168         } else {
169             return info1->range.GetFrom() < info2->range.GetFrom();
170         }
171     }
172 
173     ///merge overlapping seqalign
174     ///@param seqalign: the seqalign to merge
175     ///
176     void x_MergeSeqalign(objects::CSeq_align_set& seqalign);
177 
178     ///merge a seqalign if its range is in another seqalign
179     ///@param seqalign: the seqalign to merge
180     ///
181     void x_MergeInclusiveSeqalign(objects::CSeq_align_set& seqalign);
182 
183     ///merge a seqalign if its range is in another higher ranked seqalign
184     ///@param seqalign_higher: higher-ranked seqalign
185     ///@param seqalign_lower: lower-ranked seqalign
186     ///
187     void x_MergeLowerRankSeqalign(objects::CSeq_align_set& seqalign_higher,
188                                   objects::CSeq_align_set& seqalign_lower);
189 
190 
191     void x_GetEdgeRanges(const objects::CSeq_align& seqalign,
192                          TSeqPos master_len,
193                          TSeqPos& start_edge,
194                          TSeqPos& end_edge);
195     ///Get match type
196     ///@param seqalign: the seqalign
197     ///@param master_len: the master seq length
198     ///@return: the type
199     ///
200     MatchType x_GetMatchType(const objects::CSeq_align& seqalign,
201                              TSeqPos master_len,
202                              TSeqPos start_edge,
203                              TSeqPos end_edge);
204 
205     ///Build non overlapping internal match list
206     ///@param seqalign_vec: a vecter of catagorized seqalign set
207     ///
208     void x_BuildNonOverlappingRange(vector<CRef<objects::CSeq_align_set> > seqalign_vec);
209 
210     ///get align info
211     ///@param from: align from
212     ///@param to: align to
213     ///@param type: the match type
214     ///
215     AlnInfo* x_GetAlnInfo(TSeqPos from, TSeqPos to, MatchType type);
216 
217     ///Output the graphic
218     ///@param out: the stream for output
219     ///
220     void x_BuildHtmlBar(CNcbiOstream& out);
221 };
222 
223 END_SCOPE(align_format)
224 END_NCBI_SCOPE
225 
226 #endif /* OBJTOOLS_ALIGN_FORMAT___VECTORSCREEN_HPP */
227