Unconfigured Ad

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

    #1

    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
          Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
          by SEQadmin2



          CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

          Despite this, “CRISPR helped turn genome editing from a specialized technique into
          ...
          07-31-2026, 11:01 AM
        • SEQadmin2
          Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
          by SEQadmin2


          Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

          The systematic characterization of the human proteome has
          ...
          07-20-2026, 11:48 AM
        • SEQadmin2
          Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
          by SEQadmin2



          Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
          ...
          07-09-2026, 11:10 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, 08-03-2026, 10:13 AM
        0 responses
        21 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 07-31-2026, 02:55 AM
        0 responses
        34 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 07-24-2026, 12:17 PM
        0 responses
        25 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 07-23-2026, 11:41 AM
        0 responses
        21 views
        0 reactions
        Last Post SEQadmin2  
        Working...