#!/usr/bin/perl
# ***************************************************************
# Name:      extract_scansite_motifs_fasta.pl
# Purpose:   This script connects to Scansite server and produces
#	     predictions for serine, threonine and tyrosine phosphorylation sites
#	     in eukaryotic proteins for protein sequences in a given FASTA file. 
# Version:   0.1
# Authors:   Umer Zeeshan Ijaz (Umer.Ijaz@glasgow.ac.uk)
#                 http://userweb.eng.gla.ac.uk/umer.ijaz
# Created:   2012-10-20
# License:   Copyright (c) 2012 Computational Microbial Genomics Group, University of Glasgow, UK
#
#            This program 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.
#
#            This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
# **************************************************************/

use strict;
use Bio::SeqIO;
use Bio::PrimarySeq;
use Bio::Tools::Analysis::Protein::Scansite;
use Getopt::Long;

my %opts; #store the input arguments
GetOptions(\%opts,
	'fasta_file|f=s',
);

if(not defined $opts{"fasta_file"})
	{
print <<EOF;
Usage:
	perl extract_scansite_motifs_fasta.pl -f <fasta_file> > <results.tsv>
EOF
	exit;
	}
my $fasta_file=$opts{"fasta_file"};

my $seqio=Bio::SeqIO->new(-file=>$fasta_file,'-format'=>'Fasta');
print "ID\tKinase_name\tPercentile\tPosition_in_protein\tScore\tSequence\tPhosphorylated_residue\tZscore\n";
while (my $seq = $seqio->next_seq)
	{
	my $seqobj = Bio::PrimarySeq->new(-seq=>$seq->seq, -alphabet=>'protein', -id=>'TEST');
	my $tool = Bio::Tools::Analysis::Protein::Scansite->new(-seq=>$seqobj);
	$tool->run();
	my $data_ref=$tool->result('value');
	foreach my $hash_ref (@{$data_ref}){
		unless(${$hash_ref}{motif} eq undef){
			print $seq->id,"\t",${$hash_ref}{motif},"\t",${$hash_ref}{percentile},
				"\t",${$hash_ref}{position},"\t",${$hash_ref}{score},"\t",
				${$hash_ref}{sequence},"\t",${$hash_ref}{site},"\t",
				${$hash_ref}{zscore},"\n";
			}
		
		}
	}
