#!/usr/bin/perl

use strict;
use warnings;

use lib "$ENV{HOME}/scripts/jaiswallab/interactome_scripts";

use DbiFloret;

if ($#ARGV != 2) {
	print "usage: make_fasta_from_gene_list.pl species gene_list output_file\n";
	exit;
}

my $species = $ARGV[0];

my $gene_list_file = $ARGV[1];;

my @genes;
open(gene_file, $gene_list_file);
while(<gene_file>) {
	my $line = $_;
	chomp $line;
	push (@genes, $line);
}
close(gene_file);

my $dbh = DbiFloret::dbconnect;

my $safe_species = $dbh->quote_identifier($species);

my $sth_get_sequence = $dbh->prepare("select sequence from $safe_species where gene_id like ?");

open(OUTPUTFH, ">$ARGV[2]");

foreach my $gene (@genes) {
	$sth_get_sequence->execute($gene . "%");
	print OUTPUTFH ">$gene\n";
	while (my @line = $sth_get_sequence->fetchrow_array) {
		my $line = $line[0];
		my $len = length($line);
		for (my $pos=0; $pos < $len; $pos+=80) {
			print OUTPUTFH substr($line, $pos, 80), "\n";
		}
	}
}
close(OUTPUTFH);
		
