#!/usr/bin/perl -w
# Convert JASPAR matrices to fasta-like format
# Written by Martin C Frith
# I intend that anyone who finds this code useful be free to use,
# modify, or redistribute it without any restrictions

=head1 NAME

jaspar2fasta - conversion of JASPAR database release for use with clover

=head1 SYNOPSIS

jaspar2fasta <path to JASPAR directory>

=head1 DESCRIPTION

The tool reads the description of JASPAR and reads all files
required to provide decent input to clover.
jaspar2fasta takes the directory containing all the matrix_list.txt file
of JASPAR as the only argument.  The output should be redirected into
a new file on which to subsequently work with clover.


=head1 AUTHOR

Please contact the author Martin C. Frith <mfrith@zlab.bu.edu> for feedback or
bug reports.

=cut

use strict;
use File::Basename;

die "Usage: ", basename($0), " JASPAR-directory\n" unless @ARGV == 1;
my $dir = shift;

open LIST, "sort $dir/matrix_list.txt |" or die $!;
while (<LIST>) {
    my ($name, $tf, $type) = (split /\t/)[0,2,3];
    print ">$name $tf $type\n";
    my @mat;
    open MAT, "$dir/$name.pfm" or die $!;
    while (<MAT>) {
	push @mat, [ split ];
    }
    close MAT;
    my $end = $#{$mat[0]};
    for my $i (0..$end) {
	for my $j (0..$#mat) {
	    print $mat[$j][$i], $j < $#mat ? "\t" : "\n";
	}
    }
}
close LIST;
