-4

I have file with DNA sequence as in example below. How can i find count of A,T,G,C in each column (yes, not row, but column) using bash shell commands.

The file has 846975 rows like this:

AAGAAAGAAGAGGAACTTCTCTCCATCCAGCCTCATTCCACTGCACCAACTCTTCTGTGT
CGGGTTGTGCAGGAGAGAAAGGGAGCTTGGCAACTCTTTGCTGTGCTGAGTTGTGGTAGC
CCATCACTGGGTTGTAAAGTGCCTTGCCTCCTTTCCTCCCCTCCTTTTTTTTTGAGACAG
AGTCTCACTCTGTCGTCCAGGCTGAGGTGCAGTGGTGCGATCTCTGCTCACTGCAACCTC
AGCCTCCTGGGTTCAAGTGATTCTCCTGCCTCAGCCTCCCAGGAAGCTGGGACTACAGGC
ACATGCCACCACACCTGGCTAACTTTTTTTTATTTTTAGTAGAGAAAGGGTATCACCATG
TTGGCCAGGCTGGTCTTGAACTCCTGACTTCAGGTGATCCACCCACCTTGGCCCCCCAAA
GTGCTGGGGTTAAAGGCATGAGACACTGCGCCCGTCCACCTCCTCTTTTACTTGGGAGAA
ATGCACAGATTCTGGGTGCCATGTGCATTTGTTTTGGGAGTGATAATTGATCTAACTTAT
GGAAATAATACTAGATAGTTAGCGGATGGATTCTGTATCTGATGAGAGTTTTGGGCAAAA
CGAATTCCTAGTTTCTGAGTCTTATTTTTCCCCTGATTCAAGAAAACTGTGAATTATCCA
GCCAGTAAAAAACTCTCACAGCTCTGGATGTGAGTTTAGGACACTGGATTTCTACCACTC
ATTTTCTTACTACTTTTCCTGTGCAAGGATCATGGCACAAGTTGCAGTTTCCACCCTGCC
CATTGAAGATGAGGAGTCTGTTGAAGATGAGGAGTCCTTGGAGAGCAGGATGGTGGTGAC
ATTCCTGTCAGCTCTCGCCTCCATGGTCAGACCTTCTGTTCTCACATTCTGTAGTTCGGT
AGGACTGGGCGGTAGATAAGGTTGATTTGTTTTCGTAGAACTTACAATTTTGTGATTTTT
AGTTCTAATGAGTAGACCTTTTTCGTGAATAGTAGTTACGATCAAACACCTCTGACCAAA

For this example, the output for the first column is supposed to be A=9,T=1,G=3,C=4.

closed as unclear what you're asking by muru, sebasth, Kusalananda, Anthony Geoghegan, αғsнιη Oct 10 '17 at 8:56

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    in each column - you mean in each row? – RomanPerekhrest Oct 10 '17 at 5:52
  • i have large sequence with 846975 rows ...it would be better if u suggest me column wise A,T,G,C count.... – user254856 Oct 10 '17 at 9:04
  • column wise A,T,G,C count - post how should look the expected result (at least few lines) – RomanPerekhrest Oct 10 '17 at 9:06
  • for the above sequence i have submitted...the asuumed output should be like A=9 ,T=1,G=3,C=4 – user254856 Oct 10 '17 at 9:16
  • so my answer in second section columnwise answering your question while other answers are per row, please edit your post with the comment as you clarified – αғsнιη Oct 10 '17 at 9:23
1

In general case where your awk does support null/empty field separator (this is gawk specific), you can do with something like below (when you want counts per line).

gawk -F '' '{for (x=1;x<=NF;x++) arr[NR$x]++;
    for (i in arr) printf("%s:%d\t", i, arr[i]);delete arr;print ""}' infile

NO, if you need really counts per each column (assuming empty filed separator), first you need to transform the input data from column into rows, then do the same as above.

gawk -F '' '{for (x=1;x<=NF;x++) arr[NR$x]++;
    for (i in arr) printf("%s:%d\t", i, arr[i]);delete arr;print ""}' \
<(gawk -F '' '{for (i=1;i<=NF;i++) ctr[i]=ctr[i]$i} 
    END{for (i in ctr) print ctr[i]}' infile)

With below awk script we are transposing the columns into rows.

gawk -F '' '{for (i=1;i<=NF;i++) ctr[i]=ctr[i]$i} 
    END{for (i in ctr) print ctr[i]}' infile

You can use below command instead of above one for transposing.

sed 's/./& /g' in|datamash -t ' ' transpose |sed 's/ //g'
0

I suppose you meant to find count of A,T,G,C in each row.

GNU awk solution:

awk -v FPAT="[A-Z]" '{ 
           printf "A: %s, T: %s, G: %s, C: %s\n",
           gsub("A",""),gsub("T",""),gsub("G",""),gsub("C","") 
       }' file

The output:

A: 17, T: 15, G: 9, C: 19
A: 11, T: 16, G: 24, C: 9
A: 8, T: 23, G: 10, C: 19
A: 9, T: 16, G: 16, C: 19
A: 11, T: 13, G: 16, C: 20
A: 17, T: 20, G: 10, C: 13
A: 11, T: 14, G: 13, C: 22
A: 12, T: 15, G: 17, C: 16
A: 14, T: 23, G: 15, C: 8
A: 20, T: 18, G: 17, C: 5
A: 16, T: 23, G: 8, C: 13
A: 17, T: 16, G: 12, C: 15
A: 12, T: 21, G: 10, C: 17
A: 16, T: 14, G: 24, C: 6
A: 8, T: 22, G: 11, C: 19
A: 14, T: 25, G: 16, C: 5
A: 20, T: 18, G: 10, C: 12
-1

I'm assuming that the user want's to count the bases in each row.

Using Perl:

$ perl -ne 'chomp; printf("%s\t%2d %2d %2d %2d\n", $_, tr/A/A/, tr/C/C/, tr/G/G/, tr/T/T/)' file
AAGAAAGAAGAGGAACTTCTCTCCATCCAGCCTCATTCCACTGCACCAACTCTTCTGTGT    17 19  9 15
CGGGTTGTGCAGGAGAGAAAGGGAGCTTGGCAACTCTTTGCTGTGCTGAGTTGTGGTAGC    11  9 24 16
CCATCACTGGGTTGTAAAGTGCCTTGCCTCCTTTCCTCCCCTCCTTTTTTTTTGAGACAG     8 19 10 23
AGTCTCACTCTGTCGTCCAGGCTGAGGTGCAGTGGTGCGATCTCTGCTCACTGCAACCTC     9 19 16 16
AGCCTCCTGGGTTCAAGTGATTCTCCTGCCTCAGCCTCCCAGGAAGCTGGGACTACAGGC    11 20 16 13
ACATGCCACCACACCTGGCTAACTTTTTTTTATTTTTAGTAGAGAAAGGGTATCACCATG    17 13 10 20
TTGGCCAGGCTGGTCTTGAACTCCTGACTTCAGGTGATCCACCCACCTTGGCCCCCCAAA    11 22 13 14
GTGCTGGGGTTAAAGGCATGAGACACTGCGCCCGTCCACCTCCTCTTTTACTTGGGAGAA    12 16 17 15
ATGCACAGATTCTGGGTGCCATGTGCATTTGTTTTGGGAGTGATAATTGATCTAACTTAT    14  8 15 23
GGAAATAATACTAGATAGTTAGCGGATGGATTCTGTATCTGATGAGAGTTTTGGGCAAAA    20  5 17 18
CGAATTCCTAGTTTCTGAGTCTTATTTTTCCCCTGATTCAAGAAAACTGTGAATTATCCA    16 13  8 23
GCCAGTAAAAAACTCTCACAGCTCTGGATGTGAGTTTAGGACACTGGATTTCTACCACTC    17 15 12 16
ATTTTCTTACTACTTTTCCTGTGCAAGGATCATGGCACAAGTTGCAGTTTCCACCCTGCC    12 17 10 21
CATTGAAGATGAGGAGTCTGTTGAAGATGAGGAGTCCTTGGAGAGCAGGATGGTGGTGAC    16  6 24 14
ATTCCTGTCAGCTCTCGCCTCCATGGTCAGACCTTCTGTTCTCACATTCTGTAGTTCGGT     8 19 11 22
AGGACTGGGCGGTAGATAAGGTTGATTTGTTTTCGTAGAACTTACAATTTTGTGATTTTT    14  5 16 25
AGTTCTAATGAGTAGACCTTTTTCGTGAATAGTAGTTACGATCAAACACCTCTGACCAAA    20 12 10 18

Or

$ perl -ne 'BEGIN { print(" A  C  G  T\n") } printf("%2d %2d %2d %2d\n", tr/A/A/, tr/C/C/, tr/G/G/, tr/T/T/)' file
 A  C  G  T
17 19  9 15
11  9 24 16
 8 19 10 23
 9 19 16 16
11 20 16 13
17 13 10 20
11 22 13 14
12 16 17 15
14  8 15 23
20  5 17 18
16 13  8 23
17 15 12 16
12 17 10 21
16  6 24 14
 8 19 11 22
14  5 16 25
20 12 10 18

The tr operator in Perl does transliteration, pretty much like the tr utility in the shell. It returns the number of characters transliterated.

Not the answer you're looking for? Browse other questions tagged or ask your own question.