Run the needle program for a query against the subject files and return the result I have written the script to run locally the needle program for a query against the subject files. It returns the highest percentage result file as a string. In addition it also converts to html file (write_html function).

Can you help me to improve the script?

subject_path = settings.MEDIA_ROOT+"subject_files/"
query_path = settings.MEDIA_ROOT+"query_file/"


def run_bug():
    """The script runs the needle program for a query file against all the subject files in a folder and keeps only
    the highest identity score alignments.
    """

    query_list_fastafiles, subject_list_fastafiles = start_function()
    files_ending_with_one = filter_files_ending_with_one(subject_list_fastafiles)

    for j in range(len(query_list_fastafiles)):
        initial = 0
        needle_alignment_result = ''

        #take the scaffold sequence one by one
        for i in range(len(files_ending_with_one)):

            #path of the files
            file1 = query_path+ordered_query_fastafiles[j]
            file1 = subject_path+files[i]
            needle_output = run_needle_two_sequences(file1, file2)

            if needle_output == None:
                return needle_alignment_result = needle_output
            else:
                #print(ordered_query_fastafiles[j],ordered_scaffold_fastafiles[i])
                identity_percentage, result_file_as_string = needle_output
                #print(identity_percentage, results)
                identity_percentage = float(identity_percentage)

                if identity_percentage &gt; initial:
                #keeps the highest percentage result among the files
                    initial = identity_percentage
                    needle_alignment_result = result_file_as_string

    write_html(needle_alignment_result)
    return needle_alignment_result
A very short review, as I don't have the time for a more extensive one.

Enumerate

Some things I see right off the bat is range(len()). Instead of writing this, consider using enumerate. This will allow you to work with any iterable, not just countable, indexable objects.

Unnecessary else

I see this piece of code next:

if needle_output == None:
    return needle_alignment_result = needle_output
else:
    ... code here ...


The else here is unnecessary, as you exit the function after returning in the if. Just move the code in the else right after the if, and remove the unnecessary else, like so:

if needle_output == None:
    return needle_alignment_result = needle_output
... code here ...


is None vs == None

Below is from an amazing StackOverflow answer.


  A class is free to implement
  comparison any way it chooses, and it
  can choose to make comparison against
  None means something (which actually
  makes sense; if someone told you to
  implement the None object from
  scratch, how else would you get it to
  compare True against itself?).


Practically-speaking, there is not much difference since custom comparison operators are rare.  But you should use is None as a general rule.

Updated Code

def run_bug():
    """
    The script runs the needle program for a query file against all the
    subject files in a folder and keeps only the highest identity score alignments.
    """
    needle_alignment_result = ''

    query_list_fastafiles, subject_list_fastafiles = start_function()
    files_ending_with_one = filter_files_ending_with_one(subject_list_fastafiles)

    for j, _ in enumerate(query_list_fastafiles):
        initial = 0

        # Take the scaffold sequence one by one
        for i, _ in enumerate(files_ending_with_one):

            # Path of the files
            file1 = query_path + ordered_query_fastafiles[j]
            file1 = subject_path + files[i]
            needle_output = run_needle_two_sequences(file1, file2)

            if needle_output is None:
                return needle_alignment_result == needle_output

            identity_percentage, result_file_as_string = needle_output
            identity_percentage = float(identity_percentage)

            # Keeps the highest percentage result among the files
            if identity_percentage &gt; initial:
                initial = identity_percentage
                needle_alignment_result = result_file_as_string

    write_html(needle_alignment_result)
    return needle_alignment_result