forked from DEIB-GECO/Metadata-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformerStep.scala
711 lines (645 loc) · 30.9 KB
/
TransformerStep.scala
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
package it.polimi.genomics.metadata.step
import java.io._
import it.polimi.genomics.metadata.database.{FileDatabase, Stage}
import it.polimi.genomics.metadata.downloader_transformer.Transformer
import it.polimi.genomics.metadata.downloader_transformer.default.SchemaFinder
import it.polimi.genomics.metadata.step.utils.DatasetNameUtil
import it.polimi.genomics.metadata.step.xml.Dataset
import org.slf4j.{Logger, LoggerFactory}
import scala.collection.immutable.TreeMap
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.io.Source
import scala.util.matching.Regex
import scala.util.{Failure, Success, Try}
import scala.xml.XML
/**
* Created by Nacho on 12/6/16.
*/
object TransformerStep extends Step {
val logger: Logger = LoggerFactory.getLogger(this.getClass)
/**
* Transforms the data and metadata into GDM friendly formats using the transformers.
* Normalizes the metadata key names as indicated in the settings.
* Puts the schema file into the transformations folders.
*
* @param source source to be integrated.
* @param parallelExecution defines if the execution is in parallel or sequential
*/
override def execute(source: xml.Source, parallelExecution: Boolean): Unit = {
if (source.transformEnabled) {
logger.info("Starting integration for: " + source.outputFolder)
val sourceId = FileDatabase.sourceId(source.name)
//I load the renaming list from the source parameters.
val metadataRenaming: Seq[(Regex, String)] =
try {
(XML.loadFile(source.rootOutputFolder + File.separator + source.parameters
.filter(_._1.equalsIgnoreCase("metadata_replacement")).head._2) \\ "metadata_replace_list" \ "metadata_replace")
.map(replacement => ((replacement \ "regex").text.r, (replacement \ "replace").text))
}
catch {
case _: IOException =>
logger.error("not valid metadata replacement xml file: ")
Seq.empty
case _: NoSuchElementException =>
logger.info("no metadata replacement defined for: " + source.name)
Seq.empty
}
//counters
var modifiedRegionFilesSource = 0
var modifiedMetadataFilesSource = 0
var wrongSchemaFilesSource = 0
//integration process for each dataset contained in the source.
val integrateThreads = source.datasets.map((dataset: Dataset) => {
new Thread {
override def run(): Unit = {
if (dataset.transformEnabled) {
val transformationClass = Class
.forName(source.transformer)
.newInstance.asInstanceOf[Transformer]
val t0Dataset: Long = System.nanoTime()
var modifiedRegionFilesDataset = 0
var modifiedMetadataFilesDataset = 0
var wrongSchemaFilesDataset = 0
var totalTransformedFiles = 0
val datasetId = FileDatabase.datasetId(sourceId, dataset.name)
val datasetOutputFolder = dataset.fullDatasetOutputFolder
val downloadsFolder = datasetOutputFolder + File.separator + "Downloads"
val transformationsFolder = datasetOutputFolder + File.separator + "Transformations"
val folder = new File(transformationsFolder)
if (folder.exists()) {
deleteFolder(folder)
}
logger.info("Starting download for: " + dataset.name)
// puts the schema into the transformations folder.
if (SchemaFinder.downloadSchema(source.rootOutputFolder, dataset, transformationsFolder, source))
logger.debug("Schema downloaded for: " + dataset.name)
else
logger.warn("Schema not found for: " + dataset.name)
if (!folder.exists()) {
folder.mkdirs()
logger.debug("Folder created: " + folder)
}
logger.info("Transformation for dataset: " + dataset.name)
FileDatabase.delete(datasetId, Stage.TRANSFORM)
//id, filename, copy number.
val candidates = {
val tempCandidates: List[((String, Int), (Int, String, Int))] = FileDatabase.getFilesToProcess(datasetId, Stage.DOWNLOAD).toList.flatMap { file =>
val originalFileName =
if (file._3 == 1) file._2
else file._2.replaceFirst("\\.", "_" + file._3 + ".")
val fileDownloadPath = downloadsFolder + File.separator + originalFileName
val candidates = transformationClass.getCandidateNames(originalFileName, dataset, source)
logger.info(s"candidates: $originalFileName, $candidates")
val files = candidates.map(candidateName => {
(candidateName, FileDatabase.fileId(datasetId, fileDownloadPath, Stage.TRANSFORM, candidateName))
})
files.map((_, file))
}
// select the candidates which has cooresponding candidate (region-> meta or meta->region
val candidateNameSet = tempCandidates.map(_._1._1).toSet
tempCandidates.filter { case ((candidateName, _), _) =>
if (candidateName.endsWith(".meta"))
candidateNameSet.contains(candidateName.substring(0, candidateName.length - 5))
else
candidateNameSet.contains(candidateName + ".meta")
}
}.sortBy(_._1._1)
// candidates.foreach(t => println(t._1._1))
logger.info("-------------------------")
// candidates.foreach(t => logger.info(s"all candidates: $t"))
val filesToTransform = candidates.length
candidates.foreach { case ((candidateName, fileId), file) =>
val originalFileName =
if (file._3 == 1) file._2
else file._2.replaceFirst("\\.", "_" + file._3 + ".")
val fileNameAndCopyNumber = FileDatabase.getFileNameAndCopyNumber(fileId)
val name =
if (fileNameAndCopyNumber._2 == 1) fileNameAndCopyNumber._1
else fileNameAndCopyNumber._1.replaceFirst("\\.", "_" + fileNameAndCopyNumber._2 + ".")
val originDetails = FileDatabase.getFileDetails(file._1)
//I always transform, so the boolean checkIfUpdate is not used here.
FileDatabase.checkIfUpdateFile(fileId, originDetails._1, originDetails._2, originDetails._3)
val transformed = transformationClass.transform(source, downloadsFolder, transformationsFolder, originalFileName, name)
val fileTransformationPath = transformationsFolder + File.separator + name
//add copy numbers if needed.
if (transformed) {
if (name.endsWith(".meta")) {
val separator =
if (source.parameters.exists(_._1 == "metadata_name_separation_char"))
source.parameters.filter(_._1 == "metadata_name_separation_char").head._2
else
"__"
// enrich metadata file
{
val regionFileId = candidates.find {
case ((candidateNameTemp, _), _) => candidateName.substring(0, candidateName.length - 5) == candidateNameTemp
}.map(_._2._1).getOrElse(file._1)
val fileDetailsOption = FileDatabase.getFileAllDetails(regionFileId)
val (fileSize, md5) = {
val regionFileName = fileTransformationPath.substring(0, fileTransformationPath.length - 5)
println("regionFileName: [" + regionFileName + "]")
val file = new File(regionFileName)
val fileSize = file.length()
val fis = new FileInputStream(file)
val md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis)
fis.close()
(fileSize, md5)
}
val writer = new FileWriter(fileTransformationPath, true)
writer.write("manually_curated" + separator + "local_file_size\t" + fileSize + "\n")
writer.write("manually_curated" + separator + "local_md5\t" + md5 + "\n")
if (fileDetailsOption.isDefined) {
val fileDetails = fileDetailsOption.get
if (fileDetails.lastUpdate.nonEmpty)
writer.write("manually_curated" + separator + "download_date\t" + fileDetails.lastUpdate + "\n")
if (fileDetails.hash.nonEmpty)
writer.write("manually_curated" + separator + "origin_md5\t" + fileDetails.hash + "\n")
if (fileDetails.originLastUpdate.nonEmpty)
writer.write("manually_curated" + separator + "origin_last_modified_date\t" + fileDetails.originLastUpdate + "\n")
if (fileDetails.originSize.nonEmpty)
writer.write("manually_curated" + separator + "origin_file_size\t" + fileDetails.originSize + "\n")
}
val maxCopy = FileDatabase.getMaxCopyNumber(datasetId, file._2, Stage.DOWNLOAD)
if (maxCopy > 1) {
writer.write("manually_curated" + separator + "file_copy_total_count\t" + maxCopy + "\n")
writer.write("manually_curated" + separator + "file_copy_number\t" + file._3 + "\n")
writer.write("manually_curated" + separator + "file_name_replaced\ttrue\n")
}
writer.close()
}
//metadata renaming. (standardizing of the metadata values should happen here also.
if ( /*!metadataRenaming.isEmpty &&*/ changeMetadataKeys(metadataRenaming, fileTransformationPath))
modifiedMetadataFilesDataset = modifiedMetadataFilesDataset + 1
totalTransformedFiles = totalTransformedFiles + 1
}
//if not meta, is region data.modifiedMetadataFilesDataset+modifiedRegionFilesDataset
else {
/*val dataUrl = FileDatabase.getFileDetails(fileId)._4*/
/*val metaUrl = if (dataset.parameters.exists(_._1 == "spreadsheet_url"))
dataset.parameters.filter(_._1 == "region_sorting").head._2
else ""
val fw = new FileWriter(fileTransformationPath + ".meta", true)
try {
fw.write(metaUrl)
}
finally fw.close()*/
val schemaFilePath = transformationsFolder + File.separator + dataset.name + ".schema"
val modifiedAndSchema = checkRegionData(fileTransformationPath, schemaFilePath)
if (modifiedAndSchema._1)
modifiedRegionFilesDataset = modifiedRegionFilesDataset + 1
if (!modifiedAndSchema._2)
wrongSchemaFilesDataset = wrongSchemaFilesDataset + 1
totalTransformedFiles = totalTransformedFiles + 1
if (!dataset.parameters.exists(_._1 == "region_sorting") || dataset.parameters.filter(_._1 == "region_sorting").head._2 == "true")
if (Try(regionFileSort(fileTransformationPath)).isFailure)
logger.warn(s"fail to sort $fileTransformationPath")
else
logger.debug(s"$fileTransformationPath successfully sorted")
}
//standardization of the region data should be here.
FileDatabase.markAsUpdated(fileId, new File(fileTransformationPath).length.toString)
}
else
FileDatabase.markAsFailed(fileId)
}
FileDatabase.markAsOutdated(datasetId, Stage.TRANSFORM)
// FileDatabase.markAsProcessed(datasetId, STAGE.DOWNLOAD)
FileDatabase.runDatasetTransformAppend(datasetId, dataset, filesToTransform, totalTransformedFiles)
modifiedMetadataFilesSource = modifiedMetadataFilesSource + modifiedMetadataFilesDataset
modifiedRegionFilesSource = modifiedRegionFilesSource + modifiedRegionFilesDataset
wrongSchemaFilesSource = wrongSchemaFilesSource + wrongSchemaFilesDataset
logger.info(modifiedRegionFilesDataset + " region data files modified in dataset: " + dataset.name)
logger.info(modifiedMetadataFilesDataset + " metadata files modified in dataset: " + dataset.name)
logger.info(wrongSchemaFilesDataset + " region data files do not respect the schema in dataset: " + dataset.name)
val t1Dataset = System.nanoTime()
logger.info(s"Total time for transformation dataset ${dataset.name}: ${getTotalTimeFormatted(t0Dataset, t1Dataset)}")
DatasetNameUtil.saveDatasetName(dataset)
}
}
}
})
if (parallelExecution) {
integrateThreads.foreach(_.start())
integrateThreads.foreach(_.join())
}
else {
for (thread <- integrateThreads) {
thread.run()
// thread.join()
}
}
logger.info(modifiedRegionFilesSource + " region data files modified in source: " + source.name)
logger.info(modifiedMetadataFilesSource + " metadata files modified in source: " + source.name)
logger.info(wrongSchemaFilesSource + " region data files do not respect the schema in source: " + source.name)
logger.info(s"Source ${source.name} transformation finished")
}
}
/**
* Checks the region data file has the columns corresponding to the schema, tries to parse the data
* according to the schema types and normalizes values in the region data fields.
*
* @param dataFilePath path of the region data file.
* @param schemaFilePath path of the schema file.
* @return if the file was correctly modified and if it fits the schema.
*/
def checkRegionData(dataFilePath: String, schemaFilePath: String): (Boolean, Boolean) = {
//first I read the schema.
if (new File(schemaFilePath).exists()) {
val continue = {
try {
(XML.loadFile(schemaFilePath) \\ "field").nonEmpty
true
}
catch {
case _: Exception => false
}
}
if (continue) {
//load the schema in memory
val fields: Seq[(String, String)] = (XML.loadFile(schemaFilePath) \\ "field").map(field => (field.text, (field \ "@type").text))
val nodeSeq = XML.loadFile(schemaFilePath) \\ "gmqlSchema" \ "@type"
//detect schema type
val schemaType = if (nodeSeq.isEmpty)
"tab"
else if (nodeSeq.head.toString().toLowerCase.matches("tab|gtf"))
nodeSeq.head.toString().toLowerCase
else {
logger.warn(s"$schemaFilePath gmqlSchema type unknown, trying to treat it as a TAB")
"tab"
}
//generate a temp file
val tempFile = dataFilePath + ".temp"
val writer = new PrintWriter(tempFile)
//event register
val missingValueCount = Array.fill(fields.size)(0)
val typeMismatchCount = Array.fill(fields.size)(0)
var wrongAttNumCount = 0
var strandBadValCount = 0
var chromBadValCount = 0
var gtfMandatoryMissing = 0
var gtfOptionalMissing = 0
val gtfOptionalWrongName = Array.fill(fields.size)(0)
val gtfOptionalWrongFormt = Array.fill(fields.size)(0)
//only replaces if everything goes right, if there is an error, we do not replace.
var correctSchema = true
var modified = false
//scanning the file to check the consistency of each row to schema and manage missing value
val reader = Source.fromFile(dataFilePath)
reader.getLines().foreach(f = line => {
var writeLine = ""
var isRemoved = false
val splitLine = line.split("\t", -1)
val regAttributes = if (schemaType == "gtf") {
//check if there are all the mandatory attribute
val optionalValues = ListBuffer[String]()
if (splitLine.length != 9) {
gtfMandatoryMissing += 1
isRemoved = true
}
else {
//check if the last mandatory attribute containing the optional attributes is in the correct format
val optionalAttributes = splitLine(8).split("; |;")
//check if there are all the optional attribute
if (optionalAttributes.length + splitLine.length - 1 != fields.length) {
gtfOptionalMissing += 1
isRemoved = true
}
optionalAttributes.foreach(optAttribute => {
//check the format of optional attributes and extract the value
if (optAttribute.matches(""".+ ".*"""")) {
val optAttributeSplit = optAttribute.split(" ")
//check the optional attribute name
if (optAttributeSplit(0) != fields(optionalAttributes.indexOf(optAttribute) + 8)._1)
gtfOptionalWrongName(optionalAttributes.indexOf(optAttribute) + 8) += 1
optionalValues += optAttributeSplit(1).dropRight(1).drop(1)
}
else if (optAttribute.matches(""".+ (^["].*^["]|^["])?""")) {
val optAttributeSplit = optAttribute.split(" ")
if (optAttributeSplit(0) != fields(optionalAttributes.indexOf(optAttribute) + 8)._1)
gtfOptionalWrongName(optionalAttributes.indexOf(optAttribute) + 8) += 1
if (optAttributeSplit.length > 1)
optionalValues += optAttributeSplit(1)
else
optionalValues += ""
}
else {
gtfOptionalWrongFormt(optionalValues.indexOf(optAttribute) + 8) += 1
optionalValues += ""
}
})
}
splitLine.dropRight(1) ++ optionalValues
}
else {
if (splitLine.length != fields.length) {
wrongAttNumCount += 1
isRemoved = true
}
splitLine
}
if (!isRemoved) { //check if number of region attributes is consistent to schema
for (i <- 0 until regAttributes.size) {
//managing missing value
if (regAttributes(i) == "" || regAttributes(i).toUpperCase == "NULL" || regAttributes(i).toUpperCase == "N/A" ||
regAttributes(i).toUpperCase == "NA" || (regAttributes(i) == "." && fields(i)._1 == "score")) {
missingValueCount(i) += 1
val oldValue = regAttributes(i)
//some attribute must be treated in different way if missing
if (fields(i)._1.toLowerCase == "score" || (schemaType == "gtf" && i < 8))
regAttributes(i) = "."
else if (fields(i)._2.toUpperCase == "STRING" || fields(i)._2.toUpperCase == "CHAR")
regAttributes(i) = ""
else
regAttributes(i) = "NULL"
//check if the missing value has been modified
if (oldValue != regAttributes(i))
modified = true
}
else {
//type consistency check
val typeMatch = fields(i)._2.toUpperCase match {
case "LONG" => Try {
regAttributes(i).toLong
}
case "DOUBLE" => Try {
regAttributes(i).toDouble
}
case "INTEGER" => Try {
regAttributes(i).toInt
}
case "CHAR" => if (regAttributes(i).length == 1) Success(regAttributes(i)) else Failure(new Exception("Not a char"))
case "STRING" => Success(regAttributes(i))
case _ => Failure(new Exception("Region attribute invalid type in schema"))
}
typeMatch match {
case Success(_) => //specific attribute value check
//strand value check
val strandAttributeNames: List[String] = List("strand")
if (strandAttributeNames.contains(fields(i)._1.toLowerCase))
regAttributes(i) match {
case "+" | "-" | "*" | "." =>
case _ =>
regAttributes(i) = "."
strandBadValCount += 1
modified = true
}
//chromosome value check
val chromAttributeNames: List[String] = List("seqname", "seqnames", "chr", "chrom", "chromosome")
if (chromAttributeNames.contains(fields(i)._1.toLowerCase)) {
val validChrom = "chr[0-9A-Z]{1,2}".r
val invalidChrom1 = "[0-9A-Z]{1,2}".r
val invalidChrom2 = "[cC][hH][rR][0-9A-Za-z]{1,2}".r
regAttributes(i) match {
case validChrom() =>
case invalidChrom1() =>
regAttributes(i) = "chr" + regAttributes(i)
modified = true
case invalidChrom2() => regAttributes(i) = "chr" + regAttributes(i).drop(3).toUpperCase
modified = true
case _ => chromBadValCount += 1
}
}
case Failure(_) => //action to perform in case of type mismatch
typeMismatchCount(i) += 1
correctSchema = false
}
}
//write region on temp file
writeLine = writeLine + (if (i == 0) regAttributes(i)
else if (i >= 8 && schemaType == "gtf")
if (i == 8) s"""\t${fields(i)._1} "${regAttributes(i)}"""" else s"""; ${fields(i)._1} "${regAttributes(i)}""""
else
"\t" + regAttributes(i))
}
writeLine = writeLine + "\n"
writer.write(writeLine)
}
else {
modified = true //schema still correct because the wrong line is removed.
}
})
reader.close()
writer.close()
//if there are changes to the lines, should replace the file.
if (modified) {
//replace the file, and remove the temporary one.
try {
val df = new File(dataFilePath)
val tf = new File(tempFile)
df.delete
tf.renameTo(df)
}
catch {
case _: IOException => logger.warn("could not change the file " + dataFilePath)
}
}
else {
new File(tempFile).delete()
}
if (gtfMandatoryMissing > 0)
logger.info(s"In $dataFilePath: $gtfMandatoryMissing lines with wrong numbers of mandatory attributes removed.")
if (gtfOptionalMissing > 0)
logger.info(s"In $dataFilePath: $gtfOptionalMissing lines with wrong numbers of optional attributes removed.")
if (wrongAttNumCount > 0)
logger.info(s"In $dataFilePath: $wrongAttNumCount lines with wrong numbers of attributes removed.")
for (i <- fields.indices)
if (missingValueCount(i) > 0)
logger.info(s"In $dataFilePath attribute ${fields(i)._1}: ${missingValueCount(i)} missing value.")
for (i <- fields.indices)
if (typeMismatchCount(i) > 0)
logger.info(s"In $dataFilePath attribute ${fields(i)._1}: ${typeMismatchCount(i)} type mismatch.")
for (i <- fields.indices)
if (gtfOptionalWrongFormt(i) > 0)
logger.info(s"In $dataFilePath optional attribute ${fields(i)._1}: ${gtfOptionalWrongFormt(i)} wrong optional attribute format.")
for (i <- fields.indices)
if (gtfOptionalWrongName(i) > 0)
logger.info(s"In $dataFilePath attribute ${fields(i)._1}: ${gtfOptionalWrongName(i)} wrong optional attribute name.")
if (strandBadValCount > 0)
logger.info(s"In $dataFilePath: $strandBadValCount lines with invalid strand value replaced with default value.")
if (chromBadValCount > 0)
logger.info(s"In $dataFilePath: $chromBadValCount lines with invalid chrom value.")
(modified, correctSchema)
}
else
(false, false)
}
else
(false, false)
}
/**
* changes the name of key attribute in metadata.
* replaces all parts of the key value that matches a regular expression.
*
* @param changeKeys pair regex, replacement. uses regex.replaceAll(_1,_2)
* @param metadataFilePath origin file of metadata.
*/
def changeMetadataKeys(changeKeys: Seq[(Regex, String)], metadataFilePath: String): Boolean = {
var replaced = false
if (new File(metadataFilePath).exists()) {
var metadataList: Seq[(String, String)] = Seq[(String, String)]()
val reader = Source.fromFile(metadataFilePath)
reader.getLines().foreach(line => {
val split = line.split("\t", -1)
if (split.length == 2) {
var metadataKey = split(0)
val metadataValue = split(1)
changeKeys.filter(change => change._1.findFirstIn(metadataKey).isDefined).foreach(change => {
replaced = true
metadataKey = change._1.replaceFirstIn(metadataKey, change._2)
})
//this is already handled in metadataReplacementTcga.xml
if (metadataKey.contains(" ")) {
logger.debug(s"$metadataKey replaced to ${metadataKey.replace(" ", "_")}")
metadataKey = metadataKey.replace(" ", "_")
}
if (metadataKey.contains("|")) {
logger.debug(s"$metadataKey replaced to ${metadataKey.replace("|", "__")}")
metadataKey = metadataKey.replace("|", "__")
}
//Ensure metadata keys are in lowercase perche vogliamo cosi -- mm
metadataKey = metadataKey.toLowerCase
if (!isValidJavaIdentifier(metadataKey)) {
logger.debug(s"$metadataKey replaced to ${transformToValidJavaIdentifier(metadataKey)}")
metadataKey = transformToValidJavaIdentifier(metadataKey)
}
if (!metadataList.contains((metadataKey, metadataValue))) {
metadataList = metadataList :+ (metadataKey, metadataValue)
}
}
else {
logger.warn("file: " + metadataFilePath + " should have 2 columns. Check this line that was excluded: " + line)
}
})
reader.close()
val tempFile = metadataFilePath + ".temp"
val writer = new PrintWriter(tempFile)
metadataList.sortWith((A, B) => A._1.compare(B._1) < 0).foreach(metadata => {
writer.write(metadata._1 + "\t" + metadata._2 + "\n")
})
writer.close()
try {
val df = new File(metadataFilePath)
val tf = new File(tempFile)
df.delete
tf.renameTo(df)
}
catch {
case _: IOException => logger.error("could not change metadata key on the file " + metadataFilePath)
}
}
replaced
}
/**
* gets the time between 2 timestamps in hh:mm:ss format
*
* @param t0 start time
* @param t1 end time
* @return hh:mm:ss as string
*/
def getTotalTimeFormatted(t0: Long, t1: Long): String = {
val hours = Integer.parseInt("" + (t1 - t0) / 1000000000 / 60 / 60)
val minutes = Integer.parseInt("" + ((t1 - t0) / 1000000000 / 60 - hours * 60))
val seconds = Integer.parseInt("" + ((t1 - t0) / 1000000000 - hours * 60 * 60 - minutes * 60))
s"$hours:$minutes:$seconds"
}
/**
* Traverses a string checking all characters are valid to be java identifiers.
*
* @param s candidate name for identifier
* @return whethers the candidate name is valid or not.
*/
def isValidJavaIdentifier(s: String): Boolean = {
var isValid = true
if (s == "")
isValid = false
else if (!Character.isJavaIdentifierStart(s.charAt(0)))
isValid = false
for (i <- 1 until s.length)
if (!Character.isJavaIdentifierPart(s.charAt(i))) {
isValid = false
}
isValid
}
/**
* makes sure the name of a metadata attribute is valid for java instantiation
*
* @param s candidate name for attribute.
* @return name corrected if needed.
*/
def transformToValidJavaIdentifier(s: String): String = {
var output = s.map { c => if (Character.isJavaIdentifierPart(c)) c else '_' }
if (!Character.isJavaIdentifierStart(output.head))
output = output.replaceFirst(output.head.toString, "_")
output
}
/**
* deletes folder recursively
*
* @param path base folder path
*/
//TODO move to a util object, used in many steps
def deleteFolder(path: File): Unit = {
try {
if (path.exists()) {
try {
val files = path.listFiles()
files.foreach(file => {
try {
if (file.isDirectory)
deleteFolder(file)
else
try {
file.delete()
}
catch {
case ex: SecurityException => logger.warn(s"Couldn't delete $path: ${ex.getMessage}")
}
}
catch {
case ex: SecurityException => logger.warn(s"Couldn't access $path: ${ex.getMessage}")
}
})
}
catch {
case ex: SecurityException => logger.warn(s"Couldn't list files from $path: ${ex.getMessage}")
}
}
}
catch {
case ex: SecurityException => logger.warn(s"Couldn't delete folder $path: ${ex.getMessage}")
}
}
/**
* sort region by chrom, start and stop coordinates
*
* @param filePath file to sort
*/
def regionFileSort(filePath: String): Unit = {
var tempMap: TreeMap[(String, Long, Long), mutable.Queue[String]] = new TreeMap[(String, Long, Long), mutable.Queue[String]]
val reader = Source.fromFile(filePath)
for (line <- reader.getLines) {
val lineSplit = line.split("\t")
val regionID = (lineSplit(0), lineSplit(1).toLong, lineSplit(2).toLong)
if (tempMap.contains(regionID))
tempMap(regionID) += line
else
tempMap = tempMap + ((regionID, mutable.Queue(line)))
}
reader.close()
using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("result"))))) {
writer => {
tempMap.foreach(pair =>
while (pair._2.nonEmpty) writer.write(pair._2.dequeue() + "\n"))
}
}
}
def using[T <: Closeable, R](resource: T)(block: T => R): R = {
try {
block(resource)
}
finally {
resource.close()
}
}
}