ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/tophat_cpp/reads.cpp
(Generate patch)
# Line 173 | Line 173
173  
174   bool next_fastx_read(FLineReader& fr, Read& read, ReadFormat reads_format,
175                          FLineReader* frq) {
176 +  if (fr.pushed_read)
177 +    {
178 +      read = fr.last_read;
179 +      fr.pushed_read = false;
180 +      return true;
181 +    }
182 +  
183    read.clear();
184    char* buf=NULL;
185    while ((buf=fr.nextLine())!=NULL) {
# Line 254 | Line 261
261             return false;
262             }
263      }
264 +
265 +  fr.last_read = read;  
266    return !(read.seq.empty());
267   }
268  
# Line 609 | Line 618
618  
619   // reads must ALWAYS requested in increasing order of their ID
620   bool ReadStream::getRead(uint64_t r_id,
621 <            Read& read,
622 <            ReadFormat read_format,
623 <            bool strip_slash,
624 <            FILE* um_out, //unmapped reads output
625 <            bool um_write_found) {
621 >                         Read& read,
622 >                         ReadFormat read_format,
623 >                         bool strip_slash,
624 >                         FILE* um_out, //unmapped reads output
625 >                         bool um_write_found,
626 >                         uint64_t begin_id,
627 >                         uint64_t end_id) {
628    if (!fstream.file)
629         err_die("Error: calling ReadStream::getRead() with no file handle!");
630    if (r_id<last_id)
# Line 625 | Line 636
636        // Get the next read from the file
637      if (!next_read(read, read_format))
638          break;
639 +
640      if (strip_slash) {
641         string::size_type slash = read.name.rfind("/");
642         if (slash != string::npos)
643            read.name.resize(slash);
644         }
645 <    if ((uint64_t)atoi(read.name.c_str()) == r_id) {
646 <       found=true;
647 <       }
636 <    if (um_out && (um_write_found || !found)) {
637 <     //write unmapped reads
638 <      fprintf(um_out, "@%s\n%s\n+\n%s\n", read.alt_name.c_str(),
639 <                              read.seq.c_str(), read.qual.c_str());
640 <      }
641 <    //rt.get_id(read.name, ref_str);
642 <    } //while reads
643 <  return found;
644 < }
645 >    uint64_t id = (uint64_t)atol(read.name.c_str());
646 >    if (id >= end_id)
647 >      return false;
648  
649 +    if (id < begin_id)
650 +      continue;
651  
652 < bool get_read_from_stream(uint64_t insert_id,
648 <        FILE* reads_file,
649 <        ReadFormat reads_format,
650 <        bool strip_slash,
651 <        Read& read,
652 <        FILE* um_out,
653 <        bool um_write_found
654 <        ) {
655 <  FLineReader fr(reads_file);
656 <  bool found=false;
657 <  while(!found && !fr.isEof())
658 <    {
659 <    read.clear();
660 <      // Get the next read from the file
661 <    if (!next_fastx_read(fr, read, reads_format))
662 <        break;
663 <    if (strip_slash)
652 >    if (id == r_id)
653        {
654 <        string::size_type slash = read.name.rfind("/");
666 <        if (slash != string::npos)
667 <          read.name.resize(slash);
654 >        found=true;
655        }
656 <    if ((uint64_t)atoi(read.name.c_str()) == insert_id)
656 >    else if (id > r_id)
657        {
658 <      //return true;
659 <      found=true;
658 >        read_pq.push(make_pair(id, read));
659 >        break;
660        }
661 +
662      if (um_out && (um_write_found || !found)) {
663       //write unmapped reads
664        fprintf(um_out, "@%s\n%s\n+\n%s\n", read.alt_name.c_str(),
665                                read.seq.c_str(), read.qual.c_str());
666        }
679    //rt.get_id(read.name, ref_str);
667      } //while reads
668    return found;
669   }
670  
671  
672   bool get_read_from_stream(uint64_t insert_id,
673 <                          FILE* reads_file,
673 >                          FLineReader& fr,
674                            ReadFormat reads_format,
675                            bool strip_slash,
676 <                          char read_name [],
677 <                          char read_seq  [],
678 <                          char read_alt_name [],
692 <                          char read_qual [],
693 <                          FILE* um_out)
676 >                          Read& read,
677 >                          FILE* um_out,
678 >                          bool um_write_found)
679   {
680 <  Read read;
681 <  FLineReader fr(reads_file);
697 <  while(!fr.isEof())
680 >  bool found=false;
681 >  while(!found && !fr.isEof())
682      {
683      read.clear();
684  
685        // Get the next read from the file
686      if (!next_fastx_read(fr, read, reads_format))
687          break;
704
688      if (strip_slash)
689        {
690          string::size_type slash = read.name.rfind("/");
691          if (slash != string::npos)
692            read.name.resize(slash);
693        }
694 <
695 <    if ((uint64_t)atoi(read.name.c_str()) == insert_id)
694 >    uint64_t read_id = (uint64_t)atoi(read.name.c_str());
695 >    if (read_id == insert_id)
696 >      {
697 >        found=true;
698 >      }
699 >    else if (read_id > insert_id)
700        {
701 <        if (read_name) strcpy(read_name, read.name.c_str());
702 <        if (read_seq) strcpy(read_seq, read.seq.c_str());
716 <        if (read_alt_name) strcpy(read_alt_name, read.alt_name.c_str());
717 <        if (read_qual) strcpy(read_qual, read.qual.c_str());
718 <        return true;
701 >        fr.pushBack_read();
702 >        break;
703        }
704 <    else if (um_out!=NULL) {
704 >
705 >    if (um_out && (um_write_found || !found)) {
706       //write unmapped reads
707 <      fprintf(um_out, "@%s\n%s\n+\n%s\n", read.alt_name.c_str(), read.seq.c_str(),
708 <                          read.qual.c_str());
707 >      fprintf(um_out, "@%s\n%s\n+\n%s\n", read.alt_name.c_str(),
708 >                              read.seq.c_str(), read.qual.c_str());
709        }
710 <      
726 <        //rt.get_id(read.name, ref_str);
710 >    //rt.get_id(read.name, ref_str);
711      } //while reads
712 <  
729 <  return false;
712 >  return found;
713   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines