#!/bin/env icarus
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    $RCSfile: molprobe.is,v $
#    $Revision: 1.1 $
#    $Date: 1997/04/02 10:56:53 $
#    $Author: srs $
#    $Revision: 1.2  - hyperlink to Medline $
#    $Date: 31 Mar 1999 $
#    $Author: Angelo Facchiano angelo@crisceb.area.na.cnr.it $
#    $Revision: 1.3  - internal links to cprimer, primers, probes $
#    $Date: 24 Oct 1999 $
#    $Author: Paolo Romano paolo@ist.unige.it $
#    $Author: Domenico Marra dmarra@ist.unige.it $
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$fn={
  Name:name Type:type Sequence:seq ProteinSequence:prs TargetGene:target
  Applications:appl Methods:methods Bibliography:bibl EMBL:acc
  GeneDescription:gened OligoLocalization:oloc TechNotes:tech
  DataSource:datas Polymorphism:polym RecognizedVariants:varians
  RecognizedSpecificities:specifs ComplementaryPrimer:cprimer
  Primers:primers Probes:probes Species:species
}

# Tecnical notes

$rules={
  entry:     ~ {$In:[file:text] $Out pre $Skip:0}
             	'\n'*
               (/[^\n]+\n/ {$entryFip=$Fip $Wrt}
             	/[^\n]+\n/+ {$App}
             	'\n')?
             ~
  # fields
  fields:    ~ {$In:entry $Out $Skip:1}
               ln {$Wrt:id}
               (/([^ ][A-Za-z_\/ ]+):/ {$Wrt:$fn.$1} ln {$App}
             	(/([^ ][A-Za-z_\/ ]+):/ {$Not} ln {$App})*)+
             ~
	
  # indexing
  id:         ~ {$In:[fields c:id] $Out}
                tag /([^\n]*)\n/ {$Wrt:[s:$1]} ~
               
  name:        ~ {$In:[fields c:name] $Out}
                tag (ln {$Wrt}) ~
               
  type:       ~ {$In:[fields c:type] $Out}
                tag /([a-z]+)\n/ {$Wrt:$Ct} ~

  seq:        ~ {$In:[fields c:seq] $Out}
                htag? tag htag? /[^A-Z]+/
                /[()\/A-Za-z]+/ {$Wrt}   ~
  
  seqlen:     ~ { $Out $In:[fields c:seq] $Wrt:[s:$l]}
                htag? tag htag? /[^A-Z]+/ {$l=0}
#                ( /[A-Za-z]/ {$l+=1} | /[\/()]/ )+  ~
                ( /[A-Za-z]/ {$l.+=1} | /[\/()]/ )+  ~
  
  prs:     ~ { $Out $In:[fields c:prs] }
              tag  /[A-Za-z]+/ {$Wrt}  ~ 
  
  appl:       ~ {$Out $In:[fields c:appl] }
                tag /[^-]+/ {$Uniq:$Ct.trim}
                ( '-' /[^-]+/ {$Uniq:$Ct.trim} )* ~
    
  methods:    ~ {$Out $In:[fields c:methods] }
                tag /[^-]+/ {$Uniq:$Ct.trim}
                ( '-' /[^-]+/ {$Uniq:$Ct.trim} )* ~
                
  target:     ~ {$In:[fields c:target] $Out}
                tag /[^\n]*/ {$Uniq:$Ct} ~  

  species:    ~ {$In:[fields c:species] $Out}
                tag /[^\n]*/ {$Uniq:$Ct}  ~  

  xWords:    ~ {$In:[fields c:{datas gened polym varians
  specifs tech bibl oloc}] $Out}
               tag (/[*a-zA-Z0-9+-]+/ {$Uniq:$Itc} | /./)* ~

  cprimer:   ~ {$In:[fields c:cprimer] $Out}
               tag /[^,]+/ {$Uniq:$Ct} (/[,;]/ /[^,]+/ {$Uniq:$Ct})* ~

  primers:   ~ {$In:[fields c:primers] $Out}
               tag (/[^,]+/ {$Uniq:$Ct})(/[,;]/ /[^,;]+/ {$Uniq:$Ct})* ~

  probes:   ~ {$In:[fields c:probes] $Out}
               tag (/[^,]+/ {$Uniq:$Ct})(/[,;]/ /[^,;]+/ {$Uniq:$Ct})* ~


  # table display
  t_fields:
  ~ { $Out $In:[fields c:{datas gened polym variants 
    name type target
    specifs tech bibl acc oloc} ] }
    htag? tag htag? /.*/ { $Wrt:$Itc }
  ~
  accXref:
  ~ { $Out $In:[ t_fields c:acc ] }
    /[^\n]+/ {$Wrt}
  ~
  accXrefNo:
  ~ { $Out $In:plain_accXref
      pre if:$InCount>1 $Fail
      $set=$Query:"[EMBL-ACC:$Ct]"
#      $if=$set.!=''
      if:$set.!=""
      {  $Wrt:[s:$set.size]
	 if:$set.size>1
	   $dp:"$Ct references ($set.size) EMBL entries !!!"
	 $set.delete
      }
      else
        $Wrt:[s:-1]
    }
    /.*/
  ~

  # HTML display
  h_fields:
  ~ { $In:[ fields t:html ] }
    tag? { $Rep:"<I>$Ct</I>" }
  ~
  plain_accXref:
  ~ { $In:[ fields c:acc t:html ] $Out }
    htag? tag htag? /[^\n]+/ { $Wrt }
  ~
  h_accXref:
  ~ { $In:[ fields c:acc t:html ] }
##### modified: from EMBL to GENBANK  - Rev. 1.2
#    htag? tag htag? /[^\n]+/ { $Rep:{ $ParStr:genbankR $Ct "EMBL_ACC:$Ct" } }
    htag? tag htag? /[^\n]+/ { $Rep:$Ct }
#    htag? tag htag? /[^\n]+/  
#{ $Rep:|<A HREF="http://srs.ebi.ac.uk/\
#                          |srs6bin/cgi-bin/\
#                          |wgetz?-e+[embl-AccNumber:$Ct]">$Ct</A>\
#}
  ~

##### added plain_pmidXref and h_pmidXref as hyperlink to PMID - Rev. 1.2
#  plain_pmidXref:
#  ~ { $In:[ fields c:bibl t:html ] $Out }
#   (htag? tag htag? (/[a-zA-Z0-9+-.:,;-]+/)+ ('PMID: ' /[0-9]+/ { $Wrt })?)*
#  ~
  plain_pmidXref: ~ { $In:[ fields c:bibl t:html ] $Out }
  tag 
  mp_bibref ('[PMID' punct number {$Rep:$Hlink:[medlineR p:{$Ct $Ct}]} ']' )?
  (dash mp_bibref ('PMID' punct number {$Rep:$Hlink:[medlineR p:{$Ct $Ct}]} )? )*
 ~

  h_pmidXref: ~ { $In:[fields c:bibl t:html] }
  htag? tag htag?
  mp_bibref ('[PMID' punct number {$Rep:$Hlink:[medlineR p:{$Ct $Ct}]} ']' )?
  (dash mp_bibref ('PMID' punct number {$Rep:$Hlink:[medlineR p:{$Ct $Ct}]} )? )*
  ~

#link from complemetary primer to oligoname

  h_primer: ~ {$In:[fields c:cprimer t:html]}
              htag? tag htag? 
        /[^,\n]+/ {$Rep:"<A HREF=\"wgetz?-page+EntryPage+-e+[MOLPROBE-OligoName:$Ct]\">$Ct</A>"}
              (/[,;]/ /[^,;\n]+/ 
              {$Rep:"<A HREF=\"wgetz?-page+EntryPage+-e+[MOLPROBE-OligoName:$Ct]\">$Ct</A>"})* ~

#link from probes to oligoname

  h_probes: ~ {$In:[fields c:probes t:html]}
              htag? tag htag?
        /[^,\n]+/ {$Rep:"<A HREF=\"wgetz?-page+EntryPage+-e+[MOLPROBE-OligoName:$Ct]\">$Ct</A>"}
              (/[,;]/ /[^,;\n]+/
              {$Rep:"<A HREF=\"wgetz?-page+EntryPage+-e+[MOLPROBE-OligoName:$Ct]\">$Ct</A>"})* ~


#link from primers to oligoname

  h_primers: ~ {$In:[fields c:primers t:html]}
              htag? tag htag?
              /[^,\n]+/ {$Rep:|<A HREF="wgetz?-page+EntryPage+-e+[MOLPROBE-OligoName:$Ct]">$Ct</A>\
                       }
              (/[,;]/ /[^,;\n]+/
              {$Rep:|<A HREF="wgetz?-page+EntryPage+-e+[MOLPROBE-OligoName:$Ct]">$Ct</A>\
                       })* ~
               
#link from targetgene to HSAGENES

  h_target: ~ {$In:[fields c:target t:html]}
              htag? tag htag?
#              /[^,\n]+/ {$Rep:|<A HREF="wgetz?-e+[HSAGENES-ID:$Ct]">$Ct</A>\
              /[^,\n]+/ {$Rep:$Ct } ~

# others
  htag:      ~ /<[^>]+>/ ~
  tag:       ~ /[^ ][a-zA-Z0-9\/]+:/ ~
# mp_bibref:    ~  ( word {$dp:"mp_basic:word:$Ct"} |
#                    number {$dp:"mp_basic:number:$Ct"} |
#                    punct {$dp:"mp_basic:punct:$Ct"} |
#                    dash {$dp:"mp_basic:dash:$Ct"} )+ ~
  mp_bibref:    ~  ( word | number | punct | dash )+ ~
  punct:     ~ /[,;\.:]/ ~
  word:      ~ /[a-zA-Z]+/ ~
  number:    ~ /[0-9]+/ ~
  ln:        ~ /[^\n]*\n/ ~
  dash:      ~ /-/ ~
}


# debugging
# if:$TestMode {
#   $job = $Job:[ prod:$rules skip:' \n'
# 		fileName:'/srs/data/molprobe'
# 	      ]
#   if:$ArgN<2
#     $t='entry'
#   else
#     $t=$Arg:2
#   $job.task:[html set:on]
#   $while=$job.hasInput
#   {
#     $job.tokens:[ name:$t
#  		  print:y]
#    $job.reset
#    $dp:" ----- end of entry ----- "
#  }
# }