input files in C

Viewed 41

Code implements the dynamic programming solution for global pairwise alignment of two sequences. Trying to perform a semi-global alignment between the SARS-CoV-2 reference genome and the first read in the Nanopore sample. The length of the reference genome is 29903 base pairs and the length of the first Nanopore read is 1246 base pairs. When I run the following code, I get this message in my terminal:

Usage: align < input file >

How do I add the necessary files to the code. The file names are SARS-CoV-2 reference genome.txt and Nanopore.txt, where A = SARS-CoV-2 reference genome.txt file and B = Nanopore.txt file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define GAP -2
#define MATCH 5
#define MISMATCH -3

#define MAXLENGTH_A 29904
#define MAXLENGTH_B 1247

int max(int A, int B, int C)
{
    if (A>=B && A>=C) return A;
    else if (B>=A && B>=C) return B;
    else return C;
}

char Tmax(int A, int B, int C)
{
    if (A>B && A>C) return 'D';
    else if (B>A && B>C) return 'L';
    else return 'U';
}

int m(char p, char q)
{
    if (p==q) return MATCH;
    else return MISMATCH;
}

void append(char *st,int L,char c)
{
     int i;
     for (i=L;i>0;i--)
         st[i]=st[i-1];
     st[L+1] = '\0';
     st[0] = c;
}

int main(int argc, char **argv)
{
    FILE *fp;
    
    char A[1000];
    char B[1000];
    char RA[1000];
    char RM[1000];
    char RB[1000];
    int N,M,L;
    
    int i,j;
    
    //int S[MAXLENGTH_A][MAXLENGTH_B];
    //char T[MAXLENGTH_A][MAXLENGTH_B];
    int **S;
    char **T;
    S = (int**)malloc(sizeof(int*)*MAXLENGTH_A);
    for (int i = 0; i<29904; i++)
        S[i] = (int*)malloc(sizeof(int)*MAXLENGTH_B);
    T = (char**)malloc(sizeof(char*)*MAXLENGTH_A);
    for (int i = 0; i<29904; i++)
        T[i] = (char*)malloc(sizeof(char)*MAXLENGTH_B);
    
    if (argc!=2)
    {
                printf("Usage: align <input file>\n");
                exit(1);
    }
    
    fp = fopen(argv[2],"r");
    
    if (fp==NULL)
    {
                  printf("input file not found.\n");
                  exit(1);
    }
    fscanf(fp,"%s",A);
    fscanf(fp,"%s",B);
    
    printf("Sequence A: %s\n",A);
    printf("Sequence B: %s\n",B);
    
    N = strlen(A);
    M = strlen(B);
    S[0][0] = 0;
    T[0][0] = 'D';
    
    // initialize first column
    for (i=0;i<=N;i++)
    {
        S[i][0] = GAP*i;
        T[i][0] = 'U';
    }
    
    //initialize the firt row
    for (i=0;i<=M;i++)
    {
        S[0][i] = GAP*i;
        T[0][i] = 'L';
    }
    
    for (i=1;i<=N;i++)
        for (j=1;j<=M;j++)
        {
            S[i][j] = max(S[i-1][j-1]+m(A[i-1],B[j-1]),S[i][j-1]+GAP,S[i-1][j]+GAP);
            T[i][j] = Tmax(S[i-1][j-1]+m(A[i-1],B[j-1]),S[i][j-1]+GAP,S[i-1][j]+GAP);
        }
    
    printf("The score of the alignment is : %d\n",S[N][M]);
    
    i=N;
    j=M;
    L=0;
    RA[0]='\0';
    RB[0]='\0';
    RM[0]='\0';
    
    while (i!=0 || j!=0)
    {
          if (T[i][j]=='D')
          {
             append(RA,L,A[i-1]);
             append(RB,L,B[j-1]);
             if (A[i-1]==B[j-1]) append(RM,L,'|');
             else append(RM,L,'*');
             i--; j--;
          }
          else if (T[i][j]=='L')
          {
             append(RA,L,'-');
             append(RB,L,B[j-1]);
             append(RM,L,' ');
             j--;
          }
          else if (T[i][j]=='U')
          {
             append(RA,L,A[i-1]);
             append(RB,L,'-');
             append(RM,L,' ');
             i--;
          }
          
          L++;
          
    }
    
    printf("%s\n",RA);
    printf("%s\n",RM);
    printf("%s\n",RB);
}
1 Answers

These lines of the program

printf("Usage: align <input file>\n");

/* AND */

fp = fopen(argv[2],"r");

/* AND */
    
fscanf(fp,"%s",A);
fscanf(fp,"%s",B);

show the program expects to read from one file.

From your question, it seems you could make a third data file with one line containing the 29903 characters without spaces or breaks of the first sequence followed by a second line containing 1246 characters (again continuous characters) of the second sequence. But, don't do this...

When the program is run (eg: ./a.out filename) and one supplies the name of the 'combined' file, the first fscanf() will attempt to load the first block of characters into the array named 'A'. The second fscanf() would load the second block into the array named 'B'.

This is a problem because both 'A' and 'B' are dimensioned to hold only 1000 bytes each, maximum...

As this code is right now, you cannot use it to load the long sequences you want to. Sorry.

(It looks like this was written by a summer student, and then partially modified by someone who knew even less about coding (and maybe even the purpose of the program.))

SO is not a code writing service... You will need to find other avenues of support.

Related