Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • thom_otis
    Junior Member
    • Jun 2015
    • 2

    Script To Calculate Word Frequency For Many Sequences

    Hello!

    Who can help me with a writing a script?

    1. For each file in the set (archive, probably):

    Open input nucleotide sequence (.embl format is desirable, but fasta format is possible);

    Calculate the observable frequency for all possible words based on input word size (e.g. number of all possible words for word size = 3 (triplet) is 64). Obs. Frequency = Obs Count/Total count [Сompseq is work in a similar way, but not support a batch processing].

    Shift the reading frame by 1 nucleotide and repeat the previous step (number of shifts = word size - 1). The file with frequencies for each file in the set save as name_of_the_file(id of a contig).dic(+word size). It looks sth like this (word size = 2):



    2. Make a summary file, something like this (example for word size = 2)
  • Richard Finney
    Senior Member
    • Feb 2009
    • 701

    #2
    example usage: cat hg20/chr1.fa | ./kmer3

    Code:
    -bash-4.1$ cat kmer3.c
    
    /*
    vi kmer3.c ; gcc -Wall -O3  kmer3.c -o kmer3  -lm 
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <errno.h>
    #include <assert.h>
    
    long int x3[4][4][4];
    
    int acgt_index(char c)
    {
        if (c=='A') return 0;
        else if (c=='C') return 1;
        else if (c=='G') return 2;
        else if (c=='T') return 3;
        return -1;
    }
    
    
    void wrapup3(void)
    {
       int i,j,k; 
    
       for (i=0;i<4;i++)
       for (j=0;j<4;j++)
       for (k=0;k<4;k++)
       {
           if (i==0) {printf("A");} else if (i==1) {printf("C");} else if (i==2) {printf("G");}  else if (i==3) {printf("T"); }
           if (j==0) {printf("A");} else if (j==1) {printf("C");} else if (j==2) {printf("G");} else if (j==3) {printf("T"); }
           if (k==0) {printf("A");} else if (k==1) {printf("C");} else if (k==2) {printf("G");} else if (k==3) {printf("T"); }
           printf(" %ld\n",x3[i][j][k]);
       }
    }
    
    int is[4];
    
    static int kmer(void)
    {
       int on_head = 0;
       int c;
       long int cnt = (long int)0;
    
        while (1)
        {
             c = getchar();
             cnt++;
             if (c==-1) break;
             if (c=='>') {on_head=1; continue; }
             if ((c=='\r')|| (c=='\n')) {on_head=0; continue; }
             if (on_head == 1) continue;
             is[0] = is[1];
             is[1] = is[2];
             is[2] = acgt_index(toupper(c));
             if (is[0] == -1) continue;
             if (is[1] == -1) continue;
             if (is[2] == -1) continue;
             x3[is[0]][is[1]][is[2]]++;
       }
       return 0;
    }
    
    int main(int argc, char *argv[])
    {
        kmer();
        wrapup3();
        return 0;
    }
    Last edited by Richard Finney; 06-27-2015, 11:06 AM.

    Comment

    • thom_otis
      Junior Member
      • Jun 2015
      • 2

      #3
      How to run this script?
      Originally posted by Richard Finney View Post
      example usage: cat hg20/chr1.fa | ./kmer3

      Code:
      -bash-4.1$ cat kmer3.c
      
      /*
      vi kmer3.c ; gcc -Wall -O3  kmer3.c -o kmer3  -lm 
      */
      
      #include <stdio.h>
      #include <string.h>
      #include <stdio.h>
      #include <ctype.h>
      #include <errno.h>
      #include <assert.h>
      
      long int x3[4][4][4];
      
      int acgt_index(char c)
      {
          if (c=='A') return 0;
          else if (c=='C') return 1;
          else if (c=='G') return 2;
          else if (c=='T') return 3;
          return -1;
      }
      
      
      void wrapup3(void)
      {
         int i,j,k; 
      
         for (i=0;i<4;i++)
         for (j=0;j<4;j++)
         for (k=0;k<4;k++)
         {
             if (i==0) {printf("A");} else if (i==1) {printf("C");} else if (i==2) {printf("G");}  else if (i==3) {printf("T"); }
             if (j==0) {printf("A");} else if (j==1) {printf("C");} else if (j==2) {printf("G");} else if (j==3) {printf("T"); }
             if (k==0) {printf("A");} else if (k==1) {printf("C");} else if (k==2) {printf("G");} else if (k==3) {printf("T"); }
             printf(" %ld\n",x3[i][j][k]);
         }
      }
      
      int is[4];
      
      static int kmer(void)
      {
         int on_head = 0;
         int c;
         long int cnt = (long int)0;
      
          while (1)
          {
               c = getchar();
               cnt++;
               if (c==-1) break;
               if (c=='>') {on_head=1; continue; }
               if ((c=='\r')|| (c=='\n')) {on_head=0; continue; }
               if (on_head == 1) continue;
               is[0] = is[1];
               is[1] = is[2];
               is[2] = acgt_index(toupper(c));
               if (is[0] == -1) continue;
               if (is[1] == -1) continue;
               if (is[2] == -1) continue;
               x3[is[0]][is[1]][is[2]]++;
         }
         return 0;
      }
      
      int main(int argc, char *argv[])
      {
          kmer();
          wrapup3();
          return 0;
      }

      Comment

      • Richard Finney
        Senior Member
        • Feb 2009
        • 701

        #4
        Create source code using text editor, past in code.
        Name the file kmer3.c

        Compile with
        gcc -Wall -O3 kmer3.c -o kmer3 -lm

        Program takes standard input.
        Example ...
        cat hg20/chr1.fa | ./kmer3

        fastas only unless you hack it.

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
          by SEQadmin2


          Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


          The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
          ...
          06-02-2026, 10:05 AM
        • SEQadmin2
          Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
          by SEQadmin2


          With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


          Introduction

          Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
          05-22-2026, 06:42 AM
        • SEQadmin2
          Environmental Genomics in the Age of NGS: From Microbes to Conservation Strategies
          by SEQadmin2

          Studying ecosystems means dealing with complex, multi-species communities that are hard to observe at scale. This complexity, however, hides many important questions to be answered, from how biogeochemical cycles work and how climate change can affect species distribution to how conservation strategies can work best.


          Genomics, particularly since the expansion of NGS, has transformed ecosystem ecology. By sequencing environmental DNA, we can now assess biodiversity without direct...
          05-06-2026, 09:04 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, Today, 08:59 AM
        0 responses
        10 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-02-2026, 12:03 PM
        0 responses
        21 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-02-2026, 11:40 AM
        0 responses
        17 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 05-28-2026, 11:40 AM
        0 responses
        31 views
        0 reactions
        Last Post SEQadmin2  
        Working...