Example #1
0
def buildHan(uchHan):
    Common.say('Creating Han Definition file for ' + uchHan)

    hcf = Genome.HCF(Common.pathToURL(Common.makeHanPath(uchHan + Common.Constants.extHCF), Globals.urlHan))
    
    aryGroups = [ _HAN_GROUP % (str(hcfG.bounds), hcfG.length, hcfG.ptCenter.x, hcfG.ptCenter.y,
                            ' '.join([ str(i+1) for i in hcfG.containedStrokes])) for hcfG in hcf.aryGroups ]
    aryStrokes = [ _HAN_STROKE % (str(hcfS.bounds),
                                hcfS.length,
                                '\n'.join([ _HAN_POINT % (ptd.x, ptd.y, ptd.distance) for ptd in hcfS.aryPointsForward]),
                                '\n'.join([ _HAN_POINT % (ptd.x, ptd.y, ptd.distance) for ptd in hcfS.aryPointsReverse]) ) for hcfS in hcf.aryStrokes ]
    aryOverlaps = [ _HAN_OVERLAP % (hcfO.firstStroke, hcfO.secondStroke, hcfO.fRequired and 'true' or 'false') for hcfO in hcf.aryOverlaps ]
    strHan = _HAN_DEFINITION % (str(uuid.uuid4()).upper(), hcf.unicode, datetime.datetime.utcnow().isoformat(), _NAME,
                                str(hcf.bounds), hcf.length, hcf.minimumStrokeLength,
                                '\n'.join(aryGroups),
                                '\n'.join(aryStrokes),
                                aryOverlaps and ('<overlaps>%s</overlaps>' % '\n'.join(aryOverlaps)) or '')
                                
    strPath = Common.resolvePath(os.path.join(Globals.strArchetypePath, Common.makeHanPath(hcf.unicode + Common.Constants.extHan)))
    strDir = os.path.dirname(strPath)
    if not os.path.exists(strDir):
        try: os.makedirs(os.path.dirname(strPath))
        except OSError, err: raise Common.BiologicError('Unable to create %s - %s' % (strDir, str(err)))

    try:
        fileHan = os.open(strPath, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0664)
        os.write(fileHan, strHan)
    except IOError, err: raise Common.BiologicError('Unable to create %s - %s' % (strPath, str(err)))
    os.close(fileHan)
    
    Common.say('Han definition written to ' + strPath)
    return
Example #2
0
def createTable():
    aryEntries = [
        _CODONTABLE_ENTRY %
        (codon, vector,
         (_CODONTABLE_ISSTANDARD %
          (Globals.codonTable.hasStandardMapping(codon) and 'true'
           or 'false'))) for codon, vector in Globals.codonTable.entries()
    ]
    strCodonTable = _CODONTABLE_DEFINITION % (
        Globals.uuid, Globals.strAuthor and
        (" author='%s'" % Globals.strAuthor) or '',
        datetime.datetime.utcnow().isoformat(), _NAME, '\n'.join(aryEntries))

    if Globals.pathOutput:
        try:
            fileCodonTable = os.open(Globals.pathOutput,
                                     os.O_CREAT | os.O_TRUNC | os.O_WRONLY,
                                     0664)
            os.write(fileCodonTable, strCodonTable)
        except IOError, err:
            raise Common.BiologicError('Unable to create %s - %s' %
                                       (Globals.pathOutput, str(err)))
        os.close(fileCodonTable)

        Common.say('Codon table written to ' + Globals.pathOutput)
Example #3
0
def spawnStylus(fScript, dictEnv):
    if (Globals.strGenomes or Globals.strPlans) and Globals.fLaunchJobs:
        iJob = 0
        strExecArgs = 'STYLUS_EXECARGS' in dictEnv and dictEnv[
            'STYLUS_EXECARGS'] or ''
        strReportArgs = 'STYLUS_RPTARGS' in dictEnv and dictEnv[
            'STYLUS_RPTARGS'] or ''

        aryGenomes = Globals.strGenomes and fnmatch.filter(
            os.listdir(Globals.strGenomes),
            Common.Constants.globNotHidden) or []
        aryPlans = Globals.strPlans and fnmatch.filter(
            os.listdir(Globals.strPlans),
            Common.Constants.globNotHidden) or []

        if aryGenomes and not aryPlans:
            for fileGenome in aryGenomes:
                dictEnv['STYLUS_EXECARGS'] = '%s -g %s' % (
                    strExecArgs, os.path.join(Globals.strGenomes, fileGenome))
                dictEnv['STYLUS_RPTARGS'] = '%s -g %s' % (
                    strReportArgs, os.path.join(Globals.strGenomes,
                                                fileGenome))
                spawnSingle(fScript, iJob, dictEnv)
                iJob += 1

        elif not aryGenomes and aryPlans:
            for filePlan in aryPlans:
                dictEnv['STYLUS_EXECARGS'] = '%s -n ,,plan -p %s' % (
                    strExecArgs, os.path.join(Globals.strPlans, filePlan))
                dictEnv['STYLUS_RPTARGS'] = '%s -n ,,plan -p %s' % (
                    strReportArgs, os.path.join(Globals.strPlans, filePlan))
                spawnSingle(fScript, iJob, dictEnv)
                iJob += 1

        else:
            for fileGenome in aryGenomes:
                for filePlan in aryPlans:
                    dictEnv['STYLUS_EXECARGS'] = '%s -n ,,plan -g %s -p %s' % (
                        strExecArgs,
                        os.path.join(Globals.strGenomes, fileGenome),
                        os.path.join(Globals.strPlans, filePlan))
                    dictEnv['STYLUS_RPTARGS'] = '%s -n ,,plan -g %s -p %s' % (
                        strReportArgs,
                        os.path.join(Globals.strGenomes, fileGenome),
                        os.path.join(Globals.strPlans, filePlan))
                    spawnSingle(fScript, iJob, dictEnv)
                    iJob += 1

        if not Globals.fUseXgrid:
            Common.say('\tLaunched %d instances' % iJob)

    else:
        spawnSingle(fScript, 0, dictEnv)
Example #4
0
def createTable():
    aryEntries = [ _CODONTABLE_ENTRY % (codon, vector, (_CODONTABLE_ISSTANDARD % (Globals.codonTable.hasStandardMapping(codon) and 'true' or 'false'))) for codon, vector in Globals.codonTable.entries() ]
    strCodonTable = _CODONTABLE_DEFINITION % (Globals.uuid, Globals.strAuthor and (" author='%s'" % Globals.strAuthor) or '', datetime.datetime.utcnow().isoformat(), _NAME, '\n'.join(aryEntries))
    
    if Globals.pathOutput:
        try:
            fileCodonTable = os.open(Globals.pathOutput, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0664)
            os.write(fileCodonTable, strCodonTable)
        except IOError, err: raise Common.BiologicError('Unable to create %s - %s' % (Globals.pathOutput, str(err)))
        os.close(fileCodonTable)

        Common.say('Codon table written to ' + Globals.pathOutput)

    else:
        Common.say(strCodonTable, False, True)
    return
Example #5
0
def translateGenome():
    genome = Globals.genome

    oldCodonTable = genome.codonTable
    newCodonTable = Globals.codonTable

    bases = Codons.Constants.startCodon
    for i in xrange(3, len(genome.bases), 3):
        bases += newCodonTable.vectorToCodon(
            oldCodonTable.codonToVector(genome.bases[i:i + 3]))
    genome.bases = bases

    aryEntries = [
        _CODONTABLE_ENTRY %
        (codon, vector,
         (_CODONTABLE_ISSTANDARD %
          (Globals.codonTable.hasStandardMapping(codon) and 'true'
           or 'false'))) for codon, vector in Globals.codonTable.entries()
    ]
    aryStrokes = [
        _GENE_STROKE % (stroke.rgBases.baseFirst, stroke.rgBases.baseLast,
                        stroke.correspondsTo)
        for stroke in genome.gene.aryStrokes
    ]
    strGenome = _GENE_DEFINITION % (
        Globals.uuid, Globals.strAuthor and
        (" author='%s'" % Globals.strAuthor) or '',
        datetime.datetime.utcnow().isoformat(), _NAME, newCodonTable.uuid,
        newCodonTable.author and (" author='%s'" % newCodonTable.author)
        or '', newCodonTable.creationDate, newCodonTable.creationTool,
        '\n'.join(aryEntries), genome.bases, len(
            genome.bases), genome.gene.xOrigin, genome.gene.yOrigin,
        genome.gene.unicode, '\n'.join(aryStrokes))

    if Globals.pathOutput:
        try:
            fileGenome = os.open(Globals.pathOutput,
                                 os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0664)
            os.write(fileGenome, strGenome)
        except IOError, err:
            raise Common.BiologicError('Unable to create %s - %s' %
                                       (Globals.pathOutput, str(err)))
        os.close(fileGenome)

        Common.say('Genome written to ' + Globals.pathOutput)
Example #6
0
def main(argv=None):
    try:
        getArguments()

        if Globals.urlGenome:
            Common.say('Genome     : %s' % Globals.urlGenome)
        if Globals.urlCodonTable:
            Common.say('Codon Table: %s' % Globals.urlCodonTable)
        if Globals.pathOutput:
            Common.say('Output File: %s' % Globals.pathOutput)

        if Globals.urlGenome:
            Globals.genome = Genome.Genome(Globals.urlGenome)
        
        if Globals.urlCodonTable:
            Globals.codonTable = Genome.CodonTable(Globals.urlCodonTable)
        elif Globals.urlGenome and Globals.cmd <> Constants.cmdTranslate:
            Globals.codonTable = Globals.genome.codonTable

        if Globals.cmd == Constants.cmdCreate:
            createTable()
            
        elif Globals.cmd == Constants.cmdValidate:
            validateTable()
            
        elif Globals.cmd == Constants.cmdTranslate:
            translateGenome()

        return 0
        
    except Usage, err:
        Common.sayError(err)
        return 0
Example #7
0
def main(argv=None):
    try:
        getArguments()

        if Globals.urlGenome:
            Common.say('Genome     : %s' % Globals.urlGenome)
        if Globals.urlCodonTable:
            Common.say('Codon Table: %s' % Globals.urlCodonTable)
        if Globals.pathOutput:
            Common.say('Output File: %s' % Globals.pathOutput)

        if Globals.urlGenome:
            Globals.genome = Genome.Genome(Globals.urlGenome)

        if Globals.urlCodonTable:
            Globals.codonTable = Genome.CodonTable(Globals.urlCodonTable)
        elif Globals.urlGenome and Globals.cmd <> Constants.cmdTranslate:
            Globals.codonTable = Globals.genome.codonTable

        if Globals.cmd == Constants.cmdCreate:
            createTable()

        elif Globals.cmd == Constants.cmdValidate:
            validateTable()

        elif Globals.cmd == Constants.cmdTranslate:
            translateGenome()

        return 0

    except Usage, err:
        Common.sayError(err)
        return 0
Example #8
0
def translateGenome():
    genome = Globals.genome
    
    oldCodonTable = genome.codonTable
    newCodonTable = Globals.codonTable
    
    bases = Codons.Constants.startCodon
    for i in xrange(3,len(genome.bases),3):
        bases += newCodonTable.vectorToCodon(oldCodonTable.codonToVector(genome.bases[i:i+3]))
    genome.bases = bases
    
    aryEntries = [ _CODONTABLE_ENTRY % (codon, vector, (_CODONTABLE_ISSTANDARD % (Globals.codonTable.hasStandardMapping(codon) and 'true' or 'false'))) for codon, vector in Globals.codonTable.entries() ]
    aryStrokes = [ _GENE_STROKE % (stroke.rgBases.baseFirst, stroke.rgBases.baseLast, stroke.correspondsTo) for stroke in genome.gene.aryStrokes ]
    strGenome = _GENE_DEFINITION % (Globals.uuid, Globals.strAuthor and (" author='%s'" % Globals.strAuthor) or '', datetime.datetime.utcnow().isoformat(), _NAME,
                                    newCodonTable.uuid,
                                    newCodonTable.author and (" author='%s'" % newCodonTable.author) or '',
                                    newCodonTable.creationDate,
                                    newCodonTable.creationTool,
                                    '\n'.join(aryEntries),
                                    genome.bases,
                                    len(genome.bases),
                                    genome.gene.xOrigin, genome.gene.yOrigin,
                                    genome.gene.unicode,
                                    '\n'.join(aryStrokes))
    
    if Globals.pathOutput:
        try:
            fileGenome = os.open(Globals.pathOutput, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0664)
            os.write(fileGenome, strGenome)
        except IOError, err: raise Common.BiologicError('Unable to create %s - %s' % (Globals.pathOutput, str(err)))
        os.close(fileGenome)
        
        Common.say('Genome written to ' + Globals.pathOutput)

    else:
        Common.say(strGenome, False, True)
    return
Example #9
0
def spawnStylus(fScript, dictEnv):
    if (Globals.strGenomes or Globals.strPlans) and Globals.fLaunchJobs:
        iJob = 0
        strExecArgs = 'STYLUS_EXECARGS' in dictEnv and dictEnv['STYLUS_EXECARGS'] or ''
        strReportArgs = 'STYLUS_RPTARGS' in dictEnv and dictEnv['STYLUS_RPTARGS'] or ''
    
        aryGenomes = Globals.strGenomes and fnmatch.filter(os.listdir(Globals.strGenomes), Common.Constants.globNotHidden) or []
        aryPlans = Globals.strPlans and fnmatch.filter(os.listdir(Globals.strPlans), Common.Constants.globNotHidden) or []
    
        if aryGenomes and not aryPlans:
            for fileGenome in aryGenomes:
                dictEnv['STYLUS_EXECARGS'] = '%s -g %s' % (strExecArgs, os.path.join(Globals.strGenomes, fileGenome))
                dictEnv['STYLUS_RPTARGS'] = '%s -g %s' % (strReportArgs, os.path.join(Globals.strGenomes, fileGenome))
                spawnSingle(fScript, iJob, dictEnv)
                iJob += 1
    
        elif not aryGenomes and aryPlans:
            for filePlan in aryPlans:
                dictEnv['STYLUS_EXECARGS'] = '%s -n ,,plan -p %s' % (strExecArgs, os.path.join(Globals.strPlans, filePlan))
                dictEnv['STYLUS_RPTARGS'] = '%s -n ,,plan -p %s' % (strReportArgs, os.path.join(Globals.strPlans, filePlan))
                spawnSingle(fScript, iJob, dictEnv)
                iJob += 1
    
        else:
            for fileGenome in aryGenomes:
                for filePlan in aryPlans:
                    dictEnv['STYLUS_EXECARGS'] = '%s -n ,,plan -g %s -p %s' % (strExecArgs, os.path.join(Globals.strGenomes, fileGenome), os.path.join(Globals.strPlans, filePlan))
                    dictEnv['STYLUS_RPTARGS'] = '%s -n ,,plan -g %s -p %s' % (strReportArgs, os.path.join(Globals.strGenomes, fileGenome), os.path.join(Globals.strPlans, filePlan))
                    spawnSingle(fScript, iJob, dictEnv)
                    iJob += 1
        
        if not Globals.fUseXgrid:        
            Common.say('\tLaunched %d instances' % iJob)
    
    else:
        spawnSingle(fScript, 0, dictEnv)
Example #10
0
def doBackup():
    Common.say('Archving folders [ %s ] in %s' %
               (', '.join(Globals.aryFolders), Globals.pathSrc))

    dtNow = datetime.datetime.now()
    dtWeekAgo = dtNow - datetime.timedelta(days=Globals.nRemoveAge)

    # Purge archives and archive directories older than one week
    for item in glob.glob(os.path.join(Globals.pathDst,
                                       Constants.strArchives)):
        stat = os.stat(item)
        if datetime.datetime.fromtimestamp(stat.st_mtime) < dtWeekAgo:
            if os.path.isdir(item):
                cc = os.system('rm -drf %s' % item)
                if cc:
                    raise Common.BiologicError(
                        'Unable to remove old directory %s' % item)
            else:
                os.remove(item)

    # Create a new archive directory
    strArchive = 'Stylus_%s' % dtNow.isoformat().translate(
        Constants.strTranslate)
    pathArchive = os.path.abspath(os.path.join(Globals.pathDst, strArchive))
    os.makedirs(pathArchive)

    # Populate the archive directory
    for folder in Globals.aryFolders:
        pathSrc = os.path.abspath(os.path.join(Globals.pathSrc, folder))
        pathDst = os.path.abspath(os.path.join(pathArchive, folder))
        cc = os.system('ditto %s %s' % (pathSrc, pathDst))
        if cc:
            raise Common.BiologicError(
                'Unable to copy folder %s to archive directory %s - error code(%d)'
                % (folder, pathArchive, cc))
        Common.say('...copied %s' % folder)

    # Create the archive
    fileArchive = os.path.abspath(
        os.path.join(Globals.pathDst, strArchive + Constants.extArchive))
    cc = os.system('ditto -c -z %s %s' % (pathArchive, fileArchive))
    if cc:
        raise Common.BiologicError(
            'Unable to create archive %s - error code(%d)' % (fileArchive, cc))
    Common.say('...created archive %s' % pathArchive)

    # Remove the archive directory
    cc = os.system('rm -drf %s' % pathArchive)
    if cc:
        raise Common.BiologicError('Unable to temporary archive directory %s' %
                                   fileArchive)
Example #11
0
def doBackup():
    Common.say('Archving folders [ %s ] in %s' % (', '.join(Globals.aryFolders), Globals.pathSrc))
    
    dtNow = datetime.datetime.now()
    dtWeekAgo = dtNow - datetime.timedelta(days=Globals.nRemoveAge)

    # Purge archives and archive directories older than one week
    for item in glob.glob(os.path.join(Globals.pathDst, Constants.strArchives)):
        stat = os.stat(item)
        if datetime.datetime.fromtimestamp(stat.st_mtime) < dtWeekAgo:
            if os.path.isdir(item):
                cc = os.system('rm -drf %s' % item)
                if cc:
                    raise Common.BiologicError('Unable to remove old directory %s' % item)
            else:
                os.remove(item)
            
    # Create a new archive directory
    strArchive = 'Stylus_%s' % dtNow.isoformat().translate(Constants.strTranslate)
    pathArchive = os.path.abspath(os.path.join(Globals.pathDst, strArchive))
    os.makedirs(pathArchive)

    # Populate the archive directory
    for folder in Globals.aryFolders:
        pathSrc = os.path.abspath(os.path.join(Globals.pathSrc, folder))
        pathDst = os.path.abspath(os.path.join(pathArchive, folder))
        cc = os.system('ditto %s %s' % (pathSrc, pathDst))
        if cc:
            raise Common.BiologicError('Unable to copy folder %s to archive directory %s - error code(%d)' % (folder, pathArchive, cc))
        Common.say('...copied %s' % folder)

    # Create the archive
    fileArchive = os.path.abspath(os.path.join(Globals.pathDst, strArchive + Constants.extArchive))
    cc = os.system('ditto -c -z %s %s' % (pathArchive, fileArchive))
    if cc:
        raise Common.BiologicError('Unable to create archive %s - error code(%d)' % (fileArchive, cc))
    Common.say('...created archive %s' % pathArchive)

    # Remove the archive directory
    cc = os.system('rm -drf %s' % pathArchive)
    if cc:
        raise Common.BiologicError('Unable to temporary archive directory %s' % fileArchive)
Example #12
0
def main(argv=None):
    try:
        Globals.fInteractive = True
        Common.Globals.fQuiet = False

        setGlobals(sys.argv[1:])

        if Globals.fBuildArchetype:
            Common.say('Archetypes Directory: %s' % Globals.strArchetypePath)
        if len(Globals.aryGenes) > 0:
            Common.say('Gene Directory      : %s' % Globals.strGenePath)
        Common.say('Han URL             : %s' % Globals.urlHan)
    
        if Globals.fBuildArchetype:
            buildHan(Globals.uchHan)

        if Globals.aryGenes:
            buildGenes(Globals.uchHan, Globals.aryGenes)

        return 0

    except Common.BiologicError, err:
        Common.sayError(err.msg)
        return 2
Example #13
0
def main(argv=None):
    try:
        nCompletionCode = 0

        getArguments()

        Common.say('Data Directory: %s' % Globals.names.pathData)
        if Globals.urlGlobals:
            Common.say('Globals URL   : %s' % Globals.urlGlobals)
        Common.say('Genome URL    : %s' % Globals.names.urlGenome)
        Common.say('Plan URL      : %s' % Globals.names.urlPlan)
        Common.say('Han URL       : %s' % Globals.urls.urlHan)
        Common.say('Schema URL    : %s' % Globals.urls.urlSchema)

        if Globals.aryLog[2] or Globals.aryLog[3] == 'silent':
            os.close(_FD_STDERR)
            if Globals.aryLog[3] != 'silent':
                Common.redirectStderr(Globals.aryLog[2])

        if Globals.fVersion:
            Common.say('Version       : %s' % Stylus.getVersion())

        strGenome = Common.readFile(Globals.names.urlGenome)
        strPlan = Common.readFile(Globals.names.urlPlan)
        strGlobals = Globals.urlGlobals and Common.readFile(
            Globals.urlGlobals) or ''

        if Globals.aryTrace:
            Stylus.setLogLevel(Globals.aryLog[0], 'trace',
                               Globals.aryLog[3] == 'echo', Globals.aryLog[4])
            rc = Stylus.setTrace(Globals.aryTrace[0], Globals.aryTrace[1],
                                 Globals.aryTrace[2], Globals.aryTrace[3],
                                 Globals.aryTrace[4])
            if rc:
                raise StylusError(rc)

        if Globals.aryLog and not Globals.aryTrace:
            rc = Stylus.setLogLevel(Globals.aryLog[0], Globals.aryLog[1],
                                    Globals.aryLog[3] == 'echo',
                                    Globals.aryLog[4])
            if rc:
                raise StylusError(rc)

        rc = Stylus.setScope(Globals.urls.urlHan, Globals.urls.urlSchema)
        if rc:
            raise StylusError(rc)

        rc = Stylus.setRecordRate(Globals.aryRecord[0], Globals.aryRecord[1],
                                  Globals.names.pathData, Globals.aryRecord[2])
        if rc:
            raise StylusError(rc)

        if strGlobals:
            rc = Stylus.setGlobals(strGlobals)
            if rc:
                raise StylusError(rc)

        rc = Stylus.setGenome(strGenome, Globals.strAuthor)
        if rc:
            raise StylusError(rc)

        tStart = time.clock()
        rc = Stylus.executePlan(strPlan, Globals.names.nFirstPlanTrial,
                                Globals.names.cPlanTrialsToExecute)
        tEnd = time.clock()
        if Globals.aryStatistics and Globals.aryStatistics[1]:
            statistics = Stylus.getStatistics()
            Common.say(
                'Plan executed %d trials in %f seconds' %
                ((statistics._iTrialCurrent - statistics._iTrialInitial),
                 (tEnd - tStart)))
        if rc != Stylus.ST_RCSUCCESS:
            if not Stylus.errorIsType(rc, Stylus.ST_INFOTYPE):
                raise StylusError(rc)
            else:
                Common.say('Plan terminated - %s' % stylusError(rc))
                nCompletionCode = 1

        if Globals.aryStatistics and len(
                Globals.aryStatistics
        ) >= 1 and Globals.aryStatistics[0] != "none":
            statistics = Stylus.getStatistics()
            Common.say(
                'Statistics for %d trials (%d to %d)' %
                ((statistics._iTrialCurrent - statistics._iTrialInitial),
                 statistics._iTrialInitial, statistics._iTrialCurrent))
            if Globals.aryStatistics[0] == "summary":
                Common.say(
                    '    Fitness: %0.15f - rollbacks(%d) silent(%d) changes(%d) inserted(%d) deleted(%d)'
                    %
                    (statistics._nFitness, statistics._cTotalRollbacks,
                     statistics._cSilent, statistics._cbBasesChanged,
                     statistics._cbBasesInserted, statistics._cbBasesDeleted))
            else:
                Common.say(
                    '    Fitness: current(%0.15f) max(%0.15f in trial %d) min(%0.15f in trial %d)'
                    % (statistics._nFitness, statistics._tfMax._nValue,
                       statistics._tfMax._iTrial, statistics._tfMin._nValue,
                       statistics._tfMin._iTrial))
                Common.say(
                    '    Score: current(%0.15f) max(%0.15f in trial %d) min(%0.15f in trial %d)'
                    % (statistics._nScore, statistics._tsMax._nValue,
                       statistics._tsMax._iTrial, statistics._tsMin._nValue,
                       statistics._tsMin._iTrial))
                Common.say(
                    '    Size: current(%d) max(%d in trial %d) min(%d in trial  %d)'
                    % (statistics._cbBases, statistics._tzMax._cbBases,
                       statistics._tzMax._iTrial, statistics._tzMin._cbBases,
                       statistics._tzMin._iTrial))
                Common.say(
                    '    Rollbacks: total(%d) current(%d) max(%d in trial %d) min(%d in trial %d)'
                    %
                    (statistics._cTotalRollbacks, statistics._cRollbacks,
                     statistics._trMax._cRollbacks, statistics._trMax._iTrial,
                     statistics._trMin._cRollbacks, statistics._trMin._iTrial))
                Common.say(
                    '    Mutations: silent(%d) attempts(%d) accepted(%d) changed(%d) inserted(%d) deleted(%d)'
                    %
                    (statistics._cSilent, statistics._cAttempted,
                     statistics._cAccepted, statistics._cbBasesChanged,
                     statistics._cbBasesInserted, statistics._cbBasesDeleted))
                if statistics._atChanged._cAttempted:
                    Common.say(
                        '      Changed: attempted(%d) accepted(%d) bases-affected(%d)'
                        % (statistics._atChanged._cAttempted,
                           statistics._atChanged._cAccepted,
                           statistics._atChanged._cbBases))
                if statistics._atCopied._cAttempted:
                    Common.say(
                        '      Copied: attempted(%d) accepted(%d) bases-affected(%d)'
                        % (statistics._atCopied._cAttempted,
                           statistics._atCopied._cAccepted,
                           statistics._atCopied._cbBases))
                if statistics._atDeleted._cAttempted:
                    Common.say(
                        '      Deleted: attempted(%d) accepted(%d) bases-affected(%d)'
                        % (statistics._atDeleted._cAttempted,
                           statistics._atDeleted._cAccepted,
                           statistics._atDeleted._cbBases))
                if statistics._atInserted._cAttempted:
                    Common.say(
                        '      Inserted: attempted(%d) accepted(%d) bases-affected(%d)'
                        % (statistics._atInserted._cAttempted,
                           statistics._atInserted._cAccepted,
                           statistics._atInserted._cbBases))
                if statistics._atTransposed._cAttempted:
                    Common.say(
                        '      Transposed: attempted(%d) accepted(%d) bases-affected(%d)'
                        % (statistics._atTransposed._cAttempted,
                           statistics._atTransposed._cAccepted,
                           statistics._atTransposed._cbBases))

        return nCompletionCode

    except Usage, err:
        Common.sayError(err)
        return 0
Example #14
0
def main(argv=None):
    try:
        nCompletionCode = 0

        getArguments()
        
        Common.say('Data Directory: %s' % Globals.names.pathData)
        if Globals.urlGlobals:
            Common.say('Globals URL   : %s' % Globals.urlGlobals)
        Common.say('Genome URL    : %s' % Globals.names.urlGenome)
        Common.say('Plan URL      : %s' % Globals.names.urlPlan)
        Common.say('Han URL       : %s' % Globals.urls.urlHan)
        Common.say('Schema URL    : %s' % Globals.urls.urlSchema)
        
        if Globals.aryLog[2] or Globals.aryLog[3] == 'silent':
            os.close(_FD_STDERR)
            if Globals.aryLog[3] != 'silent':
                Common.redirectStderr(Globals.aryLog[2])

        if Globals.fVersion:
            Common.say('Version       : %s' % Stylus.getVersion())

        strGenome = Common.readFile(Globals.names.urlGenome)
        strPlan = Common.readFile(Globals.names.urlPlan)
        strGlobals = Globals.urlGlobals and Common.readFile(Globals.urlGlobals) or ''
    
        if Globals.aryTrace:
            Stylus.setLogLevel(Globals.aryLog[0], 'trace', Globals.aryLog[3] == 'echo', Globals.aryLog[4])
            rc = Stylus.setTrace(Globals.aryTrace[0], Globals.aryTrace[1], Globals.aryTrace[2], Globals.aryTrace[3], Globals.aryTrace[4])
            if rc:
                raise StylusError(rc)

        if Globals.aryLog and not Globals.aryTrace:
            rc = Stylus.setLogLevel(Globals.aryLog[0], Globals.aryLog[1], Globals.aryLog[3] == 'echo', Globals.aryLog[4])
            if rc:
                raise StylusError(rc)

        rc = Stylus.setScope(Globals.urls.urlHan, Globals.urls.urlSchema)
        if rc:
            raise StylusError(rc)
            
        rc = Stylus.setRecordRate(Globals.aryRecord[0], Globals.aryRecord[1], Globals.names.pathData, Globals.aryRecord[2])
        if rc:
            raise StylusError(rc)
            
        if strGlobals:
            rc = Stylus.setGlobals(strGlobals)
            if rc:
                raise StylusError(rc)
            
        rc = Stylus.setGenome(strGenome, Globals.strAuthor)
        if rc:
            raise StylusError(rc)

        tStart = time.clock()
        rc = Stylus.executePlan(strPlan, Globals.names.nFirstPlanTrial, Globals.names.cPlanTrialsToExecute)
        tEnd = time.clock()
        if Globals.aryStatistics and Globals.aryStatistics[1]:
            statistics = Stylus.getStatistics()
            Common.say('Plan executed %d trials in %f seconds' % ((statistics._iTrialCurrent-statistics._iTrialInitial), (tEnd - tStart)))
        if rc != Stylus.ST_RCSUCCESS:
            if not Stylus.errorIsType(rc, Stylus.ST_INFOTYPE):
                raise StylusError(rc)
            else:
                Common.say('Plan terminated - %s' % stylusError(rc))
                nCompletionCode = 1
            
        if Globals.aryStatistics and len(Globals.aryStatistics) >= 1 and Globals.aryStatistics[0] != "none":
            statistics = Stylus.getStatistics()
            Common.say('Statistics for %d trials (%d to %d)' % ((statistics._iTrialCurrent-statistics._iTrialInitial), statistics._iTrialInitial, statistics._iTrialCurrent))
            if Globals.aryStatistics[0] == "summary":
                Common.say('    Fitness: %0.15f - rollbacks(%d) silent(%d) changes(%d) inserted(%d) deleted(%d)' %
                        (statistics._nFitness, statistics._cTotalRollbacks, statistics._cSilent, statistics._cbBasesChanged, statistics._cbBasesInserted, statistics._cbBasesDeleted))
            else:
                Common.say('    Fitness: current(%0.15f) max(%0.15f in trial %d) min(%0.15f in trial %d)' %
                        (statistics._nFitness, statistics._tfMax._nValue, statistics._tfMax._iTrial, statistics._tfMin._nValue, statistics._tfMin._iTrial))
                Common.say('    Score: current(%0.15f) max(%0.15f in trial %d) min(%0.15f in trial %d)' %
                        (statistics._nScore, statistics._tsMax._nValue, statistics._tsMax._iTrial, statistics._tsMin._nValue, statistics._tsMin._iTrial))
                Common.say('    Size: current(%d) max(%d in trial %d) min(%d in trial  %d)' %
                        (statistics._cbBases, statistics._tzMax._cbBases, statistics._tzMax._iTrial, statistics._tzMin._cbBases, statistics._tzMin._iTrial))
                Common.say('    Rollbacks: total(%d) current(%d) max(%d in trial %d) min(%d in trial %d)' %
                        (statistics._cTotalRollbacks, statistics._cRollbacks, statistics._trMax._cRollbacks, statistics._trMax._iTrial, statistics._trMin._cRollbacks, statistics._trMin._iTrial))
                Common.say('    Mutations: silent(%d) attempts(%d) accepted(%d) changed(%d) inserted(%d) deleted(%d)' %
                        (statistics._cSilent, statistics._cAttempted, statistics._cAccepted, statistics._cbBasesChanged, statistics._cbBasesInserted, statistics._cbBasesDeleted))
                if statistics._atChanged._cAttempted:
                    Common.say('      Changed: attempted(%d) accepted(%d) bases-affected(%d)' %
                            (statistics._atChanged._cAttempted, statistics._atChanged._cAccepted, statistics._atChanged._cbBases))
                if statistics._atCopied._cAttempted:
                    Common.say('      Copied: attempted(%d) accepted(%d) bases-affected(%d)' %
                            (statistics._atCopied._cAttempted, statistics._atCopied._cAccepted, statistics._atCopied._cbBases))
                if statistics._atDeleted._cAttempted:
                    Common.say('      Deleted: attempted(%d) accepted(%d) bases-affected(%d)' %
                            (statistics._atDeleted._cAttempted, statistics._atDeleted._cAccepted, statistics._atDeleted._cbBases))
                if statistics._atInserted._cAttempted:
                    Common.say('      Inserted: attempted(%d) accepted(%d) bases-affected(%d)' %
                            (statistics._atInserted._cAttempted, statistics._atInserted._cAccepted, statistics._atInserted._cbBases))
                if statistics._atTransposed._cAttempted:
                    Common.say('      Transposed: attempted(%d) accepted(%d) bases-affected(%d)' %
                            (statistics._atTransposed._cAttempted, statistics._atTransposed._cAccepted, statistics._atTransposed._cbBases))

        return nCompletionCode

    except Usage, err:
        Common.sayError(err)
        return 0
Example #15
0
def buildGenes(uchHan, aryGenes):
    Common.say('Creating %d gene(s)' % len(aryGenes))
    
    han = loadHan(uchHan)
    
    strAuthor = Globals.strAuthor and (" author='%s'" % Globals.strAuthor) or ''

    aryGeneNames = []
    for specification in aryGenes:
        Common.say('Creating gene from specification ' + specification)

        gs = GeneSpecification(specification != Constants.strDefault and specification or '', han)
        gsName = gs.toName(han.unicode) + Common.Constants.extGene
        gsPoints = gs.getPoints()
        Common.say('Gene to be named ' + gsName)

        ptCurrent = Genome.Point(pt=gsPoints[0][1][0])
        arySegments = []

        # Convert each set of points into a list of vectors
        for iSegment in xrange(len(gsPoints)):
            fCoherent, aryPoints = gsPoints[iSegment]
            
            # Build the segment starting from the current location
            # - Ensure incoherent segments always begin at the current location
            #   (This causes moves to absorb the tracing error, effectively trading placement error for stroke fit error.)
            if not fCoherent:
                aryPoints[0].x = ptCurrent.x
                aryPoints[0].y = ptCurrent.y
            ptCurrent, aryVectors = buildSegment(ptCurrent, fCoherent, aryPoints)
            
            if not fCoherent:
                # If the segment contains too few vectors, pad it with an incoherent sequence
                # - If there are no vectors, use No/So/Ea/We - binding code will ensure proper coherence at the endpoints
                # - Otherwise, use the four points of the direction "compass" that are guaranteed incoherent with the last vector
                if len(aryVectors) < 3:
                    iDirection = len(aryVectors) and Codons.Directions.add(Codons.Vectors.toDirection(aryVectors[-1]), 3) or Codons.Directions.North
                    aryVectors.append(Codons.Vectors.create(iDirection))
                    aryVectors.append(Codons.Vectors.create(Codons.Directions.toOpposite(iDirection)))
                    
                    iDirection = Codons.Directions.add(iDirection, 2)
                    aryVectors.append(Codons.Vectors.create(iDirection))
                    aryVectors.append(Codons.Vectors.create(Codons.Directions.toOpposite(iDirection)))
                    
                # Inject vectors to force incoherency for incoherent segments
                # - Essentially, add a vector pair to every two that forces incoherency
                # - Walking the list must take into account injected vector pairs
                else:
                    iVector = 0
                    while iVector < len(aryVectors):
                        if Codons.isCoherent([aryVectors[iVector-2], aryVectors[iVector-1], aryVectors[iVector+0]]):
                            idVector = aryVectors[iVector]
                            aryVectors.insert(iVector+0, Codons.Vectors.toOpposite(idVector))
                            aryVectors.insert(iVector+1, idVector)
                        iVector += 1

            # Ensure the vectors "bind" without affecting the coherence of an adjoining segment
            if iSegment:
                fCoherencePrev, aryVectorsPrev = arySegments[-1]
                if Codons.isCoherent([ aryVectorsPrev[-2], aryVectorsPrev[-1], aryVectors[0] ]) or Codons.isCoherent([ aryVectorsPrev[-1], aryVectors[0], aryVectors[1] ]):
                    if not fCoherencePrev:
                        aryVectorsPrev += [ Codons.Vectors.toOpposite(aryVectorsPrev[-1]), aryVectorsPrev[-1], aryVectors[0], Codons.Vectors.toOpposite(aryVectors[0]) ]
                    else:
                        aryVectors[:0] = [ Codons.Vectors.toOpposite(aryVectorsPrev[-1]), aryVectorsPrev[-1], aryVectors[0], Codons.Vectors.toOpposite(aryVectors[0]) ]

            arySegments.append((fCoherent, aryVectors))

        validateCoherence(arySegments)

        aryCodons = [ Codons.Vectors.toVector(idVector).codon for fCoherent, aryVectors in arySegments for idVector in aryVectors ]
        aryCodons[:0] = [ 'ATG' ]
        aryCodons.append(Codons.Vectors.toVector(Codons.Vectors.create(Codons.Directions.Stop, Codons.Constants.iVectorShort)).codon)

        aryStrokes = []
        iStroke = 0
        iBase = 4
        for fCoherent, aryVectors in arySegments:
            if fCoherent:
                aryStrokes.append(_GENE_STROKE % (iBase, iBase+(len(aryVectors)*3-1), gs.mapStrokeToHan(iStroke)+1))
                iStroke += 1
            iBase += len(aryVectors) * 3

        strGene = _GENE_DEFINITION % (str(uuid.uuid4()).upper(), strAuthor, datetime.datetime.utcnow().isoformat(), _NAME, str(gs),
                                    ''.join(aryCodons), len(aryCodons)*3,
                                    gsPoints[0][1][0].x, gsPoints[0][1][0].y,
                                    han.unicode,
                                    '\n'.join(aryStrokes))

        strPath = Common.resolvePath(os.path.join(Globals.strGenePath, Common.makeHanPath(gsName)))
        strDir = os.path.dirname(strPath)
        if not os.path.exists(strDir):
            try: os.makedirs(os.path.dirname(strPath))
            except OSError, err: raise Common.BiologicError('Unable to create %s - %s' % (strDir, str(err)))
        
        try:
            fileGene = os.open(strPath, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0664)
            os.write(fileGene, strGene)
        except IOError, err: raise Common.BiologicError('Unable to create %s - %s' % (strPath, str(err)))
        os.close(fileGene)
        
        Common.say('\tWrote %s - %d codons, %d bases' % (strPath, len(aryCodons), len(aryCodons)*3))
        aryGeneNames.append(gsName)

    return aryGeneNames
Example #16
0
def validateTable():
    Globals.codonTable.validate()
    Common.say('%s successfully validated' % (Globals.urlCodonTable and Globals.urlCodonTable or Globals.urlGenome))
    return
Example #17
0
    if Globals.pathOutput:
        try:
            fileCodonTable = os.open(Globals.pathOutput,
                                     os.O_CREAT | os.O_TRUNC | os.O_WRONLY,
                                     0664)
            os.write(fileCodonTable, strCodonTable)
        except IOError, err:
            raise Common.BiologicError('Unable to create %s - %s' %
                                       (Globals.pathOutput, str(err)))
        os.close(fileCodonTable)

        Common.say('Codon table written to ' + Globals.pathOutput)

    else:
        Common.say(strCodonTable, False, True)
    return


#------------------------------------------------------------------------------
# Function: validateTable
#
#------------------------------------------------------------------------------
def validateTable():
    Globals.codonTable.validate()
    Common.say(
        '%s successfully validated' %
        (Globals.urlCodonTable and Globals.urlCodonTable or Globals.urlGenome))
    return

Example #18
0
def validateTable():
    Globals.codonTable.validate()
    Common.say(
        '%s successfully validated' %
        (Globals.urlCodonTable and Globals.urlCodonTable or Globals.urlGenome))
    return