lib/Microarray/DataMatrix/TabDelimitedDataMatrix.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 155 CC 4 SCAR 13.9
CC
4
Cov
0%
_dataLine
12.0
_numDataColumns
6.0
_numDataRows
2.0
CRAP 4.0
1 package Microarray::DataMatrix::TabDelimitedDataMatrix;
2  
3 # License information (the MIT license)
4  
5 # Copyright (c) 2003 Christian Rees, Janos Demeter, John Matese, Gavin
6 # Sherlock; Stanford University
7  
8 # Permission is hereby granted, free of charge, to any person
9 # obtaining a copy of this software and associated documentation files
10 # (the "Software"), to deal in the Software without restriction,
11 # including without limitation the rights to use, copy, modify, merge,
12 # publish, distribute, sublicense, and/or sell copies of the Software,
13 # and to permit persons to whom the Software is furnished to do so,
14 # subject to the following conditions:
15  
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18  
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
27  
28  
29 # Author : Gavin Sherlock
30 # Date : 1st August 2002
31  
32 # This package is intended to provide some primitive functions to
33 # provide an abstraction layer to a tab-delimited dataMatrix. It is
34 # an abstract class, and requires that concrete subclasses be created
35 # to take advantage of the functionality. Such concrete subclasses
36 # are expected to include a pcl file, and a cdt file. In many senses,
37 # tabDelimitedDataMatrix serves as a means for consolidating code that
38 # would otherwise be duplicated in a pclFile and a cdtFile class.
39  
40 # This package assumes that a tab-delimited dataMatrix may have
41 # leading rows of meta data, and/or leading columns of meta data.
42 # Concrete subclasses will know how to determine how many rows and
43 # columns of leading meta data there are.
44  
45  
46 use strict;
47 use vars qw (@ISA);
48  
49 $|=1; # set autoflush, so that reporting will flush immediately
50  
51 use Microarray::DataMatrix::AnySizeDataMatrix;
52 @ISA = qw (Microarray::DataMatrix::AnySizeDataMatrix);
53  
54 # CLASS GLOBALS
55  
56 # use the following so we only ever access hash entries
57 # using variables, to prevent typos, or accidental cloberring.
58  
59 # These values are assigned here and then are READ-ONLY.
60  
61 my $PACKAGE = "Microarray::DataMatrix::TabDelimitedDataMatrix";
62  
63 my $kMetaDataRecorded = $PACKAGE.'::__metaDataRecorded'; # indicates if meta data recorded
64 my $kCurrFileDataRow = $PACKAGE.'::__currFileDataRow'; # which data row we are about to read
65 my $kColumnNames = $PACKAGE.'::__columnNames'; # experiment names
66  
67 ######################################################################
68 sub _numDataRows{
69 ######################################################################
70 # This method returns the total number of data rows in the tab
71 # delimited dataMatrix. The superclass requires that this method be
72 # implemented in any subclasses, such that the base class can
73 # determine the number of rows in the matrix, even if the full data
74 # have not been read in.
75  
76 my ($self) = @_;
77  
78 my $file = $self->file;
79  
80 my $output = `/usr/ucb/wc -l $file`;
81  
82 chomp $output;
83  
84 $output =~ s/$file//; # get rid of filename from wc output
85  
86 $output =~ s/\s+//g; # get rid of whitespace
87  
88 # now subtract the number of leading meta data lines
89 # _numLeadingMetaLines is implemented in the concrete subclass
90  
91 my $numDataRows = $output - $self->_numLeadingMetaLines; # subtract the number of leading meta data lines
92  
93 return ($numDataRows);
94  
95 }
96  
97 ######################################################################
98 sub _numDataColumns{
99 ######################################################################
100 # This method returns the total number of data columns in the
101 # tab-delimited dataMatrix file. The base class requires that it be
102 # implemented in any subclasses, such that the base class can
103 # determine the number of columns, even if the full data have not been
104 # read in.
105  
106 my ($self) = @_;
107  
108 my $file = $self->file;
109  
110 if (defined ($self->columnNamesArrayRef)){
111  
112 return scalar @{$self->columnNamesArrayRef};
113  
114 }else{
115  
116 my $fh = $self->_fileHandle(reset=>1);
117  
118 # now parse out the headers
119 # _parseFileHeaders will be implemented in the concrete subclass
120  
121 $self->_parseFileHeaders($fh); # parse out the headers
122  
123 $self->_fileHandle(reset=>1); # reset the handle again
124  
125 return (scalar @{$self->columnNamesArrayRef});
126  
127 }
128
129 }
130  
131  
132 ######################################################################
133 sub _dataLine{
134 ######################################################################
135 # This method reads through the associated tab delimited matrix file,
136 # and then returns a reference to an array of data that corresponds to
137 # the requested data line. A requestedRowNum of zero means the first
138 # data line. If no data line corresponds to the requested row number,
139 # it will return undef.
140  
141 my ($self, $requestedRowNum) = @_;
142  
143 my $reset = 0; # whether we need to reset the file handle
144  
145 if ($self->__currFileDataRow > $requestedRowNum){
146  
147 $reset = 1; # we'll have to close and reopen the file
148
149 }
150  
151 my $fh = $self->_fileHandle(reset=>$reset);
152  
153 if ($self->__currFileDataRow == -1){ # we're at the beginning
154  
155 # remove the headers from the file
156  
157 $self->_parseFileHeaders($fh);
158  
159 }
160  
161 my $currFileDataRow = $self->__currFileDataRow;
162  
163 while ($currFileDataRow < $requestedRowNum){
164  
165 # discard data until we reach the correct line
166  
167 <$fh> || return undef; # if we read of the end of the file, return undef to indicate no data
168  
169 $currFileDataRow++; # increment the count
170  
171 }
172  
173 # if we get here, the next line to be read is the requested one - phew!
174  
175 my $line = <$fh> || return undef; # if we read of the end of the file, return undef to indicate no data
176  
177 my $lineRef = $self->_arrayRefForLine(\$line);
178  
179 # remove and remember the meta data - this is implemented in the concrete subclass
180  
181 $self->_removeMetaDataFromLine($currFileDataRow, $lineRef);
182  
183 # set the new line number
184  
185 $self->_setCurrFileDataRow($currFileDataRow+1); # use +1 to account for the line we just read
186  
187 return $lineRef; # return the data as an array reference
188  
189 }
190  
191 ######################################################################
192 sub __currFileDataRow{
193 ######################################################################
194 # This method is either used to retrieve the current data line number.
195  
196 return $_[0]->{$kCurrFileDataRow};
197  
198 }
199
200 ######################################################################
201 sub _setCurrFileDataRow{
202 ######################################################################
203 # This method is used to set the current data line number.
204  
205 my ($self, $lineNo) = @_;
206  
207 $self->{$kCurrFileDataRow} = $lineNo;
208  
209 }
210  
211 ######################################################################
212 sub _arrayRefForLine{
213 ######################################################################
214 # This method takes a scalar by reference, that is a line from a
215 # tab delimited file, and returns, by reference, an array that contains
216 # the chomped and split data from the line.
217  
218 my ($self, $lineRef) = @_;
219  
220 chomp $$lineRef;
221  
222 my @line = split("\t", $$lineRef, -1);
223  
224 return \@line;
225  
226 }
227  
228  
229 ######################################################################
230 sub _metaDataRecorded{
231 ######################################################################
232 # This protected method returns a boolean, to indicate whether meta data
233 # has been recorded for a particular row in the tab delimited file.
234 #
235 # Usage : if ($self->_metaDataRecorded($row)){ #blah }
236  
237 my ($self, $row) = @_;
238  
239 return exists $self->{$kMetaDataRecorded}->{$row};
240  
241 }
242  
243 ######################################################################
244 sub _indicateMetaDataRecorded{
245 ######################################################################
246 # This private method records that meta data has been recorded for a
247 # particular row in the tab delimited file. This is so concrete
248 # subclasses can keep track of whether they already know the meta data
249 # associated with a line in the file.
250 #
251 # Usage : $self->_indicateMetaDataRecordedRecorded($row);
252  
253 my ($self, $row) = @_;
254  
255 $self->{$kMetaDataRecorded}->{$row} = undef;
256
257 }
258  
259 ######################################################################
260 sub columnNamesArrayRef{
261 ######################################################################
262 # This polymorphic setter/getter method returns a reference to an
263 # array containing the column names (only of the data columns) from
264 # the original tab delimited file. If a new array reference is
265 # provided, it will rename the columns.
266 #
267 # Usage:
268 #
269 # my $columnNamesArrayRef = $self->columnNamesArrayRef;
270 # $self->columnNamesArrayRef($lineRef);
271  
272 my ($self, $data) = @_;
273  
274 defined ($data) && ($self->{$kColumnNames} = $data);
275  
276 return $self->{$kColumnNames};
277  
278 }
279  
280 ######################################################################
281 sub _printRow{
282 ######################################################################
283 # This method is used to print out a row of data to a file.
284 #
285 # Usage : $self->_printRow($fh, $row, $dataRef, $validColumnsArrayRef, $hasExtraInfo, $extraInfoHashRef);
286 #
287 # This method prints to the passed in file handle. It will only print
288 # information for those valid columns. If the $extraInfo variable is
289 # true (ie a non-zero value) it will print the extra info interleaved
290 # between each column of data. The extra info comes from the 2D hash
291 # whose reference is passed in.
292
293 my ($self, $fh, $row, $dataRef, $validColumnsArrayRef, $hasExtraInfo, $extraInfoHashRef) = @_;
294
295 # first print the meta data for the row (implemented in concrete subclass)
296  
297 $self->_printRowMetaData($fh, $row);
298
299 # now print our the actual data for the row
300  
301 foreach my $column (sort {$a <=> $b } @{$validColumnsArrayRef}){
302
303 print $fh "\t", $dataRef->[$column];
304
305 $hasExtraInfo && print($fh "\t", $extraInfoHashRef->{$row}{$column});
306
307 }
308
309 print $fh "\n";
310
311 }
312  
313 ######################################################################
314 sub _setFileHandleToStart{
315 ######################################################################
316 # This method allows the super class file handle method to communicate
317 # to subclasses that the current file handle is at the beginning of
318 # the file, so that the subclass can take care of setting/resetting
319 # any other variables that it might need to.
320  
321 my ($self) = @_;
322  
323 $self->_setCurrFileDataRow(-1); # reset the current row
324  
325 }
326  
327 ######################################################################
328 sub createIndexFile{
329 ######################################################################
330 # This method creates a simple index file for the
331 # tabDelimitedDataMatrix. The index file contains three columns, and
332 # ID column, which is a sequential number, and the then the UID and
333 # NAME columns. These are simply the annotation columns from the
334 # tabDelimitedDataMatrix.
335 #
336 # On the first row, there will be printed the word ID, followed by the
337 # column name identifiers in the file.
338 #
339 # The index file is created for the currently valid rows only.
340 #
341 # Usage : $matrix->createIndexFile($filename);
342 #
343  
344 my ($self, $file) = @_;
345  
346 open (OUT, ">".$file) || die "Cannot open $file : $!\n";
347  
348 print OUT join("\t", ("ID", $self->idName, $self->descName)), "\n";
349  
350 my $index = 0;
351  
352 foreach my $row (sort {$a<=>$b} @{$self->_validRowsArrayRef}){
353  
354 print OUT join("\t", $index, $self->rowName($row), $self->rowDesc($row)), "\n";
355  
356 ++$index;
357  
358 }
359  
360 close OUT;
361  
362 }
363  
364 ######################################################################
365 sub createRawMatrixFile{
366 ######################################################################
367 # This method simply creates an unanntotated tab-delimited file that
368 # contains only the matrix data for those currently valid rows and
369 # columns, with an index column on the left hand side, staring at
370 # zero.
371 #
372 # Usage : $matrix->createRawMatrixFile($filename);
373 #
374  
375 my ($self, $file) = @_;
376  
377 open (OUT, ">".$file) || die "Cannot create raw matrix file, $file : $!";
378  
379 my @validColumns = sort {$a<=>$b} @{$self->_validColumnsArrayRef};
380  
381 my $index = 0;
382  
383 foreach my $row (sort {$a<=>$b} @{$self->_validRowsArrayRef}){
384  
385 my $lineRef = $self->_dataLine($row);
386  
387 print OUT join ("\t", ($index, @{$lineRef}[@validColumns])), "\n";
388  
389 ++$index;
390  
391 }
392  
393 close OUT;
394
395 }
396  
397 1; # to keep perl happy
398  
399 =pod
400  
401 =head2 _dataLine
402  
403 This method reads through the associated tab delimited file, and then
404 returns a reference to an array of data that corresponds to the
405 requested data line. A requestedRowNum of zero means the first data
406 line. If no data line corresponds to the requested row number, it
407 will return undef.
408  
409 Usage:
410  
411 my $lineRef = $self->_dataLine($num);
412  
413 =head2 __currFileDataRow
414  
415 This method is either used to retrieve the current data line number.
416 It will return -1 if the file being read has had no rows read.
417  
418 Usage:
419  
420 my $currentRow = $self->__currFileDataRow;
421  
422 =head2 _setCurrFileDataRow
423  
424 This method is used to set the current data line number.
425  
426 Usage:
427  
428 $self->_setCurrFileDataRow($rowNum);
429  
430 =head2 _arrayRefForLine
431  
432 This protected method takes a scalar by reference, that is a line from a tab
433 delimited file, and returns, by reference, an array that contains the
434 chomped and split data from the line.
435  
436 Usage:
437  
438 my $lineRef = $self->_arrayRefForLine(\$line);
439  
440 =head2 _metaDataRecorded
441  
442 This protected method returns a boolean, to indicate whether meta data
443 has been recorded for a particular row in the tab delimited file.
444  
445 Usage :
446  
447 if ($self->_metaDataRecorded($row)){ #blah }
448  
449 =head2 _indicateMetaDataRecorded
450  
451 This protected method records that meta data has been recorded for a
452 particular row in the tab delimited file.
453  
454 Usage :
455  
456 $self->_indicateMetaDataRecordedRecorded($row);
457  
458  
459 =head2 columnNamesArrayRef
460  
461 This polymorphic setter/getter method returns a reference to an array
462 containing the column names (only of the data columns) from the
463 original pcl file. If a new array reference is provided, it will
464 rename the columns.
465  
466 Usage:
467  
468 my $columnNamesArrayRef = $self->columnNamesArrayRef;
469 $self->columnNamesArrayRef($lineRef);
470  
471 =head2 _printRow
472  
473 This method is used to print out a row of data to a file. It prints
474 to the passed in file handle. It will only print information for
475 those valid columns. If the $extraInfo variable is true (ie a
476 non-zero value) it will print the extra info interleaved between each
477 column of data. The extra info comes from the 2D hash whose reference
478 is passed in. This method is implemented as required by its
479 superclass, anySizeDataMatrix.
480  
481 Usage :
482  
483 $self->_printRow($fh, $row, $dataRef, $validColumnsArrayRef, $hasExtraInfo, $extraInfoHashRef);
484  
485  
486 =head1 AUTHOR
487  
488 Gavin Sherlock
489  
490 sherlock@genome.stanford.edu
491  
492 =cut