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.fusion.chp; |
11 |
13 Sep 07 |
nicklas |
22 |
|
11 |
13 Sep 07 |
nicklas |
23 |
import affymetrix.calvin.utils.*; |
11 |
13 Sep 07 |
nicklas |
24 |
import affymetrix.calvin.parsers.*; |
11 |
13 Sep 07 |
nicklas |
25 |
import affymetrix.calvin.data.*; |
11 |
13 Sep 07 |
nicklas |
26 |
import java.util.*; |
11 |
13 Sep 07 |
nicklas |
27 |
import java.io.*; |
11 |
13 Sep 07 |
nicklas |
28 |
|
11 |
13 Sep 07 |
nicklas |
/** A class used to self register CHP data classes. */ |
11 |
13 Sep 07 |
nicklas |
30 |
public abstract class FusionCHPDataReg { |
11 |
13 Sep 07 |
nicklas |
31 |
|
11 |
13 Sep 07 |
nicklas |
/** Constructor */ |
11 |
13 Sep 07 |
nicklas |
33 |
public FusionCHPDataReg() { |
11 |
13 Sep 07 |
nicklas |
34 |
next = head; |
11 |
13 Sep 07 |
nicklas |
35 |
head = this; |
11 |
13 Sep 07 |
nicklas |
36 |
} |
11 |
13 Sep 07 |
nicklas |
37 |
|
11 |
13 Sep 07 |
nicklas |
/* Sets the file type ids. |
11 |
13 Sep 07 |
nicklas |
* @param fileTypeIds The identifiers that the CHP object is compatible with. |
11 |
13 Sep 07 |
nicklas |
40 |
*/ |
11 |
13 Sep 07 |
nicklas |
41 |
public void setFileTypeIds(Vector /*AffymetrixGuidType*/ fileTypeIds) { fileTypeIdentifiers = fileTypeIds; } |
11 |
13 Sep 07 |
nicklas |
42 |
|
11 |
13 Sep 07 |
nicklas |
43 |
/* |
11 |
13 Sep 07 |
nicklas |
* Read the guid from the file. |
11 |
13 Sep 07 |
nicklas |
45 |
*/ |
11 |
13 Sep 07 |
nicklas |
46 |
private static AffymetrixGuidType readGuidFromFile(String fileName) |
11 |
13 Sep 07 |
nicklas |
47 |
{ |
11 |
13 Sep 07 |
nicklas |
48 |
if (new File(fileName).exists() == false) |
11 |
13 Sep 07 |
nicklas |
49 |
return null; |
11 |
13 Sep 07 |
nicklas |
50 |
|
11 |
13 Sep 07 |
nicklas |
51 |
GenericData data = new GenericData(); |
11 |
13 Sep 07 |
nicklas |
52 |
GenericFileReader reader = new GenericFileReader(); |
11 |
13 Sep 07 |
nicklas |
53 |
try |
11 |
13 Sep 07 |
nicklas |
54 |
{ |
11 |
13 Sep 07 |
nicklas |
55 |
reader.setFilename(fileName); |
11 |
13 Sep 07 |
nicklas |
56 |
reader.readHeader(data, ReadHeaderOption.ReadNoDataGroupHeader); |
11 |
13 Sep 07 |
nicklas |
57 |
AffymetrixGuidType guid = new AffymetrixGuidType(); |
11 |
13 Sep 07 |
nicklas |
58 |
guid.setGuid(data.getHeader().getGenericDataHdr().getFileTypeId()); |
11 |
13 Sep 07 |
nicklas |
59 |
return guid; |
11 |
13 Sep 07 |
nicklas |
60 |
} |
11 |
13 Sep 07 |
nicklas |
61 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
62 |
{ |
11 |
13 Sep 07 |
nicklas |
63 |
AffymetrixGuidType guid = new AffymetrixGuidType(); |
11 |
13 Sep 07 |
nicklas |
64 |
guid.setGuid(""); |
11 |
13 Sep 07 |
nicklas |
65 |
return guid; |
11 |
13 Sep 07 |
nicklas |
66 |
} |
11 |
13 Sep 07 |
nicklas |
67 |
} |
11 |
13 Sep 07 |
nicklas |
68 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the contents of a CHP file. |
11 |
13 Sep 07 |
nicklas |
* @param fileName The full path to the input CHP file. |
11 |
13 Sep 07 |
nicklas |
* @return A pointer to the CHP data object. NULL if the read failed. |
11 |
13 Sep 07 |
nicklas |
72 |
*/ |
11 |
13 Sep 07 |
nicklas |
73 |
public static FusionCHPData read(String fileName) { |
11 |
13 Sep 07 |
nicklas |
74 |
AffymetrixGuidType id = readGuidFromFile(fileName); |
11 |
13 Sep 07 |
nicklas |
75 |
if (id == null) |
11 |
13 Sep 07 |
nicklas |
76 |
return null; |
11 |
13 Sep 07 |
nicklas |
77 |
FusionCHPData chp = createObject(id); |
11 |
13 Sep 07 |
nicklas |
78 |
if (chp != null) |
11 |
13 Sep 07 |
nicklas |
79 |
{ |
11 |
13 Sep 07 |
nicklas |
80 |
chp.setFileName(fileName); |
11 |
13 Sep 07 |
nicklas |
81 |
if (chp.read() == false) |
11 |
13 Sep 07 |
nicklas |
82 |
{ |
11 |
13 Sep 07 |
nicklas |
83 |
chp = null; |
11 |
13 Sep 07 |
nicklas |
84 |
} |
11 |
13 Sep 07 |
nicklas |
85 |
} |
11 |
13 Sep 07 |
nicklas |
86 |
return chp; |
11 |
13 Sep 07 |
nicklas |
87 |
} |
11 |
13 Sep 07 |
nicklas |
88 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads the header of a CHP file. |
11 |
13 Sep 07 |
nicklas |
* @param fileName The full path to the input CHP file. |
11 |
13 Sep 07 |
nicklas |
* @return A pointer to the CHP data object. NULL if the read failed. |
11 |
13 Sep 07 |
nicklas |
92 |
*/ |
11 |
13 Sep 07 |
nicklas |
93 |
public static FusionCHPData readHeader(String fileName) { |
11 |
13 Sep 07 |
nicklas |
94 |
AffymetrixGuidType id = readGuidFromFile(fileName); |
11 |
13 Sep 07 |
nicklas |
95 |
if (id == null) |
11 |
13 Sep 07 |
nicklas |
96 |
return null; |
11 |
13 Sep 07 |
nicklas |
97 |
|
11 |
13 Sep 07 |
nicklas |
98 |
FusionCHPData chp = FusionCHPDataReg.createObject(id); |
11 |
13 Sep 07 |
nicklas |
99 |
if (chp != null) |
11 |
13 Sep 07 |
nicklas |
100 |
{ |
11 |
13 Sep 07 |
nicklas |
101 |
chp.setFileName(fileName); |
11 |
13 Sep 07 |
nicklas |
102 |
if (chp.readHeader() == false) |
11 |
13 Sep 07 |
nicklas |
103 |
{ |
11 |
13 Sep 07 |
nicklas |
104 |
chp = null; |
11 |
13 Sep 07 |
nicklas |
105 |
} |
11 |
13 Sep 07 |
nicklas |
106 |
} |
11 |
13 Sep 07 |
nicklas |
107 |
return chp; |
11 |
13 Sep 07 |
nicklas |
108 |
} |
11 |
13 Sep 07 |
nicklas |
109 |
|
11 |
13 Sep 07 |
nicklas |
/** Creates a CHP reading object. |
11 |
13 Sep 07 |
nicklas |
* @param fileTypeId The file type in the CHP file. |
11 |
13 Sep 07 |
nicklas |
* @return The CHP object, NULL if not able to read the file. |
11 |
13 Sep 07 |
nicklas |
113 |
*/ |
11 |
13 Sep 07 |
nicklas |
114 |
private static FusionCHPData createObject(AffymetrixGuidType fileTypeId) { |
11 |
13 Sep 07 |
nicklas |
// Find the matching CHP data object. |
11 |
13 Sep 07 |
nicklas |
116 |
for (FusionCHPDataReg p=head; p != null; p = p.next) |
11 |
13 Sep 07 |
nicklas |
117 |
{ |
11 |
13 Sep 07 |
nicklas |
118 |
Vector ids = p.fileTypeIdentifiers; |
11 |
13 Sep 07 |
nicklas |
119 |
for (int i=0; i<ids.size(); i++) |
11 |
13 Sep 07 |
nicklas |
120 |
{ |
11 |
13 Sep 07 |
nicklas |
121 |
AffymetrixGuidType id = (AffymetrixGuidType) ids.elementAt(i); |
11 |
13 Sep 07 |
nicklas |
122 |
if (id.getGuid().compareTo(fileTypeId.getGuid()) == 0) |
11 |
13 Sep 07 |
nicklas |
123 |
{ |
11 |
13 Sep 07 |
nicklas |
124 |
FusionCHPData chp = p.makeObject(); |
11 |
13 Sep 07 |
nicklas |
125 |
chp.setFileTypeIdentifiers(p.fileTypeIdentifiers); |
11 |
13 Sep 07 |
nicklas |
126 |
chp.setFileTypeIdentifier(fileTypeId); |
11 |
13 Sep 07 |
nicklas |
127 |
if (chp != null) |
11 |
13 Sep 07 |
nicklas |
128 |
{ |
11 |
13 Sep 07 |
nicklas |
129 |
return chp; |
11 |
13 Sep 07 |
nicklas |
130 |
} |
11 |
13 Sep 07 |
nicklas |
131 |
} |
11 |
13 Sep 07 |
nicklas |
132 |
} |
11 |
13 Sep 07 |
nicklas |
133 |
} |
11 |
13 Sep 07 |
nicklas |
134 |
|
11 |
13 Sep 07 |
nicklas |
// Go back now and find the generic reader (the one with no file type identifiers) |
11 |
13 Sep 07 |
nicklas |
136 |
for (FusionCHPDataReg p=head; p != null; p = p.next) |
11 |
13 Sep 07 |
nicklas |
137 |
{ |
11 |
13 Sep 07 |
nicklas |
138 |
if (p.fileTypeIdentifiers.size() == 0) |
11 |
13 Sep 07 |
nicklas |
139 |
{ |
11 |
13 Sep 07 |
nicklas |
140 |
FusionCHPData chp = p.makeObject(); |
11 |
13 Sep 07 |
nicklas |
141 |
chp.setFileTypeIdentifier(fileTypeId); |
11 |
13 Sep 07 |
nicklas |
142 |
return chp; |
11 |
13 Sep 07 |
nicklas |
143 |
} |
11 |
13 Sep 07 |
nicklas |
144 |
} |
11 |
13 Sep 07 |
nicklas |
145 |
|
11 |
13 Sep 07 |
nicklas |
146 |
return null; |
11 |
13 Sep 07 |
nicklas |
147 |
} |
11 |
13 Sep 07 |
nicklas |
148 |
|
11 |
13 Sep 07 |
nicklas |
/** Makes an CHP data object. |
11 |
13 Sep 07 |
nicklas |
* @return The CHP data object. |
11 |
13 Sep 07 |
nicklas |
151 |
*/ |
11 |
13 Sep 07 |
nicklas |
152 |
protected abstract FusionCHPData makeObject(); |
11 |
13 Sep 07 |
nicklas |
153 |
|
11 |
13 Sep 07 |
nicklas |
/** A pointer to the first registered CHP reader. */ |
11 |
13 Sep 07 |
nicklas |
155 |
private static FusionCHPDataReg head = null; |
11 |
13 Sep 07 |
nicklas |
156 |
|
11 |
13 Sep 07 |
nicklas |
/** A pointer to the next registered CHP reader. */ |
11 |
13 Sep 07 |
nicklas |
158 |
private FusionCHPDataReg next; |
11 |
13 Sep 07 |
nicklas |
159 |
|
11 |
13 Sep 07 |
nicklas |
/** The file type identifiers associated with the CHP files the reader can parse. */ |
11 |
13 Sep 07 |
nicklas |
161 |
private Vector /*AffymetrixGuidType*/ fileTypeIdentifiers; |
11 |
13 Sep 07 |
nicklas |
162 |
|
11 |
13 Sep 07 |
nicklas |
163 |
} |