11 |
13 Sep 07 |
nicklas |
1 |
///////////////////////////////////////////////////////////////// |
11 |
13 Sep 07 |
nicklas |
2 |
// |
11 |
13 Sep 07 |
nicklas |
// Copyright (C) 2005 Affymetrix, Inc. |
11 |
13 Sep 07 |
nicklas |
4 |
// |
11 |
13 Sep 07 |
nicklas |
// This library is free software; you can redistribute it and/or modify |
11 |
13 Sep 07 |
nicklas |
// it under the terms of the GNU Lesser General Public License as published |
11 |
13 Sep 07 |
nicklas |
// by the Free Software Foundation; either version 2.1 of the License, |
11 |
13 Sep 07 |
nicklas |
// or (at your option) any later version. |
11 |
13 Sep 07 |
nicklas |
9 |
// |
11 |
13 Sep 07 |
nicklas |
// This library is distributed in the hope that it will be useful, but |
11 |
13 Sep 07 |
nicklas |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 |
13 Sep 07 |
nicklas |
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
11 |
13 Sep 07 |
nicklas |
// for more details. |
11 |
13 Sep 07 |
nicklas |
14 |
// |
11 |
13 Sep 07 |
nicklas |
// You should have received a copy of the GNU Lesser General Public License |
11 |
13 Sep 07 |
nicklas |
// along with this library; if not, write to the Free Software Foundation, Inc., |
11 |
13 Sep 07 |
nicklas |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
11 |
13 Sep 07 |
nicklas |
18 |
// |
11 |
13 Sep 07 |
nicklas |
19 |
///////////////////////////////////////////////////////////////// |
11 |
13 Sep 07 |
nicklas |
20 |
|
11 |
13 Sep 07 |
nicklas |
21 |
package affymetrix.calvin.parsers; |
11 |
13 Sep 07 |
nicklas |
22 |
|
11 |
13 Sep 07 |
nicklas |
23 |
import java.io.*; |
11 |
13 Sep 07 |
nicklas |
24 |
import affymetrix.calvin.data.*; |
11 |
13 Sep 07 |
nicklas |
25 |
|
11 |
13 Sep 07 |
nicklas |
/** This class reads the FileHeader of the generic data file. */ |
11 |
13 Sep 07 |
nicklas |
27 |
public class FileHeaderReader { |
11 |
13 Sep 07 |
nicklas |
28 |
|
11 |
13 Sep 07 |
nicklas |
/** The expected magic number */ |
15 |
17 Sep 07 |
nicklas |
30 |
public static final int DATA_FILE_MAGIC_NUMBER = 59; |
11 |
13 Sep 07 |
nicklas |
31 |
|
11 |
13 Sep 07 |
nicklas |
/** The expected version number */ |
11 |
13 Sep 07 |
nicklas |
33 |
private static final int DATA_FILE_VERSION_NUMBER = 1; |
11 |
13 Sep 07 |
nicklas |
34 |
|
11 |
13 Sep 07 |
nicklas |
/** Constructor |
11 |
13 Sep 07 |
nicklas |
* @param fs Open fstream positioned at the start of the file. |
11 |
13 Sep 07 |
nicklas |
* @param fh FileHeader object to fill. |
11 |
13 Sep 07 |
nicklas |
38 |
*/ |
15 |
17 Sep 07 |
nicklas |
39 |
public FileHeaderReader(InputStream fs, FileHeader fh) { |
11 |
13 Sep 07 |
nicklas |
40 |
fileStream = fs; |
11 |
13 Sep 07 |
nicklas |
41 |
header = fh; |
11 |
13 Sep 07 |
nicklas |
42 |
dataGroupCnt = 0; |
11 |
13 Sep 07 |
nicklas |
43 |
firstDataGroupFilePos = 0; |
11 |
13 Sep 07 |
nicklas |
44 |
} |
11 |
13 Sep 07 |
nicklas |
45 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the FileHeader. |
11 |
13 Sep 07 |
nicklas |
* @exception affymetrix_calvin_exceptions::InvalidVersionException The file version does not match. |
11 |
13 Sep 07 |
nicklas |
* @exception affymetrix_calvin_exceptions::InvalidFileTypeException The file is not of the right type. |
11 |
13 Sep 07 |
nicklas |
49 |
*/ |
11 |
13 Sep 07 |
nicklas |
50 |
public void read() throws InvalidVersionException, InvalidFileTypeException { |
11 |
13 Sep 07 |
nicklas |
51 |
readMagicNumber(); |
11 |
13 Sep 07 |
nicklas |
52 |
readVersion(); |
11 |
13 Sep 07 |
nicklas |
53 |
readDataGroupCnt(); |
11 |
13 Sep 07 |
nicklas |
54 |
readFirstDataGroupFilePos(); |
11 |
13 Sep 07 |
nicklas |
55 |
readGenericDataHdr(); |
11 |
13 Sep 07 |
nicklas |
56 |
} |
11 |
13 Sep 07 |
nicklas |
57 |
|
11 |
13 Sep 07 |
nicklas |
/** Gets the number of DataGroups in the file. |
11 |
13 Sep 07 |
nicklas |
* @return Number of DataGroups. |
11 |
13 Sep 07 |
nicklas |
60 |
*/ |
11 |
13 Sep 07 |
nicklas |
61 |
public int getDataGroupCnt() { return dataGroupCnt; } |
11 |
13 Sep 07 |
nicklas |
62 |
|
11 |
13 Sep 07 |
nicklas |
/** Gets the file position of the first DataGroup header. */ |
11 |
13 Sep 07 |
nicklas |
64 |
public int getFirstDataGroupFilePos() { return firstDataGroupFilePos; } |
11 |
13 Sep 07 |
nicklas |
65 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the magic number from the file. |
11 |
13 Sep 07 |
nicklas |
* @exception affymetrix_calvin_exceptions::InvalidFileTypeException The file is not of the right type. |
11 |
13 Sep 07 |
nicklas |
68 |
*/ |
11 |
13 Sep 07 |
nicklas |
69 |
protected void readMagicNumber() throws InvalidFileTypeException { |
11 |
13 Sep 07 |
nicklas |
// Read magic number |
11 |
13 Sep 07 |
nicklas |
71 |
byte fileMagicNumber = FileInput.ReadInt8(fileStream); |
11 |
13 Sep 07 |
nicklas |
72 |
if (fileMagicNumber != DATA_FILE_MAGIC_NUMBER) |
11 |
13 Sep 07 |
nicklas |
73 |
{ |
11 |
13 Sep 07 |
nicklas |
74 |
throw new InvalidFileTypeException(); |
11 |
13 Sep 07 |
nicklas |
75 |
} |
11 |
13 Sep 07 |
nicklas |
76 |
} |
11 |
13 Sep 07 |
nicklas |
77 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the generic file version number from the file. |
11 |
13 Sep 07 |
nicklas |
* @exception affymetrix_calvin_exceptions::InvalidVersionException The file version does not match. |
11 |
13 Sep 07 |
nicklas |
80 |
*/ |
11 |
13 Sep 07 |
nicklas |
81 |
protected void readVersion() throws InvalidVersionException { |
11 |
13 Sep 07 |
nicklas |
// Read generic data file version |
11 |
13 Sep 07 |
nicklas |
83 |
byte fileVersionNumber = FileInput.ReadInt8(fileStream); |
11 |
13 Sep 07 |
nicklas |
84 |
if (fileVersionNumber != DATA_FILE_VERSION_NUMBER) |
11 |
13 Sep 07 |
nicklas |
85 |
{ |
11 |
13 Sep 07 |
nicklas |
86 |
throw new InvalidVersionException(); |
11 |
13 Sep 07 |
nicklas |
87 |
} |
11 |
13 Sep 07 |
nicklas |
88 |
} |
11 |
13 Sep 07 |
nicklas |
89 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the DataGroup count from the file. */ |
11 |
13 Sep 07 |
nicklas |
91 |
protected void readDataGroupCnt() { |
11 |
13 Sep 07 |
nicklas |
92 |
dataGroupCnt = FileInput.ReadUInt32(fileStream); |
11 |
13 Sep 07 |
nicklas |
93 |
header.setNumDataGroups(dataGroupCnt); |
11 |
13 Sep 07 |
nicklas |
94 |
} |
11 |
13 Sep 07 |
nicklas |
95 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the file position of the first DataGroup. */ |
11 |
13 Sep 07 |
nicklas |
97 |
protected void readFirstDataGroupFilePos() { |
11 |
13 Sep 07 |
nicklas |
98 |
firstDataGroupFilePos = FileInput.ReadUInt32(fileStream); |
11 |
13 Sep 07 |
nicklas |
99 |
header.setFirstDataGroupFilePos(firstDataGroupFilePos); |
11 |
13 Sep 07 |
nicklas |
100 |
} |
11 |
13 Sep 07 |
nicklas |
101 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the GenericDataHeader from the file. */ |
11 |
13 Sep 07 |
nicklas |
103 |
protected void readGenericDataHdr() { |
11 |
13 Sep 07 |
nicklas |
// Read all the GenericDataHeader |
11 |
13 Sep 07 |
nicklas |
105 |
GenericDataHeader gdh = new GenericDataHeader(); |
11 |
13 Sep 07 |
nicklas |
106 |
GenericDataHeaderReader gdhReader = new GenericDataHeaderReader(fileStream); |
11 |
13 Sep 07 |
nicklas |
107 |
gdhReader.read(gdh); |
11 |
13 Sep 07 |
nicklas |
108 |
|
11 |
13 Sep 07 |
nicklas |
// Set the GenericDataHeader in the FileHeader |
11 |
13 Sep 07 |
nicklas |
110 |
header.setGenericDataHdr(gdh); |
11 |
13 Sep 07 |
nicklas |
111 |
} |
11 |
13 Sep 07 |
nicklas |
112 |
|
11 |
13 Sep 07 |
nicklas |
/** A reference to the file stream. */ |
15 |
17 Sep 07 |
nicklas |
114 |
protected InputStream fileStream; |
11 |
13 Sep 07 |
nicklas |
115 |
|
11 |
13 Sep 07 |
nicklas |
/** FileHeader object to fill. */ |
11 |
13 Sep 07 |
nicklas |
117 |
protected FileHeader header; |
11 |
13 Sep 07 |
nicklas |
118 |
|
11 |
13 Sep 07 |
nicklas |
/** Number of DataGroups. */ |
11 |
13 Sep 07 |
nicklas |
120 |
int dataGroupCnt; |
11 |
13 Sep 07 |
nicklas |
121 |
|
11 |
13 Sep 07 |
nicklas |
/** Position of the first DataGroup. */ |
11 |
13 Sep 07 |
nicklas |
123 |
int firstDataGroupFilePos; |
11 |
13 Sep 07 |
nicklas |
124 |
|
11 |
13 Sep 07 |
nicklas |
125 |
} |