0

I'm new to Python but would really like to execute the following function on Linux server command line. Please help to figure out why nothing is being printed when I execute the following script (test.py)? To execute I typed python test.py. Thank you.

##!/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")
8
  • 2
    All you're doing is defining a function. You're not calling it. Commented Nov 15, 2016 at 17:26
  • i thought I called it here:print "the result is: ", get_minimal_representation( pos = 1001, ref = "CTCC", alt = "CCC,C,CCCC") no? Commented Nov 15, 2016 at 17:26
  • What do you want to execute it on? You have to call the function in addition to defining it, and in order to do that you need 3 values to pass to the function. Commented Nov 15, 2016 at 17:26
  • @user3781528 Nope, with the indentation you have, that call is part of the function body. Commented Nov 15, 2016 at 17:27
  • @user3781528 With your current indentation, that line is part of the function definition, not following it. Commented Nov 15, 2016 at 17:27

2 Answers 2

4

You are not calling the function.

Try

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

at the bottom of your file.

It should be like this:

##!/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")
Sign up to request clarification or add additional context in comments.

Comments

1

You had a problem with the indentation of the last print statement. It should be outside the function.

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

1 Comment

Not a problem.Happens. Just make sure the indentations are correct. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.