6 print "perl $0 depsingle\n";
11 $statf =~ s/\.[^.\/\\]+$//;
14 my ($ChrID,%Stat,%StatA)=('Empty');
15 open DEPTH
,'<',$depf or die "[x]Error opening $depf: $!\n";
16 open STAT
,'>',$statf or die "[x]Error opening $statf: $!\n";
17 open STATA
,'>',$statf.'all' or die "[x]Error opening ${statf}all: $!\n";
21 if (exists $Stat{$ChrID}) {
22 print STAT
"[Chr$ChrID]\n"; # for Reading in R, SectionName must be string
23 for my $v (sort {$a <=> $b} keys %{$Stat{$ChrID}}) {
24 print STAT
'd',$v,"=",$Stat{$ChrID}{$v},"\n"; # for Reading in R, Key must be string
25 #print $v,"=",$Stat{$ChrID}{$v},"\n";
28 print STDERR
"\b\b\bdone.\n";
31 print STDERR
"$_ ...";
45 if (exists $Stat{$ChrID}) {
46 print STAT
"[$ChrID]\n";
47 for my $v (sort {$a <=> $b} keys %{$Stat{$ChrID}}) {
48 print STAT
$v,"=",$Stat{$ChrID}{$v},"\n";
51 print STDERR
"\b\b\bo\n";
57 for my $v (sort {$a <=> $b} keys %StatA) {
58 print STATA
$v,"\t",$StatA{$v},"\n";
65 > plot
(x
,dpois
(x
,12),type
='p')
66 > lines
(x
,dpois
(x
,12))
67 > lines
(c
(12,12),c
(-1,1),col
='blue')
71 a
=read.delim
(paste
('g',Lib
,'.depstatall',sep
=''),F
)
72 a
[1,2]=a
[1,2]-239850802
73 EffLen
=sum
(as
.numeric
(a
$V2))
77 c
=sum
(as
.numeric
(a
$V1*a
$V2))/EffLen
78 pdf
(paste
('g',Lib
,'.pdf',sep
=''),width
=8,height
=6,paper
="special")
79 plot
(a
$V1,a
$V2/EffLen
,xlim
=c
(0,maxX
),ylim
=c
(0,maxY
),type
='b',xlab
='depth',ylab
='density',main
=paste
('g',Lib
,sep
=''),
80 sub=paste
('depth=',round
(c
,4),', ','cvg=',round
(100*(1-a
[1,2]/EffLen
),4),' % < ',round
(100*(1-exp(-c
)),4),' %',sep
='')
82 #lines(a$V1,a$V2/EffLen)
83 lines
(x
,dpois
(x
,c
),col
='blue')
84 lines
(c
(c
,c
),c
(-1,1),col
='red')
88 https
://stat
.ethz
.ch
/pipermail/r
-help
/2007-June/134110.html
90 Since
in my problem the structure of the INI sections is almost static
and
91 always present
, I extended your example to create an
in-memory list of
92 everything
in the INI file with this function
:
94 # Prototype of how to read INI files to process olfactometer data
96 # Thanks to Gabor Grothendieck for helpful suggestions in the R-Help
97 # mailing list on how to parse the INI file.
98 Parse
.INI
<- function
(INI
.filename
)
100 connection
<- file
(INI
.filename
)
101 Lines
<- readLines
(connection
)
104 Lines
<- chartr
("[]", "==", Lines
) # change section headers
106 connection
<- textConnection
(Lines
)
107 d
<- read.table
(connection
, as
.is
= TRUE
, sep
= "=", fill
= TRUE
)
110 L
<- d
$V1 == "" # location of section breaks
111 d
<- subset
(transform
(d
, V3
= V2
[which
(L
)[cumsum
(L
)]])[1:3],
114 ToParse
<- paste
("INI.list$", d
$V3, "$", d
$V1, " <- '",
118 eval(parse
(text
=ToParse
))
124 Here
's an example of using the above function (I'll put the sample input
127 INI1
<- Parse
.INI
("sample.ini")
129 # Explore INI contents
132 INI1
$SystemSetup$OlfactometerCode
134 unlist
(INI1
$DefaultLevels)
138 as
.integer
( unlist
( strsplit
(INI1
$Map$port1, ",") ) )
143 > INI1
<- Parse
.INI
("sample.ini")
145 > # Explore INI contents
148 SystemSetup
1 -none
- list
150 DefaultLevels
4 -none
- list
151 OdorNames
2 -none
- list
154 > INI1
$SystemSetup$OlfactometerCode
169 > unlist
(INI1
$DefaultLevels)
174 [1] "0,0,0,0,0,0,0,0,0,0,0,0"
177 [1] "0,0,0,0,0,0,0,0,0,0,0,0"
180 [1] "0,0,0,0,0,0,0,0,0,0,0,0"
184 [1] "0,0,0,0,0,0,0,0,0,0,0,0"
185 > as
.integer
( unlist
( strsplit
(INI1
$Map$port1, ",") ) )
186 [1] 0 0 0 0 0 0 0 0 0 0 0 0
189 Sample input file
, sample
.ini
:
195 date0
=2:06:27.461 PM
6/9/2007
199 date1
=2:09:16.809 PM
6/9/2007
211 port0
=0,0,0,0,0,0,0,0,0,0,0,0
212 port1
=0,0,0,0,0,0,0,0,0,0,0,0
213 port2
=0,0,0,0,0,0,0,0,0,0,0,0
215 plot
(as
.matrix
(INI1
$Chr11),type
='l')