View Single Post
Old 09-21-2017, 07:36 AM   #8
Richard Finney
Senior Member
 
Location: bethesda

Join Date: Feb 2009
Posts: 700
Default

/*
how to compile: gcc -Wall -O2 -o longestseq longestseq.c
example: ./longestseq example.fa

*/

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

char s[100000]; // input buffer
int main(int argc, char *argv[])
{
FILE *fp;
int i;
size_t cur_spot;
size_t last_spot;
size_t best_spot;
size_t cursize;
size_t bestsofarsize;

if (argc != 2) { fprintf(stderr,"Error: need inputfilefasta. This program finds first longest fasta sequence(just one, no same length "ties"). Usage: ./longestseq example.fa\n"); exit(0); }
fp = fopen(argv[1],"r");
best_spot = cur_spot = last_spot = ftell(fp);
cursize = bestsofarsize = 0;
while (fgets(s,99999,fp))
{
for (i=0;s[i];i++) { if ((s[i]=='\r')||(s[i]=='\n')) {s[i] = (char)0;break;}} //handle dos or unix
if (s[0]=='>')
{
last_spot = cur_spot;
cursize = 0;
continue;
}
cursize += strlen(s);
if (cursize > bestsofarsize)
{
bestsofarsize = cursize;
best_spot = last_spot;
}
cur_spot = ftell(fp);
}

fseek(fp,best_spot,SEEK_SET);
fgets(s,99999,fp);
printf("%s",s);
while (fgets(s,99999,fp))
{
if (s[0] == '>') break;
printf("%s",s);
}
fclose(fp);
return 0;
}


[FONT="Courier New"][SIZE="1"]____
Richard Finney is offline   Reply With Quote