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
eea29773
Commit
eea29773
authored
8 years ago
by
Wai Yi Leung
Browse files
Options
Downloads
Patches
Plain Diff
BamUtils Insertsize estimation is more acurate by sampling from all over the contig.
parent
34ffaaf0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
public/biopet-utils/src/main/scala/nl/lumc/sasc/biopet/utils/BamUtils.scala
+36
-21
36 additions, 21 deletions
...s/src/main/scala/nl/lumc/sasc/biopet/utils/BamUtils.scala
with
36 additions
and
21 deletions
public/biopet-utils/src/main/scala/nl/lumc/sasc/biopet/utils/BamUtils.scala
+
36
−
21
View file @
eea29773
...
...
@@ -18,6 +18,7 @@ package nl.lumc.sasc.biopet.utils
import
java.io.File
import
htsjdk.samtools.
{
SamReader
,
SamReaderFactory
}
import
nl.lumc.sasc.biopet.utils.intervals.
{
BedRecord
,
BedRecordList
}
import
scala.collection.JavaConversions._
import
scala.collection.parallel.immutable
...
...
@@ -57,24 +58,38 @@ object BamUtils {
* @param end postion to stop scanning
* @return Int with insertsize for this contig
*/
def
contigInsertSize
(
inputBam
:
File
,
contig
:
String
,
start
:
Int
,
end
:
Int
,
samplingSize
:
Int
=
100000
)
:
Option
[
Int
]
=
{
val
inputSam
:
SamReader
=
SamReaderFactory
.
makeDefault
.
open
(
inputBam
)
val
samIterator
=
inputSam
.
query
(
contig
,
start
,
end
,
true
)
val
insertsizes
:
List
[
Int
]
=
(
for
{
read
<-
samIterator
.
toStream
.
takeWhile
(
rec
=>
{
val
paired
=
rec
.
getReadPairedFlag
&&
rec
.
getProperPairFlag
val
bothMapped
=
if
(
paired
)
((
rec
.
getReadUnmappedFlag
==
false
)
&&
(
rec
.
getMateUnmappedFlag
==
false
))
else
false
paired
&&
bothMapped
}).
take
(
samplingSize
)
}
yield
{
read
.
getInferredInsertSize
.
asInstanceOf
[
Int
].
abs
})(
collection
.
breakOut
)
val
cti
=
insertsizes
.
foldLeft
((
0.0
,
0
))((
t
,
r
)
=>
(
t
.
_1
+
r
,
t
.
_2
+
1
))
def
contigInsertSize
(
inputBam
:
File
,
contig
:
String
,
start
:
Int
,
end
:
Int
,
samplingSize
:
Int
=
10000
,
binSize
:
Int
=
1000000
)
:
Option
[
Int
]
=
{
samIterator
.
close
()
inputSam
.
close
()
val
ret
=
if
(
cti
.
_2
==
0
)
None
else
Some
((
cti
.
_1
/
cti
.
_2
).
toInt
)
ret
// create a bedList to devide the contig into multiple pieces
val
insertSizesOnAllFragments
=
BedRecordList
.
fromList
(
Seq
(
BedRecord
(
contig
,
start
,
end
)))
.
scatter
(
binSize
)
.
allRecords
.
par
.
map
({
bedRecord
=>
// for each scatter, open the bamfile for this specific region-query
val
inputSam
:
SamReader
=
SamReaderFactory
.
makeDefault
.
open
(
inputBam
)
val
samIterator
=
inputSam
.
query
(
bedRecord
.
chr
,
bedRecord
.
start
,
bedRecord
.
end
,
true
)
val
insertsizes
:
List
[
Int
]
=
(
for
{
read
<-
samIterator
.
toStream
.
takeWhile
(
rec
=>
{
val
minQ10
=
rec
.
getMappingQuality
>=
10
val
paired
=
rec
.
getReadPairedFlag
&&
rec
.
getProperPairFlag
val
bothMapped
=
if
(
paired
)
((
rec
.
getReadUnmappedFlag
==
false
)
&&
(
rec
.
getMateUnmappedFlag
==
false
))
else
false
paired
&&
bothMapped
&&
minQ10
}).
take
(
samplingSize
)
}
yield
{
read
.
getInferredInsertSize
.
asInstanceOf
[
Int
].
abs
})(
collection
.
breakOut
)
val
cti
=
insertsizes
.
foldLeft
((
0.0
,
0
))((
t
,
r
)
=>
(
t
.
_1
+
r
,
t
.
_2
+
1
))
samIterator
.
close
()
inputSam
.
close
()
if
(
cti
.
_2
==
0
)
None
else
Some
((
cti
.
_1
/
cti
.
_2
).
toInt
)
}).
toList
.
flatten
val
contigInsertSize
=
insertSizesOnAllFragments
.
foldLeft
((
0.0
,
0
))((
t
,
r
)
=>
(
t
.
_1
+
r
,
t
.
_2
+
1
))
if
(
contigInsertSize
.
_2
==
0
)
None
else
Some
((
contigInsertSize
.
_1
/
contigInsertSize
.
_2
).
toInt
)
}
/**
...
...
@@ -83,10 +98,10 @@ object BamUtils {
* @param bamFile bamfile to estimate avg insertsize from
* @return
*/
def
sampleBamInsertSize
(
bamFile
:
File
,
samplingSize
:
Int
=
100000
)
:
Int
=
{
def
sampleBamInsertSize
(
bamFile
:
File
,
samplingSize
:
Int
=
10000
,
binSize
:
Int
=
100000
0
)
:
Int
=
{
val
inputSam
:
SamReader
=
SamReaderFactory
.
makeDefault
.
open
(
bamFile
)
val
baminsertsizes
=
inputSam
.
getFileHeader
.
getSequenceDictionary
.
getSequences
.
par
.
map
({
contig
=>
BamUtils
.
contigInsertSize
(
bamFile
,
contig
.
getSequenceName
,
1
,
contig
.
getSequenceLength
,
samplingSize
)
contig
=>
BamUtils
.
contigInsertSize
(
bamFile
,
contig
.
getSequenceName
,
1
,
contig
.
getSequenceLength
,
samplingSize
,
binSize
)
}).
toList
val
counts
=
baminsertsizes
.
flatMap
(
x
=>
x
)
...
...
@@ -103,8 +118,8 @@ object BamUtils {
* @param bamFiles input bam files
* @return
*/
def
sampleBamsInsertSize
(
bamFiles
:
List
[
File
],
samplingSize
:
Int
=
100000
)
:
immutable.ParMap
[
File
,
Int
]
=
bamFiles
.
par
.
map
{
bamFile
=>
bamFile
->
sampleBamInsertSize
(
bamFile
,
samplingSize
)
def
sampleBamsInsertSize
(
bamFiles
:
List
[
File
],
samplingSize
:
Int
=
10000
,
binSize
:
Int
=
100000
0
)
:
immutable.ParMap
[
File
,
Int
]
=
bamFiles
.
par
.
map
{
bamFile
=>
bamFile
->
sampleBamInsertSize
(
bamFile
,
samplingSize
,
binSize
)
}.
toMap
}
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