Ignore:
Timestamp:
Jul 8, 2010 2:13:35 PM (14 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/scripts/analyzer.tcl

    r1792 r1797  
    6767    protected method _simOutput {message}
    6868    protected method _resultTooltip {}
    69     protected method _istrajectory {data}
    70     protected method _trajtosequence {xmlobj {path ""}}
     69    protected method _isPdbTrajectory {data}
     70    protected method _isLammpsTrajectory {data}
     71    protected method _pdbToSequence {xmlobj path id child data}
     72    protected method _lammpsToSequence {xmlobj path id child data}
     73    protected method _trajToSequence {xmlobj {path ""}}
    7174
    7275    private variable _tool ""          ;# belongs to this tool
     
    520523    # detect molecule elements that contain trajectory data and convert
    521524    # to sequences.
    522     _trajtosequence $xmlobj output
     525    _trajToSequence $xmlobj output
    523526
    524527    # go through the analysis and find all result sets
     
    11461149
    11471150# ----------------------------------------------------------------------
    1148 # USAGE: _istrajectory <data>
     1151# USAGE: _isPdbTrajectory <data>
    11491152#
    11501153# Used internally to determine whether pdb or lammps data represents a
    11511154# trajectory rather than a single frame
    11521155# ----------------------------------------------------------------------
    1153 itcl::body Rappture::Analyzer::_istrajectory {data} {
    1154   set nmodels 0
    1155   foreach line [split $data "\n"] {
    1156     if {[regexp "^MODEL" $line] || [regexp "^ITEM: TIMESTEP" $line]} {
    1157       incr nmodels
    1158       if {$nmodels > 1} {
    1159         # Stop if more than one model found.  No need to count them all.
    1160         return 1
    1161       }
    1162     }
    1163   }
    1164   return 0
    1165 
     1156itcl::body Rappture::Analyzer::_isPdbTrajectory {data} {
     1157    if { [llength $data]  == 0 } {
     1158        return 0
     1159    }
     1160    set nModels 0
     1161    foreach line $data {
     1162        if { [string match "MODEL*" $line] }  {
     1163            incr nModels
     1164            if { $nModels > 1 } {
     1165                # Stop if more than one model found.  No need to count them
     1166                # all.
     1167                return 1
     1168            }
     1169        }
     1170    }
     1171    return 0
     1172}
     1173
     1174# ----------------------------------------------------------------------
     1175# USAGE: _isLammpsTrajectory <data>
     1176#
     1177# Used internally to determine whether pdb or lammps data represents a
     1178# trajectory rather than a single frame
     1179# ----------------------------------------------------------------------
     1180itcl::body Rappture::Analyzer::_isLammpsTrajectory { data } {
     1181    if { [llength $data]  == 0 } {
     1182        return 0
     1183    }
     1184    set nModels 0
     1185    foreach line $data {
     1186        if { [regexp {^[\t ]*ITEM:[ \t]+TIMESTEP} $line] } {
     1187            incr nModels
     1188            if { $nModels > 1 } {
     1189                # Stop if more than one model found.  No need to count them
     1190                # all.
     1191                return 1
     1192            }
     1193        }
     1194    }
     1195    return 0
    11661196}
    11671197
     
    11691199# USAGE: _trajtosequence <xmlobj> ?<path>?
    11701200#
     1201    # If the molecule element is a trajectory, delete the original
     1202    # and create a sequence of individual molecules.
    11711203# Used internally to detect any molecule output elements that contain
    11721204# trajectory data.  Trajectories will be converted into sequences of
     
    11741206# the entire xml tree if a starting path is not specified.
    11751207# ----------------------------------------------------------------------
    1176 itcl::body Rappture::Analyzer::_trajtosequence {xmlobj {path ""}} {
    1177 
    1178   # Remove leading dot from path, if present.
    1179   if {[string index $path 0] == "."} {
    1180     set path [string range $path 1 end]
    1181   }
    1182 
    1183   set clist [$xmlobj children $path]
    1184   # Return if leaf node.
    1185   if {"" == $clist} {
    1186     return
    1187   } else {
     1208itcl::body Rappture::Analyzer::_pdbToSequence {xmlobj path id child data} {
     1209
     1210    set seqLabel [$xmlobj get ${child}.about.label]
     1211    set descr    [$xmlobj get ${child}.about.description]
     1212    set formula  [$xmlobj get ${child}.components.molecule.formula]
     1213    $xmlobj remove $child
     1214
     1215    set seqPath  $path.sequence($id)
     1216    $xmlobj put ${seqPath}.about.label $seqLabel
     1217    $xmlobj put ${seqPath}.about.description $descr
     1218    $xmlobj put ${seqPath}.index.label "Frame"
     1219
     1220    set frameNum 0
     1221    set frameContents ""
     1222    set inModel 0
     1223    foreach line $data {
     1224        set line [string trim $line]
     1225        if { $line == "" } {
     1226            continue;                   # Skip blank lines
     1227        }
     1228        if { [string match "MODEL*" $line] } {
     1229            if { $inModel && $frameContents != "" } {
     1230                # Dump the current contents into the last model
     1231
     1232                set framePath ${seqPath}.element($frameNum)
     1233                $xmlobj put ${framePath}.index $frameNum
     1234               
     1235                set molPath ${framePath}.structure.components.molecule
     1236                $xmlobj put ${molPath}.pdb $frameContents
     1237                $xmlobj put ${molPath}.formula $formula
     1238               
     1239                incr frameNum
     1240                set frameContents ""
     1241            }
     1242            set inModel 1
     1243        } elseif {[string match "ATOM*" $line] } {
     1244            if { !$inModel } {
     1245                puts stderr "found \"$line\" without previous MODEL line"
     1246                set inModel 1
     1247            }
     1248            append frameContents $line\n
     1249        }
     1250    }
     1251    if { $frameContents != "" } {
     1252        # Dump the current contents into the last model
     1253
     1254        set framePath ${seqPath}.element($frameNum)
     1255        $xmlobj put ${framePath}.index $frameNum
     1256       
     1257        set molPath ${framePath}.structure.components.molecule
     1258        $xmlobj put ${molPath}.pdb $frameContents
     1259        $xmlobj put ${molPath}.formula $formula
     1260    }
     1261}
     1262
     1263itcl::body Rappture::Analyzer::_lammpsToSequence {xmlobj path id child data} {
     1264
     1265    set seqLabel [$xmlobj get ${child}.about.label]
     1266    set descr    [$xmlobj get ${child}.about.description]
     1267    set typemap  [$xmlobj get ${child}.components.molecule.lammpstypemap]
     1268    $xmlobj remove $child
     1269
     1270    set seqPath ${path}.sequence($id)
     1271    $xmlobj put ${seqPath}.about.label $seqLabel
     1272    $xmlobj put ${seqPath}.about.description $descr
     1273    $xmlobj put ${seqPath}.index.label "Frame"
     1274
     1275    set frameNum 0
     1276    set frameContents ""
     1277    set inModel 0
     1278    foreach line $data {
     1279        set line [string trim $line]
     1280        if { $line == "" } {
     1281            continue;                   # Skip blank lines
     1282        }
     1283        if {[regexp {^[\t ]*ITEM:[ \t]+ATOMS} $line] } {
     1284            if { $inModel && $frameContents != "" } {
     1285                set framePath ${seqPath}.element($frameNum)
     1286                $xmlobj put ${framePath}.index $frameNum
     1287               
     1288                set molPath ${framePath}.structure.components.molecule
     1289                $xmlobj put ${molPath}.lammps $frameContents
     1290                $xmlobj put ${molPath}.lammpstypemap $typemap
     1291               
     1292                incr frameNum
     1293                set frameContents ""
     1294            }
     1295            set inModel 1
     1296        } elseif { [scan $line "%d %d %f %f %f" a b c d e] == 5 } {
     1297            if { !$inModel } {
     1298                puts stderr "found \"$line\" without previous \"ITEM: ATOMS\""
     1299                set inModel 1
     1300            }
     1301            append frameContents $line\n
     1302        }
     1303    }
     1304    if { $frameContents != "" } {
     1305        set framePath ${seqPath}.element($frameNum)
     1306        $xmlobj put ${framePath}.index $frameNum
     1307       
     1308        set molPath ${framePath}.structure.components.molecule
     1309        $xmlobj put ${molPath}.lammps $frameContents
     1310        $xmlobj put ${molPath}.lammpstypemap $typemap
     1311    }
     1312}
     1313
     1314# ----------------------------------------------------------------------
     1315# USAGE: _trajtosequence <xmlobj> ?<path>?
     1316#
     1317#       Check for PDB and LAMMPS trajectories in molecule data and rewrite
     1318#       the individual models as a sequence of molecules.  Used internally
     1319#       to detect any molecule output elements that contain trajectory data.
     1320#       Trajectories will be converted into sequences of individual molecules.
     1321#       All other elements will be unaffected. Scans the entire xml tree if a
     1322#       starting path is not specified. 
     1323#
     1324# ----------------------------------------------------------------------
     1325itcl::body Rappture::Analyzer::_trajToSequence {xmlobj {path ""}} {
     1326    puts stderr "path=$path"
     1327    # Remove leading dot from path, if present.
     1328    if { [string index $path 0] == "." } {
     1329        set path [string range $path 1 end]
     1330    }
    11881331    # Otherwise check each child.
    1189     foreach {child} $clist {
    1190       set type [$xmlobj element -as type $path.$child]
    1191       set id [$xmlobj element -as id $path.$child]
    1192       if {[regexp -nocase "^structure" $child]} {
    1193 
    1194         # Look for trajectory if molecule element found.
    1195         # Check both pdb data and lammps data.
    1196 
    1197         set pdbdata [$xmlobj get $path.$child.components.molecule.pdb]
    1198         if {"" != $pdbdata} {
    1199           if {[_istrajectory $pdbdata]} {
    1200             # If the molecule element is a trajectory, delete the
    1201             # original and create a sequence of individual molecules.
    1202             set sequencepath $path.sequence($id)
    1203             set seqlabel [$xmlobj get $path.$child.about.label]
    1204             set descr [$xmlobj get $path.$child.about.description]
    1205             set formula [$xmlobj get $path.$child.components.molecule.formula]
    1206             $xmlobj put $sequencepath.about.label $seqlabel
    1207             $xmlobj put $sequencepath.about.description $descr
    1208             $xmlobj put $sequencepath.index.label "Frame"
    1209             $xmlobj remove $path.$child
    1210             set framenum 0
    1211             set framecontents ""
    1212             foreach line [split $pdbdata "\n"] {
    1213               if {[regexp "^MODEL" $line] && $framecontents != ""} {
    1214                 set framepath $sequencepath.element($framenum)
    1215                 set molpath $framepath.structure.components.molecule
    1216                 $xmlobj put $molpath.pdb $framecontents
    1217                 $xmlobj put $molpath.formula $formula
    1218                 $xmlobj put $framepath.index $framenum
    1219                 incr framenum
    1220                 set framecontents ""
    1221               }
    1222               if {[regexp "^ATOM" $line]} {
    1223                 append framecontents $line\n
    1224               }
    1225             }
    1226           }
    1227         }
    1228 
    1229         set lammpsdata [$xmlobj get $path.$child.components.molecule.lammps]
    1230         if {"" != $lammpsdata} {
    1231           if {[_istrajectory $lammpsdata]} {
    1232             set sequencepath $path.sequence($id)
    1233             set seqlabel [$xmlobj get $path.$child.about.label]
    1234             set descr [$xmlobj get $path.$child.about.description]
    1235             set typemap [$xmlobj get $path.$child.components.molecule.lammpstypemap]
    1236             $xmlobj put $sequencepath.about.label $seqlabel
    1237             $xmlobj put $sequencepath.about.description $descr
    1238             $xmlobj put $sequencepath.index.label "Frame"
    1239             $xmlobj remove $path.$child
    1240             set framenum 0
    1241             set framecontents ""
    1242             foreach line [split $lammpsdata "\n"] {
    1243               if {[regexp "^ITEM: ATOMS" $line] && $framecontents != ""} {
    1244                 set framepath $sequencepath.element($framenum)
    1245                 set molpath $framepath.structure.components.molecule
    1246                 $xmlobj put $molpath.lammps $framecontents
    1247                 $xmlobj put $molpath.lammpstypemap $typemap
    1248                 $xmlobj put $framepath.index $framenum
    1249                 incr framenum
    1250                 set framecontents ""
    1251               }
    1252               if {[scan $line "%d %d %f %f %f" a b c d e] == 5} {
    1253                 append framecontents $line\n
    1254               }
    1255             }
    1256           }
    1257         }
    1258       }
    1259       # Recurse over all child nodes.
    1260       _trajtosequence $xmlobj $path.$child
    1261     }
    1262   }
    1263 
     1332    foreach child [$xmlobj children $path] {
     1333        set current ${path}.${child}
     1334        if { [string match "structure*" $child] } {
     1335            # Look for trajectory if molecule element found.  Check both pdb
     1336            # data and lammps data.
     1337            set type [$xmlobj element -as type $current]
     1338            set id   [$xmlobj element -as id $current]
     1339            set pdbdata    [$xmlobj get ${current}.components.molecule.pdb]
     1340            set lammpsdata [$xmlobj get ${current}.components.molecule.lammps]
     1341            if { $pdbdata != "" && $lammpsdata != "" } {
     1342                puts stderr \
     1343                    "found both <pdb> and <lammps> elements: picking pdb"
     1344            }
     1345            set pdbdata [split $pdbdata \n]
     1346            set lammpsdata [split $lammpsdata \n]
     1347            if { [_isPdbTrajectory $pdbdata] } {
     1348                _pdbToSequence $xmlobj $path $id $current $pdbdata
     1349            } elseif { [_isLammpsTrajectory $lammpsdata] } {
     1350                _lammpsToSequence $xmlobj $path $id $current $lammpsdata
     1351            }
     1352            continue
     1353        }
     1354        # Recurse over all child nodes.
     1355        _trajToSequence $xmlobj $current
     1356    }
    12641357}
    12651358
Note: See TracChangeset for help on using the changeset viewer.