wiki:EighthQuery
Last modified 11 years ago Last modified on 12/05/06 11:02:31

return to XbrainEvaluation

let $patients_phono_not_sem :=
dxq:csm("
let $include_codes := ('2')
let $exclude_codes := ('3','6')
let $inverse :=  for $p in $root/patient
 return  
 <patient>
 {$p/pnum}
 {$p/viq}
 {$p/sex}
 <age>{$p/age_at_registration/text()}</age> 
 {
  for $s in $p/surgery
  return 
  <surgery>
  {
   for $site in $s/stimsite
   let $oid := $site/_oid
   return
   <stimsite>
   { $site/surgery}
   { $site/type}
          {$site/site_label}
          { 
            for $sitemap_elt in $p/surgery/sitetoanatomymap[preference='1']/sitetoanatomymapelement[stimsite/_oid = $oid]
            return    
              <magnet_coordinates>
                <ant_coord>{$sitemap_elt/ant_coord/text()}</ant_coord>
                <sup_coord>{$sitemap_elt/sup_coord/text()}</sup_coord>
                <right_coord>{$sitemap_elt/right_coord/text()}</right_coord>
              </magnet_coordinates>
          }    
          { 
            for $anat_elt in $p/surgery/anatomytonamemap[preference='1']/anatomytonamemapelement[stimsite/_oid = $oid]
            return    
              <anatomical_name>{$anat_elt/term/fullname/text()}</anatomical_name>
          }    

   {
    for $trial in $p//trial[stimsite1/_oid = $oid]
    return
    <trial>
    {
     $trial/node()[name() != 'stimsite1']
    }
    </trial>
   }
   </stimsite>
  }
  </surgery>
 }
 </patient>

return
<result>
{
for $p in $inverse
let $anatomical_regions :=
    for $anatomical_name in distinct-values($p/surgery/stimsite/anatomical_name)
    let $sites_with_name := $p/surgery/stimsite[anatomical_name=$anatomical_name]
    let $codes_for_sites := $sites_with_name/trial/trialcode/term[type='CSM error code']/abbrev/text()
    return
    if($codes_for_sites=$include_codes and not($codes_for_sites=$exclude_codes))
    then
    <region>
        <anatomical_name>{$anatomical_name}</anatomical_name>
        <stimsites>
           {
             for $site in $sites_with_name
             return
             <stimsite>{$site/site_label}{$site/magnet_coordinates}</stimsite>
           }
        </stimsites>
        <codes>
        {
                    for $code in distinct-values($sites_with_name/trial/trialcode/term[type='CSM error code']/abbrev/text())
                    return
                        <code>{$code}</code>
        }
        </codes>
     </region>
    else()
where exists($anatomical_regions)
return
<patient>
 {$p/pnum}
 {
 for $region in $anatomical_regions
 return
 $region
 }
</patient>
}
</result>
")

return
<results>
   {$patients_phono_not_sem/result/patient}
</results>