From 2ecea20471ca31703284f4239ac43fd09674ec8c Mon Sep 17 00:00:00 2001
From: Peter van 't Hof <p.j.van_t_hof@lumc.nl>
Date: Sun, 23 Aug 2015 13:13:04 +0200
Subject: [PATCH] Added validation

---
 .../sasc/biopet/utils/intervals/BedRecord.scala  | 16 ++++++++++++++++
 .../biopet/utils/intervals/BedRecordList.scala   | 12 +++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecord.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecord.scala
index 4c50cc857..c395d0bc5 100644
--- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecord.scala
+++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecord.scala
@@ -60,6 +60,22 @@ case class BedRecord(chr: String,
       .flatten
       .mkString("\t")
   }
+
+  def validate = {
+    require(start <= end, "Start is greater then end")
+    (thickStart, thickEnd) match {
+      case (Some(s), Some(e)) => require(s <= e, "Thick start is greater then end")
+      case _ =>
+    }
+    blockCount match {
+      case Some(count) => {
+        require(count == blockSizes.length, "Number of sizes is not the same as blockCount")
+        require(count == blockStarts.length, "Number of starts is not the same as blockCount")
+      }
+      case _ =>
+    }
+    this
+  }
 }
 
 object BedRecord {
diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecordList.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecordList.scala
index 906f90df1..783f32481 100644
--- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecordList.scala
+++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/intervals/BedRecordList.scala
@@ -70,7 +70,17 @@ object BedRecordList {
   }
 
   def fromFile(bedFile: File) = {
-    fromList(Source.fromFile(bedFile).getLines().map(BedRecord.fromLine(_)))
+    var lineCount = 0L
+    fromList(Source.fromFile(bedFile).getLines().map(line => {
+      lineCount += 1
+      try {
+        BedRecord.fromLine(line).validate
+      } catch {
+        case e: Exception =>
+          Logging.logger.error(s"Parsing line number $lineCount failed on file: ${bedFile.getAbsolutePath}")
+          throw e
+      }
+    }))
   }
 
   def combineOverlap(list: BedRecordList): BedRecordList = {
-- 
GitLab