2016-11-15 18 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") 
関連する問題