support both SAM and PAF as input

This commit is contained in:
Heng Li 2017-12-02 21:57:19 -05:00
parent e575f884e1
commit 76206f574f
1 changed files with 41 additions and 39 deletions

View File

@ -43,16 +43,15 @@ function print_lines(a, fmt) {
if (fmt == "bed") { if (fmt == "bed") {
var n_pri = 0; var n_pri = 0;
for (var i = 0; i < a.length; ++i) for (var i = 0; i < a.length; ++i)
if (a[i][9] == 0) ++n_pri; if (a[i][8] == 0) ++n_pri;
if (n_pri > 1) { if (n_pri > 1) {
for (var i = 0; i < a.length; ++i) for (var i = 0; i < a.length; ++i)
if (a[i][9] == 0) a[i][9] = 1; if (a[i][8] == 0) a[i][8] = 1;
} else if (n_pri == 0) { } else if (n_pri == 0) {
warn("Warning: " + a[0][0] + " doesn't have a primary alignment"); warn("Warning: " + a[0][3] + " doesn't have a primary alignment");
} }
for (var i = 0; i < a.length; ++i) { for (var i = 0; i < a.length; ++i) {
a[i][9] = colors[a[i][9]]; a[i][8] = colors[a[i][8]];
a[i].shift();
print(a[i].join("\t")); print(a[i].join("\t"));
} }
} }
@ -61,23 +60,15 @@ function print_lines(a, fmt) {
function main(args) { function main(args) {
var re = /(\d+)([MIDNSH])/g; var re = /(\d+)([MIDNSH])/g;
var c, fmt = "bed", with_hdr = false, hdr_only = false, name = 'splice2bed', pos = null; var c, fmt = "bed";
while ((c = getopt(args, "Hhn:p:")) != null) { while ((c = getopt(args, "f:")) != null) {
if (c == 'h') with_hdr = true; if (c == 'f') fmt = getopt.arg;
else if (c == 'p') pos = getopt.arg;
else if (c == 'H') with_hdr = hdr_only = true;
else if (c == 'n') name = getopt.arg;
} }
if (getopt.ind == args.length && !hdr_only) { if (getopt.ind == args.length) {
warn("Usage: k8 splice2bed.js [options] <in.paf>"); warn("Usage: k8 splice2bed.js <in.paf>");
exit(1); exit(1);
} }
if (with_hdr) {
if (pos != null)
print('browser position ' + pos);
print('track name=' + name + ' useScore=1 visibility=2 itemRgb="On"');
}
if (hdr_only) return;
var file = new File(args[getopt.ind]); var file = new File(args[getopt.ind]);
var buf = new Bytes(); var buf = new Bytes();
var a = []; var a = [];
@ -85,12 +76,12 @@ function main(args) {
var line = buf.toString(); var line = buf.toString();
if (line.charAt(0) == '@') continue; // skip SAM header lines if (line.charAt(0) == '@') continue; // skip SAM header lines
var t = line.split("\t"); var t = line.split("\t");
if (t.length >= 12 && (t[4] == '+' || t[4] == '-')) { var is_pri = false, cigar = null, a1;
if (a.length && a[0][0] != t[0]) { if (a.length && a[0][3] != t[0]) {
print_lines(a, fmt); print_lines(a, fmt);
a = []; a = [];
} }
var is_pri = false, cigar = null; if (t.length >= 12 && (t[4] == '+' || t[4] == '-')) {
for (var i = 12; i < t.length; ++i) { for (var i = 12; i < t.length; ++i) {
if (t[i].substr(0, 5) == 'cg:Z:') { if (t[i].substr(0, 5) == 'cg:Z:') {
cigar = t[i].substr(5); cigar = t[i].substr(5);
@ -98,6 +89,16 @@ function main(args) {
is_pri = true; is_pri = true;
} }
} }
a1 = [t[5], t[7], t[8], t[0], Math.floor(t[9]/t[10]*1000), t[4]];
} else if (t.length >= 10) {
var flag = parseInt(t[1]);
if ((flag&4) || a[2] == '*') continue;
cigar = t[5];
is_pri = (flag&0x100)? false : true;
a1 = [t[2], parseInt(t[3])-1, null, t[0], 1000, (flag&16)? '-' : '+'];
} else {
throw Error("unrecognized input format");
}
if (cigar == null) throw Error("missing CIGAR"); if (cigar == null) throw Error("missing CIGAR");
var m, x0 = 0, x = 0, bs = [], bl = []; var m, x0 = 0, x = 0, bs = [], bl = [];
while ((m = re.exec(cigar)) != null) { while ((m = re.exec(cigar)) != null) {
@ -112,10 +113,11 @@ function main(args) {
} }
bs.push(x0); bs.push(x0);
bl.push(x - x0); bl.push(x - x0);
a.push([t[0], t[5], t[7], t[8], t[0], Math.floor(t[9]/t[10]*1000), t[4], t[7], t[8], is_pri? 0:2, bs.length, bl.join(",")+",", bs.join(",")+","]); // write the BED12 line
} else { if (a1[2] == null) a1[2] = a1[1] + x;
throw Error("unrecognized input format"); a1.push(a1[1], a1[2]); // thick start/end is the same as start/end
} a1.push(is_pri? 0 : 2, bs.length, bl.join(",")+",", bs.join(",")+",");
a.push(a1);
} }
print_lines(a, fmt); print_lines(a, fmt);
buf.destroy(); buf.destroy();