ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/PrimerMatch/fasta_io.cc
Revision: 1.2
Committed: Mon Nov 2 03:26:49 2009 UTC (6 years, 9 months ago) by nje01
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -0 lines
Log Message:
#include <cstdio> fix for EOF from Ben Boese

Line File contents
1 /**************************************************************************
2 * This code is part of the supporting infrastructure for ATA Mapper.
3 * Copyright (C) 2002,2003,2004 Applera Corporation. All rights reserved.
4 * Author: Nathan Edwards
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received (LICENSE.txt) a copy of the GNU General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *************************************************************************/
20
21
22 #include "fasta_io.h"
23 #include <ctype.h>
24 #include <iostream>
25 #include <assert.h>
26 #include <cstdio>
27
28 #if !defined(NO_STD_NAMESPACE)
29 using namespace std;
30 #endif
31
32 void fasta_entry::read(istream & is) {
33 // 8K buffer sufficient for a single line? Hope so.
34 static unsigned long BUFLEN = 1024*8;
35 static char *buffer = new char[BUFLEN];
36 int peek;
37
38 peek=is.peek();
39 while (peek!=EOF && (peek=='#' || peek=='\n')) {
40 // Read and discard line from the stream...
41 is.getline(buffer,BUFLEN-1,'\n');
42 // assert(is.gcount() < BUFLEN-1);
43 peek=is.peek();
44 }
45
46 if (peek == EOF) {
47 peek=is.get();
48 defline_ = "";
49 sequence_ = "";
50 return;
51 }
52
53 // assert(peek == '>');
54 is.getline(buffer,BUFLEN-1,'\n');
55 // assert(is.gcount() < BUFLEN-1);
56 // Ensure the string is null terminated
57 buffer[is.gcount()] = '\0';
58 defline_ = (buffer+1);
59
60 // Read the sequence
61 sequence_="";
62 peek=is.peek();
63 while (peek!=EOF && peek!='>' && peek!='#' && peek!='\n') {
64 is.getline(buffer,BUFLEN-1,'\n');
65 // assert(is.gcount() < BUFLEN-1);
66 buffer[is.gcount()] = '\0';
67 sequence_ += buffer;
68 peek=is.peek();
69 }
70
71 while (peek!=EOF && (peek=='#' || peek=='\n')) {
72 // Read and discard line from the stream...
73 is.getline(buffer,BUFLEN-1,'\n');
74 // assert(is.gcount() < BUFLEN-1);
75 peek=is.peek();
76 }
77 }
78
79 void fasta_entry::write(ostream & os) const {
80 static int LINELEN=60;
81 os << '>' << defline_ << endl;
82 for (int i=0;i<sequence_.length();i+=LINELEN) {
83 os << sequence_.substr(i,LINELEN) << endl;
84 }
85 }
86
87 void fasta_entry::uppercase() {
88 for (int i=0;i<sequence_.length();i++) {
89 if (sequence_[i] >= 'a' && sequence_[i] <= 'z') {
90 sequence_[i] = toupper(sequence_[i]);
91 }
92 }
93 }
94
95 void fasta_entry::lowercase() {
96 for (int i=0;i<sequence_.length();i++) {
97 if (sequence_[i] >= 'A' && sequence_[i] <= 'Z') {
98 sequence_[i] = tolower(sequence_[i]);
99 }
100 }
101 }
102
103 ostream & operator<<(ostream & os, fasta_entry const & fe) {
104 fe.write(os);
105 return os;
106 }
107 istream & operator>>(istream & is, fasta_entry & fe) {
108 fe.read(is);
109 return is;
110 }