/
FastqRecordConverter.scala
172 lines (151 loc) · 5.83 KB
/
FastqRecordConverter.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
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.adam.converters
import org.apache.hadoop.io.Text
import org.apache.spark.Logging
import org.bdgenomics.adam.rdd.ADAMContext._
import org.bdgenomics.formats.avro.{
AlignmentRecord,
Fragment,
Sequence
}
class FastqRecordConverter extends Serializable with Logging {
def convertPair(element: (Void, Text)): Iterable[AlignmentRecord] = {
val lines = element._2.toString.split('\n')
require(lines.length == 8, "Record has wrong format:\n" + element._2.toString)
// get fields for first read in pair
val firstReadName = lines(0).drop(1)
val firstReadSequence = lines(1)
val firstReadQualities = lines(3)
require(firstReadSequence.length == firstReadQualities.length,
"Read " + firstReadName + " has different sequence and qual length.")
// get fields for second read in pair
val secondReadName = lines(4).drop(1)
val secondReadSequence = lines(5)
val secondReadQualities = lines(7)
require(secondReadSequence.length == secondReadQualities.length,
"Read " + secondReadName + " has different sequence and qual length.")
// build and return iterators
Iterable(AlignmentRecord.newBuilder()
.setReadName(firstReadName)
.setSequence(firstReadSequence)
.setQual(firstReadQualities)
.setReadPaired(true)
.setProperPair(true)
.setReadNum(0)
.setReadNegativeStrand(null)
.setMateNegativeStrand(null)
.setPrimaryAlignment(null)
.setSecondaryAlignment(null)
.setSupplementaryAlignment(null)
.build(),
AlignmentRecord.newBuilder()
.setReadName(secondReadName)
.setSequence(secondReadSequence)
.setQual(secondReadQualities)
.setReadPaired(true)
.setProperPair(true)
.setReadNum(1)
.setReadNegativeStrand(null)
.setMateNegativeStrand(null)
.setPrimaryAlignment(null)
.setSecondaryAlignment(null)
.setSupplementaryAlignment(null)
.build())
}
def convertFragment(element: (Void, Text)): Fragment = {
val lines = element._2.toString.split('\n')
require(lines.length == 8, "Record has wrong format:\n" + element._2.toString)
// get fields for first read in pair
val firstReadName = lines(0).drop(1)
val firstReadSequence = lines(1)
val firstReadQualities = lines(3)
require(firstReadSequence.length == firstReadQualities.length,
"Read " + firstReadName + " has different sequence and qual length.")
// get fields for second read in pair
val secondReadName = lines(4).drop(1)
val secondReadSequence = lines(5)
val secondReadQualities = lines(7)
require(secondReadSequence.length == secondReadQualities.length,
"Read " + secondReadName + " has different sequence and qual length.")
require(firstReadName == secondReadName,
"Reads %s and %s in Fragment have different names.".format(firstReadName,
secondReadName))
// build and return record
Fragment.newBuilder()
.setReadName(firstReadName)
.setSequences(List(Sequence.newBuilder()
.setBases(firstReadSequence)
.setQualities(firstReadQualities)
.build(), Sequence.newBuilder()
.setBases(secondReadSequence)
.setQualities(secondReadQualities)
.build()))
.build()
}
def convertRead(element: (Void, Text),
recordGroupOpt: Option[String] = None,
setFirstOfPair: Boolean = false,
setSecondOfPair: Boolean = false): AlignmentRecord = {
val lines = element._2.toString.split('\n')
require(lines.length == 4, "Record has wrong format:\n" + element._2.toString)
def trimTrailingReadNumber(readName: String): String = {
if (readName.endsWith("/1")) {
if (setSecondOfPair) {
throw new Exception(
s"Found read name $readName ending in '/1' despite second-of-pair flag being set"
)
}
readName.dropRight(2)
} else if (readName.endsWith("/2")) {
if (setFirstOfPair) {
throw new Exception(
s"Found read name $readName ending in '/2' despite first-of-pair flag being set"
)
}
readName.dropRight(2)
} else {
readName
}
}
// get fields for first read in pair
val readName = trimTrailingReadNumber(lines(0).drop(1))
val readSequence = lines(1)
val readQualities = lines(3)
require(readSequence.length == readQualities.length,
"Read " + readName + " has different sequence and qual length.")
val builder = AlignmentRecord.newBuilder()
.setReadName(readName)
.setSequence(readSequence)
.setQual(readQualities)
.setReadPaired(setFirstOfPair || setSecondOfPair)
.setProperPair(null)
.setReadNum(
if (setFirstOfPair) 0
else if (setSecondOfPair) 1
else null
)
.setReadNegativeStrand(null)
.setMateNegativeStrand(null)
.setPrimaryAlignment(null)
.setSecondaryAlignment(null)
.setSupplementaryAlignment(null)
recordGroupOpt.foreach(builder.setRecordGroupName)
builder.build()
}
}