Advertisement
BlueBear

dnaSequence.c

Mar 29th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define NO_FILE_ERR NULL
  5. #define NO_DATA_ERR NULL
  6.  
  7. #ifndef DEBUG
  8.     #undef DEBUG
  9.     #define DEBUG
  10.     #define SEVERITY 2
  11. #endif
  12. char *loadSequenceN(const char *alphabet);
  13. int printSequenceV(const char *sequence, int from, const int to);
  14. int sequenceHistogramH(const char *sequence);
  15. int subSequenceFinderP();
  16. char *insertSequenceL(const int pos, const int length, const char *newSequence, const char *sequence);
  17. int longestSubstrInSequenceO();
  18. char *substring(char *string, int position, int length);
  19. int contains(const char needle, const char *haystack);
  20. int isLegit(const char *sequence, const char *alphabet, const int size);
  21.  
  22. int main()
  23. {
  24.     //FILE *f = fopen("DNAsekvencia.txt", "r");
  25.     char *sequence = NULL;
  26.     char newSequence[1000];
  27.     char *alphabet = "ACGTacgt";
  28.     char selection = NULL;
  29.     int from = 0, to = 0;
  30.     /*if (f == NULL)
  31.     {
  32.         printf("Neotvoreny subor\n");
  33.         return NO_FILE_ERR;
  34.     }*/
  35.     while (selection != 'k')
  36.     {
  37.         selection = getchar();
  38.         if (selection == 'n')
  39.         {
  40.             sequence = loadSequenceN(alphabet);
  41.     #ifdef DEBUG
  42.         #if SEVERITY >= 1
  43.             printf("loadSequenceN() return value on line %d: %s\n", __LINE__, sequence);
  44.         #endif
  45.     #endif
  46.         }
  47.         else if (selection == 'v')
  48.         {
  49.             scanf("%d %d", &from, &to);
  50.             printSequenceV(sequence, from, to);
  51.             getchar();
  52.         }
  53.         else if (selection == 'h')
  54.         {
  55.             sequenceHistogramH(sequence);
  56.         }
  57.         else if (selection == 'p')
  58.         {
  59.             subSequenceFinderP();
  60.         }
  61.         else if (selection == 'l')
  62.         {
  63.             scanf("%d %d %s", &from, &to, newSequence);
  64.             sequence = insertSequenceL(from, to, newSequence, sequence);
  65.         }
  66.         else if (selection == 'o')
  67.         {
  68.             longestSubstrInSequenceO(sequence);
  69.         }
  70.     }
  71.  
  72.     return 0;
  73. }
  74.  
  75. char *loadSequenceN(const char *alphabet)
  76. {
  77.     FILE *f = fopen("DNAsekvencia.txt", "r");
  78.     //TODO finish if there is stuff changed...or maybe not, 'cause it works like charm :D
  79.     int index = 0;
  80.     int size = 0;
  81.     char c;
  82.     char *sequence = NULL;
  83.  
  84.     rewind(f);
  85.  
  86.     if (f == NO_FILE_ERR)
  87.     {
  88.         printf("Neotvoreny subor\n");
  89.         return NO_FILE_ERR;
  90.     }
  91.  
  92.     //file length checking
  93.     fseek( f , 0L , SEEK_END);
  94.     size = ftell( f );
  95.     rewind( f );
  96.  
  97.     //allocating the string
  98.     sequence = (char *)malloc((size + 1) * sizeof(char));
  99.     fgets (sequence, size+1, f);
  100.  
  101.     fclose(f);
  102.     #ifdef DEBUG
  103.         #if SEVERITY >= 1
  104.             printf("loadSequenceN() on line %d: %s\n", __LINE__, sequence);
  105.         #endif
  106.     #endif
  107.     if(isLegit(sequence, alphabet, size))
  108.     {
  109.         printf("Sekvenciu sa podarilo nacitat\n");
  110.         return sequence;
  111.     }
  112.     return NULL;
  113. }
  114.  
  115. int printSequenceV(const char *sequence, int from, const int to)
  116. {
  117.     if(sequence == NO_DATA_ERR)
  118.     {
  119.         printf("Sekvencia nie je nacitana\n");
  120.         return NO_DATA_ERR;
  121.     }
  122.     if(from < 1 || to < 1 || from > to)
  123.     {
  124.         printf("Zadanu podsekvenciu nie je mozne vypisat\n");
  125.         return 1;
  126.     }
  127.     for(from; from <= to; from++)
  128.     {
  129.         printf("%c", sequence[from - 1]);
  130.     }
  131.     printf("\n");
  132.     return 0;
  133. }
  134.  
  135. int sequenceHistogramH(const char *sequence)
  136. {
  137.     int aCount = 0;
  138.     int cCount = 0;
  139.     int gCount = 0;
  140.     int tCount = 0;
  141.     int i = 0;
  142.     for(i; i < strlen(sequence); i++)
  143.     {
  144.         if(sequence[i] == 'A' || sequence[i] == 'a')
  145.         {
  146.             aCount++;
  147.     #ifdef DEBUG
  148.         #if SEVERITY >= 2
  149.             printf("From sequenceHistogramH(), var aCount on line %d: %d\n", __LINE__, aCount);
  150.         #endif
  151.     #endif
  152.             continue;
  153.         }
  154.         if(sequence[i] == 'C' || sequence[i] == 'c')
  155.         {
  156.             cCount++;
  157.     #ifdef DEBUG
  158.         #if SEVERITY >= 2
  159.             printf("From sequenceHistogramH(), var cCount on line %d: %d\n", __LINE__, cCount);
  160.         #endif
  161.     #endif
  162.             continue;
  163.         }
  164.         if(sequence[i] == 'G' || sequence[i] == 'g')
  165.         {
  166.             gCount++;
  167.     #ifdef DEBUG
  168.         #if SEVERITY >= 2
  169.             printf("From sequenceHistogramH(), var gCount on line %d: %d\n", __LINE__, gCount);
  170.         #endif
  171.     #endif
  172.             continue;
  173.         }
  174.         if(sequence[i] == 'T' || sequence[i] == 't')
  175.         {
  176.             tCount++;
  177.     #ifdef DEBUG
  178.         #if SEVERITY >= 2
  179.             printf("From sequenceHistogramH(), var tCount on line %d: %d\n", __LINE__, tCount);
  180.         #endif
  181.     #endif
  182.             continue;
  183.         }
  184.     }
  185.  
  186.     printf("%c: %d\n%c: %d\n%c: %d\n%c: %d\n", 'A', aCount, 'C', cCount, 'G', gCount, 'T', tCount);
  187.     return 0;
  188. }
  189.  
  190. int subSequenceFinderP()
  191. {
  192.     printf("subSequence\n");
  193.     return 0;
  194. }
  195.  
  196. char *insertSequenceL(int pos, const int length, const char *newSequence, const char *sequence)
  197. {
  198.     char *placeholder = NULL;
  199.     int size = (strlen(newSequence) + strlen(sequence) + 2);
  200.  
  201.     if(sequence == NO_DATA_ERR)
  202.     {
  203.         printf("Sekvencia nie je nacitana\n");
  204.         return sequence;
  205.     }
  206.  
  207.     if(pos > strlen(sequence) + 1 || pos < 1)
  208.     {
  209.         printf("Do sekvencie nie je mozne podsekvenciu vlozit\n");
  210.         return sequence;
  211.     }
  212.  
  213.     placeholder = (char *)malloc(size * sizeof(char));
  214.     memset(placeholder, 0, size);
  215.  
  216.     pos--;
  217.     // TODO complete this
  218.     strncpy(placeholder, sequence, pos);
  219.     int len = strlen(placeholder);
  220.     strcpy(placeholder+len, newSequence);
  221.     len += strlen(newSequence);
  222.     strcpy(placeholder+len, sequence+pos);
  223.     printf("Podsekvencia vlozena\n");
  224.     return placeholder;
  225.  
  226. }
  227.  
  228. int longestSubstrInSequenceO(const char *str)
  229. {
  230.     int i = 0;
  231.     char *cntrlStr = (char *)malloc((strlen(str)) * sizeof(char));
  232.     char *copied  = (char *)malloc((strlen(str)) * sizeof(char));
  233.     int size;
  234.     strcpy(copied, str);
  235.  
  236.     for ( ; *copied; ++copied)
  237.     {
  238.         *copied = tolower(*copied);
  239.     }
  240.  
  241.     for(i; i < strlen(str); i++)
  242.     {
  243.         printf("%d\n", (strlen(str) - i + 1));
  244.  
  245.         memset(cntrlStr, 'a', (strlen(str) - i ) * sizeof(char));
  246.         printf("%s\n", cntrlStr);
  247.         if(strstr(copied, cntrlStr) != NULL)
  248.         {
  249.             printf("Longest sequence: %d\n", i);
  250.             break;
  251.         }
  252.         memset(cntrlStr, 'c', (strlen(str) - i) * sizeof(char));
  253.         printf("%s\n", cntrlStr);
  254.         if(strstr(copied, cntrlStr) != NULL)
  255.         {
  256.             printf("Longest sequence: %d\n", i);
  257.             break;
  258.         }
  259.         memset(cntrlStr, 'g', (strlen(str) - i) * sizeof(char));
  260.         printf("%s\n", cntrlStr);
  261.         if(strstr(copied, cntrlStr) != NULL)
  262.         {
  263.             printf("Longest sequence: %d\n", i);
  264.             break;
  265.         }
  266.         memset(cntrlStr, 't', (strlen(str) - i) * sizeof(char));
  267.         printf("%s\n", cntrlStr);
  268.         if(strstr(copied, cntrlStr) != NULL)
  269.         {
  270.             printf("Longest sequence: %d\n", i);
  271.             break;
  272.         }
  273.         memset(cntrlStr, 0, (strlen(str) - i) * sizeof(char));
  274.         printf("%s\n", cntrlStr);
  275.  
  276.         free(cntrlStr);
  277.         cntrlStr = NULL;
  278.         cntrlStr = (char *)malloc((strlen(str) - i ) * sizeof(char));
  279.         //memset(cntrlStr, 0, (strlen(str) - i ) * sizeof(char));
  280.  
  281.     }
  282.     printf("search end\n");
  283.     //strstr();
  284.     return 0;
  285. }
  286.  
  287. char *substring(char *string, int position, int length)
  288. {
  289.    char *pointer;
  290.    int c;
  291.  
  292.    pointer = (char *)malloc((length+1) * sizeof(char));
  293.    if( pointer == NULL )
  294.        exit(EXIT_FAILURE);
  295.  
  296.    for( c = 0 ; c < length ; c++ )
  297.       *(pointer+c) = *((string+position-1)+c);
  298.  
  299.    *(pointer+c) = '\0';
  300.  
  301.    return pointer;
  302. }
  303.  
  304. int contains(const char needle, const char *haystack)
  305. {
  306.     char check = NULL;
  307.     check = strchr(haystack, (int)needle);
  308.     #ifdef DEBUG
  309.         #if SEVERITY >= 3
  310.             printf("From contains() on line %d: %c\n", __LINE__, check);
  311.         #endif
  312.     #endif
  313.     if(check == NULL)
  314.     {
  315.         return 0;
  316.     }
  317.     else
  318.     {
  319.         return 1;
  320.     }
  321. }
  322.  
  323. int isLegit(const char *sequence, const char *alphabet, const int size)
  324. {
  325.     int index = 0;
  326.     for(index; index < size; index++)
  327.     {
  328.         if(contains(sequence[index], alphabet))
  329.         {
  330.     #ifdef DEBUG
  331.         #if SEVERITY >= 2
  332.             printf("From isLegit() on line %d: %c\n", __LINE__, sequence[index]);
  333.         #endif
  334.     #endif
  335.             continue;
  336.         }
  337.         else
  338.         {
  339.             printf("Sekvencia nesplna podmienky\n");
  340.             return 0;
  341.         }
  342.     }
  343.     return 1;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement