/*****************************************************************************
#   Copyright (C) 1994-2008 by David Gordon.
#   All rights reserved.                           
#                                                                           
#   This software is part of a beta-test version of the Consed/Autofinish
#   package.  It should not be redistributed or
#   used for any commercial purpose, including commercially funded
#   sequencing, without written permission from the author and the
#   University of Washington.
#   
#   This software is provided ``AS IS'' and any express or implied
#   warranties, including, but not limited to, the implied warranties of
#   merchantability and fitness for a particular purpose, are disclaimed.
#   In no event shall the authors or the University of Washington be
#   liable for any direct, indirect, incidental, special, exemplary, or
#   consequential damages (including, but not limited to, procurement of
#   substitute goods or services; loss of use, data, or profits; or
#   business interruption) however caused and on any theory of liability,
#   whether in contract, strict liability, or tort (including negligence
#   or otherwise) arising in any way out of the use of this software, even
#   if advised of the possibility of such damage.
#
#   Building Consed from source is error prone and not simple which is
#   why I provide executables.  Due to time limitations I cannot
#   provide any assistance in building Consed.  Even if you do not
#   modify the source, you may introduce errors due to using a
#   different version of the compiler, a different version of motif,
#   different versions of other libraries than I used, etc.  For this
#   reason, if you discover Consed bugs, I can only offer help with
#   those bugs if you first reproduce those bugs with an executable
#   provided by me--not an executable you have built.
# 
#   Modifying Consed is also difficult.  Although Consed is modular,
#   some modules are used by many other modules.  Thus making a change
#   in one place can have unforeseen effects on many other features.
#   It may takes months for you to notice these other side-effects
#   which may not seen connected at all.  It is not feasable for me to
#   provide help with modifying Consed sources because of the
#   potentially huge amount of time involved.
#
#*****************************************************************************/
#include    "createPrimerCandidate.h"
#include    "primerType.h"
#include    "contig.h"
#include    "mbt_exception.h"
#include    "nConvertFrom4Mer.h"
#include    "nLengthOf4Mer.h"
#include    "complementSequence.h"
#include    <stdlib.h>  // for abs function
#include    "consedParameters.h"
#include    "whyIsPrimerNotAcceptableTypes.h"
           


void createPrimerCandidate( 
                   primerType* pPrimer, 
                   const int nUnpaddedPrimerLeftMostBase, // 1-based coordinate
                   const int nLengthOfPrimer, 
                   const bool bForwardNotReversePrimer,
                   Contig* pContigOfPrimer,
                   const int nUnpaddedPosOfCursor ) {


   pPrimer->pContig_ = pContigOfPrimer;
   pPrimer->nUnpaddedLength_ = nLengthOfPrimer;

   pPrimer->nUnpaddedStart_ = nUnpaddedPrimerLeftMostBase;
   pPrimer->nUnpaddedEnd_ = pPrimer->nUnpaddedStart_ + 
         pPrimer->nUnpaddedLength_ - 1;

   pPrimer->bTopStrandNotBottomStrand_ = bForwardNotReversePrimer;

   pPrimer->nDistanceFromCursor_ = 
      ( bForwardNotReversePrimer ) ?
      ( nUnpaddedPosOfCursor - pPrimer->nUnpaddedEnd_ ) :
      ( pPrimer->nUnpaddedStart_ - nUnpaddedPosOfCursor );

   // when called from autofinish, nUnpaddedPosOfCursor is 0 (not set)
   // assert( pPrimer->nDistanceFromCursor_ >= 0 );

   int nZeroBasedUnpaddedPrimer5PrimeBase;
   int nPrimerPosOf3Prime4Bases = nLengthOfPrimer - nLengthOf4Mer;

   // nZeroBasedUnpaddedPrimer5PrimeBase is with respect to the beginning
   // of the contig.  nPrimerPos... is with respect to the beginning of 
   // the primer

   contigMatchTablesType* pUnpaddedContig;

   if (bForwardNotReversePrimer ) {
      pUnpaddedContig = pContigOfPrimer->pUnpaddedContig_;
      nZeroBasedUnpaddedPrimer5PrimeBase = pPrimer->nUnpaddedStart_ - 1;

   }
   else {
      pUnpaddedContig = pContigOfPrimer->pUnpaddedContigComplemented_;

      nZeroBasedUnpaddedPrimer5PrimeBase = 
         pContigOfPrimer->nComplementUnpaddedIndex( 
                 pPrimer->nUnpaddedStart_ + nLengthOfPrimer - 1 
                                  ) - 1;
         
   }

   pPrimer->pCurrentUnpaddedContig_ = pContigOfPrimer->pUnpaddedContig_;

   pPrimer->szPrimer_ = 
      pUnpaddedContig->szUnpaddedBases_ 
      + nZeroBasedUnpaddedPrimer5PrimeBase;
   
   
   pPrimer->pUnpaddedQualityArray_ =
      pUnpaddedContig->pUnpaddedQualityArray_
      + nZeroBasedUnpaddedPrimer5PrimeBase;



   Quality ucMinQualityIn3Prime4Bases = 100;  // some absurdly large number
                                              // that fits in an unsigned char
   int nPrimerPos;
   for( nPrimerPos = nPrimerPosOf3Prime4Bases;
        nPrimerPos < nLengthOfPrimer;
        ++nPrimerPos ) {

      if ( pPrimer->pUnpaddedQualityArray_[ nPrimerPos ] < 
           ucMinQualityIn3Prime4Bases ) 
         ucMinQualityIn3Prime4Bases = 
            pPrimer->pUnpaddedQualityArray_[ nPrimerPos ];
   }

   pPrimer->ucMinQualityIn3Prime4Bases_ = ucMinQualityIn3Prime4Bases;

   Quality ucMinQuality = ucMinQualityIn3Prime4Bases;
   for( nPrimerPos = 0;
        nPrimerPos < nPrimerPosOf3Prime4Bases;
        ++nPrimerPos ) {

      if ( pPrimer->pUnpaddedQualityArray_[ nPrimerPos ] < 
           ucMinQuality )
         ucMinQuality =
             pPrimer->pUnpaddedQualityArray_[ nPrimerPos ];
   }

   pPrimer->ucMinQuality_ = ucMinQuality;

   if ( consedParameters::pGetConsedParameters()->nPrimersMinQuality_ <=
        pPrimer->ucMinQuality_ )
      pPrimer->bAcceptable_ = true;
   else {
      pPrimer->bAcceptable_ = false;
      pPrimer->nWhyIsPrimerNotAcceptable_ = BAD_PRIMER_TOO_LOW_QUALITY;
   }

   if ( pPrimer->bAcceptable_ ) {

      if ( !pCP->bPrimersOKToChoosePrimersInSingleSubcloneRegion_ ||
           !pCP->bPrimersOKToChoosePrimersWhereHighQualityDiscrepancies_ ||
           !pCP->bPrimersOKToChoosePrimersWhereUnalignedHighQualityRegion_ ) {
      
         int nWhichProblem;
         if ( pContigOfPrimer->bIsThereAProblemHere( 
                              pPrimer->nUnpaddedStart_,
                              pPrimer->nUnpaddedEnd_,
                              nWhichProblem ) ) {

            pPrimer->bAcceptable_ = false;
            pPrimer->nWhyIsPrimerNotAcceptable_ = nWhichProblem;
         }
      }
   }


   char* sz3Prime4Bases = pPrimer->szPrimer_ + nPrimerPosOf3Prime4Bases;

   pPrimer->n3Prime4Bases_ = nConvertFrom4Mer( sz3Prime4Bases );

   pPrimer->pContigOfStickiestFalseMatch_ = NULL;
   pPrimer->nScoreOfStickiestFalseMatch_ = 0;
   pPrimer->nSelfMatchScore_ = 0;
   pPrimer->nSequenceInFileStickiestScore_ = 0;
   pPrimer->nLengthOfMononucleotideRun_ = -1; // unknown, and not
   // all primers will be tested for this

   for( int n = 0; n < nNUMBER_OF_TEMPLATES; ++n )
      pPrimer->pSubcloneTemplate_[ n ] = NULL;
}