Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
biopet.biopet
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirrors
biopet.biopet
Commits
f4ee1f10
Commit
f4ee1f10
authored
9 years ago
by
Sander Bollen
Browse files
Options
Downloads
Patches
Plain Diff
Partial #196. Split up in functions.
parent
01d04462
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/SamplesTsvToJson.scala
+35
-30
35 additions, 30 deletions
...in/scala/nl/lumc/sasc/biopet/tools/SamplesTsvToJson.scala
with
35 additions
and
30 deletions
public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/SamplesTsvToJson.scala
+
35
−
30
View file @
f4ee1f10
...
...
@@ -40,41 +40,46 @@ object SamplesTsvToJson extends ToolCommand {
val
argsParser
=
new
OptParser
val
commandArgs
:
Args
=
argsParser
.
parse
(
args
,
Args
())
getOrElse
sys
.
exit
(
1
)
val
fileMaps
=
for
(
inputFile
<-
commandArgs
.
inputFiles
)
yield
{
val
reader
=
Source
.
fromFile
(
inputFile
)
val
lines
=
reader
.
getLines
().
toList
.
filter
(!
_
.
isEmpty
)
val
header
=
lines
.
head
.
split
(
"\t"
)
val
sampleColumn
=
header
.
indexOf
(
"sample"
)
val
libraryColumn
=
header
.
indexOf
(
"library"
)
if
(
sampleColumn
==
-
1
)
throw
new
IllegalStateException
(
"Sample column does not exist in: "
+
inputFile
)
val
jsonString
=
stringFromInputs
(
commandArgs
.
inputFiles
)
println
(
jsonString
)
}
def
mapFromFile
(
inputFile
:
File
)
:
Map
[
String
,
Any
]
=
{
val
reader
=
Source
.
fromFile
(
inputFile
)
val
lines
=
reader
.
getLines
().
toList
.
filter
(!
_
.
isEmpty
)
val
header
=
lines
.
head
.
split
(
"\t"
)
val
sampleColumn
=
header
.
indexOf
(
"sample"
)
val
libraryColumn
=
header
.
indexOf
(
"library"
)
if
(
sampleColumn
==
-
1
)
throw
new
IllegalStateException
(
"Sample column does not exist in: "
+
inputFile
)
val
sampleLibCache
:
mutable.Set
[(
String
,
Option
[
String
])]
=
mutable
.
Set
()
val
sampleLibCache
:
mutable.Set
[(
String
,
Option
[
String
])]
=
mutable
.
Set
()
val
librariesValues
:
List
[
Map
[
String
,
Any
]]
=
for
(
tsvLine
<-
lines
.
tail
)
yield
{
val
values
=
tsvLine
.
split
(
"\t"
)
require
(
header
.
length
==
values
.
length
,
"Number of columns is not the same as the header"
)
val
sample
=
values
(
sampleColumn
)
val
library
=
if
(
libraryColumn
!=
-
1
)
Some
(
values
(
libraryColumn
))
else
None
val
librariesValues
:
List
[
Map
[
String
,
Any
]]
=
for
(
tsvLine
<-
lines
.
tail
)
yield
{
val
values
=
tsvLine
.
split
(
"\t"
)
require
(
header
.
length
==
values
.
length
,
"Number of columns is not the same as the header"
)
val
sample
=
values
(
sampleColumn
)
val
library
=
if
(
libraryColumn
!=
-
1
)
Some
(
values
(
libraryColumn
))
else
None
//FIXME: this is a workaround, should be removed after fixing #180
if
(
sample
.
head
.
isDigit
||
library
.
forall
(
_
.
head
.
isDigit
))
throw
new
IllegalStateException
(
"Sample or library may not start with a number"
)
//FIXME: this is a workaround, should be removed after fixing #180
if
(
sample
.
head
.
isDigit
||
library
.
forall
(
_
.
head
.
isDigit
))
throw
new
IllegalStateException
(
"Sample or library may not start with a number"
)
if
(
sampleLibCache
.
contains
((
sample
,
library
)))
throw
new
IllegalStateException
(
s
"Combination of $sample and $library is found multiple times"
)
else
sampleLibCache
.
add
((
sample
,
library
))
val
valuesMap
=
(
for
(
t
<-
0
until
values
.
size
if
!
values
(
t
).
isEmpty
&&
t
!=
sampleColumn
&&
t
!=
libraryColumn
)
yield
header
(
t
)
->
values
(
t
)).
toMap
library
match
{
case
Some
(
lib
)
=>
Map
(
"samples"
->
Map
(
sample
->
Map
(
"libraries"
->
Map
(
library
->
valuesMap
))))
case
_
=>
Map
(
"samples"
->
Map
(
sample
->
valuesMap
))
}
if
(
sampleLibCache
.
contains
((
sample
,
library
)))
throw
new
IllegalStateException
(
s
"Combination of $sample and ${library.get} is found multiple times"
)
else
sampleLibCache
.
add
((
sample
,
library
))
val
valuesMap
=
(
for
(
t
<-
0
until
values
.
size
if
!
values
(
t
).
isEmpty
&&
t
!=
sampleColumn
&&
t
!=
libraryColumn
)
yield
header
(
t
)
->
values
(
t
)).
toMap
library
match
{
case
Some
(
lib
)
=>
Map
(
"samples"
->
Map
(
sample
->
Map
(
"libraries"
->
Map
(
library
->
valuesMap
))))
case
_
=>
Map
(
"samples"
->
Map
(
sample
->
valuesMap
))
}
librariesValues
.
foldLeft
(
Map
[
String
,
Any
]())((
acc
,
kv
)
=>
mergeMaps
(
acc
,
kv
))
}
val
map
=
fileMaps
.
foldLeft
(
Map
[
String
,
Any
]())((
acc
,
kv
)
=>
mergeMaps
(
acc
,
kv
))
val
json
=
mapToJson
(
map
)
println
(
json
.
spaces2
)
librariesValues
.
foldLeft
(
Map
[
String
,
Any
]())((
acc
,
kv
)
=>
mergeMaps
(
acc
,
kv
))
}
def
stringFromInputs
(
inputs
:
List
[
File
])
:
String
=
{
val
map
=
inputs
.
map
(
f
=>
mapFromFile
(
f
)).
foldLeft
(
Map
[
String
,
Any
]())((
acc
,
kv
)
=>
mergeMaps
(
acc
,
kv
))
mapToJson
(
map
).
spaces2
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment