# awk code to reconstruct sequence from alignment

**URL:** <https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [July 22, 2013, 8:29am UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103 "2013-07-22T08:29:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fahmida](https://community.unix.com/letter_avatar/fahmida/32/6_449c533f586939b6e5f246632a46421f.png) [@Fahmida](https://community.unix.com/u/Fahmida)\
**Post date:** [July 22, 2013, 8:29am UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/1 "2013-07-22T08:29:44Z")

</div>

Hi Everyone,

I need some help to construct a long 'Sbjct' string from the following input using incremental order of 'Sbjct' starting number (e.g. 26325115,33716368,33769033,34869860 etc.)  
Different 'Sbject' string will be separated by 'NNNN's as:

(Sbjct:26325115-26325094)NNNN(Sbjct:33716368-33716347)NNNN(Sbjct:33769033-33769073)NNNN(Sbjct:34869860-34869889)

The output expected is shown in 'Example Output'.

--------- Example Input (just a small segment of the whole file)-----------

Score = 44.1 bits (22), Expect = 0.30  
Identities = 28/30 (93%)  
Strand = Plus / Plus

Query: 1684 atcaaaatgaccaaaatatttcattaaaaa 1713  
|||||||||| |||||| ||||||||||||  
Sbjct: 34869860 atcaaaatgaacaaaatgtttcattaaaaa 34869889

Score = 44.1 bits (22), Expect = 0.30  
Identities = 22/22 (100%)  
Strand = Plus / Minus

Query: 1758 ttagggtttagagttaaggggt 1779  
||||||||||||||||||||||  
Sbjct: 26325115 ttagggtttagagttaaggggt 26325094

Score = 44.1 bits (22), Expect = 0.30  
Identities = 22/22 (100%)  
Strand = Plus / Minus

Query: 1687 aaaatgaccaaaatatttcatt 1708  
||||||||||||||||||||||  
Sbjct: 33716368 aaaatgaccaaaatatttcatt 33716347

Score = 44.1 bits (22), Expect = 0.30  
Identities = 38/42 (90%), Gaps = 1/42 (2%)  
Strand = Plus / Plus

Query: 1734 ccctagggttaactaattcaaaccttagggtttagagttaag 1775  
||||||| ||||||||| |||| ||||||||||||||||||  
Sbjct: 33769033 ccctaggattaactaatctaaac-ttagggtttagagttaag 33769073

----------------------

---------------------- Example Output -----------  
Whole Sbjct string  
ttagggtttagagttaaggggtNNNNaaaatgaccaaaatatttcattNNNNccctaggattaactaatctaaac-ttagggtttagagttaagNNNNatcaaaatgaacaaaatgtttcattaaaaa

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![vidyadhar85](https://community.unix.com/user_avatar/community.unix.com/vidyadhar85/32/997_2.png) [@vidyadhar85](https://community.unix.com/u/vidyadhar85)\
**Post date:** [July 22, 2013, 8:32am UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/2 "2013-07-22T08:32:15Z")

</div>

Could you please put some code tag ?

---

<div class="post-metadata">

**Author:** ![Fahmida](https://community.unix.com/letter_avatar/fahmida/32/6_449c533f586939b6e5f246632a46421f.png) [@Fahmida](https://community.unix.com/u/Fahmida)\
**Post date:** [July 22, 2013, 3:27pm UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/3 "2013-07-22T15:27:58Z")

</div>

I need some help to construct a long 'Sbjct' string from the following input using incremental order of 'Sbjct' starting number (e.g. 26325115,33716368,33769033,34869860 etc.)  
Different 'Sbject' string will be separated by 'NNNN's as:

```nohighlight
(Sbjct:26325115-26325094)NNNN(Sbjct:33716368-33716347)NNNN(Sbjct:33769033-33769073)NNNN(Sbjct:34869860-34869889)

```

The output expected is shown in 'Example Output'.

```nohighlight
--------- Example Input (just a small segment of the whole file)-----------

Score = 44.1 bits (22), Expect = 0.30
Identities = 28/30 (93%)
Strand = Plus / Plus

Query: 1684 atcaaaatgaccaaaatatttcattaaaaa 1713
|||||||||| |||||| ||||||||||||
Sbjct: 34869860 atcaaaatgaacaaaatgtttcattaaaaa 34869889

Score = 44.1 bits (22), Expect = 0.30
Identities = 22/22 (100%)
Strand = Plus / Minus

Query: 1758 ttagggtttagagttaaggggt 1779
||||||||||||||||||||||
Sbjct: 26325115 ttagggtttagagttaaggggt 26325094

Score = 44.1 bits (22), Expect = 0.30
Identities = 22/22 (100%)
Strand = Plus / Minus

Query: 1687 aaaatgaccaaaatatttcatt 1708
||||||||||||||||||||||
Sbjct: 33716368 aaaatgaccaaaatatttcatt 33716347

Score = 44.1 bits (22), Expect = 0.30
Identities = 38/42 (90%), Gaps = 1/42 (2%)
Strand = Plus / Plus

Query: 1734 ccctagggttaactaattcaaaccttagggtttagagttaag 1775
||||||| ||||||||| |||| ||||||||||||||||||
Sbjct: 33769033 ccctaggattaactaatctaaac-ttagggtttagagttaag 33769073

```

```nohighlight
---------------------- Example Output -----------
Whole Sbjct string
ttagggtttagagttaaggggtNNNNaaaatgaccaaaatatttcattNNNNccctaggattaactaatctaaac-ttagggtttagagttaagNNNNatcaaaatgaacaaaatgtttcattaaaaa

```

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [July 22, 2013, 4:12pm UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/4 "2013-07-22T16:12:04Z")

</div>

Fahmida, for future reference, note that you could have edited the 1st message in this thread to add CODE tags instead of duplicating it in a new message. 🙂

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [July 22, 2013, 4:37pm UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/5 "2013-07-22T16:37:11Z")

</div>

If you have gawk:

```nohighlight
gawk '
        /Sbjct/ {
                A[$2] = $3
        }
        END {
                n = asorti ( A, D )
                for ( i = 1; i <= n; i++ )
                        s = s ? s "NNNN" A[D] : A[D]
                print s
        }
' file

```

Output:

```nohighlight
ttagggtttagagttaaggggtNNNNaaaatgaccaaaatatttcattNNNNccctaggattaactaatctaaac-ttagggtttagagttaagNNNNatcaaaatgaacaaaatgtttcattaaaaa

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 22, 2013, 5:02pm UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/6 "2013-07-22T17:02:21Z")

</div>

With regular awk, try:

```nohighlight
awk '
  NR==FNR{
    if(/Sbjct/)A[$2 "-" $4]=$3
    next
  }
  { 
    for(i=2; i<=NF; i+=2) $i=A[$i]
  }
  1
' file2 FS='\\(Sbjct:|\\)' OFS= file1

```

```nohighlight
$ cat file1
(Sbjct:26325115-26325094)NNNN(Sbjct:33716368-33716347)NNNN(Sbjct:33769033-33769073)NNNN(Sbjct:34869860-34869889)

```

Output:

```nohighlight
ttagggtttagagttaaggggtNNNNaaaatgaccaaaatatttcattNNNNccctaggattaactaatctaaac-ttagggtttagagttaagNNNNatcaaaatgaacaaaatgtttcattaaaaa

```

---

<div class="post-metadata">

**Author:** ![Fahmida](https://community.unix.com/letter_avatar/fahmida/32/6_449c533f586939b6e5f246632a46421f.png) [@Fahmida](https://community.unix.com/u/Fahmida)\
**Post date:** [July 23, 2013, 9:09am UTC](https://community.unix.com/t/awk-code-to-reconstruct-sequence-from-alignment/332103/7 "2013-07-23T09:09:40Z")

</div>

Dear Yoda,

Thanks for your reply. Your code is working, it adds the 'NNNN's after each subject line, it would be great if code is modified to add 'NNNN's at the end of the Sbjct of a whole block (each block starts with 'Score =...' and may contain many Sbjct lines).  
For example there are three segments below. If we look at the starting point of 'Sbjcts' at each segment: 11561582, 3294707, 11140709,.

```nohighlight
Score = 139 bits (70), Expect = 7e-30
 Identities = 103/114 (90%)
 Strand = Plus / Plus
Query: 2040 atctagagtattatggtctttttacatattaaatgaaacattttgatcattttcctactt 2099
                |||||| |||||| |||| |||||| ||||||||||||||||||| |||||||||| |||
Sbjct: 11561582 atctagggtattagggtccttttacctattaaatgaaacattttggtcattttcctcctt 11561641
                                                                     
Query: 2100 gtggtatatttttgtgatcaaaatttgaaaatagtatatttaggagaattgccc 2153
                ||||| ||||||||||||||||| ||||||| || ||||||||||||||||||
Sbjct: 11561642 gtggtctatttttgtgatcaaaacatgaaaatggtctatttaggagaattgccc 11561695

Score = 139 bits (70), Expect = 7e-30
 Identities = 103/114 (90%)
 Strand = Plus / Minus
                                                                         
Query: 2040 atctagagtattatggtctttttacatattaaatgaaacattttgatcattttcctactt 2099
               |||||| |||||| |||| |||||| ||||||||||||||||||| ||||||| || || 
Sbjct: 3294707 atctagggtattagggtcattttacctattaaatgaaacattttggtcatttttctcctc 3294648
                                                                
Query: 2100 gtggtatatttttgtgatcaaaatttgaaaatagtatatttaggagaattgccc 2153
               ||||||||||||||||||||||| |||||||| || ||||||||||||||||||
Sbjct: 3294647 gtggtatatttttgtgatcaaaacttgaaaattgtctatttaggagaattgccc 3294594

 Score = 139 bits (70), Expect = 7e-30
 Identities = 103/114 (90%)
 Strand = Plus / Minus
                                                                       
Query: 2040 atctagagtattatggtctttttacatattaaatgaaacattttgatcattttcctactt 2099
                |||||| |||||| | |||||||| ||||||||||||||||||| |||||||||| |||
Sbjct: 11140709 atctagggtattaggatctttttagctattaaatgaaacattttggtcattttcctcctt 11140650
                                                                  
Query: 2100 gtggtatatttttgtgatcaaaatttgaaaatagtatatttaggagaattgccc 2153
                ||||| ||||||||||| ||||| || |||||||||||||||||||||||||||
Sbjct: 11140649 gtggtttatttttgtgaccaaaacttaaaaatagtatatttaggagaattgccc 11140596

```

So,  
1) Sort the Sbjct based on their starting points in each block i.e 3294707, 11140709, 11561582,  
2) Add Sbjcts strings of each block in the order of (1) separated by 'NNNN'.

Expected Output is:

```nohighlight
gtggtatatttttgtgatcaaaacttgaaaattgtctatttaggagaattgccc
atctagggtattagggtcattttacctattaaatgaaacattttggtcatttttctcctcNNNN
gtggtttatttttgtgaccaaaacttaaaaatagtatatttaggagaattgcccatctagggta
ttaggatctttttagctattaaatgaaacattttggtcattttcctccttNNNNatctagggtatt
agggtccttttacctattaaatgaaacattttggtcattttcctccttgtggtctatttttgtgatca
aaacatgaaaatggtctatttaggagaattgccc

```
