-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTestDataUtils.java
More file actions
693 lines (623 loc) · 28.6 KB
/
TestDataUtils.java
File metadata and controls
693 lines (623 loc) · 28.6 KB
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
package org.labkey.test.util.data;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jetbrains.annotations.NotNull;
import org.labkey.serverapi.reader.DataLoader;
import org.labkey.serverapi.reader.TabLoader;
import org.labkey.test.TestFileUtils;
import org.labkey.test.params.FieldDefinition;
import org.labkey.test.util.TestDataGenerator;
import org.labkey.test.util.TestLogger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class TestDataUtils
{
// 'FieldDefinition's are mutable; don't store them as global constants
public static final List<Supplier<FieldDefinition>> REALISTIC_ASSAY_FIELDS = List.of(
() -> new FieldDefinition("Addition or Removal (0= addition, 1=removal)", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("Source (0=external, 1 = internal to system)", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("Raw Sample [Al] g/L", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Raw Sample [H+] g/L or %", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("KJ/Day", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("EE KCal/kg0.75", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Ratio EE in Kcal/Day to Lean Mass", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Feed %", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Consumption Rate, Glucose", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Measurement Date/Time", FieldDefinition.ColumnType.DateAndTime),
() -> new FieldDefinition("A260/A280", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Nucleic Acid (ng/uL)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Concentration (by Qubit ng/uL)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Dead (cells/ml)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("PDGF-AA/BB", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Run End Data/Time", FieldDefinition.ColumnType.DateAndTime),
() -> new FieldDefinition("Run Start Date/Time", FieldDefinition.ColumnType.DateAndTime),
() -> new FieldDefinition("Algorithm Parameter: Calc. Top", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("1.0", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("2.0"),
() -> new FieldDefinition("12.0"),
() -> new FieldDefinition("FAM-Lambda..cp.Rxn."),
() -> new FieldDefinition("VIC\\Precision...1"),
() -> new FieldDefinition("Product.Type"),
() -> new FieldDefinition("Weight.Balance_%", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Cumulative.Yield\\DCW/Glucose.Consumed_g/g", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Average.Volume.Productivity_g/L/day", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Cmol.Biomass/Cmol.Glucose.Consumed_%", FieldDefinition.ColumnType.Decimal)
);
public static final List<Supplier<FieldDefinition>> REALISTIC_SAMPLE_FIELDS = List.of(
() -> new FieldDefinition("MW (g/mol)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Batch FW (g/mol)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Sequence (5'-3')"),
() -> new FieldDefinition("Tumor%", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Viable_cells%", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Sample no."),
() -> new FieldDefinition("Pass/Fail" , FieldDefinition.ColumnType.TextChoice).setTextChoiceValues(List.of("Pass", "Fail")),
() -> new FieldDefinition("Final Positivity %", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Optimised Yes/No", FieldDefinition.ColumnType.Boolean),
() -> new FieldDefinition("G-Band Pass/Fail", FieldDefinition.ColumnType.Boolean),
() -> new FieldDefinition("Positivity/negativity notes"),
() -> new FieldDefinition("Useful for R&D/Production ?", FieldDefinition.ColumnType.Boolean),
() -> new FieldDefinition("NaCl Lot Number (External), 0.9% NaCl Expiry (In-House)"),
() -> new FieldDefinition("'GURR' 6.8 buffer tablets Lot number (External)"),
() -> new FieldDefinition("Giemsa Stain Lot number, Expiry"),
() -> new FieldDefinition("Trypsin 2.5% Lot number (External), Expiry"),
() -> new FieldDefinition("Lot No.", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("Sample Origin / Owner"),
() -> new FieldDefinition("PSS Tracking No."),
() -> new FieldDefinition("Product/bottle size", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Time point / Pull Date", FieldDefinition.ColumnType.DateAndTime),
() -> new FieldDefinition("Cell Type (Epz, Spz, PS)", FieldDefinition.ColumnType.TextChoice).setTextChoiceValues(List.of("Epz", "Spz", "PS")),
() -> new FieldDefinition("Concentration (ng/uL)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Lot no. (Replacement tube) 1"),
() -> new FieldDefinition("Date of Collection (DD/MMM/YYY)", FieldDefinition.ColumnType.Date),
() -> new FieldDefinition("Freezer/Fridge ID"),
() -> new FieldDefinition( "X Position (i.e., box row)", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("[Analysis 2] 2. Time In (Fridge)", FieldDefinition.ColumnType.Time),
() -> new FieldDefinition("Aliquot_No._/_ID"),
() -> new FieldDefinition("VIAL_ID/BARCODE/ACCESSION_No."),
() -> new FieldDefinition("No.=_464"),
() -> new FieldDefinition("Specimen_condition_(Hämolyse/insufficient_volume/…)", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("CHECKOUT_(x),_Removed_(1)"),
() -> new FieldDefinition("Age <18 years of age or >65 years of age.", FieldDefinition.ColumnType.Boolean),
() -> new FieldDefinition("CTS&L_LLS_Visit_Code_"),
() -> new FieldDefinition("Collection Tube Type & Volume 1"),
() -> new FieldDefinition("Row_&_Col"),
() -> new FieldDefinition("The participant has received any investigational compound from a different trial within 30 days or 5 half-lives (whichever is greater)."),
() -> new FieldDefinition("Barcode e.g FG30000A001"),
() -> new FieldDefinition("Information pertaining to patient recruitment e.g advertisements, bulletins and information placed on the internet - TMAR"),
() -> new FieldDefinition("Data Collection Tools (CRF's, Info Sheets, etc.)")
);
public static final List<Supplier<FieldDefinition>> REALISTIC_SOURCE_FIELDS = List.of(
() -> new FieldDefinition("Patient Race / Ethnicity", FieldDefinition.ColumnType.TextChoice).setTextChoiceValues(List.of("American Indian or Alaska Native", "Asian", "Black", "Native Hawaiian or Pacific Islander", "White", "Other", "Unknown" )),
() -> new FieldDefinition("Tumor%", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Viable_cells%", FieldDefinition.ColumnType.Decimal),
() -> new FieldDefinition("Гемоглобін тех.", FieldDefinition.ColumnType.Date),
() -> new FieldDefinition("Disposition (per SOW/MTA)", FieldDefinition.ColumnType.String),
() -> new FieldDefinition("~ Height (T to B) (mm)", FieldDefinition.ColumnType.Integer),
() -> new FieldDefinition("OD/DCW factor", FieldDefinition.ColumnType.String),
() -> new FieldDefinition("Age (years)", FieldDefinition.ColumnType.Integer)
);
public static final List<String> REALISTIC_PLATE_NAMES = List.of(
"123456.01",
"Example Plate (96-well)",
"2024-01-01 Luminex Plate #1",
"Study ABC Serum/Plasma",
"- Plate 1 this one is from an instrument file",
"Miniprep Quant BL 18JAN2023",
"CIS43LS ABCD PK Pre-Qual Run 3"
);
public static final List<String> REALISTIC_DOMAIN_NAMES = List.of(
"10 minute placenta",
"30 minute placenta",
"Adiponectin ELISA Data Fields",
"Aqueous",
"Ascitic fluid",
"Biopsy tissue",
"Buccal swabs",
"Buffy coat",
"DSX Study",
"Cell-free DNA (cfDNA) and circulating tumor DNA (ctDNA) from plasma",
"Cerebrospinal fluid (CSF)",
"cfDNA",
"Chemical compounds-Solid powders",
"Circulating tumor cells (CTCs)",
"circulating tumor DNA (ctDNA) from plasma",
"Cord blood heparin",
"cord.blood.heparin",
"Cord DNA",
"cord.DNA",
"CRC_IV st_MMR",
"CSF",
"CTC",
"ctDNA",
"Cultured Cell Lines",
"Cytokine ELISA Data Fields",
"D2G Oncology",
"2O18 Data Fields",
"NA",
"External Development",
"External Fixed Cells (Haematology)",
"FFPE - Blocks",
"FFPE",
"Formalin-fixed paraffin-embedded (FFPE) tissue",
"Fresh frozen tissue",
"Gastric_MMR",
"Gastrointestinal fluid (GI)",
"GI",
"In House Fixed Cells (Haematology)",
"Insulin ELISA Data Fields",
"Juul",
"Kindeva",
"Leptin ELISA Data Fields",
"LOY-001 PK Data Fields",
"Maternal DNA",
"Membrane",
"Microbiome",
"Molecular&other testing",
"Multiple Pathogen",
"mRNA",
"miRNA",
"NEFA Data Fields",
"Nonn Primers",
"Organoids",
"Paternal DNA",
"PAXGENE",
"PBMC",
"Peripheral blood mononuclear cells",
"Placenta DNA",
"Placenta RNA",
"Plasma",
"Platelet count",
"Primary Cells",
"Primary Cells from Tumor Tissue",
"Primes",
"PTSD",
"Recode Therapeutics",
"Research Development",
"Research Project",
"Retrospective archive_FFPE",
"RNA",
"Saliva swabs",
"Sample release",
"Samples created for V&V studies",
"Serum",
"Studies",
"Study",
"Surgical resection specimens",
"TFF pharmaceuticals",
"Trizol",
"Truvian Sciences",
"TSS",
"Urine",
"Virome",
"Vitreous",
"Water Stock",
"Whole Blood",
"Whole Globe"
);
private TestDataUtils()
{
// Utility class. Do not instantiate.
}
public static String getRealisticPlateName()
{
return getRealisticPlateName(new ArrayList<>());
}
public static String getRealisticPlateName(List<String> excludePlateNames)
{
List<String> includeNames = new ArrayList<>(REALISTIC_PLATE_NAMES);
includeNames.removeAll(excludePlateNames);
return includeNames.get(TestDataGenerator.randomInt(0, includeNames.size() - 1));
}
public static String getRealisticDomainName()
{
return getRealisticDomainName(new ArrayList<>());
}
public static String getRealisticDomainName(List<String> excludeDomains)
{
List<String> includeNames = new ArrayList<>(REALISTIC_DOMAIN_NAMES);
includeNames.removeAll(excludeDomains);
return includeNames.get(TestDataGenerator.randomInt(0, includeNames.size() - 1));
}
public static List<Map<String, Object>> rowMapsFromTsv(File tsvFile) throws IOException
{
try (DataLoader loader = new TabLoader.TsvFactory().createLoader(tsvFile, true))
{
return loader.load();
}
}
public static List<Map<String, Object>> rowMapsFromTsv(String tsvString) throws IOException
{
try (InputStream dataStream = IOUtils.toInputStream(tsvString, StandardCharsets.UTF_8))
{
return new TabLoader.TsvFactory().createLoader(dataStream, true).load();
}
}
public static List<Map<String, Object>> rowMapsFromCsv(File csvFile) throws IOException
{
try (DataLoader loader = new TabLoader.CsvFactory().createLoader(csvFile, true))
{
return loader.load();
}
}
public static List<String> columnHeaderFromCsv(File csvFile) throws IOException
{
try (DataLoader loader = new TabLoader.CsvFactory().createLoader(csvFile, true))
{
List<String> columnHeaders = new ArrayList<>();
for (var column : loader.getColumns())
columnHeaders.add(column.name);
return columnHeaders;
}
}
public static List<Map<String, Object>> rowMapsFromCsv(String tsvString) throws IOException
{
try (InputStream dataStream = IOUtils.toInputStream(tsvString, StandardCharsets.UTF_8))
{
return new TabLoader.CsvFactory().createLoader(dataStream, true).load();
}
}
public static <T> String stringFromRowMaps(List<Map<String, T>> rowMaps, List<String> columns, boolean includeHeaders, CSVFormat format)
{
return stringFromRows(rowListsFromMaps(rowMaps, columns, includeHeaders, true), format);
}
public static <T> String tsvStringFromRowMaps(List<Map<String, T>> rowMaps, List<String> columns,
boolean includeHeaders)
{
return stringFromRowMaps(rowMaps, columns, includeHeaders, CSVFormat.TDF);
}
public static <T> List<Map<String, T>> mapsFromRows(List<List<T>> allRows)
{
List<Map<String, T>> rowMaps = new ArrayList<>();
if (allRows != null && !allRows.isEmpty())
{
List<T> header = allRows.get(0);
for (int i = 1; i != allRows.size(); i++)
{
List<T> row = allRows.get(i);
Map<String, T> rowMap = new LinkedHashMap<>();
int end = Math.min(header.size(), row.size());
for (int col = 0; col < end; col++)
{
rowMap.put(header.get(col).toString(), row.get(col));
}
rowMaps.add(rowMap);
}
}
return rowMaps;
}
public static <T> List<List<String>> dataRowsFromMaps(List<Map<String, T>> rowMaps, List<String> columns)
{
return rowListsFromMaps(rowMaps, columns, false, true);
}
/**
* @see #rowListsFromMaps(List, List, boolean, boolean)
*/
public static <T> List<List<String>> rowListsFromMaps(List<Map<String, T>> rowMaps)
{
Set<String> columns = new LinkedHashSet<>();
for (Map<String, T> row : rowMaps)
{
columns.addAll(row.keySet());
}
return rowListsFromMaps(rowMaps, new ArrayList<>(columns), true, true);
}
/**
* @see #rowListsFromMaps(List, List, boolean, boolean)
*/
public static <T> List<List<String>> rowListsFromMaps(List<Map<String, T>> rowMaps, List<String> columns)
{
return rowListsFromMaps(rowMaps, columns, true, true);
}
/**
* convert a List of row maps to a tabular format. Column headers can be included optionally.<br>
* Useful for writing to file or TSV String.
* @param rowMaps Source data
* @param columns keys contained in each map, will copy values associated with them to the resulting list
* @param includeHeaders Include headers in tabular data
* @param allowMissingValues Replace missing values with an empty string. Missing values will throw otherwise.
* @return Data rows represented as a List<List<String>>
* @param <T> Value type of row maps
*/
public static <T> List<List<String>> rowListsFromMaps(List<Map<String, T>> rowMaps, List<String> columns, boolean includeHeaders, boolean allowMissingValues)
{
List<List<String>> lists = new ArrayList<>();
if (includeHeaders)
{
List<String> headers = new ArrayList<>(columns);
lists.add(headers);
}
for (Map<String, T> rowMap : rowMaps)
{
List<String> rowList = new ArrayList<>();
for (String column : columns)
{
Object value = rowMap.get(column);
if (value == null)
{
if (allowMissingValues)
value = "";
else
throw new IllegalArgumentException("Missing value for column '" + column + "' in row: " + rowMap);
}
if (value instanceof Collection<?> c)
{
value = c.stream().map(Object::toString).collect(Collectors.joining(","));
}
rowList.add(value.toString());
}
lists.add(rowList);
}
return lists;
}
/**
* Replace the column headers of the provided tabular data. The first row should represent the column headers.
* Useful for converting column headers between name, label, or fieldKey
* @param rowLists Tabular data. Will not be modified.
* @param columnMapper Mapping function to calculate new headers
* @return A new list tabular data with replaced headers. Individual data rows will be contained in the new list.
* @see ColumnNameMapper
*/
public static List<List<String>> replaceColumnHeaders(List<List<String>> rowLists, Function<String, String> columnMapper)
{
if (rowLists == null || rowLists.isEmpty())
return rowLists;
List<String> headerRow = rowLists.get(0);
List<List<String>> updatedRows = new ArrayList<>();
updatedRows.add(headerRow.stream().map(columnMapper).collect(Collectors.toList()));
updatedRows.addAll(List.copyOf(rowLists.subList(1, rowLists.size())));
return updatedRows;
}
/**
* Replace the keys for the provided data maps.
* Useful for converting column headers between name, label, or fieldKey
* @param rowMaps Data row maps. Will not be modified.
* @param columnMapper Mapping function to calculate new headers
* @return A new list of row maps with replaced headers
* @param <K> Key type of input data (usually String)
* @param <R> Key type of returned data (usually String)
* @param <T> Value type for data (usually String or Object)
* @see ColumnNameMapper
*/
public static <K, R, T> List<Map<R, T>> replaceMapKeys(List<Map<K, T>> rowMaps, Function<K, R> columnMapper)
{
List<Map<R, T>> updatedRows = new ArrayList<>();
for (Map<K, T> original : rowMaps)
{
Map<R, T> updatedRow = new LinkedHashMap<>();
for (Map.Entry<K, T> entry : original.entrySet())
{
R updatedKey = columnMapper.apply(entry.getKey());
if (updatedRow.containsKey(updatedKey))
{
throw new IllegalArgumentException("Duplicate key mapping for '" + updatedKey + "' in row: " + original);
}
updatedRow.put(updatedKey, entry.getValue());
}
updatedRows.add(updatedRow);
}
return updatedRows;
}
public static <T> File writeDataToFile(String fileName, List<Map<String, T>> rowMaps) throws IOException
{
return writeRowsToFile(fileName, new RecordIterator(rowMaps));
}
public static <T> File writeRowsToFile(String fileName, List<List<T>> rows) throws IOException
{
return writeRowsToFile(fileName, rows.iterator());
}
public static <T> File writeRowsToFile(String fileName, Iterator<List<T>> rowIterator) throws IOException
{
String fileExtension = fileName.toLowerCase().substring(fileName.lastIndexOf('.') + 1);
return switch (fileExtension)
{
case "xlsx", "xls" -> TestDataUtils.writeRowsToExcel(fileName, rowIterator);
case "csv" -> TestDataUtils.writeRowsToFile(fileName, rowIterator, CSVFormat.DEFAULT);
case "tsv" -> TestDataUtils.writeRowsToFile(fileName, rowIterator, CSVFormat.TDF);
default -> throw new IllegalArgumentException("Unsupported file extension: " + fileExtension);
};
}
public static <T> File writeRowsToTsv(String fileName, List<List<T>> rows) throws IOException
{
return writeRowsToFile(fileName, rows, CSVFormat.TDF);
}
public static <T> File writeRowsToCsv(String fileName, List<List<T>> rows) throws IOException
{
return writeRowsToFile(fileName, rows, CSVFormat.DEFAULT);
}
public static @NotNull <T> File writeRowsToFile(String fileName, List<List<T>> rows, CSVFormat format) throws IOException
{
return writeRowsToFile(fileName, rows.iterator(), format);
}
public static @NotNull <T> File writeRowsToFile(String fileName, Iterator<List<T>> rowIterator, CSVFormat format) throws IOException
{
File file = new File(TestFileUtils.getTestTempDir(), fileName);
FileUtils.forceMkdirParent(file);
TestLogger.log("Writing data file " + file.getAbsolutePath());
try (CSVPrinter printer = new CSVPrinter(new FileWriter(file, StandardCharsets.UTF_8), format)) {
while (rowIterator.hasNext())
{
printer.printRecord(rowIterator.next());
}
}
return file;
}
public static @NotNull <T> File writeRowsToExcel(String fileName, List<List<T>> rows) throws IOException
{
return writeRowsToExcel(fileName, rows.iterator());
}
public static @NotNull <T> File writeRowsToExcel(String fileName, Iterator<List<T>> rowIterator) throws IOException
{
File file = new File(TestFileUtils.getTestTempDir(), fileName);
FileUtils.forceMkdirParent(file);
TestLogger.log("Writing data file " + file.getAbsolutePath());
try (SXSSFWorkbook workbook = new SXSSFWorkbook(1000); // only holds 1000 rows in memory
FileOutputStream out = new FileOutputStream(file))
{
var sheet = workbook.createSheet("sheet1");
// write headers as row 0
List<T> columnNames = rowIterator.next();
var headerRow = sheet.createRow(0);
for (int col = 0; col < columnNames.size(); col++)
{
if (columnNames.get(col) instanceof String s)
{
headerRow.createCell(col).setCellValue(s);
}
else
{
throw new IllegalArgumentException("Expected column headers to be Strings but got " + columnNames.get(col).getClass().getSimpleName());
}
}
// write content
for (int rowNum = 1; rowIterator.hasNext(); rowNum++)
{
List<T> row = rowIterator.next();
SXSSFRow currentRow = sheet.createRow(rowNum + 1);
for (int col = 0; col < columnNames.size(); col++)
{
currentRow.createCell(col).setCellValue(row.get(col).toString());
}
}
workbook.write(out);
}
return file;
}
public static List<List<String>> readRowsFromTsv(File file) throws IOException
{
return readRowsFromFile(file, CSVFormat.TDF);
}
public static List<List<String>> readRowsFromCsv(File file) throws IOException
{
return readRowsFromFile(file, CSVFormat.DEFAULT);
}
public static List<List<String>> readRowsFromFile(File file, CSVFormat format) throws IOException
{
try (Reader in = new FileReader(file, StandardCharsets.UTF_8))
{
CSVParser parser = CSVParser.builder().setFormat(format).setReader(in).get();
List<CSVRecord> records = parser.getRecords();
return records.stream().map(CSVRecord::toList).toList();
}
}
public static String parseMultiValueText(String multiValueString) throws IOException
{
CSVFormat format = CSVFormat.RFC4180.builder()
.setIgnoreSurroundingSpaces(true).setTrim(true).get();
try (CSVParser parser = format.parse(new StringReader(multiValueString)))
{
List<CSVRecord> records = parser.getRecords();
if (records.isEmpty())
return "";
return records.getFirst().stream().collect(Collectors.joining(multiValueString.contains(", ") ? ", " : ","));
}
}
public static <T> String stringFromRows(List<List<T>> rows, CSVFormat format)
{
StringWriter stringWriter = new StringWriter();
try (CSVPrinter printer = new CSVPrinter(stringWriter, format)) {
for (List<?> row : rows)
{
printer.printRecord(row);
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
return stringWriter.toString();
}
public static <T> String stringFromRows(List<List<T>> rows)
{
return stringFromRows(rows, CSVFormat.TDF);
}
public static @NotNull <T> List<String> getColumnValues(Collection<Map<String, T>> rows, String name)
{
return getColumnValues(rows, name, String.class);
}
public static @NotNull <R, T> List<R> getColumnValues(Collection<Map<String, T>> rows, String name, Class<R> type)
{
return rows.stream().map(row -> type.cast(row.get(name))).toList();
}
/**
* Used to quote values to be written to a TSV file
* @see org.labkey.api.data.TSVWriter
* @deprecated Use {@link CSVFormat#format(Object...)} or {@link org.labkey.test.components.ui.grids.EditableGrid#quoteForPaste(String...)} to quote individual values
*/
@Deprecated (since = "25.9")
public static class TsvQuoter
{
protected char _escapeChar = '\\';
private static final char _chQuote = '"';
private final char[] _escapedChars;
public TsvQuoter(char delimiterChar)
{
_escapedChars = new char[] {'\r', '\n', _escapeChar, _chQuote, delimiterChar};
}
public TsvQuoter()
{
this('\t');
}
public String quoteValue(Object o)
{
if (o == null)
return "";
String value = o.toString();
String escaped = value;
if (shouldQuote(value))
{
StringBuilder sb = new StringBuilder(value.length() + 10);
sb.append(_chQuote);
int i;
int lastMatch = 0;
while (-1 != (i = value.indexOf(_chQuote, lastMatch)))
{
sb.append(value, lastMatch, i);
sb.append(_chQuote).append(_chQuote);
lastMatch = i + 1;
}
if (lastMatch < value.length())
sb.append(value.substring(lastMatch));
sb.append(_chQuote);
escaped = sb.toString();
}
return escaped;
}
protected boolean shouldQuote(String value)
{
int len = value.length();
if (len == 0)
return false;
char firstCh = value.charAt(0);
char lastCh = value.charAt(len - 1);
if (Character.isSpaceChar(firstCh) || Character.isSpaceChar(lastCh))
return true;
return StringUtils.containsAny(value, _escapedChars);
}
}
}