示例#1
0
文件: fasta.py 项目: dongqing7/ABFGP
        print "# testing: IsSingleFastaDna(fname)"
        print "    %s" % IsSingleFastaDna(fname)
        print "# testing: IsSingleFastaProtein(fname)"
        print "    %s" % IsSingleFastaProtein(fname)
        print "# testing: parseFasta(content,allow_decorated_headers=True)"
        _seqs = parseFasta(open(fname),allow_decorated_headers=True)
        print "    %s..." % str(tuple(_seqs.keys()[0:5]))
        print "# testing: parseFasta(content,allow_decorated_headers=False)"
        _seqs = parseFasta(open(fname),allow_decorated_headers=False)
        print "    %s..." % str(tuple(_seqs.keys()[0:5]))
        print "# testing: parseDecoratedFasta(content)"
        _seqs,_info = parseDecoratedFasta(open(fname))
        print "    %s..." % str(tuple(_seqs.keys()[0:5]))
        print "    %s..." % str(dict([(h,k) for h,k in _info.items()[0:5] ]))
    
# end of function __test

if __name__ == "__main__":
    import sys
    from os.path import isfile as osPathIsfile
    print """ Functions for parsing,accessing and writing FASTA files """
    print """ Unit testing: [ %s test ] [ %s test SINGLE_FASTA_FNAME ] """ % (sys.argv[0],sys.argv[0])
    if not 'test' in sys.argv: sys.exit()
    fname = None
    if len(sys.argv) == 3:
        fname = sys.argv[2]
        if not osPathIsfile(fname):
            fname = None
    # test all the functions
    __test(fname=fname)
示例#2
0
def runcexpander(fname_fasta, cbalignp_commandline=" -y", output='binary'):
    """
    Run the complete cascade of cexpander algorithms on an input multi fasta
    file and return the output as a CexpanderOutput object

    @type  fname_fasta: string
    @param fname_fasta: path to input multi fasta file

    @type  cbalignp_commandline: string
    @param cbalignp_commandline: (extra) command line for cbalignp

    @type  min_cols: integer
    @param min_cols: minimal number of uniformly matched positions (cols)
                     required to report transfer blocks for (>= 0)

    @type  projected_on: string
    @param projected_on: apply fasta seqeunce header which to use for projection;
                         apply ':::' to do projections on all input sequences

    @attention: requires global variable EXECUTABLE_cexpander_ALLVSALL
    @attention: requires global variable EXECUTABLE_CEXPANDER_CBALIGNP
    @attention: requires global variable EXECUTABLE_CEXPANDER_CEXPANDER
    @attention: see cexpander_dr for (additional) command line options
    @attention: only a subset of cexpander_dr commandline options are supported!
    
    @rtype:  CexpanderOutput object
    @return: CexpanderOutput object
    """
    if not fname_fasta: raise "NoProperFunctionArguments"
    if not osPathIsfile(fname_fasta): raise "FileDoesNotExist"

    # (0) create (~unique) filenames
    uniquetag = get_random_string_tag()
    fname_allvsall = ".".join([fname_fasta, uniquetag, "allvsall"])
    fname_report = ".".join([fname_fasta, uniquetag, "report"])
    fname_aligned = ".".join([fname_fasta, uniquetag, "aligned"])
    fname_settings = ".".join([fname_fasta, uniquetag, "settings"])
    fname_cexpander = ".".join([fname_fasta, uniquetag, "cexpander"])

    # (1) create complete .fa -> cexpanderstring command
    command = """
        python %s %s %s %s;
        %s -i %s %s > %s;
        %s < %s;
        """ % (
        EXECUTABLE_CEXPANDER_ALLVSALL,
        fname_fasta,
        fname_allvsall,
        fname_report,
        EXECUTABLE_CEXPANDER_CBALIGNP,
        fname_allvsall,
        cbalignp_commandline,
        fname_aligned,
        EXECUTABLE_CEXPANDER_CEXPANDER,
        fname_settings,
    )

    # (2) create fname_settings file
    binorfloat = "$dumpcv"
    if output == "float": binorfloat = "$dumpcvc"
    fh = open(fname_settings, 'w')
    content = "\n\n".join([
        "$load\n%s\n%s" % (fname_report, fname_aligned),
        "$addquery\n-1",
        "$run",
        "$dumpentries",
        "$cv_linear",
        "%s" % (binorfloat),  # BINARY == $dumpcv, FLOAT = $dumpcvc
        "$exit\n\n",
    ])
    fh.write(content)
    fh.close()

    # (3) run the command
    ci, co, ce = osPopen3(command)
    ci.close()
    # output of EXECUTABLE_CEXPANDER_ALLVSALL is cast to STDOUT as well!
    cexpanderdata = co.read()
    co.close()
    error = ce.read()
    ce.close()

    # (4) parse fname_cexpander to CexpanderOutput object
    cxpdr = parse_cexpander(cexpanderdata, fname_fasta)

    # (5) cleanup files
    osSystem("rm -f %s %s.%s.*" % (fname_fasta, fname_fasta, uniquetag))

    # (6) return the output object
    return cxpdr
示例#3
0
def runcexpander(fname_fasta,cbalignp_commandline=" -y",output='binary'):
    """
    Run the complete cascade of cexpander algorithms on an input multi fasta
    file and return the output as a CexpanderOutput object

    @type  fname_fasta: string
    @param fname_fasta: path to input multi fasta file

    @type  cbalignp_commandline: string
    @param cbalignp_commandline: (extra) command line for cbalignp

    @type  min_cols: integer
    @param min_cols: minimal number of uniformly matched positions (cols)
                     required to report transfer blocks for (>= 0)

    @type  projected_on: string
    @param projected_on: apply fasta seqeunce header which to use for projection;
                         apply ':::' to do projections on all input sequences

    @attention: requires global variable EXECUTABLE_cexpander_ALLVSALL
    @attention: requires global variable EXECUTABLE_CEXPANDER_CBALIGNP
    @attention: requires global variable EXECUTABLE_CEXPANDER_CEXPANDER
    @attention: see cexpander_dr for (additional) command line options
    @attention: only a subset of cexpander_dr commandline options are supported!
    
    @rtype:  CexpanderOutput object
    @return: CexpanderOutput object
    """
    if not fname_fasta: raise "NoProperFunctionArguments"
    if not osPathIsfile(fname_fasta): raise "FileDoesNotExist"

    # (0) create (~unique) filenames
    uniquetag = get_random_string_tag() 
    fname_allvsall  = ".".join([fname_fasta,uniquetag,"allvsall"])
    fname_report    = ".".join([fname_fasta,uniquetag,"report"])
    fname_aligned   = ".".join([fname_fasta,uniquetag,"aligned"])
    fname_settings  = ".".join([fname_fasta,uniquetag,"settings"])
    fname_cexpander = ".".join([fname_fasta,uniquetag,"cexpander"])

    # (1) create complete .fa -> cexpanderstring command
    command = """
        python %s %s %s %s;
        %s -i %s %s > %s;
        %s < %s;
        """ % (
        EXECUTABLE_CEXPANDER_ALLVSALL,
        fname_fasta,
        fname_allvsall,
        fname_report,
        EXECUTABLE_CEXPANDER_CBALIGNP,
        fname_allvsall,
        cbalignp_commandline,
        fname_aligned,
        EXECUTABLE_CEXPANDER_CEXPANDER,
        fname_settings,
        )


    # (2) create fname_settings file
    binorfloat = "$dumpcv"
    if output == "float": binorfloat = "$dumpcvc"
    fh = open(fname_settings,'w')
    content = "\n\n".join( [
        "$load\n%s\n%s" % (fname_report,fname_aligned),
        "$addquery\n-1",
        "$run",
        "$dumpentries",
        "$cv_linear",
        "%s" % ( binorfloat ), # BINARY == $dumpcv, FLOAT = $dumpcvc
        "$exit\n\n", 
        ] )
    fh.write(content)
    fh.close()


    # (3) run the command
    ci,co,ce = osPopen3(command)
    ci.close()
    # output of EXECUTABLE_CEXPANDER_ALLVSALL is cast to STDOUT as well!
    cexpanderdata = co.read()
    co.close()
    error = ce.read()
    ce.close()

    # (4) parse fname_cexpander to CexpanderOutput object
    cxpdr = parse_cexpander(cexpanderdata,fname_fasta)

    # (5) cleanup files
    osSystem("rm -f %s %s.%s.*" % ( fname_fasta, fname_fasta,uniquetag ) )

    # (6) return the output object
    return cxpdr