+# print "In:$line\n";
+# print "MD: OLD => NEW\nMD: $oldmd => $mdtag\n\n";
+
+ return $mdtag;
+}
+
+sub indeltype {
+ my $string = shift;
+ my $insert="";
+ my $indeltype;
+ if ($string =~ /([A-Za-z]+)\>/) {
+ $indeltype=">";
+ $insert=$1;
+ } elsif ($string =~ /\-/) {
+ $indeltype="-";
+ } elsif ($string =~ /\+([A-Za-z]+)/) {
+ $indeltype="+";
+ $insert=$1;
+ }
+ return ($indeltype,$insert);
+
+}
+
+
+sub cigar_method {
+ my $line = shift;
+ my $ref =shift;
+ my $rdlen = shift;
+ my @variations = @$ref;
+ my $string="";
+ my $type="";
+ my $t =1;
+ my $q=1;
+ my $indeltype="";
+ my $cigar= "";
+ my $insert = "";
+ my $len=0;
+ my @cig=();
+ foreach $string (@variations) {
+ next if $string =~ />/;
+ my $pos = $1 if $string =~ /^(\d+)/;
+
+ if ($string =~ /\+([A-Za-z]+)/) {
+ $indeltype="+";
+ $insert = $1;
+ }elsif ($string =~ /\-([A-Za-z]+)/) {
+ $indeltype="-";
+ $insert = $1;
+ }
+#print "$pos $indeltype $insert $t $q\n";
+ $len = $pos - $t;
+ if ( $len > 0) {
+ $cigar.=$len."M";
+ push(@cig,$len."M");
+ }
+ $t+=$len;
+ $q+=$len;
+
+ if ($indeltype eq "-") {
+ $cigar.="D";
+ push(@cig,"D");
+ $t++;
+ }
+ if ($indeltype eq "+") {
+ $len = length ($insert);
+ if ($len == 1) {
+ $cigar.="I";
+ push(@cig,"I");
+ }
+ if ($len > 1) {
+ $cigar.=$len."I";
+ push(@cig,$len."I")
+ }
+ $q+=$len;
+ }
+ $insert="";
+ }
+ $len= $rdlen - $q + 1;
+ if ($len > 0) {
+ $cigar.=$len."M";
+ push(@cig,$len."M");
+ }
+
+ $cigar = newcigar($cigar,'D');
+ $cigar = newcigar($cigar,'I');
+
+ #print "$line\n";
+ #print "c CIGAR:\t$cigar\n\n";
+ return $cigar;
+
+}
+
+
+
+sub newcigar {
+ my $cigar = shift;
+ my $char = shift;
+ my $new = "";
+ my $copy = $cigar;
+#print "$cigar\n";
+ $copy =~ s/^($char+)/$1;/g;
+#print "$copy\n";
+ $copy =~ s/([^0-9$char])($char+)/$1;$2;/g;
+#print "$copy\n";
+ my @parts = split(/;/,$copy);
+ my $el="";
+ foreach $el (@parts) {
+#print "$el\n";
+ if ($el =~ /^$char+$/) {
+ $new.=length($el).$char;
+ }else {
+ $new.=$el;
+ }
+
+ }
+ return $new;