在Linux上执行python函数

在Linux上执行python函数,python,Python,我是Python新手,但非常希望在Linux服务器命令行上执行以下函数。请帮助我找出为什么在执行以下脚本(test.py)时没有打印任何内容?要执行,我键入了python test.py。多谢各位 ##!/usr/bin/python def get_minimal_representation(pos, ref, alt): """ Get the minimal representation of a variant, based on the ref + alt all

我是Python新手,但非常希望在Linux服务器命令行上执行以下函数。请帮助我找出为什么在执行以下脚本(test.py)时没有打印任何内容?要执行,我键入了
python test.py
。多谢各位

##!/usr/bin/python

def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt

        print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")

您没有调用该函数

   def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt



   print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")
试一试

在你档案的底部

应该是这样的:

##!/usr/bin/python

def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt

if __name__ == '__main__':
    print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")

您没有调用该函数

   def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt



   print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")
试一试

在你档案的底部

应该是这样的:

##!/usr/bin/python

def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt

if __name__ == '__main__':
    print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")

上次打印语句的缩进有问题。它应该在功能之外

   def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt



   print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")

上次打印语句的缩进有问题。它应该在功能之外

   def get_minimal_representation(pos, ref, alt): 
    """
    Get the minimal representation of a variant, based on the ref + alt alleles in a VCF
    This is used to make sure that multiallelic variants in different datasets, 
    with different combinations of alternate alleles, can always be matched directly. 
    Note that chromosome is ignored here - in xbrowse, we'll probably be dealing with 1D coordinates 
    Args: 
        pos (int): genomic position in a chromosome (1-based)
        ref (str): ref allele string
        alt (str): alt allele string
    Returns: 
        tuple: (pos, ref, alt) of remapped coordinate
    """
    pos = int(pos)
    # If it's a simple SNV, don't remap anything
    if len(ref) == 1 and len(alt) == 1: 
        return pos, ref, alt
    else:
        # strip off identical suffixes
        while(alt[-1] == ref[-1] and min(len(alt),len(ref)) > 1):
            alt = alt[:-1]
            ref = ref[:-1]
        # strip off identical prefixes and increment position
        while(alt[0] == ref[0] and min(len(alt),len(ref)) > 1):
            alt = alt[1:]
            print "Alt: ", alt
            ref = ref[1:]
            print "Ref: ", ref
            pos += 1
            print "Pos: ", pos
        return pos, ref, alt



   print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC")

你所做的就是定义一个函数。你没有叫它。我想我在这里叫它:
print“结果是:”,得到最小表示(pos=1001,ref=“CTCC”,alt=“CCC,C,CCCC”)
no?你想在什么上执行它?除了定义函数外,您还必须调用函数,为此,您需要3个值才能传递给函数。@user3781528不,使用您的缩进,该调用是函数体的一部分。@user3781528使用当前缩进,该行是函数定义的一部分,你所做的只是定义一个函数。你没有叫它。我想我在这里叫它:
print“结果是:”,得到最小表示(pos=1001,ref=“CTCC”,alt=“CCC,C,CCCC”)
no?你想在什么上执行它?除了定义函数外,您还必须调用函数,为此,您需要3个值才能传递给函数。@user3781528不,对于您的缩进,该调用是函数体的一部分。@user3781528对于您当前的缩进,该行是函数定义的一部分,而不是在它后面。没有问题。发生了。只需确保压痕正确即可。:)没问题,会发生的。只需确保压痕正确即可。:)