-
Notifications
You must be signed in to change notification settings - Fork 7
/
Affy.pm
357 lines (296 loc) · 6.75 KB
/
Affy.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
#===============================================================================
#
# FILE: Affy.pm
#
# DESCRIPTION: Package to handle Affymetrix files
# BUGS: ---
# NOTES: The package is spun off from the AnnotationIO.pm
# AUTHOR: Chaolin Zhang (cz), czhang@rockefeller.edu
# COMPANY: Rockefeller University
# VERSION: 1.0
# CREATED: 12/17/2010
# REVISION: ---
#===============================================================================
package Affy;
our $VERSION = 1.01;
=head1 NAME
Affy - read and write Affymetrix files
subroutines starting with a hyphen should not be called outside
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp;
=head2 read1LQFile
read .1lq file that contains probe coordinates and sequences
my $ret = read1LQFile ($inFile)
$ret is an array reference. each element contains:
X=>
Y=>
SEQUENCE=>
...
=cut
sub read1LQFile
{
my $in = $_[0];
my $fin;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
while (my $line=<$fin>)
{
chomp $line;
last if $line=~/^X\tY/;
}
my @ret;
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
my @cols = split ("\t", $line);
push @ret, {
X=>$cols[0],
Y=>$cols[1],
SEQUENCE=>$cols[2],
DESTYPE=>$cols[3],
FEATURE=>$cols[4],
QUALIFIER=>$cols[5],
EXPOS=>$cols[6],
PLEN=>$cols[7],
POS=>$cols[8],
CBASE=>$cols[9],
PBASE=>$cols[10],
TBASE=>$cols[11],
IPBASE=>$cols[12],
UNIT=>$cols[13],
BLOCK=>$cols[14],
ATOM=>$cols[15]
};
}
close ($fin);
return \@ret;
}
=head2 readPgfFile
read .pgf file that contains probe coordinates and sequences
my $ret = readPgfFile ($inFile)
$ret is an array reference. each element contains:
X=>
Y=>
SEQUENCE=>
...
=cut
sub readPgfFile
{
my $in = $_[0];
my $fin;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
while (my $line=<$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
last if $line =~/^\#\%header2/;
}
my @ret;
my ($probesetId, $probesetType, $probesetName) = ("", "", "");
my ($atom_id, $exon_position);
while (my $line=<$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
#print $line, "\n";
if ($line=~/^\S/)
{
($probesetId, $probesetType, $probesetName) = split ("\t", $line);
next;
}
#next unless $line =~/pm\:st/;
$line =~/^\s*(\S.*)$/;
$line = $1;
#$line = reverse ($line);
my @cols = split ("\t", $line);
if (@cols <=2)
{
($atom_id, $exon_position) = @cols;
}
else
{
push @ret, {
probeset_id=>$probesetId,
probeset_type=>$probesetType,
probeset_name=>$probesetName,
atom_id=>$atom_id,
exon_position=>$exon_position,
probe_id=>$cols[0],
type=>$cols[1],
gc_count=>$cols[2],
probe_length=>$cols[3],
interrogation_position=>$cols[4],
probe_sequence=>$cols[5],
};
#Carp::croak Dumper (\@ret), "\n";
}
#Carp::croak Dumper (pop @ret), "\n";
}
close ($fin);
return \@ret;
}
=head2 readCelFile
Usage:
my $ret = readCelFile ($inFile);
$ret = {
header=>
intensity=>
masks=>
outliers=>
modified=>
}
=cut
sub readCelFile
{
my $in = $_[0];
my %ret;
my $fin;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
my $line = <$fin>;
$line = <$fin>;
chomp $line;
my ($name, $value) = split ("=", $line);
Carp::croak "only Cel file version 3 is recognized\n" if $value != 3;
my $iter = 0;
while ($line =<$fin>)
{
$iter++;
#last if $iter > 10;
chomp $line;
next if $line=~/^\s*$/;
#print $line, "\n";
if ($line eq '[HEADER]')
{
#print "parsing header...\n";
$ret{"header"} = parseCelHeader ($fin);
Carp::croak "missing number of columns\n" unless exists $ret{"header"}->{"Cols"};
Carp::croak "missing number of rows\n" unless exists $ret{"header"}->{"Rows"};
}
elsif ($line eq '[INTENSITY]')
{
#print "parsing intensity...\n";
$ret{"intensity"} = parseCelIntensity ($fin);
Carp::croak "incorrect number of columns\n" if @{$ret{"intensity"}} != $ret{"header"}->{"Cols"};
Carp::croak "incorrect number of rows\n" if @{$ret{"intensity"}->[0]} != $ret{"header"}->{"Rows"};
}
elsif ($line eq '[MASKS]')
{
#print "parsing masks...\n";
$ret{"masks"} = parseCelMasksOrOutliers ($fin);
}
elsif ($line eq '[OUTLIERS]')
{
$ret{"outliers"} = parseCelMasksOrOutliers ($fin);
}
elsif ($line eq '[MODIFIED]')
{
$ret{"modified"} = parseCelModified ($fin);
}
}
close ($fin);
return \%ret;
}
sub parseCelHeader
{
my $fin = $_[0];
my %ret;
while (my $line =<$fin>)
{
chomp $line;
last if $line=~/^\s*$/;
my @cols = split ("=", $line);
Carp::croak "incorrect name=value pair: $line\n" if @cols < 2;
my $name = shift @cols;
my $value = join ("=", @cols);
$ret{$name} = $value;
}
return \%ret;
}
sub parseCelIntensity
{
my $fin = $_[0];
my $ret;
my $line = <$fin>;
my ($name, $numCells) = split ("=", $line);
Carp::croak "NumberCells not found:$line\n" if $name ne 'NumberCells';
$line = <$fin>;
for (my $i = 0; $i < $numCells; $i++)
{
my $line = <$fin>;
chomp $line;
my @cols = split (/\s+/, $line);
shift @cols if @cols > 5;
Carp::croak "incorrect number of columns at line: $line\n" if @cols != 5;
my ($x, $y, $mean, $stdev, $npixels) = @cols;
$ret->[$x][$y] = {MEAN=>$mean, STDV=>$stdev};
}
return $ret;
}
sub parseCelMasksOrOutliers
{
my $fin = $_[0];
my @ret;
my $line = <$fin>;
my ($name, $numCells) = split ("=", $line);
Carp::croak "NumberCells not found:$line\n" if $name ne 'NumberCells';
$line = <$fin>;
for (my $i = 0; $i < $numCells; $i++)
{
my $line = <$fin>;
chomp $line;
my @cols = split (/\s+/, $line);
shift @cols if @cols > 2;
Carp::croak "incorrect number of columns at line: $line\n" if @cols != 2;
my ($x, $y) = @cols;
push @ret, {X=>$x, Y=>$y};
}
return \@ret;
}
sub parseCelModified
{
my $fin = $_[0];
my @ret;
my $line = <$fin>;
my ($name, $numCells) = split ("=", $line);
Carp::croak "NumberCells not found:$line\n" if $name ne 'NumberCells';
$line = <$fin>;
for (my $i = 0; $i < $numCells; $i++)
{
my $line = <$fin>;
chomp $line;
my @cols = split (/\s+/, $line);
shift @cols if @cols > 3;
Carp::croak "incorrect number of columns at line: $line\n" if @cols != 3;
my ($x, $y, $origMean) = @cols;
push @ret, {X=>$x, Y=>$y, ORIGMEAN=>$origMean};
}
return \@ret;
}
=head2 readSgrFile
my $ret = readSgrFile ($inFile)
$ret is an array reference, with each row containing:
chrom=>
pos=>
score=>
=cut
sub readSgrFile
{
my $in = $_[0];
my $fin;
my @ret;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
while (my $line =<$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
my ($chr, $pos, $score) = split ("\t", $line);
push @ret, {chrom=>$chr, pos=>$pos, score=>$score};
}
close ($fin);
return \@ret;
}
1;