-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwinsp.pas
205 lines (183 loc) · 5.84 KB
/
twinsp.pas
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
unit twinsp;
interface
uses
Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, ExtCtrls, ComCtrls, Spin;
type
{ TTWSPDlg }
TTWSPDlg = class(TForm)
LabelNCutLevels: TLabel;
OKButton: TButton;
CancelButton: TButton;
PseudospeciesStringGrid: TStringGrid;
PseudoSpeciesRadioGroup: TRadioGroup;
LabelMinGroupSize: TLabel;
LabelMaxLevelOfDivs: TLabel;
SpinEditNCutLevels: TSpinEdit;
SpinEditMinimumGroupSize: TSpinEdit;
SpinEditMaximumLevelDivision: TSpinEdit;
procedure FormCreate(Sender: TObject);
procedure OKButtonClick(Sender: TObject);
procedure PseudoSpeciesRadioGroupSelectionChanged(Sender: TObject);
procedure SpinEditNCutLevelsChange(Sender: TObject);
private
public
CT: string;
procedure CreateTWSP(fname: string; cut_levels: string;
min_group_size, levels: integer);
procedure TWSP(fname: string; cut_levels: string;
min_group_size, levels: integer; n, m: integer);
end;
var
TWSPDlg: TTWSPDlg;
implementation
{$R *.lfm}
uses report, useful;
resourcestring
strTWSP = 'ANÁLISE DE ESPÉCIES INDICADORAS';
strSamples = 'Amostras';
strSpecies = 'Espécies';
procedure DeleteRow(Grid: TStringGrid; ARow: integer);
var
i: integer;
begin
for i := ARow to Grid.RowCount - 2 do
Grid.Rows[i].Assign(Grid.Rows[i + 1]);
Grid.RowCount := Grid.RowCount - 1;
end;
{ TTWSPDlg }
procedure TTWSPDlg.PseudoSpeciesRadioGroupSelectionChanged(Sender: TObject);
var
i, r: integer;
begin
i := PseudoSpeciesRadioGroup.ItemIndex;
case i of
0:
begin
PseudoSpeciesStringGrid.InsertRowWithValues(1, ['1', '0']);
PseudoSpeciesStringGrid.InsertRowWithValues(2, ['2', '2']);
PseudoSpeciesStringGrid.InsertRowWithValues(3, ['3', '5']);
PseudoSpeciesStringGrid.InsertRowWithValues(4, ['4', '10']);
PseudoSpeciesStringGrid.InsertRowWithValues(5, ['5', '20']);
PseudoSpeciesStringGrid.Enabled := False;
LabelNCutLevels.Enabled := False;
SpinEditNCutLevels.Enabled := False;
end;
1:
begin
PseudoSpeciesStringGrid.InsertRowWithValues(1, ['1', '0']);
for i := 2 to 5 do
PseudoSpeciesStringGrid.InsertRowWithValues(i, ['', '']);
PseudoSpeciesStringGrid.Enabled := False;
LabelNCutLevels.Enabled := False;
SpinEditNCutLevels.Enabled := False;
end;
2:
begin
LabelNCutLevels.Enabled := True;
SpinEditNCutLevels.Enabled := True;
PseudoSpeciesStringGrid.Enabled := True;
PseudoSpeciesStringGrid.RowCount := SpinEditNCutLevels.Value + 1;
for r := 1 to PseudoSpeciesStringGrid.RowCount - 1 do
DeleteRow(PseudoSpeciesStringGrid, r);
for r := 1 to SpinEditNCutLevels.Value do
PseudoSpeciesStringGrid.InsertRowWithValues(r, [IntToStr(r), '0']);
end
end;
end;
procedure TTWSPDlg.SpinEditNCutLevelsChange(Sender: TObject);
var
r: integer;
begin
PseudoSpeciesStringGrid.RowCount := SpinEditNCutLevels.Value + 1;
for r := 1 to PseudoSpeciesStringGrid.RowCount - 1 do
DeleteRow(PseudoSpeciesStringGrid, r);
for r := 1 to SpinEditNCutLevels.Value do
PseudoSpeciesStringGrid.InsertRowWithValues(r, [IntToStr(r), '0']);
end;
procedure TTWSPDlg.FormCreate(Sender: TObject);
begin
PseudoSpeciesRadioGroup.ItemIndex := 0;
PseudoSpeciesRadioGroupSelectionChanged(Sender);
//PseudoSpeciesStringGrid.InsertRowWithValues(0, [strPseudospecies, strCutLevel, strWeight, strIndicatorPotential])
end;
procedure TTWSPDlg.OKButtonClick(Sender: TObject);
var
r: integer;
l: TStringList;
begin
CT := '';
l := TStringList.Create;
for r := 1 to PseudoSpeciesStringGrid.RowCount - 1 do
begin
if PseudoSpeciesStringGrid.Cells[1, r] <> '' then
l.Add(PseudoSpeciesStringGrid.Cells[1, r]);
end;
CT := l.DelimitedText;
l.Free;
CreateTWSP('', CT, SpinEditMinimumGroupSize.Value, SpinEditMaximumLevelDivision.Value);
end;
procedure TTWSPDlg.CreateTWSP(fname: string; cut_levels: string;
min_group_size, levels: integer);
var
outfile: TextFile;
begin
AssignFile(outfile, 'twsp.R');
Rewrite(outfile);
WriteLn(outfile, 'options(warn=-1)');
WriteLn(outfile, 'options(digits=4)');
WriteLn(outfile, 'suppressPackageStartupMessages(library(twinspanR))');
WriteLn(outfile, 'library(twinspanR, quietly=TRUE)');
WriteLn(outfile, 'df.data <- read.csv("rdata.csv", row.names=1)');
WriteLn(outfile, 'df.data <- t(df.data)');
WriteLn(outfile, 'twsp <- twinspan(df.data, cut.levels=c(', cut_levels, ')',
', min.group.size=', min_group_size, ', levels=', levels, ')');
WriteLn(outfile, 'sink("twsp.txt")');
WriteLn(outfile, 'summary(twsp)');
WriteLn(outfile, 'sink()');
WriteLn(outfile,
'write.table(file="table.txt", capture.output(print(twsp, what="table")), quote=FALSE, row.names=FALSE)');
WriteLn(outfile, 'options(warn=0)');
CloseFile(outfile);
end;
procedure TTWSPDlg.TWSP(fname: string; cut_levels: string;
min_group_size, levels: integer; n, m: integer);
var
line: string;
infile, outfile: TextFile;
begin
AssignFile(outfile, fname);
Rewrite(outfile);
Header(outfile, strTWSP);
WriteLn(outfile, '<br>');
WriteLn(outfile, IntToStr(n) + ' ' + LowerCase(strSamples) + ' x ' +
IntToStr(m) + ' ' + LowerCase(strSpecies) + '<br><br>');
WriteLn(outfile, '<pre>');
AssignFile(infile, 'twsp.txt');
Reset(infile);
while not EOF(infile) do
begin
ReadLn(infile, line);
WriteLn(outfile, line);
end;
CloseFile(infile);
WriteLn(outfile);
AssignFile(infile, 'table.txt');
Reset(infile);
while not EOF(infile) do
begin
ReadLn(infile, line);
if not line.StartsWith('x') then
WriteLn(outfile, line);
end;
CloseFile(infile);
WriteLn(outfile, '</pre>');
WriteLn(outfile, '</body>');
WriteLn(outfile, '</html>');
CloseFile(outfile);
if FileExists('twsp.txt') then
DeleteFile('twsp.txt');
if FileExists('table.txt') then
DeleteFile('table.txt');
end;
end.