#!/usr/bin/perl -w

# set the one letter aa to three letter aa representation
if(!@ARGV){
	printf "USAGE: getSeq.pl -if pdbfile\n";
	exit(1);
}

$one_thr{"ALA"}="A";$one_thr{"CYS"}="C";$one_thr{"ASP"}="D";$one_thr{"GLU"}="E";
$one_thr{"PHE"}="F";$one_thr{"GLY"}="G";$one_thr{"HIS"}="H";$one_thr{"ILE"}="I";
$one_thr{"LYS"}="K";$one_thr{"LEU"}="L";$one_thr{"MET"}="M";$one_thr{"ASN"}="N";
$one_thr{"PRO"}="P";$one_thr{"GLN"}="Q";$one_thr{"ARG"}="R";$one_thr{"SER"}="S";
$one_thr{"THR"}="T";$one_thr{"VAL"}="V";$one_thr{"TRP"}="W";$one_thr{"TYR"}="Y";

foreach $i (0..$#ARGV) {
    if($ARGV[$i] eq "-if") {
        $FileName=$ARGV[++$i];
    }
}

if(-e "$FileName.pdb"){
	open IN, "$FileName.pdb";
}
elsif(-e "$FileName.ab.pdb"){
    open IN, "$FileName.ab.pdb";
}
elsif( -e "/home/jinfeng/SharedData/pc30_res2.0_CA/$FileName.ca.pdb"){
    open IN, "/home/jinfeng/SharedData/pc30_res2.0_CA/$FileName.ca.pdb";
}
elsif( -e "/home/jinfeng/SharedData/pc90_res3.0_CA/$FileName.ca.pdb"){
    open IN, "/home/jinfeng/SharedData/pc90_res3.0_CA/$FileName.ca.pdb";
}
else {
    printf "cannot open $FileName.ca.pdb!";
    exit();
}

open OUT, ">$FileName.seq";
$seq="";
while(defined($line=<IN>)){
if(substr($line,13,2) eq "CA"){
    $resName = substr($line, 17,3); $resName =~ s/^\s+|\s+$//g;
    if(defined($one_thr{$resName})){
	$seq = $seq . $one_thr{$resName};
    }
    else {
	printf "$FileName $resName\n";
    }
}
}

printf OUT "$FileName $seq\n";
