1

I have a program that runs through a list of Amino Acid sequences for a protein in Influenza A that I'm analyzing and finds amino acid at position 627 in each amino acid sequence in my FASTA file for each protein.

My code works like this

with open(file, "r" ) as source:
    for heading_and_lines in group_by_heading( source ):
        heading= heading_and_lines[0]
        lines= heading_and_lines[1:]
        lines = ''.join(lines)
    if lines[627-1] == 'K':
        print "---------------MUTATION BELOW--------------"
        print heading
        print lines[627-1]
        #print "-------------------------------------------"

print "end of file"

But my code does not work like this

with open(file, "r" ) as source:
    for heading_and_lines in group_by_heading( source ):
        heading= heading_and_lines[0]
        lines= heading_and_lines[1:]
        lines = ''.join(lines)
    if lines[627-1] == 'K':
        print "---------------MUTATION BELOW--------------"
        print heading
        print lines[627-1]
        print "-------------------------------------------"

print "end of file"

For some reason printing the line below the mutation that I found returns an error, and unexpected indent. See I want the line beneath each mutation, just like the line on top, so I can clearly organize the mutations. Any reason why you think the printing statement below doesn't work?

Sorry if this sounded confusion, and as always thanks for your time.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
SweetJD14
  • 73
  • 1
  • 2
  • 11
  • 2
    Check tabs and spacing. If you have both in your program, it'll break. – TigerhawkT3 Apr 28 '16 at 08:11
  • When I tab it back it prints a line below for each strain, when I only want to print the line below for strains that have the mutation of lysine (AKA an amino acid 'K' match) – SweetJD14 Apr 28 '16 at 08:14
  • Darn. I just checked all the spacing and it just won't let me print any string literal after I have printed the amino acid at position [627-1]. For some reason it keeps saying unexpected indent. – SweetJD14 Apr 28 '16 at 08:17

1 Answers1

1

Your indentation is wrong, indent your code like this

with open(file, "r" ) as source:
    for heading_and_lines in group_by_heading( source ):
        heading= heading_and_lines[0]
        lines= heading_and_lines[1:]
        lines = ''.join(lines)

        if lines[627-1] == 'K':
            print "---------------MUTATION BELOW--------------"
            print heading
            print lines[627-1]
            print "-------------------------------------------"
danidee
  • 9,298
  • 2
  • 35
  • 55
  • Perfect. Works Perfectly as I wanted. Could you go into a bit more detail about why my indentation was wrong? – SweetJD14 Apr 28 '16 at 08:20
  • 1
    @SweetJD14 Hey there, you might be new, but the best way of saying "thanks" around here is to accept answers. You can do this by clicking the gray "V" under the arrows to the left of the question's text. This way, other users can see that you accepted the answer and it's probably the best one. – Amit Gold Apr 28 '16 at 08:23
  • Got it, just clicked it! Yeah I'm a bit new, thanks for the advice. Have a good one. – SweetJD14 Apr 28 '16 at 08:26
  • indentation is the way python groups blocks of code (usually done by `{}`) in c based languages. take a look at indentation in the docs https://www.python.org/dev/peps/pep-0008/ and this detailed answer about variable scopes in for loops which applies to your situation. the if statement should be indented so you can compare all the `lines` you've joined not at the end (it'll only compare the last one) http://stackoverflow.com/questions/3611760/scoping-in-python-for-loops – danidee Apr 28 '16 at 08:42