-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdna_file.c
165 lines (158 loc) · 4.63 KB
/
dna_file.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* dna_file.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "prim.h"
#include "dna.h"
#define DB_DNA_IO if(DB[105])
/***************************************************************************
* Figure out sequence type given options and input name
* Return format code or false if error
*/
int FigureSeqFileTypeI(int iraw, int iseq, int ifas, char *fnameS, int error)
{
int form;
if(iraw) {
form = SEQFM_RAW;
}
else if(ifas) {
form = SEQFM_FASTA;
}
else if(iseq) {
form = SEQFM_SEQ;
}
else {
if( STDIN_STR(fnameS) ) {
form = SEQFM_RAW;
}
else {
form = GuessSeqFileTypeI(fnameS, error);
}
}
if(!OkSeqInFormatI(form,fnameS,error)) {
return(FALSE);
}
return(form);
}
/***************************************************************************
* Guess a file's type based on its extension
* fnameS is the file name
*/
int GuessSeqFileTypeI(char *fnameS, int error)
{
int type;
char extS[DEF_BS];
DB_DNA_IO DB_PrI(">> GuessSeqFileTypeI fname=|%s|\n",fnameS);
INIT_S(extS);
GetFilePartsI(fnameS,NULL,NULL,extS);
Upperize(extS);
type = ParseSeqTypeI(extS, TRUE);
if(IS_BOG(type) && error) {
PROBLINE;
printf("Unrecognized sequence file format extension\n");
printf(" File: %s\n",fnameS);
}
DB_DNA_IO DB_PrI("<< GuessSeqFileTypeI %d\n",type);
return(type);
}
/***************************************************************************
* Parse file extension and return associated filetype if recognized
* If exact then full correct extension is needed, else just first
* unambiguous characters of the extension
*
* Returns BOGUS for unknown / incomplete file extensions
*/
int ParseSeqTypeI(char *extS, int exact)
{
int type;
DB_DNA_IO DB_PrI(">> ParseSeqTypeI |%s| exact=%d\n",extS,exact);
type = BOGUS;
if(!(isgraph(INT(extS[0])))) {
DB_DNA_IO DB_PrI("<< ParseSeqTypeI %d !isgraph\n",type);
return(type);
}
if(exact) {
if(EQSTRING(extS,"DNA",3)) {
type = SEQFM_RAW;
}
else if(EQSTRING(extS,"FAS",3)) {
type = SEQFM_FASTA;
}
else if(EQSTRING(extS,"SEQ",3)) {
type = SEQFM_SEQ;
}
}
else {
switch(toupper(INT(extS[0])))
{
case 'D': type = SEQFM_RAW; break;
case 'F': type = SEQFM_FASTA; break;
case 'S': type = SEQFM_SEQ; break;
}
}
DB_DNA_IO DB_PrI("<< ParseSeqTypeI %d\n",type);
return(type);
}
/*************************************************************************
* Fill seq file format extension string
*/
void FillSeqFtypeExtString(int type,char *typeS)
{
switch(type)
{
case SEQFM_RAW: sprintf(typeS,"raw"); break;
case SEQFM_SEQ: sprintf(typeS,"seq"); break;
case SEQFM_FASTA: sprintf(typeS,"fas"); break;
default: sprintf(typeS,"unk"); break;
}
}
/*************************************************************************
* Fill seq file format description string
*/
void FillSeqFtypeDescString(int type,char *typeS)
{
switch(type)
{
case SEQFM_RAW: sprintf(typeS,"raw / dna"); break;
case SEQFM_SEQ: sprintf(typeS,"seq"); break;
case SEQFM_FASTA: sprintf(typeS,"fasta"); break;
default: sprintf(typeS,"Unknown??? (%d)",type); break;
}
}
/*************************************************************************
* Is format code ok?
*/
int OkSeqInFormatI(int type,char *nameS,int error)
{
switch(type) {
case SEQFM_RAW:
case SEQFM_SEQ:
case SEQFM_FASTA:
return(TRUE);
}
if(nameS) {
if( (!NO_S(nameS)) && error ) {
printf("Unknown format (type code: %d) for file\n",type);
printf(" |%s| = ????\n",nameS);
}
}
return(FALSE);
}