| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
|
| 4 |
|
| 5 |
$argc = @ARGV; |
| 6 |
if ($argc == 0) { |
| 7 |
print "usage: cat aln1.sam | sam_add_rg.pl <read group id> <sample name>\n"; |
| 8 |
print "changes all alignments to have RG tags matching the new sample name.\n"; |
| 9 |
print "prints SAM format out stdout\n"; |
| 10 |
exit(0); |
| 11 |
} |
| 12 |
|
| 13 |
$ID = $ARGV[0]; |
| 14 |
$SM = $ARGV[1]; |
| 15 |
|
| 16 |
$in_header = 1; |
| 17 |
|
| 18 |
while (<STDIN>) { |
| 19 |
if ($_ =~ /^@/) { |
| 20 |
print $_; |
| 21 |
next; |
| 22 |
} else { |
| 23 |
# add the new RG group to the end of the header |
| 24 |
if ($in_header) { |
| 25 |
print "\@RG\tID:$ID\tSM:$SM\n"; |
| 26 |
$in_header = 0; |
| 27 |
} |
| 28 |
if ($_ =~ /\tRG:Z:(.+?)/) { |
| 29 |
$_ =~ s/\tRG:Z:.+?([\t\n])/\tRG:Z:$ID$1/; |
| 30 |
} else { |
| 31 |
$_ =~ s/\n/\tRG:Z:$ID\n/; |
| 32 |
} |
| 33 |
print $_; |
| 34 |
} |
| 35 |
} |
| 36 |
|