You are here: Home V2 Software Software More ... Python Macro Scripts Export XPLOR distance restraints

Export XPLOR distance restraints

Export a CCPN distance restraint list as an XPLOR file

ExportXplorDists.py — Python Source, 4 kB (4908 bytes)

File contents

import re, os

def exportXplorDistances(argServer):
  
  argServer.showInfo('Select restraint set, restraint list and export file name')
  
  cSet = argServer.getConstraintSet()
  
  if not cSet:
    return

  constraintList = argServer.getConstraintList(cSet)
  if not constraintList:
    return
  
  useIupac = argServer.askYesNo('Use IUPAC atom names?')
  
  name = constraintList.name or constraintList.className
  defaultName = 'CCPN_%d_%d_%s.tbl' % (cSet.serial, constraintList.serial, name)
  fileName = argServer.askString('Output XPLOR file name', defaultName)
  
  if not fileName:
    return
  
  constraints = constraintList.sortedConstraints()

  resonanceList = []
  useSegIds = False
  chains = set()

  fileHandle = open(fileName, 'w')
  for c in constraints:
    
    resonances0 = set()
    resonances1 = set()
    for item in c.sortedItems():
      fixedResonances = list(item.resonances)
      rA, rB = [fr.resonance for fr in fixedResonances]
      
      if not (rA and rB):
        continue 
       
      resonances0.add(fixedResonances[0])
      resonances1.add(fixedResonances[1])
      
    resonances = []
    for resonance in resonances1:
      seqCode = 0
      resonanceSet = resonance.resonanceSet
      if resonanceSet:
        residue = resonanceSet.findFirstAtomSet().findFirstAtom().residue
        seqCode = residue.seqCode
        chains.add(residue.chain)
      resonances.append([seqCode,resonance])

    resonances.sort()
    resonancesA = [x[1] for x in resonances]
    
    resonances = []
    for resonance in resonances0:
      seqCode = 0
      resonanceSet = resonance.resonanceSet
      if resonanceSet:
        residue = resonanceSet.findFirstAtomSet().findFirstAtom().residue
        seqCode = residue.seqCode
        chains.add(residue.chain)
      resonances.append([seqCode,resonance])
    resonances.sort()
    resonancesB = [x[1] for x in resonances]

    resonanceList.append((c, resonancesA, resonancesB))

  if len(chains) > 1:
    if showYesNo('Query','Use SEGIDs in output?'):
      useSegIds = True
    else:
      useSegIds = False

  for c, resonancesA, resonancesB in resonanceList:
    for resonance in resonancesA:
      if resonance is resonancesA[0]:
        a = 'assign ( '
      else:
        a = '\t or '
        
      resonanceSet = resonance.resonanceSet
      if resonanceSet:
        atomSets = resonanceSet.sortedAtomSets()
        atom = atomSets[0].findFirstAtom()
        residue = atom.residue
         
        if resonance is resonanceSet.sortedResonances()[0]:
          name = atomSets[0].name
        else:
          name = atomSets[-1].name
          
        name = re.sub('\*','%',name)
        
        if (atom.chemAtom.elementSymbol == 'H') and not useIupac:
          chemAtomSet = atom.chemAtom.chemAtomSet
          if chemAtomSet and chemAtomSet.isEquivalent is False:
            if name[-1] == '3':
              name = name[:-1] + '1'
              
          elif not chemAtomSet:
            if name == 'H':
              name = 'HN'
 
        if useSegIds:
          b = 'segid %s and resid %d and name %s' % (residue.chain.code, residue.seqCode, name.upper())
        else:
          b = 'resid %d and name %s' % (residue.seqCode, name.upper())
      else:
        b = 'resid ? and name ?'
      fileHandle.write('%s%s\n' % (a,b))
    fileHandle.write('\t )\n')
    
    for resonance in resonancesB:
      if resonance is resonancesB[0]:
        a = '       ( '
      else:
        a = '\t or '
        
      resonanceSet = resonance.resonanceSet
      if resonance.resonanceSet:
        atomSets = resonanceSet.sortedAtomSets()
        atom = atomSets[0].findFirstAtom()
        residue  = atom.residue
         
        if resonance is resonanceSet.sortedResonances()[0]:
          name = atomSets[0].name
        else:
          name = atomSets[-1].name
          
        name = re.sub('\*','%',name)
        
        if (atom.chemAtom.elementSymbol == 'H') and not useIupac:
          chemAtomSet = atom.chemAtom.chemAtomSet
          if chemAtomSet and chemAtomSet.isEquivalent is False:
            if name[-1] == '3':
              name = name[:-1] + '1'
              
          elif not chemAtomSet:
            if name == 'H':
              name = 'HN'
          
        if useSegIds:
          b = 'segid %s and resid %d and name %s' % (residue.chain.code, residue.seqCode, name.upper())
        else:
          b = 'resid %d and name %s' % (residue.seqCode, name.upper() )
      else:
        b = 'resid ? and name ?'
      fileHandle.write('%s%s\n' % (a,b))
      
    deltaMin = abs(c.targetValue - c.lowerLimit)
    deltaMax = abs(c.targetValue - c.upperLimit)
    data = (c.targetValue,deltaMin,deltaMax)
    fileHandle.write('\t ) %.1f %.1f %.1f\n\n' % data)
  
  fileHandle.close()
  
  argServer.showInfo('Done.\n\nExported XPLOR restraint file to %s' % os.path.abspath(fileName))