#!/usr/bin/perl
package printReferenceMod;

# Author:           Shuai Weng                                     
# Date:             April 2001  
# Description:      This package contains some reference 
#                   modules for dictyBase database
#
use strict;
use DBI;
use CGI qw/:all/;
use lib "/usr/local/dicty/www_dictybase/db/lib/common"; 
use Login  qw (ConnectToDatabase);
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase"; 
use dictyBaseCentralMod qw(:formatPage :getInfo);
use MedlineParse qw(CreateCitation);
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects"; 

use Exporter();
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION = 1.00;
@ISA = qw( Exporter );

%EXPORT_TAGS = (formatSub => [qw/author refTitle journal volume page year pubmed abstract publishedStatus refSource pubType findFullJournalInfo parseJournalInfo/]);
		
#  add all tags to @EXPORT_OK
Exporter::export_ok_tags('formatSub');

my $configUrl = ConfigURLdictyBase->new;
my $database = 'dictyBase';
my $dblink = $configUrl->dblink($database);

#####################################################################
sub smTitle {
#####################################################################
    my ($smTitle) = @_;
    return table(Tr(td({-bgcolor=>'#ACBDCC',
		        -width=>'200'}, 
		       b($smTitle))));
}

#####################################################################
sub author {
#####################################################################
    my (@author) = @_;
    print p, &smTitle("Authors");
    my $rows;
    for (my $i = 1; $i <= 12; $i += 2) {
	my $j = $i + 1;
	$rows .= Tr(td($i).
		    td(textfield(-name=>"author$i",
				 -default=>$author[$i-1],
				 -size=>'27')).
		    td($j).
	 	    td(textfield(-name=>"author$j",
	 			 -default=>$author[$i],
				 -size=>'27')));
	
    }
    print table({-border=>'2'},
		Tr(th({-colspan=>'2',
		       -align=>'center'},
		      "Last name, First initial").
		   th({-colspan=>'2',
		       -align=>'center'},
		      "Last name, First initial")).
		$rows);
}

#####################################################################
sub refTitle {
#####################################################################
    my ($reftitle) = @_;
    print p, &smTitle("Title");
    print textarea(-name=>'reftitle', 
		   -value=>"$reftitle",
		   -rows=>'2',
		   -columns=>'60');
}

#####################################################################
sub journal {
#####################################################################
    my ($journal) = @_;
    return Tr(td({-width=>'200',
		  -bgcolor=>'#ACBDCC'},
		 b("Journal(name or journal_no)".
		 br.a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/curation/ListJournal", -target=>"infowin"}, "View Journal list"))).
	      td(textfield(-name=>'journal',
			   -value=>"$journal",
			   -size=>40)));

}

#####################################################################
sub volume {
#####################################################################
    my ($volume) = @_;
    return Tr(td({-width=>'200',
		  -bgcolor=>'#ACBDCC'},
		 b("Volume")).
	      td(textfield(-name=>'volume',
			   -value=>"$volume",
			   -size=>10)));
}

#####################################################################
sub page {
#####################################################################
    my ($page) = @_;
    return Tr(td({-width=>'200',
		  -bgcolor=>'#ACBDCC'},
		 b("Page Numbers(eg.11-21)")).
	      td(textfield(-name=>'page',
			   -value=>"$page",
			   -size=>20)));
	
}

#####################################################################
sub year {
#####################################################################
    my ($year) = @_;
    if (!$year) {
	$year = `date +%Y`;
	chomp $year;
    }
    return Tr(td({-width=>'200',
		  -bgcolor=>'#ACBDCC'},
		 b("Year (".font({-color=>'red'}, "Mandatory").")")).
	      td(textfield(-name=>'year',
			   -value=>"$year",
			   -size=>10)));
}

#####################################################################
sub pubmed {
#####################################################################
    my ($pubmed) = @_;
    return Tr(td({-width=>'200',
		  -bgcolor=>'#ACBDCC'},
		 b("Pubmed ID")).
	      td(textfield(-name=>'pubmed',
			   -value=>"$pubmed",
			   -size=>20)));
	
}

#####################################################################
sub abstract {
#####################################################################
    my ($abstract) = @_;
    print p, &smTitle("Abstract");
    print textarea(-name=>'abstract', 
		   -value=>"$abstract",
		   -rows=>'6',
		   -columns=>'60');
}

#####################################################################
sub publishedStatus {
#####################################################################
    my ($status) = @_;
    print p, &smTitle("Published Status"),p;
    print radio_group(-name=>'reftype',
                      -values=>['Published', 'In press', 
				'Submitted', 'In preparation', 
				'Unpublished'],
                      -default=>"$status"),p;
}

#####################################################################
sub refSource {
#####################################################################
    my ($source, $pubmed) = @_;
    print p, &smTitle("Reference Source"),p;
    print radio_group(-name=>'refsource',
                      -values=>['Curator', 'PubMed'],
                      -default=>"$source");
    print ", if Pubmed, enter PubMed ID:".
	textfield(-name=>'pubmed',
		  -value=>"$pubmed",
		  -size=>'10'),p;

}

#####################################################################
sub pubType {
#####################################################################
    my (@type) = @_;
    print p, &smTitle("Publication Type"),p;
    print b(font({-color=>'red'}, "NOTE:"))." If the paper's status is 'In preparation', 'Submitted', or 'Unpublished', then check the 'Personal Communication to dictyBase' box. If the status is 'In press' or 'Published', then check 'Journal article' (and/or whatever else is appropriate, for eg, 'Reviews'). The 'Direct Submission to dictyBase' type is used when bulk data like SAGE or systematic deletions are submitted.",p;
    print checkbox_group(-name=>'pubtype',
			 -values=>['Comment', 'Congresses', 
				   'Corrected and republished article',
				   'Database',
				   'Direct submission to dictyBase',
				   'Historical article',
				   'Journal article', 
				   'Lectures', 'Letter',
				   'Monograph', 'News', 'Patent', 
				   'Personal communication to dictyBase',
				   'Published erratum',
				   'Retracted publication',
				   'Review', 'Review literature',
				   'Review, academic',
				   'Review, tutorial',
				   'Technical report', 'Thesis'],
                         -default=>\@type,
			 -rows=>'10'), p;
    

}


########################################################################
sub findFullJournalInfo { 
########################################################################
    my ($journal) = @_;
    my $journalFile = "/share/dictyBase/data/J_Medline.txt";
    my $tmpfile = "/var/tmp/journal.tmp";
    system("/bin/tr '\12' '\15' < $journalFile > $tmpfile");
    open(IN, "$tmpfile") || 
	die "Can't open '$tmpfile' for reading:$!\n";
    while(<IN>) {
	my @journalRecord = split(/JrId:/);
	foreach my $record (@journalRecord) {
	    my @line = split(/\15/, $record);
	    my ($abbrev, $issn, $Jtitle);
	    foreach my $line (@line) {
		if ($line =~ /^JournalTitle: (.+)$/i) {
		    $Jtitle = $1;
		}
		elsif ($line =~ /^MedAbbr: (.+)$/i) {
		    $abbrev = $1;
		}
		elsif ($line =~ /^ISSN: (.+)$/i) {
		    $issn = $1;
		}
		if (uc($abbrev) eq uc($journal)) {
		    close(IN);
		    unlink ("$tmpfile");
		    return ($abbrev, $issn, $Jtitle);
		}
	    }
	}
    }
    close(IN);
    unlink ("$tmpfile");
    return ;
}

########################################################################
sub parseJournalFile {
########################################################################
    my ($ISSN4AbbrevRef, $FullName4AbbrevRef) = @_;
    my $journalFile = "/share/dictyBase/data/J_Medline.txt";
    my $tmpfile = "/var/tmp/journal.tmp";
    system("/bin/tr '\12' '\15' < $journalFile > $tmpfile");
    open(IN, "$tmpfile") || 
	die "Can't open '$tmpfile' for reading:$!\n";
    while(<IN>) {
	my @journalRecord = split(/JrId:/);
	foreach my $record (@journalRecord) {
	    my @line = split(/\15/, $record);
	    my ($abbrev, $issn, $Jtitle);
	    foreach my $line (@line) {
		if ($line =~ /^JournalTitle: (.+)$/i) {
		    $Jtitle = $1;
		}
		elsif ($line =~ /^MedAbbr: (.+)$/i) {
		    $abbrev = $1;
		}
		elsif ($line =~ /^ISSN: (.+)$/i) {
		    $issn = $1;
		}
	    }
	    $$ISSN4AbbrevRef{"\U$abbrev"} = $issn;
	    $$FullName4AbbrevRef{"\U$abbrev"} = $Jtitle;
	}
    }
    close(IN);
    unlink ("$tmpfile");
}

#####################################################################
1;  ##### Return it to the calling program
#####################################################################











