/
read_abatch.c
executable file
·5453 lines (4241 loc) · 173 KB
/
read_abatch.c
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************
**
** It should be noted that Laurent Gautier provided the
** initial CEL file parsing code as part of the file
** read_cdf.c. This code served us well for the 1.0-1.2
** releases of Bioconductor. He should be commended for his
** fine code.
**
** The following code became the default parsing code
** at the BioC 1.3 release.
**
** Also note that this code was originally part of the affy package
** before being moved to affyio
**
************************************************************/
/*************************************************************
**
** file: read_abatch.c
**
** aim: read in from 1st to nth chips of CEL data
**
** Copyright (C) 2003-2007 B. M. Bolstad
**
** Created on Jun 13, 2003
**
** Notes:
**
** The following assumptions are made about text CEL files.
**
** 1. A CEL file has a series of sections in the order
**
** [CEL]
** [HEADER]
** [INTENSITY]
** [MASKS]
** [OUTLIERS]
**
** 2. As part of opening the file we will check that
** the first characters of the file are "[CEL]"
**
** 3. In the [HEADER] section we expect lines beginning
** 3a. Cols=
** 3b. Rows=
** 3ab.1 Cols should appear before Rows
** 3c. DatHeader=
** 3c.1 On the DatHeader line there should appear a
** string with the final characters ".1sq". We
** will assume that this is the name of the
** CDF file (trim off the ".1sq")
**
** 4. In the [INTENSITY] section there should be
** 4a. A line beginning "CellHeader="
** 4b. After this line there should be cols*rows probe
** intensity lines. Each of these lines should have
** 4.b.1 Five tokens.
** 4.b.2 the first token is an integer to be treated as
** the x location
** 4.b.3 the second token is an integer to be treated as
** the y location
** 4.b.4 the third token is a floating point number (double)
** to be treated as the probe intensity value.
**
** 5. The [MASKS] and [OUTLIERS] sections will be treated similarly
** 5a. We look for a line beginning NumberCells=
** this will be the number of Masked or Outlier CELS
** that we will expect to see.
** 5b. For each line of these sections we will expect to see
** the first two items are integers indicating the
** X and Y locations of probes that should be set to NA
** if the user sets the right flags.
**
** The implementation of parsing for binary files was based upon
** file format information supplied from Affymetrix.
**
**
** History
**
** Jun 13, 2003 - Initial version
** Jun 14, 2003 - Further implementation
** Jun 15, 2003 - testing.
** Jun 16, 2003 - Extra verbosity (user controlled).
** Jun 17, 2003 - MASKS, OUTLIERS
** Added mechanism for reading the header.
** this function called ReadHeader
** this can be used to check all files same
** as first file.
** Jun 19, 2003 - Naming of columns of intensity matrix done in
** C code
** Jun 29, 2003 - remove some unnecessary cruft from tokenize
** routine
** Jun 30, 2003 - remove tokenize step from read_cel_intensities.
** aim is to gain more speed.
** Jul 1, 2003 - deal with compressed files.
** To avoid ugly pre-processor constructs code
** for gz functions is seperate from that in
** text files.
** Made BUF_SIZE 1024
** Jul 3, 2003 - add missing pre-processor command
** Jul 4, 2003 - A function for reading PM or MM or both
** as individual matrices.
** Made all the internal functions static
** to prevent namespace pollution
** Oct 3, 2003 - fix an error in check_cel_file /check_gzcel_file
** cdffile was not being properly checked
** Oct 14, 2003 - fix a long standing memory leak.
** Oct 17, 2003 - integrate binary format reading, Make
** it possible to read in a mixture of
** binary, text and gzipped text CEL files
** Nov 16, 2003 - More work on binary file support (in particular
** work on endian issues for no ia32 platforms)
** Nov 20, 2003 - Fix endian bug in readfloat32. Clean up the
** some of the documentation
** Dec 2, 2003 - Fix up fopen commands (hopefully) for a problem
** on w32 machines with text files.
** May 7, 2004 - make strncmp strncasecmp. No more problems
** with cdf name capitalization.
** May 11, 2004 - fix error message in case when zlib not available
** and a gzipped file has been supplied.
** Aug 16, 2005 - fix bit flipping when more than one number read
** Nov 15, 2005 - ability to read in SD and npixels values
** Nov 30, 2005 - remove compress argument from functions where it
** appears. it is legacy and has not been used in
** a great deal of time.
** Dec 14, 2005 - Added ReadHeaderDetailed: which attempts to
** be more complete then ReadHeader in what it returns
** from the header information basically the [CEL] and [HEADER]
** sections in the text files and similar information contained in
** the first section of the binary CEL file format
** Jan 27, 2006 - Complete ReadHeaderDetailed for supported formats.
** Add in a C level object for storing contents of a single
** CEL file
** May 31, 2006 - Fix some compiler warnings
** Jul 17, 2006 - Fix application of masks and outliers for binary cel files.
** Jul 21, 2006 - Binary parser checks for file truncation
** Aug 11, 2006 - Additional truncation checks for text files
** Aug 12, 2006 - Build the R construct that holds the CEL file
** Nov 3, 2006 - add gzipped binary CEL file support
** Apr 19, 2007 - Deal appropriately with non square CEL files (in binary format, the affymetrix documentation is inconsistent with the reality)
** May 13, 2007 - small fix for gzclose situation
** Aug 10, 2007 - fix for dangling open files each time a file is checked for binary format (Simon de Bernard, AltraBio)
** Aug 25, 2007 - Move file reading functions to centralized location
** Sep 6, 2007 - add support for generic (aka command console) format cel files
** Sep 7, 2007 - add support for gzipped generic (aka command console) format cel files
** Oct 28, 2007 - add pthread based multi-threaded read_probematrix this is based on a submission by Paul Gordon (U Calgary)
** Feb 18, 2008 - R_read_cel_file now can be told to read only the mean intensities (rather than also the SD and npixels)
** Mar 6, 2008 - Add additional CEL file corruption checking.
**
*************************************************************/
#include <R.h>
#include <Rdefines.h>
#include <Rmath.h>
#include <Rinternals.h>
#ifdef BIOLIB
#include <biolib_R_map.h>
#endif
#include "stdlib.h"
#include "stdio.h"
#include "fread_functions.h"
#include "read_celfile_generic.h"
#include "read_abatch.h"
#define HAVE_ZLIB 1
#if defined(HAVE_ZLIB)
#include <zlib.h>
#endif
#if USE_PTHREADS
#include <pthread.h>
pthread_mutex_t mutex_R;
int n_probesets;
int *n_probes = NULL;
double **cur_indexes = NULL;
struct thread_data{
SEXP filenames;
double *CurintensityMatrix;
double *pmMatrix;
double *mmMatrix;
int i;
int t;
int chunk_size;
int ref_dim_1;
int ref_dim_2;
int n_files;
int num_probes;
SEXP cdfInfo;
const char *refCdfName;
int which_flag;
SEXP verbose;
};
#define THREADS_ENV_VAR "R_THREADS"
#endif
#define BUF_SIZE 1024
/****************************************************************
****************************************************************
**
** Code for spliting strings into tokens.
** Not heavily used anymore
**
***************************************************************
***************************************************************/
/***************************************************************
**
** tokenset
**
** char **tokens - a array of token strings
** int n - number of tokens in this set.
**
** a structure to hold a set of tokens. Typically a tokenset is
** created by breaking a character string based upon a set of
** delimiters.
**
**
**************************************************************/
typedef struct{
char **tokens;
int n;
} tokenset;
/******************************************************************
**
** tokenset *tokenize(char *str, char *delimiters)
**
** char *str - a string to break into tokens
** char *delimiters - delimiters to use in breaking up the line
**
**
** RETURNS a new tokenset
**
** Given a string, split into tokens based on a set of delimitors
**
*****************************************************************/
static tokenset *tokenize(char *str, char *delimiters){
#if USE_PTHREADS
char *tmp_pointer;
#endif
int i=0;
char *current_token;
tokenset *my_tokenset = Calloc(1,tokenset);
my_tokenset->n=0;
my_tokenset->tokens = NULL;
#if USE_PTHREADS
current_token = strtok_r(str,delimiters,&tmp_pointer);
#else
current_token = strtok(str,delimiters);
#endif
while (current_token != NULL){
my_tokenset->n++;
my_tokenset->tokens = Realloc(my_tokenset->tokens,my_tokenset->n,char*);
my_tokenset->tokens[i] = Calloc(strlen(current_token)+1,char);
strcpy(my_tokenset->tokens[i],current_token);
my_tokenset->tokens[i][(strlen(current_token))] = '\0';
i++;
#if USE_PTHREADS
current_token = strtok_r(NULL,delimiters,&tmp_pointer);
#else
current_token = strtok(NULL,delimiters);
#endif
}
return my_tokenset;
}
/******************************************************************
**
** int tokenset_size(tokenset *x)
**
** tokenset *x - a tokenset
**
** RETURNS the number of tokens in the tokenset
**
******************************************************************/
static int tokenset_size(tokenset *x){
return x->n;
}
/******************************************************************
**
** char *get_token(tokenset *x, int i)
**
** tokenset *x - a tokenset
** int i - index of the token to return
**
** RETURNS pointer to the i'th token
**
******************************************************************/
static char *get_token(tokenset *x,int i){
return x->tokens[i];
}
/******************************************************************
**
** void delete_tokens(tokenset *x)
**
** tokenset *x - a tokenset
**
** Deallocates all the space allocated for a tokenset
**
******************************************************************/
static void delete_tokens(tokenset *x){
int i;
for (i=0; i < x->n; i++){
Free(x->tokens[i]);
}
Free(x->tokens);
Free(x);
}
/*******************************************************************
**
** int token_ends_with(char *token, char *ends)
**
** char *token - a string to check
** char *ends_in - we are looking for this string at the end of token
**
**
** returns 0 if no match, otherwise it returns the index of the first character
** which matchs the start of *ends.
**
** Note that there must be one additional character in "token" beyond
** the characters in "ends". So
**
** *token = "TestStr"
** *ends = "TestStr"
**
** would return 0 but if
**
** ends = "estStr"
**
** we would return 1.
**
** and if
**
** ends= "stStr"
** we would return 2 .....etc
**
**
******************************************************************/
static int token_ends_with(char *token, char *ends_in){
int tokenlength = strlen(token);
int ends_length = strlen(ends_in);
int start_pos;
char *tmp_ptr;
if (tokenlength <= ends_length){
/* token string is too short so can't possibly end with ends */
return 0;
}
start_pos = tokenlength - ends_length;
tmp_ptr = &token[start_pos];
if (strcmp(tmp_ptr,ends_in)==0){
return start_pos;
} else {
return 0;
}
}
/****************************************************************
****************************************************************
**
** Code for dealing with text CEL files.
**
***************************************************************
***************************************************************/
/****************************************************************
**
** void ReadFileLine(char *buffer, int buffersize, FILE *currentFile)
**
** char *buffer - place to store contents of the line
** int buffersize - size of the buffer
** FILE *currentFile - FILE pointer to an opened CEL file.
**
** Read a line from a file, into a buffer of specified size.
** otherwise die.
**
***************************************************************/
static void ReadFileLine(char *buffer, int buffersize, FILE *currentFile){
if (fgets(buffer, buffersize, currentFile) == NULL){
error("End of file reached unexpectedly. Perhaps this file is truncated.\n");
}
}
/****************************************************************
**
** FILE *open_cel_file(const char *filename)
**
** const char *filename - name of file to open
**
**
** RETURNS a file pointer to the open file
**
** this file will open the named file and check to see that the
** first characters agree with "[CEL]"
**
***************************************************************/
static FILE *open_cel_file(const char *filename){
const char *mode = "r";
FILE *currentFile = NULL;
char buffer[BUF_SIZE];
currentFile = fopen(filename,mode);
if (currentFile == NULL){
error("Could not open file %s", filename);
} else {
/** check to see if first line is [CEL] so looks like a CEL file**/
ReadFileLine(buffer, BUF_SIZE, currentFile);
if (strncmp("[CEL]", buffer, 4) == 0) {
rewind(currentFile);
} else {
error("The file %s does not look like a CEL file",filename);
}
}
return currentFile;
}
/******************************************************************
**
** void findStartsWith(FILE *my_file,char *starts, char *buffer)
**
** FILE *my_file - an open file to read from
** char *starts - the string to search for at the start of each line
** char *buffer - where to place the line that has been read.
**
**
** Find a line that starts with the specified character string.
** At exit buffer should contain that line
**
*****************************************************************/
static void findStartsWith(FILE *my_file,char *starts, char *buffer){
int starts_len = strlen(starts);
int match = 1;
do {
ReadFileLine(buffer, BUF_SIZE, my_file);
match = strncmp(starts, buffer, starts_len);
} while (match != 0);
}
/******************************************************************
**
** void AdvanceToSection(FILE *my_file,char *sectiontitle, char *buffer)
**
** FILE *my_file - an open file
** char *sectiontitle - string we are searching for
** char *buffer - return's with line starting with sectiontitle
**
**
*****************************************************************/
static void AdvanceToSection(FILE *my_file,char *sectiontitle, char *buffer){
findStartsWith(my_file,sectiontitle,buffer);
}
/******************************************************************
**
** int check_cel_file(const char *filename, const char *ref_cdfName, int ref_dim_1, int ref_dim_2)
**
** const char *filename - the file to read
** const char *ref_cdfName - the reference CDF filename
** int ref_dim_1 - 1st dimension of reference cel file
** int ref_dim_2 - 2nd dimension of reference cel file
**
** returns 0 if no problem, 1 otherwise
**
** The aim of this function is to read the header of the CEL file
** in particular we will look for the rows beginning "Cols=" and "Rows="
** and then for the line DatHeader= to scope out the appropriate cdf
** file. An error() will be flagged if the appropriate conditions
** are not met.
**
**
******************************************************************/
static int check_cel_file(const char *filename, const char *ref_cdfName, int ref_dim_1, int ref_dim_2){
int i;
int dim1,dim2;
FILE *currentFile;
char buffer[BUF_SIZE];
tokenset *cur_tokenset;
currentFile = open_cel_file(filename);
AdvanceToSection(currentFile,"[HEADER]",buffer);
findStartsWith(currentFile,"Cols",buffer);
cur_tokenset = tokenize(buffer,"=");
dim1 = atoi(get_token(cur_tokenset,1));
delete_tokens(cur_tokenset);
findStartsWith(currentFile,"Rows",buffer);
cur_tokenset = tokenize(buffer,"=");
dim2 = atoi(get_token(cur_tokenset,1));
delete_tokens(cur_tokenset);
if ((dim1 != ref_dim_1) || (dim2 != ref_dim_2)){
error("Cel file %s does not seem to have the correct dimensions",filename);
}
findStartsWith(currentFile,"DatHeader",buffer);
cur_tokenset = tokenize(buffer," ");
for (i =0; i < tokenset_size(cur_tokenset);i++){
if (strncasecmp(get_token(cur_tokenset,i),ref_cdfName,strlen(ref_cdfName)) == 0){
break;
}
if (i == (tokenset_size(cur_tokenset) - 1)){
error("Cel file %s does not seem to be of %s type",filename,ref_cdfName);
}
}
delete_tokens(cur_tokenset);
fclose(currentFile);
return 0;
}
/************************************************************************
**
** int read_cel_file_intensities(const char *filename, double *intensity, int chip_num, int rows, int cols)
**
** const char *filename - the name of the cel file to read
** double *intensity - the intensity matrix to fill
** int chip_num - the column of the intensity matrix that we will be filling
** int rows - dimension of intensity matrix
** int cols - dimension of intensity matrix
**
** returns 0 if successful, non zero if unsuccessful
**
** This function reads from the specified file the cel intensities for that
** array and fills a column of the intensity matrix.
**
************************************************************************/
static int read_cel_file_intensities(const char *filename, double *intensity, int chip_num, int rows, int cols,int chip_dim_rows){
#if USE_PTHREADS
char *tmp_pointer;
#endif
int i, cur_x,cur_y,cur_index;
double cur_mean;
FILE *currentFile;
char buffer[BUF_SIZE];
/* tokenset *cur_tokenset;*/
char *current_token;
currentFile = open_cel_file(filename);
AdvanceToSection(currentFile,"[INTENSITY]",buffer);
findStartsWith(currentFile,"CellHeader=",buffer);
for (i=0; i < rows; i++){
ReadFileLine(buffer, BUF_SIZE, currentFile);
/* cur_tokenset = tokenize(buffer," \t");
cur_x = atoi(get_token(cur_tokenset,0));
cur_y = atoi(get_token(cur_tokenset,1));
cur_mean = atof(get_token(cur_tokenset,2)); */
if (strlen(buffer) <=2){
Rprintf("Warning: found an empty line where not expected in %s.\nThis means that there is a cel intensity missing from the cel file.\nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, i);
break;
}
#if USE_PTHREADS
current_token = strtok_r(buffer," \t",&tmp_pointer);
#else
current_token = strtok(buffer," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_x = atoi(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_y = atoi(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
if (cur_x < 0 || cur_x >= chip_dim_rows){
error("It appears that the file %s is corrupted.",filename);
return 1;
}
if (cur_y < 0 || cur_y >= chip_dim_rows){
error("It appears that the file %s is corrupted.",filename);
return 1;
}
cur_mean = atof(current_token);
cur_index = cur_x + chip_dim_rows*(cur_y);
intensity[chip_num*rows + cur_index] = cur_mean;
/* delete_tokens(cur_tokenset); */
}
fclose(currentFile);
if (i != rows){
return 1;
}
return 0;
}
/************************************************************************
**
** int read_cel_file_stddev(const char *filename, double *intensity, int chip_num, int rows, int cols)
**
** const char *filename - the name of the cel file to read
** double *intensity - the intensity matrix to fill
** int chip_num - the column of the intensity matrix that we will be filling
** int rows - dimension of intensity matrix
** int cols - dimension of intensity matrix
**
** returns 0 if successful, non zero if unsuccessful
**
** This function reads from the specified file the cel stddev for that
** array and fills a column of the intensity matrix.
**
************************************************************************/
static int read_cel_file_stddev(const char *filename, double *intensity, int chip_num, int rows, int cols,int chip_dim_rows){
#if USE_PTHREADS
char *tmp_pointer;
#endif
int i, cur_x,cur_y,cur_index;
double cur_mean, cur_stddev;
FILE *currentFile;
char buffer[BUF_SIZE];
/* tokenset *cur_tokenset;*/
char *current_token;
currentFile = open_cel_file(filename);
AdvanceToSection(currentFile,"[INTENSITY]",buffer);
findStartsWith(currentFile,"CellHeader=",buffer);
for (i=0; i < rows; i++){
ReadFileLine(buffer, BUF_SIZE, currentFile);
/* cur_tokenset = tokenize(buffer," \t");
cur_x = atoi(get_token(cur_tokenset,0));
cur_y = atoi(get_token(cur_tokenset,1));
cur_mean = atof(get_token(cur_tokenset,2)); */
if (strlen(buffer) <=2){
Rprintf("Warning: found an empty line where not expected in %s.\n This means that there is a cel intensity missing from the cel file.\n Sucessfully read to cel intensity %d of %d expected\n", filename, i-1, i);
break;
}
#if USE_PTHREADS
current_token = strtok_r(buffer," \t",&tmp_pointer);
#else
current_token = strtok(buffer," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_x = atoi(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_y = atoi(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_mean = atof(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_stddev = atof(current_token);
cur_index = cur_x + chip_dim_rows*(cur_y);
intensity[chip_num*rows + cur_index] = cur_stddev;
/* delete_tokens(cur_tokenset); */
}
fclose(currentFile);
if (i != rows){
return 1;
}
return 0;
}
/************************************************************************
**
** int read_cel_file_npixels(const char *filename, double *intensity, int chip_num, int rows, int cols)
**
** const char *filename - the name of the cel file to read
** double *intensity - the intensity matrix to fill
** int chip_num - the column of the intensity matrix that we will be filling
** int rows - dimension of intensity matrix
** int cols - dimension of intensity matrix
**
** returns 0 if successful, non zero if unsuccessful
**
** This function reads from the specified file the cel stddev for that
** array and fills a column of the intensity matrix.
**
************************************************************************/
static int read_cel_file_npixels(const char *filename, double *intensity, int chip_num, int rows, int cols,int chip_dim_rows){
#if USE_PTHREADS
char *tmp_pointer;
#endif
int i, cur_x,cur_y,cur_index,cur_npixels;
double cur_mean, cur_stddev;
FILE *currentFile;
char buffer[BUF_SIZE];
/* tokenset *cur_tokenset;*/
char *current_token;
currentFile = open_cel_file(filename);
AdvanceToSection(currentFile,"[INTENSITY]",buffer);
findStartsWith(currentFile,"CellHeader=",buffer);
for (i=0; i < rows; i++){
ReadFileLine(buffer, BUF_SIZE, currentFile);
/* cur_tokenset = tokenize(buffer," \t");
cur_x = atoi(get_token(cur_tokenset,0));
cur_y = atoi(get_token(cur_tokenset,1));
cur_mean = atof(get_token(cur_tokenset,2)); */
if (strlen(buffer) <=2){
Rprintf("Warning: found an empty line where not expected in %s.\n This means that there is a cel intensity missing from the cel file.\n Sucessfully read to cel intensity %d of %d expected\n", filename, i-1, i);
break;
}
#if USE_PTHREADS
current_token = strtok_r(buffer," \t",&tmp_pointer);
#else
current_token = strtok(buffer," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_x = atoi(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_y = atoi(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_mean = atof(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_stddev = atof(current_token);
#if USE_PTHREADS
current_token = strtok_r(NULL," \t",&tmp_pointer);
#else
current_token = strtok(NULL," \t");
#endif
if (current_token == NULL){
Rprintf("Warning: found an incomplete line where not expected in %s.\nThe CEL file may be truncated. \nSucessfully read to cel intensity %d of %d expected\n", filename, i-1, rows);
break;
}
cur_npixels = atoi(current_token);
cur_index = cur_x + chip_dim_rows*(cur_y);
intensity[chip_num*rows + cur_index] = (double)cur_npixels;
/* delete_tokens(cur_tokenset); */
}
fclose(currentFile);
if (i != rows){
return 1;
}
return 0;
}
/****************************************************************
**
** void apply_masks(const char *filename, double *intensity, int chip_num,
** int rows, int cols,int chip_dim_rows,
** int rm_mask, int rm_outliers)
**
** const char *filename - name of file to open
** double *intensity - matrix of probe intensities
** int chip_num - the index 0 ...n-1 of the chip we are dealing with
** int rows - dimension of the intensity matrix
** int cols - dimension of the intensity matrix
** int chip_dim_rows - a dimension of the chip
** int rm_mask - if true locations in the MASKS section are set NA
** int rm_outliers - if true locations in the OUTLIERS section are set NA
**
** This function sets the MASK and OUTLIER probes to NA
**
**
****************************************************************/
static void apply_masks(const char *filename, double *intensity, int chip_num, int rows, int cols,int chip_dim_rows, int rm_mask, int rm_outliers){
int i;
int numcells, cur_x, cur_y, cur_index;
FILE *currentFile;
char buffer[BUF_SIZE];
tokenset *cur_tokenset;
if ((!rm_mask) && (!rm_outliers)){
/* no masking or outliers */
return;
}
currentFile = open_cel_file(filename);
/* read masks section */
if (rm_mask){
AdvanceToSection(currentFile,"[MASKS]",buffer);
findStartsWith(currentFile,"NumberCells=",buffer);
cur_tokenset = tokenize(buffer,"=");
numcells = atoi(get_token(cur_tokenset,1));
delete_tokens(cur_tokenset);
findStartsWith(currentFile,"CellHeader=",buffer);
for (i =0; i < numcells; i++){
ReadFileLine(buffer, BUF_SIZE, currentFile);
cur_tokenset = tokenize(buffer," \t");
cur_x = atoi(get_token(cur_tokenset,0));
cur_y = atoi(get_token(cur_tokenset,1));
cur_index = cur_x + chip_dim_rows*(cur_y);
intensity[chip_num*rows + cur_index] = R_NaN;
delete_tokens(cur_tokenset);
}
}
/* read outliers section */
if (rm_outliers){
AdvanceToSection(currentFile,"[OUTLIERS]",buffer);
findStartsWith(currentFile,"NumberCells=",buffer);
cur_tokenset = tokenize(buffer,"=");
numcells = atoi(get_token(cur_tokenset,1));
delete_tokens(cur_tokenset);
findStartsWith(currentFile,"CellHeader=",buffer);
for (i = 0; i < numcells; i++){
ReadFileLine(buffer, BUF_SIZE, currentFile);
cur_tokenset = tokenize(buffer," \t");
cur_x = atoi(get_token(cur_tokenset,0));
cur_y = atoi(get_token(cur_tokenset,1));
cur_index = cur_x + chip_dim_rows*(cur_y);
intensity[chip_num*rows + cur_index] = R_NaReal;
delete_tokens(cur_tokenset);
}
}
fclose(currentFile);
}
/****************************************************************
**
** static void get_masks_outliers(const char *filename,
** int *nmasks, short **masks_x, short **masks_y,
** int *noutliers, short **outliers_x, short **outliers_y
**
** This gets the x and y coordinates stored in the masks and outliers sections
** of the cel files.
**
****************************************************************/
static void get_masks_outliers(const char *filename, int *nmasks, short **masks_x, short **masks_y, int *noutliers, short **outliers_x, short **outliers_y){
FILE *currentFile;