示例#1
0
文件: ignore.py 项目: rhelmot/patman
def main(args, repo):
    if repo is None:
        repo = manager.resolve()
    with repo.lock:
        if len(args) == 0:
            import help as p_help

            p_help.main(["ignore"])
            return 1
        elif args[0] == "list":
            print "\n".join(repo.get_ignores())
        elif args[0] == "remove":
            ignores = repo.get_ignores()
            for pattern in args[1:]:
                if not pattern in ignores:
                    print 'Can\'t remove "%s" from ignores, not present' % pattern
                else:
                    ignores.remove(pattern)
            repo.set_ignores(ignores)
        else:
            ignores = repo.get_ignores()
            ignores += args
            repo.set_ignores(ignores)

        return 0
示例#2
0
文件: use.py 项目: rhelmot/patman
def main(args, repo):
    if repo is None: repo = manager.resolve()
    with repo.lock:
        if len(args) == 0:
            import help as p_help
            p_help.main(['use'])
            return 1
        if args[0] not in zip(*repo.list_snapshots())[0]:
            print 'Argument is not a snapshot name!'
            return 1
        repo.restore(args[0])
        return 0
示例#3
0
文件: snap.py 项目: rhelmot/patman
def main(args, repo):
    if repo is None: repo = manager.resolve()
    with repo.lock:
        if len(args) == 0:
            import help as p_help
            p_help.main(['snap'])
            return 1
        if args[0] in zip(*repo.list_snapshots())[0]:
            print 'There is already a snapshot named "%s"!' % args[0]
            return 1

        repo.snapshot(args[0])
        return 0
示例#4
0
文件: init.py 项目: rhelmot/patman
def main(args, repo):
    if repo is not None:
        import help as p_help
        p_help.main(['init'])
        return 1

    try:
        manager.resolve()
        print 'No nested projects!'
        return 1
    except Exception:
        pass

    if not any(not x.startswith('.') for x in os.listdir('.')):
        raise Exception('Not initializing empty directory!')

    ppath = os.environ['PWD']
    phash = manager.shash(ppath)
    if len(args) < 1:
        args.append(raw_input("Give a name for this project: "))
    #if len(args) < 2:
    #    args.append(raw_input("Give the name of the executable for this project: "))
    # TODO: validate names
    #try:
    #    open(args[1])
    #except:
    #    print "Executable file doesn't exist!"
    #    print 'it was "%s"' % args[1]
    #    return 1

    cfgdir = os.path.join(manager.PATH, phash)
    os.mkdir(cfgdir)
    os.symlink(cfgdir, os.path.join(manager.PATH, args[0]))
    os.mkdir(os.path.join(cfgdir, 'snapshots'))
    os.mkdir(os.path.join(cfgdir, 'config'))
    open(os.path.join(cfgdir, 'config/destination'), 'w').write(ppath)
    repo = manager.Project(cfgdir)
    with repo.lock:
        #repo.set_executable(args[1])
        repo.set_ignores([])
        repo.snapshot('original')
        return 0
示例#5
0
def main(args):
    command = ""
    if len(args) > 1 :
        command = args[1]

    if command == "create":
        return create.main(args)
    elif command == "build":
        return build.main(args)
    else:
        return help.main(args)
示例#6
0
def main():
    # Help module controls that query and genbank are correctly provided and has a tutorial.
    query, Genbank = help.main()

    basename = os.path.basename(
        query)  # We do not want the full path as the query name.

    # Results and Data directory are created if they do not exist.
    # Within them a directory with the basename of the query is created.
    # All the files will be saved to these directories.
    if not os.path.exists('Results'):
        os.mkdir('Results')
    if not os.path.exists('Results/' + str(basename)):
        os.mkdir('Results/' + str(basename))
    if not os.path.exists('Data'):
        os.mkdir('Data')
    if not os.path.exists('Data/' + str(basename)):
        os.mkdir('Data/' + str(basename))
    if not os.path.exists('Data/' + str(basename) + '/Queries'):
        os.mkdir('Data/' + str(basename) + '/Queries')

    # We check the query is in fasta format. If it is not, we end the execution.
    # If it is fasta, we create a separate fasta file for each sequence in the query.
    if fasta.is_it_fasta(query, basename):
        pass
    else:
        print("\n" + (query + ' is not fasta.').center(80))
        print('Please check the format of your query_file.'.center(80) + "\n")
        try:
            os.rmdir('Results/' +
                     str(basename))  # We delete this directory if it is empty.
        except:
            pass  # If it is not empty we do not delete it. We do not want to delete previous analysis.
        shutil.rmtree(
            'Data/' + str(basename)
        )  # We delete this directory which is not empty. We do not loose important information by deleting this directory.
        sys.exit()

    # We check the genbank file is in genbank format and do the same as with the query file.
    # If it is a genbank, this function gets al the protein sequences to make a multifasta.
    if gbk.is_it_genbank(Genbank, basename):
        pass
    else:
        print("\n" + (Genbank + ' is not a Genbank.').center(80))
        print('Please check the format of your Genbank_file.'.center(80) +
              "\n")
        try:
            os.rmdir('Results/' +
                     str(basename))  # We delete this empty directory.
        except:
            pass
        shutil.rmtree('Data/' + str(basename))
        sys.exit()

    Data_Dir = 'Data/' + str(basename)
    subject = Data_Dir + '/gbk_fasta.fa'

    # Query and Genbank files and their metadata are copied to Data directory.
    shutil.copy2(query, Data_Dir)
    shutil.copy2(Genbank, Data_Dir)

    # We run the program for each sequence in the query.
    with os.scandir(Data_Dir + '/Queries') as it:
        for query_file in it:
            # First, we ask for the coverage and identity thresholds.
            coverage, identity = blaster.coverage_identity(query_file.name)

            new_query = Data_Dir + '/Queries/' + query_file.name
            # We define the directories for the results.
            Results_Dir = 'Results/' + str(
                basename) + '/' + query_file.name + '_Cov_' + str(
                    coverage) + '_Id_' + str(identity) + '/'
            Results_Blast = Results_Dir + 'Blast/'
            Results_Prosite = Results_Dir + 'Prosite/'
            Results_Muscle = Results_Dir + 'Muscle/'
            # If they do not exist, we create them.
            if not os.path.exists(Results_Dir):
                os.mkdir(Results_Dir)
            if not os.path.exists(Results_Blast):
                os.mkdir(Results_Blast)
            if not os.path.exists(Results_Prosite):
                os.mkdir(Results_Prosite)
            if not os.path.exists(Results_Muscle):
                os.mkdir(Results_Muscle)

            Process_log = open(Results_Dir + 'Process.log', "w")
            Process_log.write(' Analysis for query: %s '.center(80, '*') %
                              (query_file.name))
            Process_log.close(
            )  # We close the log file because other functions are going to open it.

            ## BLAST analysis ##
            blaster.blaster(new_query,
                            subject,
                            coverage,
                            identity,
                            basename,
                            results_dir=Results_Dir)  # BLAST analysis.

            # We continue with the analysis if at least one homologue is found for the criteria indicated.
            if len(open(Results_Blast + 'Blast_result.tsv').readlines()
                   ) > 1:  # The first row of the file is the header.

                plots.blast_plot('Blast_result.tsv', Results_Blast, identity)

                Hits = blaster.blaster_hits(
                    Results_Blast +
                    'Blast_result.tsv')  # List containing all the hits_ids.

                fasta.seq_from_id(
                    subject, Hits
                )  # We get the hit sequences from the genbank converted to fasta.

                # We add the queries to the hits to have all the proteins together for the subsequent analyses.
                fasta.add_queries("HitsFile.txt", new_query)

                HitsFile = "HitsFile.txt"  # Temporary fasta file with all the sequences.

                ## MUSCLE analysis ##
                muscle.align_seqs(HitsFile)

                # We save the alignment to MUSCLE folder
                shutil.copy2('HitsAligned.aln', Results_Muscle)

                AlignedHitsFile = 'HitsAligned.afa'  # Temporary alignment file.
                # With the alignment, we create the Neighbor Joining tree with muscle in newick format.
                muscle.create_tree(AlignedHitsFile,
                                   Results_Dir=Results_Dir,
                                   Results_Muscle=Results_Muscle)
                # With the newick format tree, we draw a simple plot.
                Tree = muscle.draw_tree(Results_Muscle + 'NJ_Tree.phy',
                                        Results_Dir=Results_Muscle)

                ## PROSITE analysis ##
                Process_log = open(
                    Results_Dir + 'Process.log',
                    "a")  ## The file is closed. We open in append mode.
                Process_log.write('\n\n\n' +
                                  ('Domain search in Prosite').center(80))
                prosite_dat = 'prosite.dat'

                # We check the prosite.dat file is in the directory and has the correct format.
                if not os.path.isfile(prosite_dat):
                    print('\n' + '\x1b[1;31;40m' +
                          "Please, make sure you have a file named prosite.dat"
                          .center(80) + '\x1b[0m')
                    sys.exit()
                try:
                    Process_log.write('\n\n' + ('Parsing ' + prosite_dat +
                                                '...').center(80))
                    PatternDict = prosite.dat_parser(prosite_dat)
                except:
                    Process_log.write()
                    Process_log.write('\n\n' +
                                      ('Errors encountered while parsing ' +
                                       prosite_dat).center(80))
                    print('Errors encountered while parsing ' + prosite_dat)
                    sys.exit()

                Process_log.write(
                    '\n\n' + ('Looking for domains in hits...').center(80))

                HitsDict = prosite.hits_file_to_dict(
                    HitsFile
                )  # Dictionary with BLAST hits as keys and no values.
                # We look for PROSITE domains in the hit sequences.
                ResultDict = prosite.pattern_search(PatternDict, HitsDict)

                prosite.output_results(prosite_dat, ResultDict,
                                       Results_Prosite)

                # More information about the domains?
                prosite.want_more_info("prosite.doc", ResultDict,
                                       Results_Prosite)

                # Printing information in Process.log file
                Process_log.write('\n\n' +
                                  ('Domain search completed').center(80) +
                                  '\n')
                Process_log.write(('Check Prosite results at: ' +
                                   Results_Prosite).center(80))
                Process_log.write('\n\n\n' +
                                  ('Analysis completed').center(80) + '\n\n\n')
                Process_log.write(('Analysis summary').center(80) + '\n\n')
                Process_log.write('BLAST'.center(80) + '\n\n')
                Process_log.write('Coverage threshold: ' + str(coverage) +
                                  '\n')
                Process_log.write('Identity threshold: ' + str(identity) +
                                  '\n')
                Process_log.write('Query name: ' + query_file.name + '\n')
                Process_log.write('Genbank: ' + os.path.basename(Genbank) +
                                  '\n')
                Process_log.write(
                    'Number of hits: ' +
                    str(len(open('Blast_result.tsv').readlines())) + '\n\n')
                Process_log.write('PROSITE'.center(80) + '\n\n')
                for protein in ResultDict.keys():
                    Process_log.write('Number of domains found in ' + protein +
                                      ': ' + str(len(ResultDict[protein])) +
                                      '\n')

                Process_log.close()

            # If no homologues are found for the query protein with the thresholds indicated.
            else:
                print('\n' + '\x1b[1;31;40m' +
                      'BLAST analysis yielded no results'.center(80) +
                      '\x1b[0m')
                Process_log = open(
                    Results_Dir + 'Process.log',
                    "a")  ## The file is closed. We open in append mode.
                Process_log.write('\n\n\n' + (
                    'Analysis ended because BLAST analysis yielded no results'
                ).center(80))

    # Accessory files are deleted.
    try:
        os.remove(HitsFile)
        os.remove('Blast_result.tsv')
        os.remove('HitsAligned.afa')
        os.remove('HitsAligned.aln')
        shutil.rmtree(Data_Dir + '/Queries')
    except:
        pass

    return basename  # byebye function needs basename to indicate where the results have been saved.
示例#7
0
def callModule(tokList, lineNo):
    #this function will be incharge of analyzing the token list, and calling the relative call_module

    #incase the line is a comment, that is it starts with a hash, no processing needed
    if(tokList[0][0] == '#'):
        return

    #the command to execute will be the first word, hence, look up the commands
    command = tokList[0]

    #if else ladder of the commands
    if command == "get":
        tokListLists = preprocess(tokList, lineNo)

        if tokListLists == -1: #identifier not found
            return -1

        for tokenList in tokListLists:
            status = get.main(tokenList, lineNo)
            if(status == -1):
                return status
    
    
    elif command == "help":
        status = help.main(tokList, lineNo)
        return status


    elif command == "view":
        tokListLists = preprocess(tokList, lineNo)

        if tokListLists == -1: #identifier not found
            return -1

        for tokenList in tokListLists:
            status = view.main(tokenList, lineNo)
            if(status == -1):
                return status


    elif command == "let":
        status = let.main(tokList, lineNo, varTable, listTable)

        #if status is -1, error has occoured, or else, add variable to the table
        if status != -1:
            varTable[status[0]] = status[1]

        return status
    

    elif command == "list":
        status = listCmd.main(tokList, lineNo, varTable, listTable)

        #if status is -1, error has occoured, or else, add list to the table
        if status != -1:
            listTable[status[0]] = status[1]
        
        return status

    elif command == "freevar":
        #this block of code is to remove all the variables that user wants to delete

        varToDelete = tokList[1:]

        #error checking to ensure all the variables do infact exist
        flag=1
        for var in varToDelete:
            if var in varTable:
                continue
            else:
                print("Error on line " + str(lineNo) + " no variable: " + var + " found")
                flag=0

        if(flag==0):
            #if flag is 0, the command is incorrect
            return -1
        
        #now to go about and deleting all the variables from the varTable

        for var in varToDelete:
            del varTable[var]
    
    elif command == "freelist":
        #this block of code is to remove all the lists that the user wants to delete

        listsToDelete = tokList[1:]

        #error checking to ensure all the lists do infact exist
        flag=1
        for list_ in listsToDelete:
            if list_ in listTable:
                continue
            else:
                print("Error on line " + str(lineNo) + " no list: " + list_ + " found")
                flag=0

        if(flag==0):
            #if flag is 0, the command is incorrect
            return -1
        
        #now to go about and deleting all the variables from the varTable

        for list_ in listsToDelete:
            del listTable[list_]
    
    elif command == "mem":
        status = mem.main(tokList, lineNo, varTable, listTable)

        #if an error is there in the command
        if(status == -1):
            return status


    else:
        print("Unkown command: " + command + " on line no: " + str(lineNo))
        return -1
示例#8
0
def main(argString=''):
    pd.set_option('display.width', 1366)
    sep = os.path.sep
    dirname = os.path.dirname(os.path.abspath(__file__)) + sep

    cmd = 'main'
    arg = ''
    modCache = {}
    while True:
        I = input('stock-{}> '.format(cmd)).strip()

        if I == '':
            continue
        elif I in ('exit', 'e'):
            if cmd == 'main':
                return
            else:
                cmd = 'main'
                arg = ''
                continue
        elif I in ('cache', 'c', 'list', 'l'):
            cache.codes.list()
            continue
        elif I in ('help', 'h'):
            import help
            help.main(cmd)
            continue
        elif len(I) == 6 and I.isdigit():
            import detail
            detail.main(I)
            continue
        elif I[0:6].upper() in ('SELECT', 'INSERT', 'UPDATE'):
            arg = I
        elif I.isdigit():
            arg = 'page:' + I
        elif I in ('n', '>', '.'):
            arg = 'page:+1'
        elif I in ('p', '<', ','):
            arg = 'page:-1'
        elif (I.startswith('+') or I.startswith('-')) and I[1:].isdigit():
            arg = 'page:' + I


#		省略命令
        elif I.split()[0].find(':') > 0:
            arg = I
        elif I in ('asc', 'a'):
            arg = 'asc:True'
        elif I in ('desc', 'd'):
            arg = 'asc:False'
        elif I in ('rerender', 'r'):
            pass
        else:
            i = (I + ' ').find(' ')
            _cmd, _arg = I[:i], I[i:].strip()
            if modCache.get(_cmd) is None:
                if os.path.isfile(dirname + _cmd + '.py'):
                    modCache[_cmd] = importlib.import_module(_cmd)
                else:
                    print('Command error, please input again.')
                    continue
            cmd = _cmd
            arg = _arg

        if cmd == 'main':
            continue
        else:
            modCache.get(cmd).main(arg)