1

I'm working on a program that reads trough a FASTQ file and gives the amount of N's per sequence in this file. I managed to get the number of N's per line and I put these in a list. The problem is that I need all the numbers in one list to sum op the total amount of N's in the file but they get printed in their own list.

C:\Users\Zokids\Desktop>N_counting.py test.fastq
[4]
4
[3]
3
[5]
5 

This is my output, the List and total amount in the list. I've seen ways to manually combine lists but one can have hundreds of sequences so that's a no go.

def Count_N(line):
    '''
    This function takes a line and counts the anmount of N´s in the line
    '''
    List = []
    Count = line.count("N") # Count the amount of N´s that are in the line returned by import_fastq_file
    List.append(int(Count))

    Total = sum(List)
    print(List)
    print(Total)

This is what I have as code, another function selects the lines.

I hope someone can help me with this. Thank you in advance.

Paul
  • 10,381
  • 13
  • 48
  • 86
Thije Zuidewind
  • 87
  • 1
  • 1
  • 7
  • You may want to look into [`itertools.chain`](https://docs.python.org/3/library/itertools.html#itertools.chain). – Paul Apr 03 '16 at 15:43

2 Answers2

1

The List you're defining in your function never gets more than one item, so it's not very useful. Instead, you should probably return the count from the function, and let the calling code (which is presumably running in some kind of loop) append the value to its own list. Of course, since there's not much to the function, you might just move it's contents out to the loop too!

For example:

list_of_counts = []
for line in my_file:
    count = line.count("N")
    list_of_counts.append(count)
total = sum(list_of_counts)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

Looks from your code you send one line each time you call count_N(). List you declared is a local list and gets reinitialized when you call the function each time. You can declare the list global using:

global List =[]

I think you will also need to declare the list outside function in order to access it globally.

Also it would be better if you Total the list outside the function. Right now you are summing up list inside function. For that you will need to match indentation with the function declaration.

Denis
  • 1,219
  • 1
  • 10
  • 15
  • 1
    It is usually bad form to use a global like this. If you really want to append to a list, you should probably just pass it to `Count_N` as a parameter. – Paul Apr 03 '16 at 15:53
  • 1
    Yeah I agree but given give this problem I would also suggest pass the file name and iter through lines inside function. – Denis Apr 03 '16 at 15:55