File: mauve2anchors.pl

package info (click to toggle)
murasaki 1.68.6-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,928 kB
  • ctags: 3,100
  • sloc: cpp: 16,010; perl: 8,365; makefile: 186
file content (146 lines) | stat: -rwxr-xr-x 4,012 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl -w

#Copyright (C) 2006-2008 Keio University
#(Kris Popendorf) <comp@bio.keio.ac.jp> (2006)
#
#This file is part of Murasaki.
#
#Murasaki is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#Murasaki is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Murasaki.  If not, see <http://www.gnu.org/licenses/>.

##################
## convert mauve alignments to Murasaki anchor files -- krisp
##################

use File::Basename;
use Getopt::Long;
use Pod::Usage;
#use Data::Dump qw{dump};

BEGIN {
  unshift(@INC,(fileparse($0))[1].'perlmodules');
}
use Murasaki;

use strict;
my ($help,$man,$align_type);
my $autoout=1;
our $flexible=0;
our $signed=1;
my $useAlignment;
GetOptions('help|?' => \$help, man => \$man, 'autoout!'=>\$autoout);
pod2usage(1) if $help or $#ARGV<0;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

my ($filename,$outfile)=@ARGV;

my ($basename,$path,$suffix) = fileparse($filename);
die "Input file not found: $filename" unless -e $filename;

$outfile="$path/$basename.anchors" if $autoout;
print "Outfile is $outfile\n" if $autoout;

if($outfile){
  open(OUTF,">$outfile");
} else {
  open(OUTF,">-");
}

my %mauvedat=%{loadMauveAlignment($filename)};
foreach my $lcb (@{$mauvedat{LCBs}}){
  print OUTF (join("\t",map {($_->{start},$_->{stop},($_->{start}<0 ? "-":"+"))} (@$lcb))."\n");
}

if($basename){ #we can also make the seqs file
  open(my $seqfh,">$basename.seqs");
  local $,="\n";
  local $\="\n";
  print $seqfh (map {$_->{seqFile}} @{$mauvedat{seqs}});
}

sub loadMauveAlignment {
  my $alignment=shift;
  open(MAUVE,"<$alignment");
  <MAUVE>=~m/FormatVersion\s+(\d+)/ or die "Not a mauve file: $alignment";
  my $version=$1;
  my @seqs=();
  do {print "This program is written for Mauve Format Version 4.\n
This file is version $version. Weird stuff may happen.\n"; $flexible=1;} if $version!=4;
  <MAUVE>=~m/SequenceCount\s+(\d+)/ or die "Unknown sequence count\n";
  my $seqCount=$1;
  while(<MAUVE>){
    next unless m/Sequence(\d+)File\s+(\S.*)/;
    my ($seqId,$seqFile)=($1,$2);
    $_=<MAUVE>;
    m/Sequence${seqId}Length\s+(\d+)/ or $flexible or die "Input file is weird: $_";
    my $seqLength=$1;
    $seqs[$seqId]={'seqId' => $seqId,'seqFile' => $seqFile,'seqLength'=>$seqLength, 'seqName' => getName($seqFile) };
    last if $seqId==$seqCount-1;
  }

  my @LCBs=();
  $_=<MAUVE>;
  m/IntervalCount\s(\d+)/ or $flexible or die "Interval Count line weird: $_";
  my $LCBCount=$1;
  while(<MAUVE>){
    m/Interval\s(\d+)/ or next;
    my $LCBId=$1;
    $_=<MAUVE>;
    chomp;
    my ($length,@start)=split(/\s+/);
    my @stop=map {$_+$length} @start;
#    print "Start is at     : ".join("\t",@start)."\n";
#    print "Updating stop to: ".join("\t",@stop)."\n";
    my @segs;
    while(<MAUVE>){
      chomp;
      last if $_ eq '';
      next if $_ eq 'GappedAlignment' or m/^[A-Z-]+$/; #skip gapped lines
      ($length,@segs)=split(/\s+/);
      @stop=map {$_+$length} @segs;
    }
    next if (grep {$_==0} @start)>0;
    my @LCB=map {
      { start => $start[$_], stop => $stop[$_], LCBId => $LCBId }} 0..$#stop;
    
    push(@LCBs,\@LCB);
  }
  return {'seqs' => \@seqs, 'LCBs' => \@LCBs};
}

sub getName {
  my @ret=map {
    my ($name,$path,$suffix) = fileparse($_, qr{\.[^.]*});
    $name
    } @_;
  return @ret if $#_;
  return $ret[0];
}

__END__

=head1 NAME

mauve2anchors - converts a Mauve alignment into Murasaki anchor format

=head1 SYNOPSIS

mauve2anchors <input> [output]

=head1 OPTIONS

--autoout -- automatically pick an output name (otherwise output goes to stdout)

=over 8

=back