<p>Install biopython with conda and use the following script:</p>

<pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python
import sys
from Bio import Align
aligner = Align.PairwiseAligner()
aligner.mode = "local"
alignments = aligner.align(sys.argv[1], sys.argv[2])
print(alignments[0])
</code></pre>

<p>The output is then:</p>

<pre><code>.GGA-GGAGGGAG--AAGGAGGGAGGGAAGA-GGAGGGAG--AAGGAGGGAGGC
.|-|-||||||||--|||-|||-|||||-||-||||||||--|||-|||-|||.
AG-AAGGAGGGAGGGAAG-AGG-AGGGA-GAAGGAGGGAGGGAAG-AGG-AGG.
</code></pre>

<p>There are a LOT of alignments with that same score and you can tweak the details of the mode by changing <code>aligner.mode</code>.</p>

CC BY-SA 4.0
2018-11-26T15:53:28.473Z
yes, this should be pretty fast too since the alignment work is done in C (rather than python)
Chris_Rands
CC BY-SA 4.0
2018-11-26T15:40:59.550Z
2018-11-26T15:40:59.550Z
Devon Ryan
<p>I'd just use something like <code>t_coffee</code> and write a little bash function to run it on the strings you give:</p>

<pre class="lang-bash prettyprint-override"><code>aligner(){
  tmp=<span class="math-container">$(mktemp);
  args=("$</span>@")
  for ((i=0; i&lt;<span class="math-container">$#; i++)); do
    printf '&gt;%s\n%s\n' $</span>i <span class="math-container">${args[i]} &gt;&gt; "$</span>tmp"
  done
  t_coffee "<span class="math-container">$tmp"  2&gt;/dev/null | /bin/grep -Pv '^CLUSTAL|^\s*$</span>'
  rm "$tmp"

}
</code></pre>

<p>If you add that to your shell's initialization file (e.g. <code>~/.bashrc</code> if using bash on Linux, <code>~/.profile</code> if using bash on macOS), you can run <code>align2seqs</code> with as many sequences as you want to align:</p>

<pre><code><span class="math-container">$ aligner 'GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC' 'AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG'
0               GG--AGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC
1               nAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGG-GAAGAGGAGG
                    *********      *******  *********  * ** *  * 
$</span> aligner 'GAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC' 'GGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG' GAGGGAGGGAAGAGGAGGGAGAAGGATCCGAGGAAGAGGAGG AGAAGGAGGGAGGGAAGAGGCCCGAGATATATAAGGATCCGAGGAA
0               GA-GG----GAGAAGGAGGGAGGGAAGAGGAGGGAGAAGG--AGGGAGGC
1               GGAGG----GAGGGAAGAGGAGGGAGAAGGAGGGAG-GGA--AGAGGAGG
2               GA-GG----GAGGGAAGAGGAGGGAGAAGGATCCGA-GGA--AGAGGAGG
3               AGAAGGAGGGAGGGAAGAGGCCCGAGATATAT--AA-GGATCCGAG-GAA
                    *    ***      **   **     *       *    * *    
</code></pre>

CC BY-SA 4.0
2018-11-26T16:37:33.573Z
2018-11-26T16:37:33.573Z
terdon
<p>Here's another option: <a href="https://github.com/noporpoise/seq-align" rel="nofollow noreferrer">https://github.com/noporpoise/seq-align</a>; I've used it in the past as it is small and has no dependencies (just <code>make</code> and you're set).</p>

CC BY-SA 4.0
2018-11-29T08:23:56.683Z
This tool actually prompted my question! Do you know if anyone has packaged it?
winni2k
CC BY-SA 4.0
2018-11-29T21:27:35.127Z
Ah, no... I didn't notice the last part of your question.
Unknown artist
CC BY-SA 4.0
2018-12-04T13:25:33.730Z
Could you edit and include an example of how the OP could use this tool to do what they ask for? That will help future readers of this question.
terdon
CC BY-SA 4.0
2018-11-29T04:32:19.963Z
2018-11-29T04:32:19.963Z
Unknown artist
<p>You are looking for the <code>needle</code> program from the <code>EMBOSS</code> suite. Available in bioconda. </p>

<p><a href="http://emboss.sourceforge.net/apps/release/6.6/emboss/apps/needle.html" rel="noreferrer">http://emboss.sourceforge.net/apps/release/6.6/emboss/apps/needle.html</a></p>

<p>To read sequences from the commandline, you need so specify the format as <code>asis</code>. To get the output on the screen, you'll need <code>-stdout</code> and to use the default alignment parameters (gap penalty 10, extend_penalty 0.5) you'll need <code>-auto</code>. Thus your query above would be:</p>

<pre><code>$ needle -asequence asis:GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC -bsequence asis:AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG -auto -stdout
########################################
# Program: needle
# Rundate: Tue 27 Nov 2018 10:48:50
# Commandline: needle
#    -asequence asis:GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC
#    -bsequence asis:AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG
#    -auto
#    -stdout
# Align_format: srspair
# Report_file: stdout
########################################

#=======================================
#
# Aligned_sequences: 2
# 1: asis
# 2: asis
# Matrix: EDNAFULL
# Gap_penalty: 10.0
# Extend_penalty: 0.5
#
# Length: 56
# Identity:      37/56 (66.1%)
# Similarity:    37/56 (66.1%)
# Gaps:          18/56 (32.1%)
# Score: 181.0
# 
#
#=======================================

asis               1 GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC---     47
                              |||||||||||||||||||||||||||||||||||||.   
asis               1 ---------AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAG     41

asis              47 ------     47

asis              42 AGGAGG     47


#---------------------------------------
#---------------------------------------
</code></pre>

CC BY-SA 4.0
2018-11-26T20:13:32.210Z
2018-11-27T10:51:10.863Z
2018-11-27T10:51:10.863Z
Ian Sudbery
<p>I have two DNA strings:  </p>

<blockquote>
  <p>GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC </p>
</blockquote>

<p>and</p>

<blockquote>
  <p>AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG</p>
</blockquote>

<p>I want a tool that allows me to do something like this on the command line:</p>

<pre><code>$ aligner  GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG
</code></pre>

<p>and receive an ASCII visualization of a pairwise alignment. Something like this would work:</p>

<pre><code>GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC---------
---------AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG
</code></pre>

<p>I prefer a tool that is packaged on bioconda. A tool that also does protein and RNA sequences is even more preferable.</p>

CC BY-SA 4.0
2018-11-26T16:07:54.457Z
Is this a toy example? If so, maybe update it to another one. These sequences are low complexity and the actual correct alignment output is unclear to me
Chris_Rands
CC BY-SA 4.0
2018-11-26T16:11:02.023Z
The sequences are not important. Edits are welcome.
winni2k
CC BY-SA 4.0
2018-11-27T03:43:25.943Z
To get the alignment in your example, you have to use zero (or very small) gap cost at the ends of sequences. That is not the standard NW.
user172818
CC BY-SA 4.0
2019-04-15T10:33:54.347Z
I'm not sure that's true? To quote from the [EMBOSS package manual](http://bio.biomedicine.gu.se/cgi-bin/emboss/help/needle): 
"In a Needleman-Wunsch global alignment, the entire length of each sequence is aligned. The sequences might be partially overlapping or one sequence might be aligned entirely internally to the other. There is no penalty for the hanging ends of the overlap. In bioinformatics, it is usually reasonable to assume that the sequences are incomplete and there should be no penalty for failing to align the missing bases."
winni2k
<phylogenetics>
What is a simple command line tool for doing Needleman-Wunsch pair-wise alignment on the command line
# What is a simple command line tool for doing Needleman-Wunsch pair-wise alignment on the command line

(Asked by: username_0 on 2018-11-26)

I have two DNA strings:  

GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC 

and

AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG

I want a tool that allows me to do something like this on the command line:
$ aligner  GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG

and receive an ASCII visualization of a pairwise alignment. Something like this would work:
GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC---------
---------AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG

I prefer a tool that is packaged on bioconda. A tool that also does protein and RNA sequences is even more preferable.

**Tags:** phylogenetics
> **Comments:**
> **username_1**: Is this a toy example? If so, maybe update it to another one. These sequences are low complexity and the actual correct alignment output is unclear to me  
> **username_0**: The sequences are not important. Edits are welcome.  
> **username_2**: To get the alignment in your example, you have to use zero (or very small) gap cost at the ends of sequences. That is not the standard NW.  
> **username_0**: I'm not sure that's true? To quote from the [EMBOSS package manual](http://bio.biomedicine.gu.se/cgi-bin/emboss/help/needle):  "In a Needleman-Wunsch global alignment, the entire length of each sequence is aligned. The sequences might be partially overlapping or one sequence might be aligned entirely internally to the other. There is no penalty for the hanging ends of the overlap. In bioinformatics, it is usually reasonable to assume that the sequences are incomplete and there should be no penalty for failing to align the missing bases."  

---

## 4 answers

### Answer by username_6 on 2018-11-26. Score: 11 | Accepted answer

You are looking for the needle program from the EMBOSS suite. Available in bioconda. 
http://emboss.sourceforge.net/apps/release/6.6/emboss/apps/needle.html
To read sequences from the commandline, you need so specify the format as asis. To get the output on the screen, you'll need -stdout and to use the default alignment parameters (gap penalty 10, extend_penalty 0.5) you'll need -auto. Thus your query above would be:
$ needle -asequence asis:GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC -bsequence asis:AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG -auto -stdout
########################################
# Program: needle
# Rundate: Tue 27 Nov 2018 10:48:50
# Commandline: needle
#    -asequence asis:GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC
#    -bsequence asis:AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG
#    -auto
#    -stdout
# Align_format: srspair
# Report_file: stdout
########################################

#=======================================
#
# Aligned_sequences: 2
# 1: asis
# 2: asis
# Matrix: EDNAFULL
# Gap_penalty: 10.0
# Extend_penalty: 0.5
#
# Length: 56
# Identity:      37/56 (66.1%)
# Similarity:    37/56 (66.1%)
# Gaps:          18/56 (32.1%)
# Score: 181.0
# 
#
#=======================================

asis               1 GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC---     47
                              |||||||||||||||||||||||||||||||||||||.   
asis               1 ---------AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAG     41

asis              47 ------     47

asis              42 AGGAGG     47


#---------------------------------------
#---------------------------------------


---

### Answer by username_3 on 2018-11-26. Score: 9

Install biopython with conda and use the following script:
#!/usr/bin/env python
import sys
from Bio import Align
aligner = Align.PairwiseAligner()
aligner.mode = "local"
alignments = aligner.align(sys.argv[1], sys.argv[2])
print(alignments[0])

The output is then:
.GGA-GGAGGGAG--AAGGAGGGAGGGAAGA-GGAGGGAG--AAGGAGGGAGGC
.|-|-||||||||--|||-|||-|||||-||-||||||||--|||-|||-|||.
AG-AAGGAGGGAGGGAAG-AGG-AGGGA-GAAGGAGGGAGGGAAG-AGG-AGG.

There are a LOT of alignments with that same score and you can tweak the details of the mode by changing aligner.mode.

> **Comments:**
> **username_1**: yes, this should be pretty fast too since the alignment work is done in C (rather than python)  

---

### Answer by username_4 on 2018-11-26. Score: 4

I'd just use something like t_coffee and write a little bash function to run it on the strings you give:
aligner(){
  tmp=$(mktemp);
  args=("$@")
  for ((i=0; i<$#; i++)); do
    printf '>%s\n%s\n' $i ${args[i]} >> "$tmp"
  done
  t_coffee "$tmp"  2>/dev/null | /bin/grep -Pv '^CLUSTAL|^\s*$'
  rm "$tmp"

}

If you add that to your shell's initialization file (e.g. ~/.bashrc if using bash on Linux, ~/.profile if using bash on macOS), you can run align2seqs with as many sequences as you want to align:
$ aligner 'GGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC' 'AGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG'
0               GG--AGGAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC
1               nAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGG-GAAGAGGAGG
                    *********      *******  *********  * ** *  * 
$ aligner 'GAGGGAGAAGGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGC' 'GGAGGGAGGGAAGAGGAGGGAGAAGGAGGGAGGGAAGAGGAGG' GAGGGAGGGAAGAGGAGGGAGAAGGATCCGAGGAAGAGGAGG AGAAGGAGGGAGGGAAGAGGCCCGAGATATATAAGGATCCGAGGAA
0               GA-GG----GAGAAGGAGGGAGGGAAGAGGAGGGAGAAGG--AGGGAGGC
1               GGAGG----GAGGGAAGAGGAGGGAGAAGGAGGGAG-GGA--AGAGGAGG
2               GA-GG----GAGGGAAGAGGAGGGAGAAGGATCCGA-GGA--AGAGGAGG
3               AGAAGGAGGGAGGGAAGAGGCCCGAGATATAT--AA-GGATCCGAG-GAA
                    *    ***      **   **     *       *    * *


---

### Answer by username_5 on 2018-11-29. Score: 3

Here's another option: <https://github.com/noporpoise/seq-align>; I've used it in the past as it is small and has no dependencies (just `make` and you're set).

> **Comments:**
> **username_0**: This tool actually prompted my question! Do you know if anyone has packaged it?  
> **username_5**: Ah, no... I didn't notice the last part of your question.  
> **username_4**: Could you edit and include an example of how the OP could use this tool to do what they ask for? That will help future readers of this question.  

---

