Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Mirrors
biopet.biopet
Commits
5aedab83
Commit
5aedab83
authored
Apr 14, 2015
by
Peter van 't Hof
Browse files
Added a option to allow fallback on strings
parent
25aaee6e
Changes
2
Hide whitespace changes
Inline
Side-by-side
public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/extensions/picard/Picard.scala
View file @
5aedab83
...
...
@@ -90,7 +90,7 @@ object Picard {
val
header
=
lines
(
start
).
split
(
"\t"
)
val
content
=
(
for
(
i
<-
(
start
+
1
)
until
end
)
yield
lines
(
i
).
split
(
"\t"
))
.
map
(
row
=>
row
.
map
(
col
=>
tryToParseNumber
(
col
).
getOrElse
(
col
)))
.
map
(
row
=>
row
.
map
(
col
=>
tryToParseNumber
(
col
,
true
).
getOrElse
(
col
)))
Option
((
header
,
content
.
toList
))
}
else
{
...
...
public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala
View file @
5aedab83
...
...
@@ -25,13 +25,10 @@ package object utils {
* @param funcs one or more conversion functions to apply.
* @return a [[Try]] object encapsulating the conversion result.
*/
def
tryToConvert
(
raw
:
String
,
funcs
:
(
String
=>
Any
)*)
:
Try
[
Any
]
=
funcs
match
{
case
Seq
(
firstFunc
,
otherFuncs
@
_
*)
=>
Try
(
firstFunc
(
raw
))
.
transform
(
s
=>
Success
(
s
),
f
=>
tryToConvert
(
raw
,
otherFuncs
:
_
*
))
case
Nil
=>
Try
(
throw
new
Exception
(
s
"Can not extract value from string $raw"
))
def
tryToConvert
(
raw
:
String
,
funcs
:
(
String
=>
Any
)*)
:
Try
[
Any
]
=
{
if
(
funcs
.
isEmpty
)
Try
(
throw
new
Exception
(
s
"Can not extract value from string $raw"
))
else
Try
(
funcs
.
head
(
raw
))
.
transform
(
s
=>
Success
(
s
),
f
=>
tryToConvert
(
raw
,
funcs
.
tail
:
_
*
))
}
/**
...
...
@@ -44,16 +41,13 @@ package object utils {
* a [[Double]], then a [[BigDecimal]].
*
* @param raw the string to convert.
* @param fallBack Allows also to return the string itself when converting fails, default false.
* @return a [[Try]] object encapsulating the conversion result.
*/
def
tryToParseNumber
(
raw
:
String
)
=
raw
match
{
case
isInteger
(
i
)
=>
tryToConvert
(
i
,
x
=>
x
.
toInt
,
x
=>
x
.
toLong
,
x
=>
BigInt
(
x
))
case
isDecimal
(
f
)
=>
tryToConvert
(
f
,
x
=>
x
.
toDouble
,
x
=>
BigDecimal
(
x
))
case
otherwise
=>
Try
(
throw
new
Exception
(
s
"Can not extract number from string $raw"
))
def
tryToParseNumber
(
raw
:
String
,
fallBack
:
Boolean
=
false
)
=
raw
match
{
case
isInteger
(
i
)
=>
tryToConvert
(
i
,
x
=>
x
.
toInt
,
x
=>
x
.
toLong
,
x
=>
BigInt
(
x
))
case
isDecimal
(
f
)
=>
tryToConvert
(
f
,
x
=>
x
.
toDouble
,
x
=>
BigDecimal
(
x
))
case
_
if
fallBack
=>
Try
(
raw
)
case
_
=>
Try
(
throw
new
Exception
(
s
"Can not extract number from string $raw"
))
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment