2016-11-15 112 views
0

我是Python新手,但真的很想在Linux服務器命令行上執行以下功能。請幫忙找出當我執行下面的腳本(test.py)時爲什麼沒有打印出來?執行我鍵入python test.py。謝謝。在Linux上執行python函數

##!/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") 
+2

所有你正在做的是定義一個函數。你不叫它。 –

+0

我想我在這裏調用它:'print'結果是:「,get_minimal_representation(pos = 1001,ref =」CTCC「,alt =」CCC,C,CCCC「)'否? – user3781528

+0

你想執行它什麼?除了定義它之外,你必須調用函數,爲了達到這個目的,你需要3個值傳遞給函數。 – chepner

回答

1

你有一個問題,最後一個print語句的縮進。它應該在功能之外。

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") 
+0

這絕不會發生在Perl中。 :) 我有很多要學習的。謝謝 – user3781528

+0

不是問題。有問題。只要確保縮進是正確的。 :) – Inconnu

4

您沒有調用該函數。

嘗試

if __name__ == '__main__': 
    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")