include/algo/blast/api/blast_aux.hpp

Go to the documentation of this file.
00001 /*  $Id: blast_aux.hpp 165919 2009-07-15 16:50:05Z avagyanv $
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:  Christiam Camacho
00027 *
00028 */
00029 
00030 /// @file blast_aux.hpp
00031 /// Contains C++ wrapper classes to structures in algo/blast/core as well as
00032 /// some auxiliary functions to convert CSeq_loc to/from BlastMask structures.
00033 
00034 #ifndef ALGO_BLAST_API___BLAST_AUX__HPP
00035 #define ALGO_BLAST_API___BLAST_AUX__HPP
00036 
00037 #include <corelib/ncbistd.hpp>
00038 #include <corelib/ddumpable.hpp>
00039 #include <corelib/ncbifile.hpp>
00040 #include <corelib/metareg.hpp>
00041 #include <objects/seqalign/Seq_align_set.hpp>
00042 #include <objects/seqloc/Seq_id.hpp>
00043 #include <objects/seqloc/Seq_interval.hpp>
00044 #include <util/range.hpp>       // For TSeqRange
00045 #include <objects/seq/seqlocinfo.hpp>
00046 
00047 // BLAST includes
00048 #include <algo/blast/api/blast_types.hpp>
00049 #include <algo/blast/api/blast_exception.hpp>
00050 #include <algo/blast/core/blast_query_info.h>
00051 #include <algo/blast/core/blast_util.h>
00052 #include <algo/blast/core/blast_options.h>
00053 #include <algo/blast/core/blast_filter.h> // Needed for BlastMaskLoc & BlastSeqLoc
00054 #include <algo/blast/core/blast_extend.h>
00055 #include <algo/blast/core/blast_gapalign.h>
00056 #include <algo/blast/core/blast_hits.h>
00057 #include <algo/blast/core/blast_psi.h>
00058 #include <algo/blast/core/blast_hspstream.h>
00059 
00060 BEGIN_NCBI_SCOPE
00061 
00062 BEGIN_SCOPE(objects)
00063     class CSeq_loc;
00064     class CPacked_seqint;
00065 END_SCOPE(objects)
00066 
00067 template <>
00068 struct Deleter<BlastHSPStream>
00069 {
00070     static void Delete(BlastHSPStream* p) 
00071     { BlastHSPStreamFree(p); }
00072 };
00073 
00074 /** @addtogroup AlgoBlast
00075  *
00076  * @{
00077  */
00078 
00079 BEGIN_SCOPE(blast)
00080 
00081 /* Convenience typedef's for common types used in BLAST to implement the RAII
00082  * idiom.
00083  */
00084 
00085 /// Uses C Deleter (free) - used in functions that deal with CORE BLAST
00086 #define TYPEDEF_AUTOPTR_CDELETER(type) \
00087 typedef AutoPtr<type, CDeleter<type> > TAuto ## type ## Ptr
00088 
00089 /// Uses delete [] operator - for C++ arrays
00090 #define TYPEDEF_AUTOPTR_ARRAYDELETER(type) \
00091 typedef AutoPtr<type, ArrayDeleter<type> > TAuto ## type ## ArrayPtr
00092 
00093 #ifndef SKIP_DOXYGEN_PROCESSING
00094 /// Declares TAutoUint1Ptr (for Uint1 arrays allocated with malloc/calloc)
00095 TYPEDEF_AUTOPTR_CDELETER(Uint1);
00096 /// Declares TAutoCharPtr (for Char arrays allocated with malloc/calloc)
00097 TYPEDEF_AUTOPTR_CDELETER(Char);
00098 /// Declares TAutoUint1ArrayPtr (for Uint1 arrays allocated with new[])
00099 TYPEDEF_AUTOPTR_ARRAYDELETER(Uint1);
00100 #endif
00101 
00102 /// Returns a string program name, given a blast::EBlastProgramType enumeration.
00103 /// @param program Enumerated program value [in]
00104 /// @return String program name.
00105 
00106 string Blast_ProgramNameFromType(EBlastProgramType program);
00107 
00108 /** Converts a CSeq_loc into a BlastSeqLoc structure used in NewBlast
00109  * @param slp CSeq_loc to convert [in]
00110  * @return Linked list of BlastSeqLoc structures
00111  */
00112 
00113 BlastSeqLoc*
00114 CSeqLoc2BlastSeqLoc(const objects::CSeq_loc* slp);
00115 
00116 /** Retrieves the requested genetic code in Ncbistdaa format. 
00117  * @param genetic_code numeric identifier for genetic code requested [in]
00118  * @return NULL if genetic_code is invalid or in case of memory allocation 
00119  * failure, otherwise genetic code string.
00120  * @note the returned string has length GENCODE_STRLEN
00121  */
00122 
00123 TAutoUint1ArrayPtr
00124 FindGeneticCode(int genetic_code);
00125 
00126 /** Function object to assist in finding all CSeqLocInfo objects which
00127  * corresponds to a given frame.
00128  */
00129 class CFrameFinder : public unary_function<CRef<CSeqLocInfo>, bool>
00130 {
00131 public:
00132     /// Convenience typedef
00133     typedef CSeqLocInfo::ETranslationFrame ETranslationFrame;
00134 
00135     /// ctor
00136     /// @param frame the translation frame [in]
00137     CFrameFinder(ETranslationFrame frame) : m_Frame(frame) {}
00138 
00139     /// Returns true if its argument's frame corresponds to the one used to
00140     /// create this object
00141     /// @param seqlocinfo the object to examine [in]
00142     bool operator() (const CRef<CSeqLocInfo>& seqlocinfo) const {
00143         if (seqlocinfo.Empty()) {
00144             NCBI_THROW(CBlastException, eInvalidArgument, 
00145                        "Empty CRef<CSeqLocInfo>!");
00146         }
00147         return (seqlocinfo->GetFrame() == m_Frame) ? true : false;
00148     }
00149 private:
00150     ETranslationFrame m_Frame;  ///< Frame to look for
00151 };
00152 
00153 /// Returns true if the CSeq_id is a local id
00154 /// @param seqid Sequence identifier to examine [in]
00155 /// @return if seqid is NULL or if it is NOT a local id, it returns false
00156 
00157 bool IsLocalId(const objects::CSeq_id* seqid);
00158 
00159 /// Auxiliary function to convert a Seq-loc describing masked query regions to a 
00160 /// TMaskedQueryRegions object
00161 /// @param sloc Seq-loc to use as source (must be Packed-int or Seq-int) [in]
00162 /// @param program BLAST program type [in]
00163 /// @param assume_both_strands ignores the strand of sloc_in and adds masking
00164 /// locations to both strands in return value. This is irrelevant for protein
00165 /// queries
00166 /// @return List of masked query regions.
00167 /// @throws CBlastException if Seq-loc type cannot be converted to Packed-int
00168 
00169 TMaskedQueryRegions
00170 PackedSeqLocToMaskedQueryRegions(CConstRef<objects::CSeq_loc> sloc,
00171                                  EBlastProgramType program,
00172                                  bool assume_both_strands = false);
00173 
00174 /// Interface to build a CSeq-loc from a TMaskedQueryRegion; note that
00175 /// conversion conversion in this direction can be lossy.
00176 /// It might be better to convert the calling code to use a CBlastQueryVector.
00177 
00178 CRef<objects::CSeq_loc>
00179 MaskedQueryRegionsToPackedSeqLoc( const TMaskedQueryRegions & sloc);
00180 
00181 
00182 /// Converts a BlastMaskLoc internal structure into an object returned by the 
00183 /// C++ API.
00184 /// @param program Type of BLAST program [in]
00185 /// @param queries Container of query ids and start/stop locations [in]
00186 /// @param mask All masking locations [in]
00187 /// @param mask_v Vector of per-query lists of mask locations in CSeqLocInfo 
00188 ///               form. [out]
00189 
00190 void 
00191 Blast_GetSeqLocInfoVector(EBlastProgramType program, 
00192                           const objects::CPacked_seqint& queries,
00193                           const BlastMaskLoc* mask, 
00194                           TSeqLocInfoVector& mask_v);
00195 
00196 /** Initializes and uninitializes the genetic code singleton as if it was an
00197  * automatic variable. It also provides MT-safety.*/
00198 class  CAutomaticGenCodeSingleton {
00199 public:
00200     /// Default constructor
00201     CAutomaticGenCodeSingleton();
00202     /// destructor
00203     ~CAutomaticGenCodeSingleton();
00204 private:
00205     /// Reference counter for this object so that the genetic code singleton is
00206     //not deleted prematurely
00207     static Uint4 m_RefCounter;
00208 };
00209 
00210 
00211 /** Declares class to handle deallocating of the structure using the appropriate
00212  * function
00213  */
00214 
00215 #define DECLARE_AUTO_CLASS_WRAPPER(struct_name, free_func)                  \
00216 /** Wrapper class for struct_name. */                                       \
00217                                                                             \
00218 class  C##struct_name : public CObject                    \
00219 {                                                                           \
00220 public:                                                                     \
00221     C##struct_name() : m_Ptr(NULL) {}                                       \
00222     C##struct_name(struct_name* p) : m_Ptr(p) {}                            \
00223     virtual ~C##struct_name() { Reset(); }                                  \
00224     void Reset(struct_name* p = NULL) {                                     \
00225         if (m_Ptr) {                                                        \
00226             free_func(m_Ptr);                                               \
00227         }                                                                   \
00228         m_Ptr = p;                                                          \
00229     }                                                                       \
00230     struct_name* Release() {                                                \
00231         struct_name* retval = m_Ptr;                                        \
00232         m_Ptr = NULL;                                                       \
00233         return retval;                                                      \
00234     }                                                                       \
00235     struct_name* Get() const { return m_Ptr; }                              \
00236     operator struct_name *() { return m_Ptr; }                              \
00237     operator struct_name *() const { return m_Ptr; }                        \
00238     struct_name* operator->() { return m_Ptr; }                             \
00239     struct_name* operator->() const { return m_Ptr; }                       \
00240     struct_name** operator&() { return &m_Ptr; }                            \
00241     virtual void DebugDump(CDebugDumpContext ddc, unsigned int depth) const;\
00242 private:                                                                    \
00243     struct_name* m_Ptr;                                                     \
00244 }
00245 
00246 #ifndef SKIP_DOXYGEN_PROCESSING
00247 
00248 /* Don't forget to define DebugDump for each of these in blast_aux.cpp! */
00249 
00250 DECLARE_AUTO_CLASS_WRAPPER(BLAST_SequenceBlk, BlastSequenceBlkFree);
00251 
00252 DECLARE_AUTO_CLASS_WRAPPER(BlastQueryInfo, BlastQueryInfoFree);
00253 DECLARE_AUTO_CLASS_WRAPPER(QuerySetUpOptions, BlastQuerySetUpOptionsFree);
00254 
00255 DECLARE_AUTO_CLASS_WRAPPER(LookupTableOptions, LookupTableOptionsFree);
00256 DECLARE_AUTO_CLASS_WRAPPER(LookupTableWrap, LookupTableWrapFree);
00257 
00258 DECLARE_AUTO_CLASS_WRAPPER(BlastInitialWordOptions,
00259                            BlastInitialWordOptionsFree);
00260 DECLARE_AUTO_CLASS_WRAPPER(BlastInitialWordParameters,
00261                            BlastInitialWordParametersFree);
00262 
00263 DECLARE_AUTO_CLASS_WRAPPER(Blast_ExtendWord, BlastExtendWordFree);
00264 DECLARE_AUTO_CLASS_WRAPPER(BlastExtensionOptions, BlastExtensionOptionsFree);
00265 DECLARE_AUTO_CLASS_WRAPPER(BlastExtensionParameters, BlastExtensionParametersFree);
00266 
00267 DECLARE_AUTO_CLASS_WRAPPER(BlastHitSavingOptions, BlastHitSavingOptionsFree);
00268 DECLARE_AUTO_CLASS_WRAPPER(BlastHitSavingParameters,
00269                            BlastHitSavingParametersFree);
00270 
00271 DECLARE_AUTO_CLASS_WRAPPER(PSIBlastOptions, PSIBlastOptionsFree);
00272 DECLARE_AUTO_CLASS_WRAPPER(BlastDatabaseOptions, BlastDatabaseOptionsFree);
00273 
00274 DECLARE_AUTO_CLASS_WRAPPER(BlastScoreBlk, BlastScoreBlkFree);
00275 DECLARE_AUTO_CLASS_WRAPPER(BlastScoringOptions, BlastScoringOptionsFree);
00276 DECLARE_AUTO_CLASS_WRAPPER(BlastScoringParameters, BlastScoringParametersFree);
00277 
00278 DECLARE_AUTO_CLASS_WRAPPER(BlastEffectiveLengthsOptions,
00279                            BlastEffectiveLengthsOptionsFree);
00280 DECLARE_AUTO_CLASS_WRAPPER(BlastEffectiveLengthsParameters,
00281                            BlastEffectiveLengthsParametersFree);
00282 
00283 DECLARE_AUTO_CLASS_WRAPPER(BlastGapAlignStruct, BLAST_GapAlignStructFree);
00284 DECLARE_AUTO_CLASS_WRAPPER(BlastHSPResults, Blast_HSPResultsFree);
00285 
00286 DECLARE_AUTO_CLASS_WRAPPER(PSIMsa, PSIMsaFree);
00287 DECLARE_AUTO_CLASS_WRAPPER(PSIMatrix, PSIMatrixFree);
00288 DECLARE_AUTO_CLASS_WRAPPER(PSIDiagnosticsRequest, PSIDiagnosticsRequestFree);
00289 DECLARE_AUTO_CLASS_WRAPPER(PSIDiagnosticsResponse, PSIDiagnosticsResponseFree);
00290 
00291 DECLARE_AUTO_CLASS_WRAPPER(BlastSeqSrc, BlastSeqSrcFree);
00292 DECLARE_AUTO_CLASS_WRAPPER(BlastSeqSrcIterator, BlastSeqSrcIteratorFree);
00293 DECLARE_AUTO_CLASS_WRAPPER(Blast_Message, Blast_MessageFree);
00294 
00295 DECLARE_AUTO_CLASS_WRAPPER(BlastMaskLoc, BlastMaskLocFree);
00296 DECLARE_AUTO_CLASS_WRAPPER(BlastSeqLoc, BlastSeqLocFree);
00297 
00298 DECLARE_AUTO_CLASS_WRAPPER(SBlastProgress, SBlastProgressFree);
00299 
00300 #endif /* SKIP_DOXYGEN_PROCESSING */
00301 
00302 END_SCOPE(blast)
00303 END_NCBI_SCOPE
00304 
00305 /* @} */
00306 
00307 #endif  /* ALGO_BLAST_API___BLAST_AUX__HPP */
00308 
00309 

Generated on Wed Dec 9 02:54:29 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Wed Dec 09 08:17:25 2009 by modify_doxy.py rev. 173732