-1

I have used the below code to get the local alignment score between two strings using Smith-Waterman Algorithm. However, I'm getting the required output, I'm finding it difficult to save the result into some variable for further analysis.

import swalign


def Local_Alignment(string1, string2):
    match_score = 100
    mismatch_score = -100
    matrix = swalign.NucleotideScoringMatrix(match_score, mismatch_score)
    lalignment_object = swalign.LocalAlignment(matrix)
    alignment_object = lalignment_object.align(string1, string2)
    return alignment_object.dump()

string1 = "ABCDEFGHIJKLMNOP"
string2 = "CDGIKNOP"
temp = Local_Alignment(string1, string2)

Whenever I try to save the result into some variable, it simply stores a None value. Even though I tried storing the result in a text file, that also didn't work.

wovano
  • 4,543
  • 5
  • 22
  • 49
  • Was the answer helpful? Since you're (relatively) new here, you might want to read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – wovano Nov 27 '22 at 08:05

2 Answers2

0

if you got to the implementation of the library you can see in the dump function the results are dumped on the console. That is why it is returning nothing when you call the function and display temp in your case.

However what you can do is go to the implementation copy the dump function and paste it there and rename it to something else, add a addition argument as filename and try to write everything in the file whatever is put on to the console

  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 21 '22 at 19:16
0

As can be seen in the source code, the dump() method writes to a parameter out, which is sys.stdout by default. So if you don't supply the out parameter, everything is written to the standard output (console). The method does not return anything, so your statement return alignment_object.dump() doesn't do what you want, but returns None instead.

Fortunately, out is expected to be a Text I/O object, which is pretty common in Python. You can pass any file-like object as out parameter, for example an actual file or an io.StringIO object.

So, if you want to write the output to a file, you could pass the file as argument like this:

with open('dump.txt', 'wt', encoding='utf-8') as file:
    return alignment_object.dump(out=file)

If you want to store the output in a variable, so you can do whatever you want with it, use an io.StringIO object like this:

import io

import swalign


def Local_Alignment(string1, string2):
    match_score = 100
    mismatch_score = -100
    matrix = swalign.NucleotideScoringMatrix(match_score, mismatch_score)
    lalignment_object = swalign.LocalAlignment(matrix)
    alignment_object = lalignment_object.align(string1, string2)
    with io.StringIO() as file:
        alignment_object.dump(out=file)
        return file.getvalue()


string1 = "ABCDEFGHIJKLMNOP"
string2 = "CDGIKNOP"
temp = Local_Alignment(string1, string2)
wovano
  • 4,543
  • 5
  • 22
  • 49