+sub sam2align
+{
+ my ( $seq,
+ $cigar,
+ $nm,
+ $md,
+ ) = @_;
+
+ # Returns a string.
+
+ my ( $offset, $len, $op, $i, $nt, $nt2, @nts, $insertions, @align, $aln_str );
+
+ $offset = 0;
+ $insertions = 0;
+
+ while ( length( $cigar ) > 0 )
+ {
+ if ( $cigar =~ s/^(\d+)(\w)// )
+ {
+ $len = $1;
+ $op = $2;
+
+ if ( $op eq 'I' )
+ {
+ for ( $i = 0; $i < $len; $i++ )
+ {
+ $nt = substr $seq, $offset + $i, 1;
+
+ push @align, [ $offset + $i, "->$nt" ];
+
+ $nm--;
+ }
+ }
+
+ $offset += $len;
+ }
+ }
+
+ $offset = 0;
+
+ while ( $nm > 0 )
+ {
+ if ( $md =~ s/^(\d+)// ) # match
+ {
+ $offset += $1;
+ }
+ elsif( $md =~ s/^\^([ATCGN]+)// ) # insertions
+ {
+ @nts = split //, $1;
+
+ foreach $nt ( @nts )
+ {
+ $nt2 = substr $seq, $offset, 1;
+
+ push @align, [ $offset, "$nt2>-" ];
+
+ $offset++;
+
+ $insertions++;
+
+ $nm--;
+ }
+ }
+ elsif ( $md =~ s/^([ATCGN]+)// ) # mismatch
+ {
+ @nts = split //, $1;
+
+ foreach $nt ( @nts )
+ {
+ $nt2 = substr $seq, $offset - $insertions, 1;
+
+ push @align, [ $offset, "$nt>$nt2" ];
+
+ $offset++;
+
+ $nm--;
+ }
+ }
+ else
+ {
+ Maasha::Common::error( qq(Bad SAM field -> MD: "$md") );
+ }
+ }
+
+ $aln_str = "";
+
+ @align = sort { $a->[ 0 ] <=> $b->[ 0 ] } @align;
+
+ map { $aln_str .= "$_->[ 0 ]:$_->[ 1 ]," } @align;
+
+ $aln_str =~ s/,$//;
+
+ return $aln_str;
+}
+
+
+sub biopiece2sam
+{
+
+}
+