| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use Getopt::Long; |
| 4 |
use Pod::Usage; |
| 5 |
|
| 6 |
sub version { |
| 7 |
print <<EOF; |
| 8 |
abyss-samtoafg (ABySS) |
| 9 |
Written by Shaun Jackman. |
| 10 |
|
| 11 |
Copyright 2012 Canada's Michael Smith Genome Science Centre |
| 12 |
EOF |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
my ($opt_eid, $opt_iid, $opt_mean, $opt_sd) = (1, 1); |
| 17 |
Getopt::Long::Configure(qw'bundling'); |
| 18 |
GetOptions( |
| 19 |
'eid|e=s' => \$opt_eid, |
| 20 |
'iid|i=s' => \$opt_iid, |
| 21 |
'mean|m=i' => \$opt_mean, |
| 22 |
'sd|s=i' => \$opt_sd, |
| 23 |
'help' => sub { pod2usage(-verbose => 1) }, |
| 24 |
'man' => sub { pod2usage(-verbose => 2) }, |
| 25 |
'version' => \&version); |
| 26 |
|
| 27 |
for (@ARGV) { die "cannot read `$_'" unless $_ eq '-' || -r } |
| 28 |
|
| 29 |
# Output the library record (LIB). |
| 30 |
print "{LIB\neid:$opt_eid\niid:$opt_iid\n"; |
| 31 |
print "{DST\nmea:$opt_mean\nstd:$opt_sd\n}\n" |
| 32 |
if defined $opt_mean && defined $opt_sd; |
| 33 |
print "}\n"; |
| 34 |
|
| 35 |
sub getMateID($) |
| 36 |
{ |
| 37 |
my $id = shift; |
| 38 |
return $id =~ s%/1$%/2% || $id =~ s%/2$%/1% |
| 39 |
? $id : undef; |
| 40 |
} |
| 41 |
|
| 42 |
my ($g_red_iid, $g_frg_iid, |
| 43 |
@ctg_eids, @ctg_seqs, |
| 44 |
%reds, %frgs, %tles); |
| 45 |
|
| 46 |
# Output a read (RED) and possibly a fragment (FRG). |
| 47 |
sub createRead($$$) |
| 48 |
{ |
| 49 |
my ($eid, $seq, $qlt) = @_; |
| 50 |
die "error: duplicate sequence ID `$eid'" if exists $reds{$eid}; |
| 51 |
my $red_iid = ++$g_red_iid; |
| 52 |
|
| 53 |
(my $frg_eid = $eid) =~ s/\/[12]$//; |
| 54 |
my ($my_frg_iid, $mate_iid); |
| 55 |
if (exists $frgs{$frg_eid}) { |
| 56 |
$my_frg_iid = delete $frgs{$frg_eid}; |
| 57 |
my $mate_eid = getMateID($eid); |
| 58 |
die unless defined $mate_eid; |
| 59 |
$mate_iid = delete $reds{$mate_eid}; |
| 60 |
die unless defined $mate_iid; |
| 61 |
} else { |
| 62 |
$my_frg_iid = $frgs{$frg_eid} = ++$g_frg_iid; |
| 63 |
$reds{$eid} = $red_iid; |
| 64 |
} |
| 65 |
|
| 66 |
# Output a read (RED) record. |
| 67 |
my $qlength = length $seq; |
| 68 |
print "{RED\nclr:0,$qlength\niid:$red_iid\neid:$eid\n", |
| 69 |
"frg:$my_frg_iid\n", |
| 70 |
"seq:\n$seq\n.\nqlt:\n$qlt\n.\n}\n"; |
| 71 |
|
| 72 |
# Output a fragment (FRG) record. |
| 73 |
if (defined $mate_iid) { |
| 74 |
print "{FRG\nrds:$mate_iid,$red_iid\nlib:$opt_iid\n", |
| 75 |
"eid:$frg_eid\niid:$my_frg_iid\ntyp:I\n}\n"; |
| 76 |
} |
| 77 |
|
| 78 |
return $red_iid; |
| 79 |
} |
| 80 |
|
| 81 |
# Return the left and right soft clipping of this CIGAR string. |
| 82 |
sub parseCigar($) |
| 83 |
{ |
| 84 |
my $cigar = shift; |
| 85 |
my $clipLeft = $cigar =~ /^([0-9]+)S/ ? $1 : 0; |
| 86 |
my $clipRight = $cigar =~ /([0-9]+)S$/ ? $1 : 0; |
| 87 |
return ($clipLeft, $clipRight); |
| 88 |
} |
| 89 |
|
| 90 |
# Record the alignment (TLE) records. |
| 91 |
while (<>) { |
| 92 |
chomp; |
| 93 |
next if /^#/ || /^@/; |
| 94 |
|
| 95 |
if (/^>([^ ]+)/) { |
| 96 |
my $eid = $1; |
| 97 |
chomp (my $seq = <>); |
| 98 |
push @ctg_eids, $eid; |
| 99 |
push @ctg_seqs, $seq; |
| 100 |
next; |
| 101 |
} |
| 102 |
|
| 103 |
my ($qid, $flag, $tid, $tstart, $mapq, $cigar, |
| 104 |
$rnext, $pnext, $tlen, $qseq, $qqual) = split '\t'; |
| 105 |
die unless defined $qqual; |
| 106 |
|
| 107 |
$tstart--; # convert to zero-based coordinate |
| 108 |
next if $flag & 0x100; # secondary alignment |
| 109 |
$qid .= "/1" if $flag & 0x40; #FREAD1 |
| 110 |
$qid .= "/2" if $flag & 0x80; #FREAD2 |
| 111 |
|
| 112 |
my $rc = $flag & 0x10; #FREVERSE |
| 113 |
if ($rc) { |
| 114 |
# Reverse and complement the sequence. |
| 115 |
$qseq =~ tr/ACGTacgt/TGCAtgca/; |
| 116 |
$qseq = reverse $qseq; |
| 117 |
$qqual = reverse $qqual; |
| 118 |
} |
| 119 |
my $riid = createRead($qid, $qseq, $qqual); |
| 120 |
next if $flag & 0x4; #FUNMAP |
| 121 |
|
| 122 |
my $qlength = length $qseq; |
| 123 |
die if length $qqual != $qlength; |
| 124 |
my ($qstart, $clipRight) = parseCigar($cigar); |
| 125 |
my $qend = $qlength - $clipRight; |
| 126 |
die unless $qstart < $qend; |
| 127 |
|
| 128 |
my $clr = $rc ? "$qend,$qstart" : "$qstart,$qend"; |
| 129 |
$tles{$tid} .= "{TLE\nclr:$clr\noff:$tstart\nsrc:$riid\n}\n"; |
| 130 |
} |
| 131 |
|
| 132 |
# Output the contig (CTG) and alignment (TLE) records. |
| 133 |
my $ctg_iid = 0; |
| 134 |
for my $ctg_eid (@ctg_eids) { |
| 135 |
my $seq = shift @ctg_seqs; |
| 136 |
next if length $tles{$ctg_eid} == 0; |
| 137 |
|
| 138 |
# Split long lines. |
| 139 |
my $qlt = 'I' x (length $seq); |
| 140 |
$seq =~ s/.{60}/$&\n/sg; |
| 141 |
$qlt =~ s/.{60}/$&\n/sg; |
| 142 |
|
| 143 |
# Contig sequence. |
| 144 |
$ctg_iid++; |
| 145 |
print "{CTG\niid:$ctg_iid\n", |
| 146 |
"eid:$ctg_eid\n", |
| 147 |
"seq:\n", $seq, "\n.\n", |
| 148 |
"qlt:\n", $qlt, "\n.\n"; |
| 149 |
|
| 150 |
print $tles{$ctg_eid}; |
| 151 |
|
| 152 |
print "}\n"; |
| 153 |
} |
| 154 |
|
| 155 |
=pod |
| 156 |
|
| 157 |
=head1 NAME |
| 158 |
|
| 159 |
abyss-samtoafg - create an AMOS AFG file from a SAM file |
| 160 |
|
| 161 |
=head1 SYNOPSIS |
| 162 |
|
| 163 |
B<abyss-samtoafg> F<contigs.fa> F<alignments.sam> >F<assembly.afg> |
| 164 |
|
| 165 |
B<bank-transact> B<-cb> F<assembly.bnk> B<-m> F<assembly.afg> |
| 166 |
|
| 167 |
B<hawkeye> F<assembly.bnk> |
| 168 |
|
| 169 |
=head1 DESCRIPTION |
| 170 |
|
| 171 |
Create an AMOS AFG file from a FASTA file and a SAM file. |
| 172 |
|
| 173 |
=head1 OPTIONS |
| 174 |
|
| 175 |
=over |
| 176 |
|
| 177 |
=item B<-e>,B<--eid> |
| 178 |
|
| 179 |
the EID of the library |
| 180 |
|
| 181 |
=item B<-i>,B<--iid> |
| 182 |
|
| 183 |
the IID of the library |
| 184 |
|
| 185 |
=item B<-m>,B<--mean> |
| 186 |
|
| 187 |
the mean of the fragment-size |
| 188 |
|
| 189 |
=item B<-s>,B<--sd> |
| 190 |
|
| 191 |
the standard deviation of the fragment-size |
| 192 |
|
| 193 |
=back |
| 194 |
|
| 195 |
=head1 AUTHOR |
| 196 |
|
| 197 |
Written by Shaun Jackman. |
| 198 |
|
| 199 |
=head1 REPORTING BUGS |
| 200 |
|
| 201 |
Report bugs to <[email protected]>. |
| 202 |
|
| 203 |
=head1 COPYRIGHT |
| 204 |
|
| 205 |
Copyright 2012 Canada's Michael Smith Genome Science Centre |
| 206 |
|
| 207 |
=head1 SEE ALSO |
| 208 |
|
| 209 |
http://www.bcgsc.ca/platform/bioinfo/software/abyss |
| 210 |
|
| 211 |
http://amos.sourceforge.net/hawkeye |