libStatGen Software 1
Loading...
Searching...
No Matches
GlfRefSection.cpp
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "GlfRefSection.h"
19#include "GlfException.h"
20#include "StringBasics.h"
21
22GlfRefSection::GlfRefSection()
23 : myRefName()
24{
25 resetRefSection();
26}
27
28
29GlfRefSection::~GlfRefSection()
30{
32}
33
34
35// Copy Constructor
36GlfRefSection::GlfRefSection(const GlfRefSection& refSection)
37 : myRefName()
38{
39 copy(refSection);
40}
41
42
43// Overload operator = to copy the passed in refSection into this refSection.
45{
46 copy(refSection);
47 return(*this);
48}
49
50
51bool GlfRefSection::copy(const GlfRefSection& refSection)
52{
53 // Check to see if the passed in value is the same as this.
54 if(this == &refSection)
55 {
56 return(true);
57 }
58
60
61 // Copy the refSection.
62 myRefName = refSection.myRefName;
63 myRefLen = refSection.myRefLen;
64
65 return(true);
66}
67
68
69// Reset the refSection for a new entry, clearing out previous values.
71{
72 myRefName.reset();
73 myRefLen = 0;
74}
75
76
77// Read the refSection from the specified file. Assumes the file is in
78// the correct position for reading the refSection.
80{
81 // Read the reference sequence name length
82 int numRead = 0;
83 int32_t refNameLen = 0;
84 int byteLen = sizeof(int32_t);
85 numRead = ifread(filePtr, &refNameLen, byteLen);
86 if(numRead != byteLen)
87 {
88 // If no bytes were read and it is the end of the file, then return
89 // false, but do not throw an exception. This is not an error, just
90 // the end of the file.
91 if((numRead == 0) && ifeof(filePtr))
92 {
93 return(false);
94 }
95
96 String errorMsg =
97 "Failed to read the length of the reference sequence name (";
98 errorMsg += byteLen;
99 errorMsg += " bytes). Only read ";
100 errorMsg += numRead;
101 errorMsg += " bytes.";
102 std::string errorString = errorMsg.c_str();
103 throw(GlfException(GlfStatus::FAIL_IO, errorString));
104 return(false);
105 }
106
107 // Read the refSection from the file.
108 numRead = myRefName.readFromFile(filePtr, refNameLen);
109 if(numRead != refNameLen)
110 {
111 String errorMsg = "Failed to read the reference sequence name (";
112 errorMsg += refNameLen;
113 errorMsg += " bytes). Only read ";
114 errorMsg += numRead;
115 errorMsg += " bytes.";
116 std::string errorString = errorMsg.c_str();
117 throw(GlfException(GlfStatus::FAIL_IO, errorString));
118 return(false);
119 }
120
121 // Read the ref length.
122 byteLen = sizeof(uint32_t);
123 numRead = ifread(filePtr, &myRefLen, byteLen);
124 if(numRead != byteLen)
125 {
126 String errorMsg = "Failed to read the reference sequence length (";
127 errorMsg += byteLen;
128 errorMsg += " bytes). Only read ";
129 errorMsg += numRead;
130 errorMsg += " bytes.";
131 std::string errorString = errorMsg.c_str();
132 throw(GlfException(GlfStatus::FAIL_IO, errorString));
133 return(false);
134 }
135
136 // Successfully read, return success.
137 return(true);
138}
139
140
141// Write the refSection to the specified file.
142bool GlfRefSection::write(IFILE filePtr) const
143{
144 int refNameLen = myRefName.length();
145 int byteLen = sizeof(int32_t);
146 int numWrite = ifwrite(filePtr, &refNameLen, byteLen);
147 if(numWrite != byteLen)
148 {
149 String errorMsg =
150 "Failed to write the length of the reference sequence name (";
151 errorMsg += byteLen;
152 errorMsg += " bytes). Only wrote ";
153 errorMsg += numWrite;
154 errorMsg += " bytes.";
155 std::string errorString = errorMsg.c_str();
156 throw(GlfException(GlfStatus::FAIL_IO, errorString));
157 return(false);
158 }
159
160 numWrite = ifwrite(filePtr, myRefName.c_str(), refNameLen);
161 if(numWrite != refNameLen)
162 {
163 String errorMsg = "Failed to write the reference sequence name (";
164 errorMsg += refNameLen;
165 errorMsg += " bytes). Only wrote ";
166 errorMsg += numWrite;
167 errorMsg += " bytes.";
168 std::string errorString = errorMsg.c_str();
169 throw(GlfException(GlfStatus::FAIL_IO, errorString));
170 return(false);
171 }
172
173 // Write the length of the reference sequence
174 byteLen = sizeof(uint32_t);
175 numWrite = ifwrite(filePtr, &myRefLen, byteLen);
176 if(numWrite != byteLen)
177 {
178 String errorMsg = "Failed to write the reference sequence length (";
179 errorMsg += byteLen;
180 errorMsg += " bytes). Only wrote ";
181 errorMsg += numWrite;
182 errorMsg += " bytes.";
183 std::string errorString = errorMsg.c_str();
184 throw(GlfException(GlfStatus::FAIL_IO, errorString));
185 return(false);
186 }
187
188 // Successfully wrote, return success.
189 return(true);
190}
191
192
193bool GlfRefSection::getName(std::string& name) const
194{
195 name = myRefName.c_str();
196 return(true);
197}
198
199
201{
202 return(myRefLen);
203}
204
205
206bool GlfRefSection::setName(const std::string& name)
207{
208 myRefName = name;
209 return(true);
210}
211
212
213bool GlfRefSection::setRefLen(uint32_t refLen)
214{
215 myRefLen = refLen;
216 return(true);
217}
218
219
221{
222 std::cout << "l_name: " << myRefName.length()
223 << "; name: " << myRefName.c_str()
224 << "; ref_len: " << myRefLen
225 << "\n";
226}
227
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition InputFile.h:654
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition InputFile.h:600
unsigned int ifwrite(IFILE file, const void *buffer, unsigned int size)
Write the specified number of bytes from the specified buffer into the file.
Definition InputFile.h:669
GlfException objects should be thrown by functions that operate on Glf files for exceptions.
This class allows a user to easily get/set the fields in a GLF section/chromosome header.
void resetRefSection()
Clear this reference section back to the default setting.
bool setName(const std::string &name)
Set the reference name.
bool copy(const GlfRefSection &refSection)
Copy the passed in refSection into this refSection.
bool read(IFILE filePtr)
Read the refSection from the specified file (file MUST be in the correct position for reading a refSe...
GlfRefSection & operator=(const GlfRefSection &refSection)
Overload operator= to copy the passed in refSection into this one.
void print() const
Print the reference section in a readable format.
bool write(IFILE filePtr) const
Write the refSection to the specified file.
bool setRefLen(uint32_t refLen)
Set the length of the reference sequence.
uint32_t getRefLen() const
Get the length of the reference sequence.
bool getName(std::string &name) const
Get the reference name.
@ FAIL_IO
method failed due to an I/O issue.
Definition GlfStatus.h:34
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition InputFile.h:37