#include #include #include #define NO_FILE_ERR NULL #define NO_DATA_ERR NULL #ifndef DEBUG #undef DEBUG #define DEBUG #define SEVERITY 2 #endif char *loadSequenceN(const char *alphabet); int printSequenceV(const char *sequence, int from, const int to); int sequenceHistogramH(const char *sequence); int subSequenceFinderP(); char *insertSequenceL(const int pos, const int length, const char *newSequence, const char *sequence); int longestSubstrInSequenceO(); char *substring(char *string, int position, int length); int contains(const char needle, const char *haystack); int isLegit(const char *sequence, const char *alphabet, const int size); int main() { //FILE *f = fopen("DNAsekvencia.txt", "r"); char *sequence = NULL; char newSequence[1000]; char *alphabet = "ACGTacgt"; char selection = NULL; int from = 0, to = 0; /*if (f == NULL) { printf("Neotvoreny subor\n"); return NO_FILE_ERR; }*/ while (selection != 'k') { selection = getchar(); if (selection == 'n') { sequence = loadSequenceN(alphabet); #ifdef DEBUG #if SEVERITY >= 1 printf("loadSequenceN() return value on line %d: %s\n", __LINE__, sequence); #endif #endif } else if (selection == 'v') { scanf("%d %d", &from, &to); printSequenceV(sequence, from, to); getchar(); } else if (selection == 'h') { sequenceHistogramH(sequence); } else if (selection == 'p') { subSequenceFinderP(); } else if (selection == 'l') { scanf("%d %d %s", &from, &to, newSequence); sequence = insertSequenceL(from, to, newSequence, sequence); } else if (selection == 'o') { longestSubstrInSequenceO(sequence); } } return 0; } char *loadSequenceN(const char *alphabet) { FILE *f = fopen("DNAsekvencia.txt", "r"); //TODO finish if there is stuff changed...or maybe not, 'cause it works like charm :D int index = 0; int size = 0; char c; char *sequence = NULL; rewind(f); if (f == NO_FILE_ERR) { printf("Neotvoreny subor\n"); return NO_FILE_ERR; } //file length checking fseek( f , 0L , SEEK_END); size = ftell( f ); rewind( f ); //allocating the string sequence = (char *)malloc((size + 1) * sizeof(char)); fgets (sequence, size+1, f); fclose(f); #ifdef DEBUG #if SEVERITY >= 1 printf("loadSequenceN() on line %d: %s\n", __LINE__, sequence); #endif #endif if(isLegit(sequence, alphabet, size)) { printf("Sekvenciu sa podarilo nacitat\n"); return sequence; } return NULL; } int printSequenceV(const char *sequence, int from, const int to) { if(sequence == NO_DATA_ERR) { printf("Sekvencia nie je nacitana\n"); return NO_DATA_ERR; } if(from < 1 || to < 1 || from > to) { printf("Zadanu podsekvenciu nie je mozne vypisat\n"); return 1; } for(from; from <= to; from++) { printf("%c", sequence[from - 1]); } printf("\n"); return 0; } int sequenceHistogramH(const char *sequence) { int aCount = 0; int cCount = 0; int gCount = 0; int tCount = 0; int i = 0; for(i; i < strlen(sequence); i++) { if(sequence[i] == 'A' || sequence[i] == 'a') { aCount++; #ifdef DEBUG #if SEVERITY >= 2 printf("From sequenceHistogramH(), var aCount on line %d: %d\n", __LINE__, aCount); #endif #endif continue; } if(sequence[i] == 'C' || sequence[i] == 'c') { cCount++; #ifdef DEBUG #if SEVERITY >= 2 printf("From sequenceHistogramH(), var cCount on line %d: %d\n", __LINE__, cCount); #endif #endif continue; } if(sequence[i] == 'G' || sequence[i] == 'g') { gCount++; #ifdef DEBUG #if SEVERITY >= 2 printf("From sequenceHistogramH(), var gCount on line %d: %d\n", __LINE__, gCount); #endif #endif continue; } if(sequence[i] == 'T' || sequence[i] == 't') { tCount++; #ifdef DEBUG #if SEVERITY >= 2 printf("From sequenceHistogramH(), var tCount on line %d: %d\n", __LINE__, tCount); #endif #endif continue; } } printf("%c: %d\n%c: %d\n%c: %d\n%c: %d\n", 'A', aCount, 'C', cCount, 'G', gCount, 'T', tCount); return 0; } int subSequenceFinderP() { printf("subSequence\n"); return 0; } char *insertSequenceL(int pos, const int length, const char *newSequence, const char *sequence) { char *placeholder = NULL; int size = (strlen(newSequence) + strlen(sequence) + 2); if(sequence == NO_DATA_ERR) { printf("Sekvencia nie je nacitana\n"); return sequence; } if(pos > strlen(sequence) + 1 || pos < 1) { printf("Do sekvencie nie je mozne podsekvenciu vlozit\n"); return sequence; } placeholder = (char *)malloc(size * sizeof(char)); memset(placeholder, 0, size); pos--; // TODO complete this strncpy(placeholder, sequence, pos); int len = strlen(placeholder); strcpy(placeholder+len, newSequence); len += strlen(newSequence); strcpy(placeholder+len, sequence+pos); printf("Podsekvencia vlozena\n"); return placeholder; } int longestSubstrInSequenceO(const char *str) { int i = 0; char *cntrlStr = (char *)malloc((strlen(str)) * sizeof(char)); char *copied = (char *)malloc((strlen(str)) * sizeof(char)); int size; strcpy(copied, str); for ( ; *copied; ++copied) { *copied = tolower(*copied); } for(i; i < strlen(str); i++) { printf("%d\n", (strlen(str) - i + 1)); memset(cntrlStr, 'a', (strlen(str) - i ) * sizeof(char)); printf("%s\n", cntrlStr); if(strstr(copied, cntrlStr) != NULL) { printf("Longest sequence: %d\n", i); break; } memset(cntrlStr, 'c', (strlen(str) - i) * sizeof(char)); printf("%s\n", cntrlStr); if(strstr(copied, cntrlStr) != NULL) { printf("Longest sequence: %d\n", i); break; } memset(cntrlStr, 'g', (strlen(str) - i) * sizeof(char)); printf("%s\n", cntrlStr); if(strstr(copied, cntrlStr) != NULL) { printf("Longest sequence: %d\n", i); break; } memset(cntrlStr, 't', (strlen(str) - i) * sizeof(char)); printf("%s\n", cntrlStr); if(strstr(copied, cntrlStr) != NULL) { printf("Longest sequence: %d\n", i); break; } memset(cntrlStr, 0, (strlen(str) - i) * sizeof(char)); printf("%s\n", cntrlStr); free(cntrlStr); cntrlStr = NULL; cntrlStr = (char *)malloc((strlen(str) - i ) * sizeof(char)); //memset(cntrlStr, 0, (strlen(str) - i ) * sizeof(char)); } printf("search end\n"); //strstr(); return 0; } char *substring(char *string, int position, int length) { char *pointer; int c; pointer = (char *)malloc((length+1) * sizeof(char)); if( pointer == NULL ) exit(EXIT_FAILURE); for( c = 0 ; c < length ; c++ ) *(pointer+c) = *((string+position-1)+c); *(pointer+c) = '\0'; return pointer; } int contains(const char needle, const char *haystack) { char check = NULL; check = strchr(haystack, (int)needle); #ifdef DEBUG #if SEVERITY >= 3 printf("From contains() on line %d: %c\n", __LINE__, check); #endif #endif if(check == NULL) { return 0; } else { return 1; } } int isLegit(const char *sequence, const char *alphabet, const int size) { int index = 0; for(index; index < size; index++) { if(contains(sequence[index], alphabet)) { #ifdef DEBUG #if SEVERITY >= 2 printf("From isLegit() on line %d: %c\n", __LINE__, sequence[index]); #endif #endif continue; } else { printf("Sekvencia nesplna podmienky\n"); return 0; } } return 1; }