File: mbgd-to-kog.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 (103 lines) | stat: -rwxr-xr-x 2,931 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
#!/usr/bin/perl

#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/>.

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

use strict;

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

my $output=pop(@ARGV);
if(!$output or (-f $output and formatCheck($output))){
  push(@ARGV,$output) if $output;
  $output=">-";
}
pod2usage({-verbose=>1,-exitval=>2,-message=>'Need some input file...'}) if $#ARGV<0;

open(our $outf,">$output") or die "Couldn't open $output for writing.";

foreach my $file (@ARGV){
  open(my $inf,$file);
  my $line=<$inf>;
  chomp $line;
  my $headerline=$line; #keep this around because (as of recently) it occurs again at the end of the file for some dumb reason
  my @cols=split(/\t/,$line);
  unless($cols[$#cols]){
    print STDERR "Removing stupid extra tab from header line.\n";
    pop(@cols);
  }
  my @specs=@cols[1..($#cols-2)];
  print STDERR "Using species: @specs\n";
  while(<$inf>){
    chomp;
    next if m/^$headerline/;
    my @dat=split(/\t/);
    die "Invalid number of dat ($#dat vs $#cols)! $_" unless $#dat==$#cols or $#dat+1==$#cols;
    my @locii=@dat[1..($#cols-2)];
    my $func=$dat[$#cols-1];
    my $gene=$dat[$#cols];
    $gene="MBGD gene" unless $gene;
    print $outf "[$func] $dat[0] $gene\n";
    foreach my $i (0..$#locii){
      my $spec=$specs[$i];
      foreach my $locii (split(/ /,$locii[$i])){
	die "Weird entry: $locii" unless $locii=~m/$spec:(.*)/;
	$locii=$1;
	$locii=~s/MG(\d+)/MG_$1/;
	$locii=~s/MPN(\d+)/MPN_$1/;
	$locii=~s/\(\d+\)//;
	$locii=~s/\.\d+//;
	print $outf "  $spec:  $locii\n";
      }
    }
    print $outf "\n";
  }
}

sub formatCheck {
  my $file=pop;
  open(my $inf,$file) or return undef;
  my ($line1,$line2)=map {readline($inf)} (0,0);
  my @fields=map {scalar(split(/\t/,$line1))} ($line1,$line2);
  return 1 if $line1=~m/^ClusterID/ and ($fields[0]==$fields[1] or $fields[0]==($fields[1]+1));
  return undef;
}

__END__

=head1 NAME

mbgd-to-kog.pl - converts mbgd cluster files to kog files

=head1 SYNOPSIS

mbgd-to-kog.pl [options] <input1> [input2 ... ] [output file]

=head1 OPTIONS

If "output file" exists, and is of the right format, it is considered an input file.

No other options exist yet.