-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding fragment InFormatter for Bowtie tab6 format
- Loading branch information
Showing
6 changed files
with
263 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
adam-core/src/main/scala/org/bdgenomics/adam/rdd/fragment/Tab6InFormatter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* 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.rdd.fragment | ||
|
||
import java.io.OutputStream | ||
import org.apache.hadoop.conf.Configuration | ||
import org.bdgenomics.adam.converters.AlignmentRecordConverter | ||
import org.bdgenomics.adam.rdd.{ InFormatter, InFormatterCompanion } | ||
import org.bdgenomics.formats.avro.Fragment | ||
import org.bdgenomics.utils.misc.Logging | ||
|
||
/** | ||
* InFormatter companion that creates an InFormatter that writes Bowtie tab6 format. | ||
*/ | ||
object Tab6InFormatter extends InFormatterCompanion[Fragment, FragmentRDD, Tab6InFormatter] { | ||
|
||
/** | ||
* Builds an Tab6InFormatter to write Bowtie tab6 format. | ||
* | ||
* @param gRdd GenomicRDD of Fragments. Used to get HadoopConfiguration. | ||
* @return Returns a new Tab6InFormatter. | ||
*/ | ||
def apply(gRdd: FragmentRDD): Tab6InFormatter = { | ||
new Tab6InFormatter(gRdd.rdd.context.hadoopConfiguration) | ||
} | ||
} | ||
|
||
class Tab6InFormatter private ( | ||
conf: Configuration) extends InFormatter[Fragment, FragmentRDD, Tab6InFormatter] with Logging { | ||
|
||
protected val companion = Tab6InFormatter | ||
private val newLine = "\n".getBytes | ||
private val converter = new AlignmentRecordConverter | ||
private val writeSuffixes = conf.getBoolean(FragmentRDD.WRITE_SUFFIXES, false) | ||
private val writeOriginalQualities = conf.getBoolean(FragmentRDD.WRITE_ORIGINAL_QUALITIES, false) | ||
|
||
/** | ||
* Writes alignment records to an output stream in Bowtie tab6 format. | ||
* | ||
* In Bowtie tab6 format, each alignment record or pair is on a single line. | ||
* An unpaired alignment record line is [name]\t[seq]\t[qual]\n. | ||
* For paired-end alignment records, the second end can have a different name | ||
* from the first: [name1]\t[seq1]\t[qual1]\t[name2]\t[seq2]\t[qual2]\n. | ||
* | ||
* @param os An OutputStream connected to a process we are piping to. | ||
* @param iter An iterator of records to write. | ||
*/ | ||
def write(os: OutputStream, iter: Iterator[Fragment]) { | ||
iter.map(frag => { | ||
val reads = converter.convertFragment(frag).toSeq | ||
|
||
if (reads.size < 2) { | ||
reads | ||
} else { | ||
if (reads.size > 2) { | ||
log.warn("More than two reads for %s. Taking first 2.".format(frag)) | ||
} | ||
reads.take(2) | ||
} | ||
}).foreach(reads => { | ||
|
||
// write unpaired read or first of paired-end reads | ||
val first = converter.convertToTab6(reads(0), | ||
maybeAddSuffix = writeSuffixes, | ||
outputOriginalBaseQualities = writeOriginalQualities) | ||
|
||
os.write(first.getBytes) | ||
|
||
// write second of paired-end reads, if present | ||
if (reads.size > 1) { | ||
val second = "\t" + converter.convertToTab6(reads(1), | ||
maybeAddSuffix = writeSuffixes, | ||
outputOriginalBaseQualities = writeOriginalQualities) | ||
|
||
os.write(second.getBytes) | ||
} | ||
|
||
// end line | ||
os.write(newLine) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import print_function | ||
import sys | ||
|
||
# read lines from stdin | ||
lines = sys.stdin.readlines() | ||
|
||
# print sam header | ||
print("@HD\tVN:1.5\tSO:unsorted") | ||
|
||
# loop and print sam lines | ||
for line in lines: | ||
fields = line.split() | ||
firstReadName = fields[0] | ||
firstSequence = fields[1] | ||
firstQualities = fields[2] | ||
secondReadName = fields[3] | ||
secondSequence = fields[4] | ||
secondQualities = fields[5] | ||
|
||
# flags: | ||
# 1 = paired (we assume that in this script) | ||
# 4 = unmapped | ||
# 8 = mate unmapped | ||
# 64 = first of pair | ||
# 128 = second of pair | ||
firstFlags = 64 | 8 | 4 | 1 | ||
secondFlags = 128 | 8 | 4 | 1 | ||
|
||
# sam is the following tab-delimited columns: | ||
# | ||
# 1. read name | ||
# 2. flags | ||
# 3. ref (* = unaligned) | ||
# 4. pos (0 = unaligned) | ||
# 5. map qual (0 if unmapped) | ||
# 6. cigar (* = unavailable) | ||
# 7. mate ref (* = unaligned) | ||
# 8. mate pos (0 = unaligned) | ||
# 9. tlen (0 = unknown) | ||
# 10. sequence | ||
# 11. qualities | ||
print("%s\t%d\t*\t0\t0\t*\t*\t0\t0\t%s\t%s" % (firstReadName, | ||
firstFlags, | ||
firstSequence, | ||
firstQualities)) | ||
print("%s\t%d\t*\t0\t0\t*\t*\t0\t0\t%s\t%s" % (secondReadName, | ||
secondFlags, | ||
secondSequence, | ||
secondQualities)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters