diff options
author | Calvin Morrison <mutantturkey@gmail.com> | 2014-03-21 13:09:20 -0400 |
---|---|---|
committer | Calvin Morrison <mutantturkey@gmail.com> | 2014-03-21 13:09:20 -0400 |
commit | fa141b5216ac2ab8959cb1a6751e73e6596c6b92 (patch) | |
tree | c6e8cb1f55960bd1388d06cd78c4302d783380bc | |
parent | 05b0385f094fdd4db4f8fd9c5fdd2fef037cb637 (diff) |
add sequence length command
-rw-r--r-- | src/sequence_length.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/sequence_length.c b/src/sequence_length.c new file mode 100644 index 0000000..b31aac3 --- /dev/null +++ b/src/sequence_length.c @@ -0,0 +1,55 @@ +// Copyright 2013 Calvin Morrison +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <stdbool.h> +#include <errno.h> +int main() { + + ssize_t len = 0; + + char buffer[4096]; + bool header = false; + + len = fread(&buffer, 1, 1, stdin); + + unsigned long long seq_length = 0; + if(!errno) { + if(buffer[0] == '>') { + header = true; + + while((len = fread(&buffer, 1, 4096, stdin)) != 0) { + size_t i = 0; + for(i = 0; i < len; i++) { + if(buffer[i] == '>') { + printf("%llu\n", seq_length); + seq_length = 0; + header = true; + continue; + } + else if(buffer[i] == '\n' && header == true) { + header = false; + continue; + } + if(header == false && buffer[i] != '\n') { + seq_length++; + } + } + } + } + else { + fprintf(stderr, "this does not look like a fasta file\n"); + return EXIT_FAILURE; + } + } + else { + fprintf(stderr, "could not read file\n"); + return EXIT_FAILURE; + } + + printf("%llu\n", seq_length); + + return EXIT_SUCCESS; +} + |