Exemplo n.º 1
0
static int load_discordant_reads(MEI_data& mei_data, std::vector<bam_info>& bam_sources, const std::string& chr_name,
                                 const SearchWindow& window, UserDefinedSettings* userSettings) {
    // Loop over associated bam files.
    for (size_t i = 0; i < bam_sources.size(); i++) {
        // Locate file.
        bam_info source = bam_sources.at(i);
        
        LOG_DEBUG(*logStream << time_log() << "Loading discordant reads from " << source.BamFile << std::endl);
        
        // Setup link to bamfile, its index and header.
        bamFile fp = bam_open(source.BamFile.c_str(), "r");
        bam_index_t *idx = bam_index_load(source.BamFile.c_str());
        
        if (idx == NULL) {
            LOG_WARN(*logStream << time_log() << "Failed to load index for " << source.BamFile.c_str() << std::endl);
            LOG_WARN(*logStream << "Skipping window: " << chr_name << ", " << window.getStart() << "--" <<
                     window.getEnd() << " for BAM-file: " << source.BamFile.c_str() << std::endl);
            continue;
        }
        
        bam_header_t *header = bam_header_read(fp);
        bam_init_header_hash(header);
        int tid = bam_get_tid(header, chr_name.c_str());
        
        if (tid < 0) {
            LOG_WARN(*logStream << time_log() << "Could not find sequence in alignment file: '" << chr_name <<
                     "'" << std::endl);
            LOG_WARN(*logStream << "Skipping window: " << chr_name << ", " << window.getStart() << "--" <<
                     window.getEnd() << " for BAM-file: " << source.BamFile.c_str() << std::endl);
            continue;
        }
        
        mei_data.sample_names = get_sample_dictionary(header);
        
        // Save insert size of current bamfile in data object provided for callback function.
        // Note: the insert size should ideally be separate from the MEI_data object, tried to do
        // this using a std::pair object, which did not work.  Suggestions are welcome here.
        mei_data.current_insert_size = source.InsertSize;
        mei_data.current_chr_name = chr_name;
        
        // Set up environment variable for callback function.
        std::pair<MEI_data*, UserDefinedSettings*> env = std::make_pair(&mei_data, userSettings);
        
        // Load discordant reads into mei_data.
        bam_fetch(fp, idx, tid, window.getStart(), window.getEnd(), &env, fetch_disc_read_callback);
        bam_index_destroy(idx);
    }
    return 0;
}
Exemplo n.º 2
0
int searchIndels(ControlState& currentState, unsigned NumBoxes, const SearchWindow& window )
{

   static int Count_DI = 0;
   static int Count_DI_Plus = 0;
   static int Count_DI_Minus = 0;

   unsigned CloseIndex, FarIndex;

   std::vector<unsigned> DI[NumBoxes];
   unsigned TempBoxIndex;
   LOG_INFO(*logStream << "Searching deletion-insertions ... " << std::endl);

   //UserDefinedSettings *userSettings = UserDefinedSettings::Instance();

   for (unsigned ReadIndex = 0; ReadIndex < currentState.Reads_SR.size(); ReadIndex++) {
      SPLIT_READ& currentRead = currentState.Reads_SR[ReadIndex];
      if (currentRead.Used
            || currentRead.UP_Far.empty() || currentRead.FragName != currentRead.FarFragName) {
         continue;
      }

      CloseIndex = currentRead.UP_Close.size() - 1;
      FarIndex = currentRead.UP_Far.size() - 1;
      if (currentRead.UP_Far[FarIndex].Mismatches + currentRead.UP_Close[CloseIndex].Mismatches
            > (short) (1 + userSettings->Seq_Error_Rate * (currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr))) {
         continue;
      }

      if (currentRead.MatchedD == Plus) {
         if (currentRead.UP_Far[FarIndex].Direction == Minus) {
            if (currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr < currentRead.getReadLength() &&
                  currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr >= userSettings->Min_Num_Matched_Bases &&
                  currentRead.UP_Far[FarIndex].AbsLoc > currentRead.UP_Close[CloseIndex].AbsLoc + 1) {
               currentRead.Left = currentRead.UP_Close[CloseIndex].AbsLoc - currentRead.UP_Close[CloseIndex].LengthStr + 1;
               currentRead.Right = currentRead.UP_Far[FarIndex].AbsLoc + currentRead.UP_Far[FarIndex].LengthStr - 1;
               currentRead.BP = currentRead.UP_Close[CloseIndex].LengthStr - 1;
               currentRead.NT_size = currentRead.getReadLength() - currentRead.UP_Far[FarIndex].LengthStr - currentRead.UP_Close[CloseIndex].LengthStr;

               currentRead.NT_str = ReverseComplement( currentRead. getUnmatchedSeq()). substr( currentRead.BP + 1, currentRead.NT_size);
               currentRead.IndelSize = (currentRead.Right - currentRead.Left) + currentRead.NT_size - currentRead.getReadLengthMinus();

               currentRead.BPLeft = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
               currentRead.BPRight = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;

               if (1) {

                  if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                     saveReadForNextCycle(currentRead, currentState.FutureReads_SR);
                  } else {
                     if (readInSpecifiedRegion( currentRead, userSettings->getRegion() ) ) {
                        TempBoxIndex = (int) (currentRead. BPLeft) / BoxSize;
                        if (TempBoxIndex < NumBoxes) {

                           DI[TempBoxIndex]. push_back(ReadIndex);
                           currentRead.Used = true;
                           Count_DI++;
                           Count_DI_Plus++;
                        }
                     }
                  }
               }
            }
         }
      } else if (currentRead.MatchedD == Minus) {
         if (currentRead.UP_Far[FarIndex].Direction == Plus) {
            if (currentRead.UP_Close[CloseIndex].LengthStr + currentRead.UP_Far[FarIndex].LengthStr < currentRead.getReadLength() &&
                  currentRead.UP_Close[CloseIndex].LengthStr + currentRead.UP_Far[FarIndex].LengthStr >= userSettings->Min_Num_Matched_Bases &&
                  currentRead.UP_Close[CloseIndex].AbsLoc > currentRead.UP_Far[FarIndex].AbsLoc + 1) {

               currentRead.Left = currentRead.UP_Far[FarIndex].AbsLoc - currentRead.UP_Far[FarIndex].LengthStr + 1;
               currentRead.Right = currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr - 1;
               currentRead.BP = currentRead.UP_Far[FarIndex].LengthStr - 1;
               currentRead.NT_size = currentRead.getReadLength() - currentRead.UP_Close[CloseIndex].LengthStr - currentRead.UP_Far[FarIndex].LengthStr;
               currentRead.NT_str = currentRead.getUnmatchedSeq(). substr( currentRead.BP + 1, currentRead.NT_size);

               currentRead.IndelSize = (currentRead.Right - currentRead.Left) - currentRead.getReadLengthMinus() + currentRead.NT_size;
               currentRead.BPLeft = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;
               currentRead.BPRight = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
               {
                  if ( 1 ) {

                     if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                        saveReadForNextCycle( currentRead, currentState.FutureReads_SR);
                     } else {
                        if (readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                           TempBoxIndex = (int) (currentRead. BPLeft) / BoxSize;
                           if (TempBoxIndex < NumBoxes) {

                              DI[TempBoxIndex]. push_back(ReadIndex);
                              currentRead.Used = true;
                              Count_DI++;
                              Count_DI_Minus++;
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
   LOG_INFO(*logStream << "Total: " << Count_DI << "\t+" << Count_DI_Plus << "\t-"
            << Count_DI_Minus << std::endl);
   std::ofstream DeletionOutf( userSettings->getDOutputFilename().c_str(), std::ios::app);
   std::ofstream inversionsOutf( userSettings->getINVOutputFilename().c_str(), std::ios::app);
   SortOutputDI(currentState, NumBoxes, window.getChromosome()->getSeq(), currentState.Reads_SR, DI, DeletionOutf, inversionsOutf);
   DeletionOutf.close();
   for (unsigned int i = 0; i < NumBoxes; i++) {
      DI[i].clear();
   }

   return EXIT_SUCCESS;
}
int searchTandemDuplicationsNT(ControlState& currentState, unsigned NumBoxes, const SearchWindow& window)
{

   static int Count_TD_NT = 0;
   static int Count_TD_NT_Plus = 0;
   static int Count_TD_NT_Minus = 0;

   std::vector<unsigned> TD_NT[NumBoxes];

   int CloseIndex = 0;
   int FarIndex = 0;

	//UserDefinedSettings* userSettings = UserDefinedSettings::Instance();

   LOG_INFO(*logStream << "Searching tandem duplication events with non-template sequence ... " << std::endl);
   for (unsigned ReadIndex = 0; ReadIndex < currentState.Reads_SR.size(); ReadIndex++) {
		SPLIT_READ& currentRead = currentState.Reads_SR[ReadIndex];
      if (currentRead.Used || currentRead.UP_Far.empty() || currentRead.FragName != currentRead.FarFragName) {
         continue;
      }
      CloseIndex = currentRead.UP_Close.size() - 1;
      FarIndex = currentRead.UP_Far.size() - 1;
      if (currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr >= currentRead.getReadLength()) {
         continue;
      }
      if (currentRead.UP_Far[FarIndex].Mismatches + currentRead.UP_Close[CloseIndex].Mismatches > 
          (short) (1 + userSettings->Seq_Error_Rate * (currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr))) {
         continue;
      }
      if (currentRead.MatchedD == Plus) {
         if (currentRead.UP_Far[FarIndex].Direction == Minus) {
            if (currentRead.UP_Far[FarIndex].AbsLoc + currentRead.UP_Far[FarIndex].LengthStr < currentRead.UP_Close[CloseIndex].AbsLoc && 
                currentRead.UP_Far[FarIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr < currentRead.UP_Close[CloseIndex].AbsLoc && 
                currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr > userSettings->Min_Num_Matched_Bases) {

               currentRead.Right = currentRead.UP_Close[CloseIndex].AbsLoc - currentRead.UP_Close[CloseIndex].LengthStr + 1;
               currentRead.Left = currentRead.UP_Far[FarIndex].AbsLoc + currentRead.UP_Far[FarIndex].LengthStr - 1;
               currentRead.BP = currentRead.UP_Close[CloseIndex].LengthStr - 1;

               currentRead.IndelSize = currentRead.UP_Close[CloseIndex].AbsLoc - currentRead.UP_Far[FarIndex].AbsLoc + 1;
               currentRead.NT_size = currentRead.getReadLength() - currentRead.UP_Close[CloseIndex].LengthStr - currentRead.UP_Far[FarIndex].LengthStr;
               currentRead.NT_str = currentRead.getUnmatchedSeqRev().substr( currentRead.BP + 1, currentRead.NT_size);
               currentRead.BPRight = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
               currentRead.BPLeft = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;
               if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                  saveReadForNextCycle(currentRead, currentState.FutureReads_SR);
               }
               else {
                  if ( readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                     TD_NT[(int) currentRead. BPLeft / BoxSize]. push_back(ReadIndex);
                     currentRead.Used = true;
                     Count_TD_NT++;
                     Count_TD_NT_Plus++;
                  }
               }
            }
         }
      }
      else if (currentRead.MatchedD == Minus) {
         if (currentRead.UP_Far[FarIndex].Direction == Plus) {
            if (currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr < currentRead.UP_Far[FarIndex].AbsLoc && 
                currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Far[FarIndex].LengthStr < currentRead.UP_Far[FarIndex].AbsLoc && 
                currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr > userSettings->Min_Num_Matched_Bases) {

               currentRead.Right = currentRead.UP_Far[FarIndex].AbsLoc - currentRead.UP_Far[FarIndex].LengthStr + 1;
               currentRead.Left = currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr - 1;
               currentRead.BP = currentRead.UP_Far[FarIndex].LengthStr - 1;

               currentRead.IndelSize = currentRead.UP_Far[FarIndex].AbsLoc - currentRead.UP_Close[CloseIndex].AbsLoc + 1;
               currentRead.NT_size = currentRead.getReadLength() - currentRead.UP_Close[CloseIndex].LengthStr - currentRead.UP_Far[FarIndex].LengthStr;
               currentRead.NT_str = currentRead.getUnmatchedSeq(). substr( currentRead.BP + 1, currentRead.NT_size);

               currentRead.BPRight = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;
               currentRead.BPLeft = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
               if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                  saveReadForNextCycle(currentRead, currentState.FutureReads_SR);
               }
               else {
                  if ( readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                     TD_NT[(int) currentRead. BPLeft / BoxSize]. push_back(ReadIndex);
                     currentRead.Used = true;

                     Count_TD_NT++;
                     Count_TD_NT_Minus++;
                  }
               }
            }
         }
      }
   }
   LOG_INFO(*logStream << "Total: " << Count_TD_NT << "\t+" << Count_TD_NT_Plus << "\t-" << Count_TD_NT_Minus << std::endl);
   std::ofstream TDOutf(userSettings->getTDOutputFilename().c_str(), std::ios::app);
   SortAndOutputTandemDuplications(currentState, NumBoxes, window.getChromosome()->getSeq(), currentState.Reads_SR, TD_NT, TDOutf, true);
   TDOutf.close();
   for (unsigned int i = 0; i < NumBoxes; i++) {
      TD_NT[i].clear();
   }

   return EXIT_SUCCESS;
}
Exemplo n.º 4
0
//			std::cout << currentRead.BPLeft << " " << BoxSize << " " << NumBoxes << " " << TempBoxIndex << std::endl;
int SearchVariant::Search(BDData & g_bdData, ControlState& currentState, const unsigned NumBoxes, const SearchWindow& window)
{
   std::vector<unsigned> Vars[NumBoxes];
   unsigned TempBoxIndex;

   unsigned int farEndExists = 0;
   unsigned int readsUsed = 0;
   unsigned int bpSum = 0;
   for (unsigned ReadIndex = 0; ReadIndex < currentState.Reads_SR.size(); ReadIndex++)  {
      SPLIT_READ& currentRead = currentState.Reads_SR[ReadIndex];
      if (currentRead.Used) {
         readsUsed++;
      }
      if (!currentRead.UP_Far.empty()) {
         farEndExists++;
         bpSum += currentRead.UP_Far[ currentRead.UP_Far.size()-1 ].AbsLoc;
      }
      if (bpSum>1000000000) {
         bpSum -= 1000000000;
      }
   }
   *logStream << "Reads already used: " << readsUsed << std::endl;
   *logStream << "Far ends already mapped " << farEndExists << std::endl;
   *logStream << "Checksum of far ends: " << bpSum << std::endl;

   //UserDefinedSettings* userSettings = UserDefinedSettings::Instance();
   LOG_INFO(*logStream << "Searching " << typeOfVariant << " ... " << std::endl);
   const std::string ChrSeq = window.getChromosome()->getSeq();
   for (unsigned ReadIndex = 0; ReadIndex < currentState.Reads_SR.size(); ReadIndex++) {

      SPLIT_READ& currentRead = currentState.Reads_SR[ReadIndex];
      //std::cout << ReadIndex << std::endl;
      //std::cout << currentRead << std::endl;

      if (currentRead.FragName != currentRead.FarFragName) {
         //std::cout << "inter-chromosome split-read" << std::endl;
         continue;
      }

      if (currentRead.Used || currentRead.UP_Far.empty()) {
         continue;
      }
      if (currentRead.MatchedD == Plus) {
         for (short MAX_SNP_ERROR_index = 0; MAX_SNP_ERROR_index <= currentRead.getMAX_SNP_ERROR(); MAX_SNP_ERROR_index++) {
            if (currentRead.Used) {
               break;
            }
            for (unsigned int CloseIndex = 0; CloseIndex
                  < currentRead.UP_Close.size(); CloseIndex++) {
               if (currentRead.Used) {
                  break;
               }
               if (currentRead.UP_Close[CloseIndex].Mismatches > MAX_SNP_ERROR_index) {
                  continue;
               }
               for (int FarIndex = currentRead.UP_Far.size() - 1; FarIndex >= 0; FarIndex--) {
                  if (currentRead.Used) {
                     break;
                  }
                  if (currentRead.UP_Far[FarIndex]. Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex]. Mismatches + currentRead.UP_Close[CloseIndex]. Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex]. Direction == Minus) {
                     if (decisionBranch1(currentRead, CloseIndex, FarIndex)) {

                        currentRead.Left = currentRead. UP_Close[CloseIndex].AbsLoc - currentRead. UP_Close[CloseIndex].LengthStr + 1;
                        currentRead.Right = currentRead.UP_Far[FarIndex]. AbsLoc + currentRead.UP_Far[FarIndex]. LengthStr - 1;
                        currentRead.BP = currentRead. UP_Close[CloseIndex].LengthStr - 1;

                        currentRead.IndelSize = calculateIndelSize(currentRead);

                        currentRead.NT_str = getInsertedStr1(currentRead);

                        currentRead.BPLeft = currentRead. UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
                        currentRead.BPRight = currentRead.UP_Far[FarIndex]. AbsLoc - g_SpacerBeforeAfter;
                        unsigned RealBP_left= currentRead.BPLeft;
                        unsigned RealBP_right = currentRead.BPRight;//, DIFF;
                        if (ChrSeq.size() < RealBP_left || ChrSeq.size() < RealBP_right) {
                           currentRead.Used = true;
                           break;
                        }
                        if (currentRead.NT_str.size()) {
                           //std::cout << "ns i currentRead.NT_str.size()" << std::endl;
                           //std::cout << currentRead.NT_str << " " << RealBP_left << " " << RealBP_right << std::endl;
                           GetRealStart4Insertion(ChrSeq, currentRead.NT_str, RealBP_left, RealBP_right);
                           //std::cout << "ne i currentRead.NT_str.size()" << std::endl;
                        } else {
                           //std::cout << "no_ns d currentRead.NT_str.size()" << std::endl;
                           //std::cout << RealBP_left << " " << RealBP_right << std::endl;
                           GetRealStart4Deletion(ChrSeq, RealBP_left, RealBP_right);
                           //std::cout << "no_ns d currentRead.NT_str.size()" << std::endl;
                        }
                        short DIFF = currentRead.BPLeft - RealBP_left;
                        DIFF = !((currentRead.BP - 1)<DIFF)?DIFF:(currentRead.BP - 1); // min(DIFF, currentRead.BP - 1);
                        if (DIFF > 0)  {
                           //std::cout << "DIFF " << DIFF << std::endl;
                           currentRead.BP -= DIFF;
                           currentRead.BPLeft -= DIFF;
                           currentRead.BPRight  -= DIFF;
                        }
                        if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                           saveReadForNextCycle( currentRead, currentState.FutureReads_SR);
                        } else {
                           if (readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                              TempBoxIndex = (int) (currentRead. BPLeft) / BoxSize;
                              //std::cout << currentRead.BPLeft << " " << BoxSize << " " << NumBoxes << " " << TempBoxIndex << std::endl;
                              if (TempBoxIndex < NumBoxes) {

                                 Vars[TempBoxIndex].push_back(ReadIndex);
                                 currentRead.Used = true;
                                 Count_Var_Plus++;
                                 Count_Var++;
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      } else if (currentRead.MatchedD == Minus) {
         for (short MAX_SNP_ERROR_index = 0; MAX_SNP_ERROR_index <= currentRead.getMAX_SNP_ERROR(); MAX_SNP_ERROR_index++) {
            if (currentRead.Used) {
               break;
            }
            for (int CloseIndex = currentRead.UP_Close.size() - 1; CloseIndex >= 0; CloseIndex--) {
               if (currentRead.Used) {
                  break;
               }
               if (currentRead.UP_Close[CloseIndex]. Mismatches > MAX_SNP_ERROR_index) {
                  continue;
               }
               for (int FarIndex = currentRead.UP_Far.size() - 1; FarIndex >= 0; FarIndex--) {
                  if (currentRead.Used) {
                     break;
                  }
                  if (currentRead.UP_Far[FarIndex]. Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex]. Mismatches + currentRead.UP_Close[CloseIndex]. Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex]. Direction == Plus) {
                     if (decisionBranch2(currentRead, CloseIndex, FarIndex)) {

                        currentRead.Left = currentRead.UP_Far[FarIndex]. AbsLoc - currentRead.UP_Far[FarIndex]. LengthStr + 1;
                        currentRead.Right = currentRead. UP_Close[CloseIndex].AbsLoc + currentRead. UP_Close[CloseIndex].LengthStr - 1;
                        currentRead.BP = currentRead.UP_Far[FarIndex]. LengthStr - 1;


                        currentRead.IndelSize = calculateIndelSize(currentRead);

                        currentRead.NT_str = getInsertedStr2(currentRead);

                        currentRead.BPLeft = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;
                        currentRead.BPRight = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
                        unsigned RealBP_left= currentRead.BPLeft;
                        unsigned RealBP_right = currentRead.BPRight;//, DIFF;

                        if (ChrSeq.size() < RealBP_left || ChrSeq.size() < RealBP_right) {
                           currentRead.Used = true;
                           break;
                        }
                        if (currentRead.NT_str.size()) {
                           //std::cout << "ns i currentRead.NT_str.size()" << std::endl;
                           //std::cout << currentRead.NT_str << " " << RealBP_left << " " << RealBP_right << std::endl;
                           GetRealStart4Insertion(ChrSeq, currentRead.NT_str, RealBP_left, RealBP_right);
                           //std::cout << "ne i currentRead.NT_str.size()" << std::endl;
                        } else {
                           //std::cout << "no_ns d currentRead.NT_str.size()" << std::endl;
                           //std::cout <<  RealBP_left << " " << RealBP_right << std::endl;
                           GetRealStart4Deletion(ChrSeq, RealBP_left, RealBP_right);
                           //std::cout << "no_ne d currentRead.NT_str.size()" << std::endl;
                        }
                        short DIFF = currentRead.BPLeft - RealBP_left;
                        DIFF = !((currentRead.BP - 1)<DIFF)?DIFF:(currentRead.BP - 1);
                        if (DIFF > 0) {
                           // std::cout << DIFF << std::endl;
                           //std::cout << "DIFF " << DIFF << std::endl;
                           currentRead.BP -= DIFF;
                           currentRead.BPLeft -= DIFF;
                           currentRead.BPRight  -= DIFF;
                        }
                        if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                           saveReadForNextCycle( currentRead, currentState.FutureReads_SR);
                        } else {
                           if (readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                              TempBoxIndex = (int) (currentRead. BPLeft) / BoxSize;
                              //std::cout << currentRead.BPLeft << " " << BoxSize << " " << NumBoxes << " " << TempBoxIndex << std::endl;
                              if (TempBoxIndex < NumBoxes) {

                                 Vars[TempBoxIndex]. push_back(ReadIndex);
                                 currentRead.Used = true;
                                 Count_Var++;
                                 Count_Var_Minus++;
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
   LOG_INFO(*logStream << "Total: " << Count_Var << "\t+" << Count_Var_Plus << "\t-" << Count_Var_Minus << std::endl);


   outputResults(g_bdData, currentState, Vars, NumBoxes, window);

   for (unsigned int i = 0; i < NumBoxes; i++) {
      Vars[i].clear();
   }

   return EXIT_SUCCESS;
}
int searchTandemDuplications(ControlState& currentState, unsigned NumBoxes, const SearchWindow& window)
{

   static int Count_TD = 0;
   static int Count_TD_Plus = 0;
   static int Count_TD_Minus = 0;

   std::vector<unsigned> TD[NumBoxes];
   //UserDefinedSettings* userSettings = UserDefinedSettings::Instance();
   unsigned TempBoxIndex;

   LOG_INFO(*logStream << "Searching tandem duplication events ... " << std::endl);
   //std::cout << "std1" << std::endl;
   for (unsigned ReadIndex = 0; ReadIndex < currentState.Reads_SR.size(); ReadIndex++) {
      SPLIT_READ& currentRead = currentState.Reads_SR[ReadIndex];
      //std::cout << "std1a" << std::endl;
      if (currentRead.Used || currentRead.UP_Far.empty() || currentRead.FragName != currentRead.FarFragName) {
         continue;
      }
      //std::cout << "std1b" << std::endl;
      if (currentRead.MatchedD == Plus) {
         //std::cout << "in TD plus" << std::endl;
         for (short MAX_SNP_ERROR_index = 0; MAX_SNP_ERROR_index <= currentRead.getMAX_SNP_ERROR(); MAX_SNP_ERROR_index++) {
            //std::cout << "std1c" << std::endl;
            for (unsigned int CloseIndex = 0; CloseIndex < currentRead.UP_Close.size(); CloseIndex++) {
               //std::cout << "std1d" << std::endl;
               if (currentRead.Used /* || currentRead. BPLeft == 0*/) {
                  break;
               }
               if (currentRead.UP_Close[CloseIndex].Mismatches > MAX_SNP_ERROR_index) {
                  continue;
               }
               //std::cout << "std1e " << currentRead.UP_Far.size() << std::endl;
               for (int FarIndex = currentRead.UP_Far.size() - 1; FarIndex >= 0; FarIndex--) {
                  //std::cout << "std1f" << std::endl;
                  if (currentRead.Used) {
                     break;
                  }
                  if (currentRead.UP_Far[FarIndex].Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex].Mismatches + currentRead.UP_Close[CloseIndex].Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  //std::cout << "std1g" << std::endl;
                  if (currentRead.UP_Far[FarIndex].Direction  == Minus) {

                     if (currentRead.UP_Far[FarIndex].LengthStr + currentRead.UP_Close[CloseIndex].LengthStr == currentRead.getReadLength() &&
                           currentRead.UP_Far[FarIndex].AbsLoc + currentRead.UP_Far[FarIndex].LengthStr < currentRead.UP_Close[CloseIndex].AbsLoc &&
                           currentRead.UP_Far[FarIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr < currentRead.UP_Close[CloseIndex].AbsLoc) {

                        currentRead.Right = currentRead.UP_Close[CloseIndex].AbsLoc - currentRead.UP_Close[CloseIndex].LengthStr + 1;
                        currentRead.Left = currentRead. UP_Far[FarIndex].AbsLoc + currentRead. UP_Far[FarIndex].LengthStr - 1;
                        currentRead.BP = currentRead.UP_Close[CloseIndex].LengthStr - 1;

                        currentRead.IndelSize = currentRead.UP_Close[CloseIndex].AbsLoc - currentRead. UP_Far[FarIndex].AbsLoc + 1;
                        currentRead.BPRight = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
                        currentRead.BPLeft = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;
                        if (currentRead. BPLeft == 0) {
                           continue;
                        }
                        LeftMostTD(currentState, currentRead, window);
                        if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                           saveReadForNextCycle( currentRead, currentState.FutureReads_SR);
                        } else {
                           if (readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                              TempBoxIndex = (int) (currentRead. BPLeft) / BoxSize;
                              if (TempBoxIndex < NumBoxes) {
                                 //std::cout << currentRead << std::endl;
                                 TD[TempBoxIndex]. push_back(ReadIndex);
                                 currentRead.Used = true;
                                 Count_TD++;
                                 Count_TD_Plus++;
                              }
                           }
                        }
                     }
                  }
               }
            }
         }

      } else if (currentRead.MatchedD == Minus) {
         //std::cout << "in Minus" << std::endl;
         for (short MAX_SNP_ERROR_index = 0; MAX_SNP_ERROR_index <= currentRead.getMAX_SNP_ERROR(); MAX_SNP_ERROR_index++) {
            //std::cout << "in Minus 1" << std::endl;
            for (int CloseIndex = currentRead.UP_Close.size() - 1; CloseIndex >= 0; CloseIndex--) {
               //std::cout << "in Minus 2" << std::endl;
               if (currentRead.Used /*|| currentRead. BPLeft == 0*/) {
                  break;
               }
               if (currentRead.UP_Close[CloseIndex].Mismatches > MAX_SNP_ERROR_index) {
                  continue;
               }
               //std::cout << "in Minus 3" << std::endl;
               for (int FarIndex = 0; FarIndex < (int) currentRead.UP_Far.size(); FarIndex++) {
                  if (currentRead.Used) {
                     break;
                  }
                  if (currentRead.UP_Far[FarIndex].Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex].Mismatches + currentRead.UP_Close[CloseIndex].Mismatches > MAX_SNP_ERROR_index) {
                     continue;
                  }
                  if (currentRead.UP_Far[FarIndex]. Direction == Plus) {
                     //std::cout << "BPLeft" << currentRead. BPLeft << std::endl;
                     if (currentRead.UP_Close[CloseIndex].LengthStr + currentRead.UP_Far[FarIndex].LengthStr == currentRead.getReadLength() &&
                           currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr < currentRead.UP_Far[FarIndex]. AbsLoc &&
                           currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Far[FarIndex].LengthStr < currentRead.UP_Far[FarIndex]. AbsLoc) {
                        currentRead.Right = currentRead. UP_Far[FarIndex].AbsLoc - currentRead. UP_Far[FarIndex].LengthStr + 1;
                        currentRead.Left = currentRead.UP_Close[CloseIndex].AbsLoc + currentRead.UP_Close[CloseIndex].LengthStr - 1;
                        currentRead.BP = currentRead. UP_Far[FarIndex].LengthStr - 1;

                        currentRead.IndelSize = currentRead. UP_Far[FarIndex].AbsLoc - currentRead.UP_Close[CloseIndex].AbsLoc + 1;
                        currentRead.BPRight = currentRead.UP_Far[FarIndex].AbsLoc - g_SpacerBeforeAfter;
                        currentRead.BPLeft = currentRead.UP_Close[CloseIndex].AbsLoc - g_SpacerBeforeAfter;
                        //std::cout << "in Minus 4a" << std::endl;
                        if (currentRead. BPLeft == 0) {
                           continue;
                        }
                        LeftMostTD(currentState, currentRead, window);
                        //std::cout << "in Minus 4b" << std::endl;
                        if (readTransgressesBinBoundaries( currentRead, window.getEnd())) {
                           saveReadForNextCycle( currentRead, currentState.FutureReads_SR);
                        } else {
                           if (readInSpecifiedRegion( currentRead, userSettings->getRegion())) {
                              TempBoxIndex = (int) (currentRead. BPLeft) / BoxSize;
                              if (TempBoxIndex < NumBoxes) {

                                 //std::cout << currentRead << std::endl;
                                 TD[TempBoxIndex]. push_back(ReadIndex);
                                 currentRead.Used = true;

                                 Count_TD++;
                                 Count_TD_Minus++;
                              }
                           }
                        }
                     }
                  }
               }
               //std::cout << "in Minus 4" << std::endl;
            }
         }
      }
   }
   //std::cout << "std2" << std::endl;
   LOG_INFO(*logStream << "Total: " << Count_TD << "\t+" << Count_TD_Plus << "\t-"  << Count_TD_Minus << std::endl);
   //std::cout << "TD 1" << std::endl;
   std::ofstream TDOutf(userSettings->getTDOutputFilename().c_str(), std::ios::app);
   SortAndOutputTandemDuplications(currentState, NumBoxes, window.getChromosome()->getSeq(), currentState.Reads_SR, TD, TDOutf, false);
   //std::cout << "TD 2" << std::endl;
   //std::cout << "std3" << std::endl;
   for (unsigned int i = 0; i < NumBoxes; i++) {
      TD[i].clear();
   }
   // std::cout << "std4" << std::endl;
   return EXIT_SUCCESS;
}