1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/* $Id: faread.cpp,v 1.6 2004/04/06 04:53:17 rotmistr Exp $
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* =========================================================================
*
* Author: Kirill Rotmistrovsky
*
* ========================================================================= */
#include <epcr/faread.hpp>
#include <epcr/bin-io.hpp>
#include <stdexcept>
#include <string>
#include <cstdio>
#include <ctype.h>
#include <errno.h>
USING_NCBI_SCOPE;
USING_SCOPE(EPCR_SCOPE);
struct SFile
{
bool m_pipe;
FILE * f;
SFile(bool pipe, const char * name) {
#ifdef NO_POPEN
if(pipe)
throw runtime_error("Pipes are not supported in windows!");
f=fopen(name,"r");
#else
f=pipe?popen(name,"r"):fopen64(name,"r");
#endif
if(f==0) throw runtime_error(string(name)+": "+strerror(errno));
setvbuf(f,0,_IOFBF,16192);
m_pipe=pipe;
}
~SFile() {
#ifndef NO_POPEN
if(m_pipe) pclose(f); else
#endif
fclose(f);
}
};
void CFastaReader::Open(const string& fname)
{
if(IsOpen()) Close();
// FILE * f=fopen64(fname.c_str(),"r");
// if(f==0) throw runtime_error(fname+": "+strerror(errno));
// setvbuf(f,0,_IOFBF,16192);
m_Fptr=new SFile(false,fname.c_str());
}
void CFastaReader::PipeIn(const string& command)
{
if(IsOpen()) Close();
m_Fptr=new SFile(true,command.c_str());
}
void CFastaReader::Close()
{
if(m_Fptr) {
// fclose((FILE*)m_Fptr);
delete (SFile*)m_Fptr;
m_Fptr=0;
}
}
void CFastaReader::ReadFile(IFastaReaderCallback * cbk)
{
if(!cbk) return;
if(!IsOpen()) return;
FILE * f=((SFile*)m_Fptr)->f;
char buffer[16192];
bool is_defline=false;
bool is_newline=true;
bool beginning_of_file=true;
cbk->CbkFileBegin();
string defline;
while(!feof(f)) {
if(!fgets(buffer,sizeof(buffer),f)) break;
char * eol=strlen(buffer)+buffer;
bool complete_line=eol>buffer && (eol[-1]=='\r' || eol[-1]=='\n');
while(eol>buffer && isspace(eol[-1])) --eol;
*eol=0;
if(!is_newline) {
if(is_defline) { defline.append(buffer); }
else SeqlineCbk(cbk,buffer,eol-buffer);
}
else {
if(*buffer=='>') {
if(!beginning_of_file)
cbk->CbkEntryEnd();
cbk->CbkEntryBegin();
beginning_of_file=false;
is_defline=true;
defline.assign(buffer);
}
else {
if(beginning_of_file)
throw runtime_error("Leading garbage in fasta file!");
if(defline.length()) { // should have at least '>'
cbk->CbkDefline(defline.c_str(),defline.length());
const char * s=defline.c_str()+1;
for(;*s && isspace(*s);++s);
const char * ss=s;
for(;*s && !isspace(*s);++s);
cbk->CbkIdent(ss,s-ss);
defline.clear();
}
is_defline=false;
SeqlineCbk(cbk,buffer,eol-buffer);
}
}
is_newline=complete_line;
}
if(!beginning_of_file) cbk->CbkEntryEnd();
cbk->CbkFileEnd();
}
void CFastaReader::SeqlineCbk(IFastaReaderCallback* cbk,
char * buffer, unsigned length)
{
char * eol=buffer+length;
if(m_CvtTable) {
for(char * x=buffer; x<eol; ++x) {
char c=m_CvtTable[*x];
if(c==0) {
char * xx=x+1;
while(xx<eol && m_CvtTable[*xx]==0) ++xx;
memmove(x,xx,eol-xx);
eol -= xx-x;
if(x>=eol) break;
c=m_CvtTable[*x];
}
*x=c;
}
}
cbk->CbkSeqline(buffer,eol-buffer);
}
char CFastaReader::sm_NucleotidesExt[]=
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0ABCDNNGHNNKNMNNNNRNTTNWNYN\0\0\0\0\0"
//.abcdefghijklmnopqrstuvwxyz.....
"\0abcdnnghnnknmnnnnrnttnwnyn\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
;
char CFastaReader::sm_NucleotidesExtUc[]=
"\0\0\0\0\0\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
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
;
char CFastaReader::sm_NucleotidesUc[]=
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0ANCNNNGNNNNNNNNNNNNTNNNNNN\0\0\0\0\0"
//.abcdefghijklmnopqrstuvwxyz.....
"\0ANCNNNGNNNNNNNNNNNNTNNNNNN\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
;
/*
* $Log: faread.cpp,v $
* Revision 1.6 2004/04/06 04:53:17 rotmistr
* All is compileable with BCC5.5 and runnable on WIndows
*
* Revision 1.5 2004/04/01 05:57:52 rotmistr
* Compilable with borland C++
*
* Revision 1.4 2004/03/23 22:35:25 rotmistr
* Fixed processing of -mid flag in cmdline
* Fixed destructor for fasta reader
* Removed cgi
*
* Revision 1.3 2004/03/07 06:35:59 rotmistr
* Many bugfixes and optimisations -- cgi is to go to production
*
* Revision 1.2 2004/01/08 23:22:41 rotmistr
* Fixed init error in faread,
* Adjusted output to standard,
* Added output format style and output file to parameters.
*
* Revision 1.1.1.1 2003/12/23 18:17:28 rotmistr
* Package that includes e-PCR, reverse e-PCR, and sequence data preparation
* program for reverse e-PCR looks ready
*
*/
|