Пример #1
0
def getChemCompCoordXmlFile(repository,
                            chemCompCoordPath,
                            sourceName,
                            molType,
                            ccpCode,
                            showError=None,
                            copyFile=True):
    """
  Fetch chemCompCoord 'sourceName', 'molType', 'ccpCode' to local repository 'repository'
  from repository defined by chemCompPath
  showError is an error display function, passed in were appropriate for GUI
  contexts
  Returns name of copied file, or None if unsuccessful
  """

    fileSearchString = "%s+%s+%s+*.xml" % (getCcpFileString(sourceName),
                                           molType, getCcpFileString(ccpCode))
    fileSearchPath = getChemCompCoordArchiveXmlFilePath(
        chemCompCoordPath, sourceName, molType, ccpCode)

    className = 'ChemCompCoord'
    identifier = '%s.%s.%s' % (sourceName, molType, ccpCode)

    return getChemCompOrCoordXmlFile(repository, fileSearchString,
                                     fileSearchPath, className, identifier,
                                     showError, copyFile)
Пример #2
0
def getNewChemCompOrCoordFilePath(chemCompOrCoord, mode = 'test'):


  """
  Gets a new file path for the XML file for a chemComp(Coord) object.
    
  mode        If 'test' will use test/
              If 'archive' will use archive/
              If 'obsolete' will use obsolete/

  """

  molType = chemCompOrCoord.molType
  ccpCode = chemCompOrCoord.ccpCode
  guid = chemCompOrCoord.guid
  
  xmlFileName = "%s+%s+%s.xml" % (molType,getCcpFileString(ccpCode),guid)
  
  if mode == 'obsolete':
    xmlFileName += ".%s" % time.strftime("%Y_%m_%d_%H_%M_%S")

  if chemCompOrCoord.className == 'ChemCompCoord':
  
    xmlFileName = "%s+%s" % (chemCompOrCoord.sourceName,xmlFileName)

    if mode == 'test':
      dataDir = testChemCompCoordDataDir
    elif mode == 'obsolete':
      dataDir = obsoleteChemCompCoordDataDir
    elif mode == 'archive':
      dataDir = editChemCompCoordDataDir
    elif mode == 'archiveLink':
      dataDir = editLinkedChemCompCoordDataDir

    xmlFileDir = getChemCompCoordArchiveXmlFilePath(dataDir,chemCompOrCoord.sourceName,molType,ccpCode)

  else:
  
    if mode == 'test':
      dataDir = testChemCompDataDir
    elif mode == 'obsolete':
      dataDir = obsoleteChemCompDataDir
    elif mode == 'archive':
      dataDir = editChemCompDataDir
    elif mode == 'archiveLink':
      dataDir = editLinkedChemCompDataDir
      
    xmlFileDir = getChemCompArchiveXmlFilePath(dataDir,molType,ccpCode)
  
  # Create if doesn't exist yet!!!
  if not os.path.exists(xmlFileDir):
    os.makedirs(xmlFileDir)

  xmlFilePath = os.path.join(xmlFileDir,xmlFileName)

  return xmlFilePath
Пример #3
0
def getChemCompCoordFilePath(archiveDataDir,sourceName,molType,ccpCode):

  fileSearchString = "%s+%s+%s+*.xml" % (sourceName,molType,getCcpFileString(ccpCode))
  searchPath = getChemCompCoordArchiveXmlFilePath(archiveDataDir,sourceName,molType,ccpCode)
    
  xmlFileNameMatches = glob.glob(os.path.join(searchPath,fileSearchString))

  xmlFileName = None
  if xmlFileNameMatches:
    xmlFileName = xmlFileNameMatches[-1]

  return xmlFileName
Пример #4
0
def getTopObjectFile(topObject):
    """Get topObject file name (not path)
  where topObject can be of class MemopsRoot or TopObject
  """

    from memops.general.Io import getCcpFileString

    if topObject.root is topObject:
        # This is MemopsRoot
        result = topObject.name + fileSuffix

    else:
        ll = [getCcpFileString(str(x)) for x in topObject.getFullKey()]
        ll.append(topObject.guid + fileSuffix)
        result = separatorFileNameChar.join(ll)

    return result[-254:]
Пример #5
0
def saveTemporaryChemCompOrCoord(chemCompOrCoord,testMode = True, isFresh = False):

  """
  Saves the XML for a chemComp(Coord) object to a temporary directory.
  
  testMode          If False, will use archive/, otherwise will use test/ for existingFilePath.  
  isFresh           If False, will remove tag that indicates chemComp(Coord) generated from scratch
                    If True, will set the tag.
  """
  
  #
  # Set/remove tag that indicates whether chemComp is newly generated or not
  #

  if isFresh:
    tagAsFresh(chemCompOrCoord)
  else:
    removeFreshTag(chemCompOrCoord)


  if chemCompOrCoord.className == 'ChemCompCoord':
    className = 'ChemCompCoord'
  else:
    className = 'ChemComp'
  
  tempRepositoryName = 'temporaryChemComp'
  tempRepository = chemCompOrCoord.root.findFirstRepository(name = tempRepositoryName)
  
  if not tempRepository:
    tempRepository = chemCompOrCoord.root.newRepository(name= tempRepositoryName, 
                                                        url=Implementation.Url(path=tempChemCompDir))

  curStdOut = sys.stdout
  textPipe = TextPipe([])
  sys.stdout = textPipe
  chemCompOrCoord.saveTo(tempRepository)
  sys.stdout = curStdOut
  #sys.stdout = sys.__stdout__
  
  print
  print "  *** CCPN save output ***"
  for text in textPipe.textArea:
    if text.strip():
      print "    %s" % text.strip()
  print
  
  # This was failing horribly in large runs, now fixed (Wim 2010/03/08)
  filePath = glob.glob(os.path.join(tempRepository.url.path,'ccp','molecule',className,"*%s*%s*.xml" % (chemCompOrCoord.molType,getCcpFileString(chemCompOrCoord.ccpCode))))[0]
  
  (localPath,localFileName) = os.path.split(filePath)
    
  existingFilePath = findChemCompOrCoordFilePath(chemCompOrCoord, testMode = testMode)
  
  return (filePath,existingFilePath)
Пример #6
0
def getChemComp(project,
                molType,
                ccpCode,
                download=True,
                showError=None,
                partialLoad=False,
                chemCompArchiveDir=None,
                copyFile=True):
    """ get ChemComp corresponding to molType,ccpCode, 
  looking 1) in memory, 2) in Repositories on lookup path,
  3) in chemCompArchiveDir directory, 4) downloading from msd ChemComp server.
  For 3) and 4) save new ChemComp in first Repository on PAckageLocator lookup path
  Do 4) only if download==True
  
  showError is an optional GUI error handler that can be passed in.
  partialLoad controls if only the TopObject (default) or the entire file is loaded
  
  copyFile can be set to False to avoid copying file from central archive (good for testing)
  
  Optimised to avoid mass reading.
  """

    # First get it if already loaded
    chemComp = project.getByNavigation(('chemComps', (molType, ccpCode)))

    if chemComp is None:
        # try to load it from an existing repository - avoiding mass loading
        packageName = 'ccp.molecule.ChemComp'
        chemCompFileSearchString = "%s+%s+*.xml" % (molType,
                                                    getCcpFileString(ccpCode))

        chemCompXmlFile = findCcpXmlFile(project, packageName,
                                         chemCompFileSearchString)
        if chemCompXmlFile:
            chemComp = XmlIO.loadFromFile(project,
                                          chemCompXmlFile,
                                          partialLoad=partialLoad)

        if chemComp is None:
            # try to get it from chemCompArchiveDir directory, if any, or to download it.
            # Use custom directory if it was passed in!!!!
            if not chemCompArchiveDir:
                chemCompPath = getChemCompArchiveDataDir()
            else:
                chemCompPath = chemCompArchiveDir

            ccLocator = (
                project.findFirstPackageLocator(targetName=packageName)
                or project.findFirstPackageLocator(targetName='any'))
            repository = ccLocator.findFirstRepository()

            fileFound = getChemCompXmlFile(repository,
                                           chemCompPath,
                                           molType,
                                           ccpCode,
                                           showError=showError,
                                           copyFile=copyFile)
            if not fileFound and download:
                fileFound = downloadChemCompXmlFile(repository,
                                                    molType,
                                                    ccpCode,
                                                    showError=showError)
            if fileFound:
                chemComp = XmlIO.loadFromFile(project,
                                              fileFound,
                                              partialLoad=partialLoad)
    #
    return chemComp
Пример #7
0
def downloadChemCompXmlFile(repository, molType, ccpCode, showError=None):
    """
  Fetch chemComp 'molType', 'ccpCode' to local repository 'repository'
  from chemCompServer
  showError is an error display function, passed in were appropriate for GUI
  contexts
  Returns name of copied file, or None if unsuccessful
  """

    if showError is None:
        showError = uniIo.printError

    result = None

    chemCompXmlFilePatt = re.compile("(" + molType + "\+" +
                                     getCcpFileString(ccpCode) +
                                     "\+[^\s\"\>]+\.xml)")

    try:
        urlLocation = "http://%s%s" % (chemCompServer,
                                       os.path.join(chemCompWebPath, molType))

        r1 = urllib.urlopen(os.path.join(urlLocation, "files.txt"))

        try:
            data = r1.read()
            r1.close()

            dataLines = data.split("\n")
            chemCompXmlFile = None
            for dataLine in dataLines:
                chemCompSearch = chemCompXmlFilePatt.search(dataLine)
                if chemCompSearch:
                    chemCompXmlFile = chemCompSearch.group(1)

            if chemCompXmlFile:
                urlLocation = os.path.join(urlLocation, chemCompXmlFile)
                r2 = urllib.urlopen(urlLocation)

                try:
                    data = r2.read()
                    r2.close()

                    try:
                        saveChemCompPath = repository.getFileLocation(
                            'ccp.molecule.ChemComp')
                        if not os.path.exists(saveChemCompPath):
                            os.makedirs(saveChemCompPath)

                        chemCompFile = os.path.join(saveChemCompPath,
                                                    chemCompXmlFile)
                        fout = open(chemCompFile, 'w')
                        fout.write(data)
                        fout.close()

                        print(
                            "Downloaded chemComp %s, %s from server %s, written to file %s!"
                            % (molType, ccpCode, chemCompServer, chemCompFile))
                        result = chemCompFile

                    except IOError, e:
                        showError(
                            "Cannot write file",
                            "Cannot write chemComp XML file %s, %s: %s" %
                            (molType, ccpCode, str(e)))

                except IOError, e:
                    showError(
                        "Cannot read file", "Cannot read chemComp %s, %s: %s" %
                        (molType, ccpCode, str(e)))

            else:
                showError(
                    "Cannot find file",
                    "Cannot find chemComp XML file %s, %s." %
                    (molType, ccpCode))
Пример #8
0
def copyBaseToModifiedFile(project,
                           newBaseUnit,
                           origBaseUnit,
                           testMode,
                           repository,
                           replace=False):

    #
    # Reset the still empty new base unit parameters
    #

    newBaseUnit.__dict__['isLoaded'] = False
    newBaseUnit.__dict__['isModified'] = False
    newBaseUnit.__dict__['isStored'] = True

    #
    # Some pattern for identifying elements that have to be changed in the original file
    #

    ccpCodePatt = re.compile("ccpCode=\"([^ ]+)\"")
    namePatt = re.compile("name=\"([^ ]+)\"")
    guidPatt = re.compile("guid=\"([^ ]+)\"")

    #
    # Get the template (original) base unit file location
    #

    print origBaseUnit, testMode

    origFilePath = findChemCompOrCoordFilePath(origBaseUnit, testMode=testMode)

    #
    # Get the repository path for the new base unit
    #

    packageName = newBaseUnit.packageLocator.targetName
    repPath = repository.getFileLocation(packageName)

    if not os.path.exists(repPath):
        os.makedirs(repPath)

    #
    # Find out the file path for the new base unit
    #

    baseFileName = "%s" % (newBaseUnit.molType)
    if newBaseUnit.className == 'ChemCompCoord':
        baseFileName = "%s+" % (getCcpFileString(
            newBaseUnit.sourceName)) + baseFileName

    newFilePath = os.path.join(
        repPath,
        "%s+%s+" % (baseFileName, getCcpFileString(newBaseUnit.ccpCode)))
    newFilePath += "%s.xml" % newBaseUnit.guid

    #
    # Read in the template file and modify it for the new base unit
    #

    fin = open(origFilePath)
    lines = fin.readlines()
    fin.close()

    for lindex in range(len(lines)):
        line = lines[lindex]

        if line.count('<CHEM.NonStdChemComp') or line.count(
                "<CCCO.ChemCompCoord"):

            ccpCodeSearch = ccpCodePatt.search(line)
            line = line.replace('ccpCode="%s"' % ccpCodeSearch.group(1),
                                'ccpCode="%s"' % newBaseUnit.ccpCode)

            # Only relevant if name set to ccpCode...
            nameSearch = namePatt.search(line)
            if nameSearch and nameSearch.group(1) == ccpCodeSearch.group(1):
                line = line.replace('name="%s"' % nameSearch.group(1),
                                    'name="%s"' % newBaseUnit.ccpCode)

            guidSearch = guidPatt.search(line)
            line = line.replace('guid="%s"' % guidSearch.group(1),
                                'guid="%s"' % newBaseUnit.guid)

            lines[lindex] = line

            break

    #
    # If new file already exists, only replace it if replace flag set to True
    #

    if os.path.exists(newFilePath):
        if replace:
            os.remove(newFilePath)
        else:
            print "  Modified chemComp already exists - aborting..."
            return False

    #
    # Write out the new base unit file
    #

    fout = open(newFilePath, 'w')
    fout.write(''.join(lines))
    fout.close()

    return True
Пример #9
0
        if not customCcpCodeList:
            targetCcpCodeList = getCcpCodeList(targetDir, prefix=prefix)
        else:
            targetCcpCodeList = customCcpCodeList

        if prefix:
            prefixText = "%s+" % prefix
        else:
            prefixText = ""

        for (molType, ccpCodes) in targetCcpCodeList:

            for ccpCode in ccpCodes:

                chemCompFileSearchString = "%s%s+%s+*.xml" % (
                    prefixText, molType, getCcpFileString(ccpCode))
                chemCompFileNameMatches = glob.glob(
                    os.path.join(targetDir, chemCompFileSearchString))

                if molType == 'other':
                    sourceSubDir = os.path.join(molType, ccpCode[0])
                else:
                    sourceSubDir = molType

                if customCcpCodeList and not chemCompFileNameMatches:
                    # See if new file, copy over directly in this case
                    chemCompFileNameMatches = glob.glob(
                        os.path.join(sourceDir, sourceSubDir,
                                     chemCompFileSearchString))

                if chemCompFileNameMatches: