fasta_header_handler.c 2.75 KB
Newer Older
Celine Mercier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sequence.h"
#include "fasta_header_parser.h"
#include "fasta_header_handler.h"


char* char_header_add_field(char* header, char* name, char* value)
{
	int lheader = strlen(header);
	header = (char*) realloc(header, (lheader+strlen(name)+strlen(value)+4)*sizeof(char));
	if (header[lheader-1] == '.')
	{
		strcpy(header+lheader-1,";");
		strcpy(header+lheader," ");
		strcpy(header+lheader+1,name);
		strcpy(header+lheader+1+strlen(name),"=");
		strcpy(header+lheader+1+strlen(name)+1,value);
	}
	else
	{
		strcpy(header+lheader,";");
		strcpy(header+lheader+1," ");
		strcpy(header+lheader+2,name);
		strcpy(header+lheader+2+strlen(name),"=");
		strcpy(header+lheader+2+strlen(name)+1,value);
	}
	return header;
}


char* fastaSeqPtr_header_add_field(fastaSeqPtr seq, char* name, char* value)
{
	int lheader = strlen(seq->rawheader);
	int i;
	char* buffer;
	char* rawheader;

	rawheader = (char*) malloc((lheader+strlen(name)+strlen(value)+5)*sizeof(char));
	strcpy(rawheader, seq->rawheader);

	buffer = calloc(lheader, sizeof(char));

	i=0;

	while ((rawheader[i] != ' ') && (rawheader[i] != 0))
		i++;

	if (rawheader[i] == ' ')
		strcpy(buffer, rawheader+i);
	else
		strcpy(rawheader+i, " ");

	i++;

	strcpy(rawheader+i,name);
	strcpy(rawheader+i+strlen(name),"=");
	strcpy(rawheader+i+strlen(name)+1,value);
	strcpy(rawheader+i+strlen(name)+1+strlen(value),";");
	strcpy(rawheader+i+strlen(name)+1+strlen(value)+1, buffer);

	free(buffer);

	return(rawheader);
}


element_from_header* table_header_add_field(element_from_header* header, char* name, char* value)
{
	int nbf;
	nbf = atoi(header[0].value);
73 74 75 76 77 78 79 80
	nbf++;
	header = (element_from_header*) realloc(header, (nbf+1)*sizeof(element_from_header));
	header[nbf].name = (char*) malloc((1+strlen(name))*sizeof(char));
	strcpy(header[nbf].name, name);
	header[nbf].value = (char*) malloc((1+strlen(value))*sizeof(char));
	strcpy(header[nbf].value, value);
	sprintf(header[0].value, "%d", nbf);
	return(header);
Celine Mercier committed
81 82 83 84 85 86 87 88
}


void free_header_table(element_from_header* header)
{
	int i;
	int nbf = atoi(header[0].value);

89
	for (i = 0; i <= nbf; i++)
Celine Mercier committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103
	{
		free((header[i]).name);
	    free((header[i]).value);
	}
	free(header);
}


char* getItemFromHeader(char* name, element_from_header* header)
{
	char* value = 0;
	int nbf;
	int i;
	nbf = atoi(header[0].value);
104
	for (i = 1; i <= nbf; i++)
Celine Mercier committed
105 106 107 108 109 110 111 112 113 114 115 116 117
	{
		if (strcmp(header[i].name,name)==0)
			value = header[i].value;
	}
	return value;
}


void changeValue(element_from_header* header, char* name, char* newValue)
{
	int i;
	int nbf = atoi(header[0].value);

118
	for (i = 1; i <= nbf; i++)
Celine Mercier committed
119 120 121 122 123 124 125 126
	{
		if (strcmp(header[i].name, name)==0)
		{
			header[i].value = realloc(header[i].value, (1+strlen(newValue))*sizeof(char));
			strcpy(header[i].value, newValue);
		}
	}
}