1 /* @source ajmatrices *********************************************************
2 **
3 ** AJAX matrices functions
4 **
5 ** @author Copyright (C) 2003 Alan Bleasby
6 ** @author Copyright (C) 2003 Peter Rice
7 ** @version $Revision: 1.44 $
8 ** @modified Copyright (C) 2003 Jon Ison. Rewritten for string matrix labels
9 ** @modified $Date: 2012/07/02 17:17:59 $ by $Author: rice $
10 ** @@
11 **
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License as published by the Free Software Foundation; either
15 ** version 2.1 of the License, or (at your option) any later version.
16 **
17 ** This library is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ** Lesser General Public License for more details.
21 **
22 ** You should have received a copy of the GNU Lesser General Public
23 ** License along with this library; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 ** MA  02110-1301,  USA.
26 **
27 ******************************************************************************/
28 
29 #include "ajlib.h"
30 
31 #include "ajmatrices.h"
32 #include "ajarr.h"
33 #include "ajfileio.h"
34 #include "ajfiledata.h"
35 
36 
37 static AjPStr matrixStrQuery = NULL;
38 
39 
40 
41 
42 /* @func ajMatrixNew **********************************************************
43 **
44 ** Creates a new, zero matrix from an array of strings and a matrix name. If
45 ** the matrix is a residue substitution matrix then each string would be a
46 ** defined sequence character.
47 **
48 ** The matrix comparison value table Matrix is created and initialised
49 ** with zeroes.
50 **
51 ** @param [r] codes [const AjPPStr] Matrix labels, e.g. valid sequence
52 **                                  character codes
53 ** @param [r] n [ajint] Number of labels
54 ** @param [r] filename [const AjPStr] Matrix filename
55 ** @return [AjPMatrix] New matrix, or NULL if codes, n or filename are 0.
56 **
57 ** @release 1.0.0
58 ** @@
59 ******************************************************************************/
60 
ajMatrixNew(const AjPPStr codes,ajint n,const AjPStr filename)61 AjPMatrix ajMatrixNew(const AjPPStr codes, ajint n, const AjPStr filename)
62 {
63     ajint     i   = 0;
64     AjPMatrix ret = NULL;
65     ajint nsize;
66 
67     if((!n) || (!codes) || (!filename))
68 	return NULL;
69 
70     nsize = n + 1;
71 
72     AJNEW0(ret);
73 
74     ajStrAssignS(&ret->Name, filename);
75 
76     AJCNEW0(ret->Codes, n);
77 
78     for(i=0; i<n; i++)
79 	ret->Codes[i] = ajStrNew();
80 
81     for(i=0; i<n; i++)
82 	ajStrAssignS(&ret->Codes[i], codes[i]);
83 
84     ret->Size = nsize;
85 
86     AJCNEW0(ret->Matrix, nsize);
87 
88     for(i=0; i<nsize; i++)
89 	AJCNEW0(ret->Matrix[i], nsize);
90 
91     ret->Cvt = ajSeqcvtNewStr(codes, n);
92 
93     return ret;
94 }
95 
96 
97 
98 
99 /* @func ajMatrixNewAsym ******************************************************
100 **
101 ** Creates a new, zero asymmetrical matrix from two array of strings and a
102 ** matrix name. If the matrix is a substitution matrix then each string would
103 ** be a defined code, e.g. sequence character.
104 **
105 ** The matrix comparison value table Matrix is created and initialised
106 ** with zeroes.
107 **
108 ** @param [r] codes [const AjPPStr] Matrix column labels, e.g. valid sequence
109 **                                  character codes
110 ** @param [r] n [ajint] Number of column labels
111 ** @param [r] rcodes [const AjPPStr] Matrix row labels, e.g. valid sequence
112 **                                  character codes
113 ** @param [r] rn [ajint] Number of row labels
114 ** @param [r] filename [const AjPStr] Matrix filename
115 ** @return [AjPMatrix] New matrix, or NULL if codes, n or filename are 0.
116 **
117 ** @release 3.0.0
118 ** @@
119 ******************************************************************************/
120 
ajMatrixNewAsym(const AjPPStr codes,ajint n,const AjPPStr rcodes,ajint rn,const AjPStr filename)121 AjPMatrix ajMatrixNewAsym(const AjPPStr codes, ajint n,
122 			  const AjPPStr rcodes, ajint rn,
123 			  const AjPStr filename)
124 {
125     ajint     i   = 0;
126     AjPMatrix ret = NULL;
127     ajint nsize;
128     ajint rnsize;
129 
130     if((!n) || (!codes) || (!filename))
131 	return NULL;
132 
133     nsize  = n + 1;
134     rnsize = rn + 1;
135 
136     AJNEW0(ret);
137 
138     ajStrAssignS(&ret->Name, filename);
139 
140     AJCNEW0(ret->Codes, n);
141 
142     for(i=0; i<n; i++)
143 	ret->Codes[i] = ajStrNew();
144 
145     for(i=0; i<n; i++)
146 	ajStrAssignS(&ret->Codes[i], codes[i]);
147 
148     ret->Size  = nsize;
149     AJCNEW0(ret->CodesRow, rn);
150 
151     for(i=0; i<rn; i++)
152 	ret->CodesRow[i] = ajStrNew();
153 
154     for(i=0; i<rn; i++)
155 	ajStrAssignS(&ret->CodesRow[i], rcodes[i]);
156 
157     ret->SizeRow = rnsize;
158     AJCNEW0(ret->Matrix, rnsize);
159 
160     for(i=0; i<rnsize; i++)
161 	AJCNEW0(ret->Matrix[i], nsize);
162 
163     ret->Cvt = ajSeqcvtNewStrAsym(codes, n, rcodes, rn);
164 
165     return ret;
166 }
167 
168 
169 
170 
171 /* @func ajMatrixfNew *********************************************************
172 **
173 ** Creates a new, zero matrix from an array of strings and a matrix name. If
174 ** the matrix is a residue substitution matrix then each string would be a
175 ** defined sequence character.
176 **
177 ** The matrix comparison value table Matrix is created and initialised
178 ** with zeroes.
179 **
180 ** @param [r] codes [const AjPPStr] Matrix labels,
181 **                                 e.g. valid sequence char codes
182 ** @param [r] n [ajint] Number of labels
183 ** @param [r] filename [const AjPStr] Matrix filename
184 ** @return [AjPMatrixf] New matrix, or NULL if codes, n or filename are 0.
185 **
186 ** @release 1.0.0
187 ** @@
188 ******************************************************************************/
189 
ajMatrixfNew(const AjPPStr codes,ajint n,const AjPStr filename)190 AjPMatrixf ajMatrixfNew(const AjPPStr codes, ajint n, const AjPStr filename)
191 {
192     ajint i = 0;
193     AjPMatrixf ret = 0;
194     ajint nsize;
195 
196     if((!n) || (!codes) || (!filename))
197 	return NULL;
198 
199     nsize = n + 1;
200 
201     AJNEW0(ret);
202 
203     ajStrAssignS(&ret->Name, filename);
204 
205     AJCNEW0(ret->Codes, n);
206 
207     for(i=0; i<n; i++)
208 	ret->Codes[i] = ajStrNew();
209 
210     for(i=0; i<n; i++)
211 	ajStrAssignS(&ret->Codes[i], codes[i]);
212 
213     ret->Size = nsize;
214     AJCNEW0(ret->Matrixf, nsize);
215 
216     for(i=0; i<nsize; i++)
217 	AJCNEW0(ret->Matrixf[i], nsize);
218 
219     ret->Cvt = ajSeqcvtNewStr(codes, n);
220 
221     return ret;
222 }
223 
224 
225 
226 
227 /* @func ajMatrixfNewAsym *****************************************************
228 **
229 ** Creates a new, zero asymmetrical matrix from an array of strings and a
230 ** matrix name. If the matrix is a residue substitution matrix then each
231 ** string would be a defined sequence character.
232 **
233 ** The matrix comparison value table Matrix is created and initialised
234 ** with zeroes.
235 **
236 ** @param [r] codes [const AjPPStr] Matrix labels,
237 **                                 e.g. valid sequence char codes
238 ** @param [r] n [ajint] Number of labels
239 ** @param [r] rcodes [const AjPPStr] Matrix row labels, e.g. valid sequence
240 **                                  character codes.
241 ** @param [r] rn [ajint] Number of row labels
242 ** @param [r] filename [const AjPStr] Matrix filename
243 ** @return [AjPMatrixf] New matrix, or NULL if codes, n or filename are 0.
244 **
245 ** @release 3.0.0
246 ** @@
247 ******************************************************************************/
248 
ajMatrixfNewAsym(const AjPPStr codes,ajint n,const AjPPStr rcodes,ajint rn,const AjPStr filename)249 AjPMatrixf ajMatrixfNewAsym(const AjPPStr codes, ajint n,
250 			    const AjPPStr rcodes, ajint rn,
251 			    const AjPStr filename)
252 {
253     ajint i = 0;
254     AjPMatrixf ret = 0;
255     ajint nsize;
256     ajint rnsize;
257 
258     if((!n) || (!codes) || (!filename))
259 	return NULL;
260 
261     nsize = n + 1;
262     rnsize = rn + 1;
263 
264     AJNEW0(ret);
265 
266     ajStrAssignS(&ret->Name, filename);
267 
268     AJCNEW0(ret->Codes, n);
269 
270     for(i=0; i<n; i++)
271 	ret->Codes[i] = ajStrNew();
272 
273     for(i=0; i<n; i++)
274 	ajStrAssignS(&ret->Codes[i], codes[i]);
275 
276     ret->Size = nsize;
277 
278 
279     AJCNEW0(ret->CodesRow, rn);
280 
281     for(i=0; i<rn; i++)
282 	ret->CodesRow[i] = ajStrNew();
283 
284     for(i=0; i<rn; i++)
285 	ajStrAssignS(&ret->CodesRow[i], rcodes[i]);
286 
287     ret->SizeRow = rnsize;
288 
289 
290     AJCNEW0(ret->Matrixf, rnsize);
291 
292     for(i=0; i<rnsize; i++)
293 	AJCNEW0(ret->Matrixf[i], nsize);
294 
295     ret->Cvt = ajSeqcvtNewStrAsym(codes, n, rcodes, rn);
296 
297     return ret;
298 }
299 
300 
301 
302 
303 /* @func ajMatrixfDel *********************************************************
304 **
305 ** Delete a float matrix
306 **
307 ** @param [w] thys [AjPMatrixf*] Matrix to delete
308 ** @return [void]
309 **
310 ** @release 1.6.1
311 ** @@
312 ******************************************************************************/
313 
ajMatrixfDel(AjPMatrixf * thys)314 void ajMatrixfDel(AjPMatrixf *thys)
315 {
316     ajint isize  = 0;
317     ajint iisize = 0;
318     ajint rsize  = 0;
319     ajint ssize  = 0;
320     ajint i      = 0;
321 
322 
323     if(!*thys || !thys)
324 	return;
325 
326     isize = (*thys)->Size;
327     iisize = isize - 1;
328     rsize = (*thys)->SizeRow;
329     ssize = rsize - 1;
330 
331     for(i=0; i<iisize; ++i)
332 	ajStrDel(&(*thys)->Codes[i]);
333 
334     AJFREE((*thys)->Codes);
335 
336     for(i=0; i<ssize; ++i)
337 	ajStrDel(&(*thys)->CodesRow[i]);
338 
339     AJFREE((*thys)->CodesRow);
340 
341     ajStrDel(&(*thys)->Name);
342 
343     for(i=0; i<rsize; ++i)
344 	AJFREE((*thys)->Matrixf[i]);
345 
346     AJFREE((*thys)->Matrixf);
347 
348     ajSeqcvtDel(&(*thys)->Cvt);
349     AJFREE(*thys);
350 
351     return;
352 }
353 
354 
355 
356 
357 /* @func ajMatrixDel **********************************************************
358 **
359 ** Delete an integer matrix
360 **
361 ** @param [w] thys [AjPMatrix*] Matrix to delete
362 ** @return [void]
363 **
364 ** @release 2.1.0
365 ** @@
366 ******************************************************************************/
367 
ajMatrixDel(AjPMatrix * thys)368 void ajMatrixDel(AjPMatrix *thys)
369 {
370     ajint isize  = 0;
371     ajint iisize = 0;
372     ajint rsize  = 0;
373     ajint ssize  = 0;
374     ajint i      = 0;
375 
376 
377     if(!*thys || !thys)
378 	return;
379 
380     isize = (*thys)->Size;
381     iisize = isize - 1;
382     rsize = (*thys)->SizeRow;
383     ssize = rsize - 1;
384 
385 
386     for(i=0; i<iisize; ++i)
387 	ajStrDel(&(*thys)->Codes[i]);
388 
389     AJFREE((*thys)->Codes);
390 
391     for(i=0; i<ssize; ++i)
392 	ajStrDel(&(*thys)->CodesRow[i]);
393 
394     AJFREE((*thys)->CodesRow);
395 
396     ajStrDel(&(*thys)->Name);
397 
398     for(i=0; i<rsize; ++i)
399 	AJFREE((*thys)->Matrix[i]);
400     AJFREE((*thys)->Matrix);
401 
402     ajSeqcvtDel(&(*thys)->Cvt);
403     AJFREE(*thys);
404 
405     return;
406 }
407 
408 
409 
410 
411 /* @func ajMatrixNewFile ******************************************************
412 **
413 ** Constructs a comparison matrix from a given local data file
414 **
415 ** @param [r] filename [const AjPStr] Input filename
416 ** @return [AjPMatrix] Matrix object
417 **
418 ** @release 6.2.0
419 ** @@
420 ******************************************************************************/
421 
ajMatrixNewFile(const AjPStr filename)422 AjPMatrix ajMatrixNewFile(const AjPStr filename)
423 {
424     AjPMatrix ret = NULL;
425     AjPStr buffer = NULL;
426     const AjPStr tok    = NULL;
427 
428     AjPStr firststring  = NULL;
429     AjPStr *orderstring = NULL;
430 
431     AjPFile file    = NULL;
432     AjBool first    = ajTrue;
433     const char *ptr = NULL;
434     ajint **matrix  = NULL;
435 
436     ajint minval = -1;
437     ajint i      = 0;
438     ajint l      = 0;
439     ajint k      = 0;
440     ajint cols   = 0;
441     ajint rows   = 0;
442 
443     ajint *templine = NULL;
444 
445     AjPList rlabel_list = NULL;
446     AjPStr  *rlabel_arr  = NULL;
447 
448 #ifndef WIN32
449     static const char *delimstr = " :\t\n";
450 #else /* WIN32 */
451     static const char *delimstr = " :\t\n\r";
452 #endif /* !WIN32 */
453 
454     rlabel_list = ajListNew();
455 
456 
457     firststring = ajStrNew();
458 
459     file = ajDatafileNewInNameS(filename);
460 
461     if(!file)
462     {
463 	ajStrDel(&firststring);
464 	ajListFree(&rlabel_list);
465 
466 	return NULL;
467     }
468 
469     /* Read row labels */
470     while(ajReadline(file,&buffer))
471     {
472 	ptr = ajStrGetPtr(buffer);
473 #ifndef WIN32
474 	if(*ptr != '#' && *ptr != '\n')
475 #else /* WIN32 */
476 	if(*ptr != '#' && *ptr != '\n' && *ptr != '\r')
477 #endif /* !WIN32 */
478 	{
479 	    if(first)
480 		first = ajFalse;
481 	    else
482 	    {
483 		ajFmtScanC(ptr, "%S", &firststring);
484 		ajListPushAppend(rlabel_list, firststring);
485 		firststring = ajStrNew();
486 	    }
487 	}
488     }
489 
490     first = ajTrue;
491     ajStrDel(&firststring);
492     rows = (ajuint) ajListToarray(rlabel_list, (void ***) &rlabel_arr);
493     ajFileSeek(file, 0, 0);
494 
495 
496     while(ajReadline(file,&buffer))
497     {
498 	ajStrRemoveWhiteExcess(&buffer);
499 	ptr = ajStrGetPtr(buffer);
500 
501 	if(*ptr && *ptr != '#')
502 	{
503 	    if(first)
504 	    {
505 		cols = ajStrParseCountC(buffer,delimstr);
506 		AJCNEW0(orderstring, cols);
507 
508 		for(i=0; i<cols; i++)
509 		    orderstring[i] = ajStrNew();
510 
511 		tok = ajStrParseC(buffer, " :\t\n");
512 		ajStrAssignS(&orderstring[l++], tok);
513 
514 		while((tok = ajStrParseC(NULL, " :\t\n")))
515 		    ajStrAssignS(&orderstring[l++], tok);
516 
517 		first = ajFalse;
518 
519 		ret = ajMatrixNewAsym(orderstring, cols,
520 						rlabel_arr, rows,
521 						filename);
522 		matrix = ret->Matrix;
523 	    }
524 	    else
525 	    {
526 		ajFmtScanC(ptr, "%S", &firststring);
527 
528 		/* JISON 19/7/4
529 		   k = ajSeqcvtGetCodeK(ret->Cvt, ajStrGetCharFirst(firststring)); */
530 		k = ajSeqcvtGetCodeS(ret->Cvt, firststring);
531 
532 		/*
533 		 ** cols+1 is used below because 2nd and subsequent lines have
534 		 ** one more string in them (the residue label)
535 		 */
536 		templine = ajArrIntLine(buffer,delimstr,2,cols+1);
537 
538 		for(i=0; i<cols; i++)
539 		{
540 		    if(templine[i] < minval)
541 			minval = templine[i];
542 
543 		    /* JISON 19/7/4
544 		    matrix[k][ajSeqcvtGetCodeK(ret->Cvt,
545 					ajStrGetCharFirst(orderstring[i]))]
546 					    = templine[i]; */
547 		    matrix[k][ajSeqcvtGetCodeAsymS(ret->Cvt,
548 					       orderstring[i])]
549 						   = templine[i];
550 		}
551 
552 		AJFREE(templine);
553 	    }
554 	}
555     }
556 
557     ajDebug("fill rest with minimum value %d\n", minval);
558 
559 
560     ajFileClose(&file);
561     ajStrDel(&buffer);
562 
563     for(i=0; i<cols; i++)
564 	ajStrDel(&orderstring[i]);
565 
566     AJFREE(orderstring);
567 
568 
569     ajDebug("read matrix file %S\n", filename);
570 
571     ajStrDel(&firststring);
572 
573     for(i=0; i<rows; i++)
574 	ajStrDel(&rlabel_arr[i]);
575 
576     AJFREE(rlabel_arr);
577     ajListFree(&rlabel_list);
578 
579     return ret;
580 }
581 
582 
583 
584 
585 /* @func ajMatrixfNewFile *****************************************************
586 **
587 ** Constructs a comparison matrix from a given local data file
588 **
589 ** @param [r] filename [const AjPStr] Input filename
590 ** @return [AjPMatrixf] Float matrix object
591 **
592 ** @release 6.2.0
593 ** @@
594 ******************************************************************************/
595 
ajMatrixfNewFile(const AjPStr filename)596 AjPMatrixf ajMatrixfNewFile(const AjPStr filename)
597 {
598     AjPMatrixf ret = NULL;
599     AjPStr *orderstring = NULL;
600     AjPStr buffer       = NULL;
601     AjPStr firststring  = NULL;
602     AjPStr reststring   = NULL;
603     const AjPStr tok    = NULL;
604 
605     ajint len  = 0;
606     ajint i    = 0;
607     ajint l    = 0;
608     ajint k    = 0;
609     ajint cols = 0;
610     ajint rows   = 0;
611 
612     const char *ptr = NULL;
613 
614     AjPFile file    = NULL;
615     AjBool  first   = ajTrue;
616 
617     float **matrix  = NULL;
618     float *templine = NULL;
619     float minval    = -1.0;
620 
621     AjPList rlabel_list = NULL;
622     AjPStr  *rlabel_arr  = NULL;
623 #ifndef WIN32
624     static const char *delimstr = " :\t\n";
625 #else /* WIN32 */
626     static const char *delimstr = " :\t\n\r";
627 #endif /* !WIN32 */
628 
629 
630 
631     rlabel_list = ajListNew();
632 
633 
634 
635     firststring = ajStrNew();
636     reststring  = ajStrNew();
637 
638     file = ajDatafileNewInNameS(filename);
639 
640     if(!file)
641     {
642 	ajStrDel(&firststring);
643 	ajStrDel(&reststring);
644 
645 	return NULL;
646     }
647 
648 
649     /* Read row labels */
650     while(ajReadline(file,&buffer))
651     {
652 	ptr = ajStrGetPtr(buffer);
653 #ifndef WIN32
654 	if(*ptr != '#' && *ptr != '\n')
655 #else /* WIN32 */
656 	if(*ptr != '#' && *ptr != '\n' && *ptr != '\r')
657 #endif /* !WIN32 */
658 	{
659 	    if(first)
660 		first = ajFalse;
661 	    else
662 	    {
663 		ajFmtScanC(ptr, "%S", &firststring);
664 		ajListPushAppend(rlabel_list, firststring);
665 		firststring = ajStrNew();
666 	    }
667 	}
668     }
669     first = ajTrue;
670     ajStrDel(&firststring);
671     rows = (ajuint) ajListToarray(rlabel_list, (void ***) &rlabel_arr);
672     ajFileSeek(file, 0, 0);
673 
674 
675     while(ajReadline(file,&buffer))
676     {
677 	ajStrRemoveWhiteExcess(&buffer);
678 	ptr = ajStrGetPtr(buffer);
679 
680 	if(*ptr && *ptr != '#')
681 	{
682 	    if(first)
683 	    {
684 		cols = ajStrParseCountC(buffer,delimstr);
685 		AJCNEW0(orderstring, cols);
686 
687 		for(i=0; i<cols; i++)
688 		    orderstring[i] = ajStrNew();
689 
690 		tok = ajStrParseC(buffer, " :\t\n");
691 		ajStrAssignS(&orderstring[l++], tok);
692 
693 		while((tok = ajStrParseC(NULL, " :\t\n")))
694 		    ajStrAssignS(&orderstring[l++], tok);
695 
696 		first = ajFalse;
697 
698 		ret = ajMatrixfNewAsym(orderstring, cols,
699 						 rlabel_arr, rows,
700 						 filename);
701 		matrix = ret->Matrixf;
702 	    }
703 	    else
704 	    {
705 		ajFmtScanC(ptr, "%S", &firststring);
706 		/* JISON 19/7/4
707 		   k = ajSeqcvtGetCodeK(ret->Cvt,
708                    ajStrGetCharFirst(firststring)); */
709 		k = ajSeqcvtGetCodeS(ret->Cvt, firststring);
710 
711 		len = MAJSTRGETLEN(firststring);
712 		ajStrAssignSubC(&reststring, ptr, len, -1);
713 
714 		/*
715 		** Must discard the first string (label) and use
716 		** reststring otherwise ajArrFloatLine would fail (it
717 		** cannot convert a string to a float)
718 		**
719 		** Use cols,1,cols in below because although 2nd and
720 		** subsequent lines have one more string in them (the
721 		** residue label in the 1st column) we've discarded that
722 		** from the string that's passed
723 		*/
724 		templine = ajArrFloatLine(reststring,delimstr,1,cols);
725 
726 		for(i=0; i<cols; i++)
727 		{
728 		    if(templine[i] < minval)
729 			minval = templine[i];
730 
731 		    /* JISON 19/7/4
732 		    matrix[k][ajSeqcvtGetCodeK(ret->Cvt,
733 					ajStrGetCharFirst(orderstring[i]))]
734 					    = templine[i]; */
735 
736 		    matrix[k][ajSeqcvtGetCodeAsymS(ret->Cvt,
737 					       orderstring[i])]
738 						   = templine[i];
739 		}
740 		AJFREE(templine);
741 	    }
742 	}
743     }
744     ajDebug("fill rest with minimum value %d\n", minval);
745 
746 
747     ajFileClose(&file);
748     ajStrDel(&buffer);
749 
750     for(i=0; i<cols; i++)
751 	ajStrDel(&orderstring[i]);
752 
753     AJFREE(orderstring);
754 
755 
756     ajDebug("read matrix file %S\n", filename);
757 
758     ajStrDel(&firststring);
759     ajStrDel(&reststring);
760 
761    for(i=0; i<rows; i++)
762 	ajStrDel(&rlabel_arr[i]);
763 
764    AJFREE(rlabel_arr);
765    ajListFree(&rlabel_list);
766 
767    return ret;
768 }
769 
770 
771 
772 
773 /* @func ajMatrixSeqIndex *****************************************************
774 **
775 ** Converts a sequence to index numbers using the matrix's
776 ** internal conversion table. Sequence characters not defined
777 ** in the matrix are converted to zero.
778 **
779 ** @param [r] thys [const AjPMatrix] Matrix object
780 ** @param [r] seq [const AjPSeq] Sequence object
781 ** @param [w] numseq [AjPStr*] Index code version of the sequence
782 ** @return [AjBool] ajTrue on success.
783 **
784 ** @release 6.2.0
785 ** @@
786 ******************************************************************************/
787 
ajMatrixSeqIndex(const AjPMatrix thys,const AjPSeq seq,AjPStr * numseq)788 AjBool ajMatrixSeqIndex(const AjPMatrix thys, const AjPSeq seq, AjPStr* numseq)
789 {
790     return ajSeqConvertNum(seq, thys->Cvt, numseq);
791 }
792 
793 
794 
795 
796 /* @func ajMatrixfSeqIndex ****************************************************
797 **
798 ** Converts a sequence to index numbers using the matrix's
799 ** internal conversion table. Sequence characters not defined
800 ** in the matrix are converted to zero.
801 **
802 ** @param [r] thys [const AjPMatrixf] Float Matrix object
803 ** @param [r] seq [const AjPSeq] Sequence object
804 ** @param [w] numseq [AjPStr*] Index code version of the sequence
805 ** @return [AjBool] ajTrue on success.
806 **
807 ** @release 6.2.0
808 ** @@
809 ******************************************************************************/
810 
ajMatrixfSeqIndex(const AjPMatrixf thys,const AjPSeq seq,AjPStr * numseq)811 AjBool ajMatrixfSeqIndex(const AjPMatrixf thys, const AjPSeq seq,
812                          AjPStr* numseq)
813 {
814     return ajSeqConvertNum(seq, thys->Cvt, numseq);
815 }
816 
817 
818 
819 
820 /* @func ajMatrixGetCodes *****************************************************
821 **
822 ** Returns the character codes for each offset in the matrix
823 **
824 ** @param [r] thys [const AjPMatrix] Matrix object
825 ** @return [AjPStr] Matrix codes
826 **
827 ** @release 4.0.0
828 ******************************************************************************/
829 
ajMatrixGetCodes(const AjPMatrix thys)830 AjPStr ajMatrixGetCodes(const AjPMatrix thys)
831 {
832     AjPStr ret = NULL;
833     ajint i;
834     ajint maxcode;
835 
836     ret = ajStrNewRes(thys->Size);
837     maxcode = thys->Size - 1;
838 
839     for (i=0;i<maxcode;i++)
840 	ajStrAppendK(&ret, ajStrGetCharFirst(thys->Codes[i]));
841 
842     return ret;
843 }
844 
845 
846 
847 
848 /* @func ajMatrixfGetCodes ****************************************************
849 **
850 ** Returns the character codes for each offset in the matrix
851 **
852 ** @param [r] thys [const AjPMatrixf] Matrix object
853 ** @return [AjPStr] Matrix codes
854 **
855 ** @release 4.0.0
856 ******************************************************************************/
857 
ajMatrixfGetCodes(const AjPMatrixf thys)858 AjPStr ajMatrixfGetCodes(const AjPMatrixf thys)
859 {
860     AjPStr ret = NULL;
861     ajint i;
862     ajint maxcode;
863 
864     ret = ajStrNewRes(thys->Size + 1);
865     maxcode = thys->Size - 1;
866 
867     for (i=0;i<maxcode;i++)
868 	ajStrAppendK(&ret, ajStrGetCharFirst(thys->Codes[i]));
869 
870     return ret;
871 }
872 
873 
874 
875 
876 /* @func ajMatrixGetMatrix ****************************************************
877 **
878 ** Returns the matrix values array in the matrix
879 **
880 ** @param [r] thys [const AjPMatrix] Matrix object
881 ** @return [AjIntArray*] Matrix values array
882 **
883 ** @release 6.2.0
884 ******************************************************************************/
885 
ajMatrixGetMatrix(const AjPMatrix thys)886 AjIntArray* ajMatrixGetMatrix(const AjPMatrix thys)
887 {
888     if(!thys)
889         return NULL;
890 
891     return thys->Matrix;
892 }
893 
894 
895 
896 
897 /* @func ajMatrixfGetMatrix ***************************************************
898 **
899 ** Returns the matrix values array in the matrix
900 **
901 ** @param [r] thys [const AjPMatrixf] Matrix object
902 ** @return [AjFloatArray*] Matrix values array
903 **
904 ** @release 6.2.0
905 ******************************************************************************/
906 
ajMatrixfGetMatrix(const AjPMatrixf thys)907 AjFloatArray* ajMatrixfGetMatrix(const AjPMatrixf thys)
908 {
909     if(!thys)
910         return NULL;
911 
912     return thys->Matrixf;
913 }
914 
915 
916 
917 
918 /* @func ajMatrixGetRows ******************************************************
919 **
920 ** Returns the comparison matrix row size.
921 **
922 ** @param [r] thys [const AjPMatrix] Matrix object
923 ** @return [ajuint] .
924 **
925 ** @release 6.3.0
926 ** @@
927 ******************************************************************************/
928 
ajMatrixGetRows(const AjPMatrix thys)929 ajuint ajMatrixGetRows(const AjPMatrix thys)
930 {
931     if(thys)
932 	return thys->SizeRow;
933 
934     return 0;
935 }
936 
937 
938 
939 
940 /* @func ajMatrixGetSize ******************************************************
941 **
942 ** Returns the comparison matrix size.
943 **
944 ** @param [r] thys [const AjPMatrix] Matrix object
945 ** @return [ajuint] .
946 **
947 ** @release 6.2.0
948 ** @@
949 ******************************************************************************/
950 
ajMatrixGetSize(const AjPMatrix thys)951 ajuint ajMatrixGetSize(const AjPMatrix thys)
952 {
953     if(thys)
954 	return thys->Size;
955 
956     return 0;
957 }
958 
959 
960 
961 
962 /* @func ajMatrixfGetRows *****************************************************
963 **
964 ** Returns the comparison matrix row size.
965 **
966 ** @param [r] thys [const AjPMatrixf] Matrix object
967 ** @return [ajuint] .
968 **
969 ** @release 6.3.0
970 ** @@
971 ******************************************************************************/
972 
ajMatrixfGetRows(const AjPMatrixf thys)973 ajuint ajMatrixfGetRows(const AjPMatrixf thys)
974 {
975     if(thys)
976 	return thys->SizeRow;
977 
978     return 0;
979 }
980 
981 
982 
983 
984 /* @func ajMatrixfGetSize *****************************************************
985 **
986 ** Returns the comparison matrix size.
987 **
988 ** @param [r] thys [const AjPMatrixf] Matrix object
989 ** @return [ajuint] .
990 **
991 ** @release 6.2.0
992 ** @@
993 ******************************************************************************/
994 
ajMatrixfGetSize(const AjPMatrixf thys)995 ajuint ajMatrixfGetSize(const AjPMatrixf thys)
996 {
997     if(thys)
998 	return thys->Size;
999 
1000     return 0;
1001 }
1002 
1003 
1004 
1005 
1006 /* @func ajMatrixGetCvt *******************************************************
1007 **
1008 ** Returns the sequence character conversion table for a matrix.
1009 ** This table converts any character defined in the matrix to a
1010 ** positive integer, and any other character is converted to zero.
1011 **
1012 ** @param [r] thys [const AjPMatrix] Matrix object
1013 ** @return [AjPSeqCvt] sequence character conversion table
1014 **
1015 ** @release 6.2.0
1016 ** @@
1017 ******************************************************************************/
1018 
ajMatrixGetCvt(const AjPMatrix thys)1019 AjPSeqCvt ajMatrixGetCvt(const AjPMatrix thys)
1020 {
1021     if(thys)
1022 	return thys->Cvt;
1023 
1024     return NULL;
1025 }
1026 
1027 
1028 
1029 
1030 /* @func ajMatrixfGetCvt ******************************************************
1031 **
1032 ** Returns the sequence character conversion table for a matrix.
1033 ** This table converts any character defined in the matrix to a
1034 ** positive integer, and any other character is converted to zero.
1035 **
1036 ** @param [r] thys [const AjPMatrixf] Float Matrix object
1037 ** @return [AjPSeqCvt] sequence character conversion table
1038 **
1039 ** @release 6.2.0
1040 ** @@
1041 ******************************************************************************/
1042 
ajMatrixfGetCvt(const AjPMatrixf thys)1043 AjPSeqCvt ajMatrixfGetCvt(const AjPMatrixf thys)
1044 {
1045     if(thys)
1046 	return thys->Cvt;
1047 
1048     return NULL;
1049 }
1050 
1051 
1052 
1053 
1054 
1055 /* @func ajMatrixGetLabelNum **************************************************
1056 **
1057 ** Returns the sequence character (or asymmetric matrix label)
1058 ** for a matrix column.
1059 **
1060 ** @param [r] thys [const AjPMatrix] Matrix object
1061 ** @param [r] i [ajint] Character index
1062 ** @return [const AjPStr] Matrix label, e.g. sequence character code
1063 **                        or '?' if not found
1064 **
1065 ** @release 6.2.0
1066 ** @@
1067 ******************************************************************************/
1068 
ajMatrixGetLabelNum(const AjPMatrix thys,ajint i)1069 const AjPStr ajMatrixGetLabelNum(const AjPMatrix thys, ajint i)
1070 {
1071     if(!matrixStrQuery)
1072         ajStrAssignK(&matrixStrQuery, '?');
1073 
1074     if(!thys)
1075         return matrixStrQuery;
1076 
1077     if(i >= thys->Size)
1078         return matrixStrQuery;
1079 
1080     if(i < 0)
1081         return matrixStrQuery;
1082 
1083     return thys->Codes[i];
1084 }
1085 
1086 
1087 
1088 
1089 /* @func ajMatrixfGetLabelNum *************************************************
1090 **
1091 ** Returns the sequence character (or asymmetric matrix label)
1092 ** for a matrix column.
1093 **
1094 ** @param [r] thys [const AjPMatrixf] Matrix object
1095 ** @param [r] i [ajint] Character index
1096 ** @return [const AjPStr] Matrix label, e.g. sequence character code
1097 **                        or '?' if not found
1098 **
1099 ** @release 6.2.0
1100 ** @@
1101 ******************************************************************************/
1102 
ajMatrixfGetLabelNum(const AjPMatrixf thys,ajint i)1103 const AjPStr ajMatrixfGetLabelNum(const AjPMatrixf thys, ajint i)
1104 {
1105     if(!matrixStrQuery)
1106         ajStrAssignK(&matrixStrQuery, '?');
1107 
1108     if(!thys)
1109         return matrixStrQuery;
1110 
1111     if(i >= thys->Size)
1112         return matrixStrQuery;
1113 
1114     if(i < 0)
1115         return matrixStrQuery;
1116 
1117     return thys->Codes[i];
1118 }
1119 
1120 
1121 
1122 
1123 /* @func ajMatrixGetName ******************************************************
1124 **
1125 ** Returns the name of a matrix object, usually the filename from
1126 ** which it was read.
1127 **
1128 ** @param [r] thys [const AjPMatrix] Matrix object
1129 ** @return [const AjPStr] The name, a pointer to the internal name.
1130 **
1131 ** @release 6.2.0
1132 ** @@
1133 ******************************************************************************/
1134 
ajMatrixGetName(const AjPMatrix thys)1135 const AjPStr ajMatrixGetName(const AjPMatrix thys)
1136 {
1137     static AjPStr emptystr = NULL;
1138 
1139     if(!thys)
1140     {
1141 	if (!emptystr)
1142 	    emptystr = ajStrNewC("");
1143 
1144 	return emptystr;
1145     }
1146 
1147     return thys->Name;
1148 }
1149 
1150 
1151 
1152 
1153 /* @func ajMatrixfGetName *****************************************************
1154 **
1155 ** Returns the name of a matrix object, usually the filename from
1156 ** which it was read.
1157 **
1158 ** @param [r] thys [const AjPMatrixf] Matrix object
1159 ** @return [const AjPStr] The name, a pointer to the internal name.
1160 **
1161 ** @release 6.2.0
1162 ** @@
1163 ******************************************************************************/
1164 
ajMatrixfGetName(const AjPMatrixf thys)1165 const AjPStr ajMatrixfGetName(const AjPMatrixf thys)
1166 {
1167     static AjPStr emptystr = NULL;
1168 
1169     if(!thys)
1170     {
1171 	emptystr = ajStrNewC("");
1172 
1173 	return emptystr;
1174     }
1175 
1176     return thys->Name;
1177 }
1178 
1179 
1180 
1181 
1182 /* @func ajMatrixExit *********************************************************
1183 **
1184 ** Cleans matrix processing internals
1185 **
1186 ** @return [void]
1187 **
1188 ** @release 6.2.0
1189 ** @@
1190 ******************************************************************************/
1191 
ajMatrixExit(void)1192 void ajMatrixExit(void)
1193 {
1194     ajStrDel(&matrixStrQuery);
1195 
1196     return;
1197 }
1198 
1199 
1200 
1201 
1202 #ifdef AJ_COMPILE_DEPRECATED_BOOK
1203 #endif /* AJ_COMPILE_DEPRECATED_BOOK */
1204 
1205 
1206 
1207 
1208 #ifdef AJ_COMPILE_DEPRECATED
1209 /* @obsolete ajMatrixRead
1210 ** @remove Use ajMatrixNewFile
1211 */
1212 
ajMatrixRead(AjPMatrix * pthis,const AjPStr filename)1213 __deprecated AjBool ajMatrixRead(AjPMatrix* pthis, const AjPStr filename)
1214 {
1215     *pthis = ajMatrixNewFile(filename);
1216     if(!*pthis)
1217         return ajFalse;
1218     return ajTrue;
1219 }
1220 
1221 
1222 
1223 
1224 /* @obsolete ajMatrixfRead
1225 ** @remove Use ajMatrixfNewFile
1226 */
1227 
ajMatrixfRead(AjPMatrixf * pthis,const AjPStr filename)1228 __deprecated AjBool ajMatrixfRead(AjPMatrixf* pthis, const AjPStr filename)
1229 {
1230     *pthis = ajMatrixfNewFile(filename);
1231     if(!*pthis)
1232         return ajFalse;
1233     return ajTrue;
1234 }
1235 
1236 
1237 
1238 
1239 /* @obsolete ajMatrixSeqNum
1240 ** @rename ajMatrixSeqIndex
1241 */
1242 
ajMatrixSeqNum(const AjPMatrix thys,const AjPSeq seq,AjPStr * numseq)1243 __deprecated AjBool ajMatrixSeqNum(const AjPMatrix thys, const AjPSeq seq,
1244                                     AjPStr* numseq)
1245 {
1246     return ajSeqConvertNum(seq, thys->Cvt, numseq);
1247 }
1248 
1249 
1250 
1251 
1252 /* @obsolete ajMatrixfSeqNum
1253 ** @rename ajMatrixfSeqIndex
1254 */
1255 
ajMatrixfSeqNum(const AjPMatrixf thys,const AjPSeq seq,AjPStr * numseq)1256 __deprecated AjBool ajMatrixfSeqNum(const AjPMatrixf thys, const AjPSeq seq,
1257                                     AjPStr* numseq)
1258 {
1259     return ajSeqConvertNum(seq, thys->Cvt, numseq);
1260 }
1261 
1262 
1263 
1264 
1265 /* @obsolete ajMatrixArray
1266 ** @rename ajMatrixGetMatrix
1267 */
1268 
ajMatrixArray(const AjPMatrix thys)1269 __deprecated AjIntArray* ajMatrixArray(const AjPMatrix thys)
1270 {
1271     if(thys)
1272 	return thys->Matrix;
1273 
1274     return NULL;
1275 }
1276 
1277 
1278 
1279 
1280 /* @obsolete ajMatrixfArray
1281 ** @rename ajMatrixfGetMatrix
1282 */
1283 
ajMatrixfArray(const AjPMatrixf thys)1284 __deprecated AjFloatArray* ajMatrixfArray(const AjPMatrixf thys)
1285 {
1286     if(thys)
1287 	return thys->Matrixf;
1288 
1289     return NULL;
1290 }
1291 
1292 
1293 
1294 
1295 /* @obsolete ajMatrixSize
1296 ** @rename ajMatrixGetSize
1297 */
1298 
ajMatrixSize(const AjPMatrix thys)1299 __deprecated ajint ajMatrixSize(const AjPMatrix thys)
1300 {
1301     if(thys)
1302 	return thys->Size;
1303 
1304     return 0;
1305 }
1306 
1307 
1308 
1309 
1310 /* @obsolete ajMatrixfSize
1311 ** @rename ajMatrixfGetSize
1312 */
1313 
ajMatrixfSize(const AjPMatrixf thys)1314 __deprecated ajint ajMatrixfSize(const AjPMatrixf thys)
1315 {
1316     if(thys)
1317 	return thys->Size;
1318 
1319     return 0;
1320 }
1321 
1322 
1323 
1324 
1325 /* @obsolete ajMatrixCvt
1326 ** @rename ajMatrixGetCvt
1327 */
1328 
ajMatrixCvt(const AjPMatrix thys)1329 __deprecated AjPSeqCvt ajMatrixCvt(const AjPMatrix thys)
1330 {
1331     if(thys)
1332 	return thys->Cvt;
1333 
1334     return NULL;
1335 }
1336 
1337 
1338 
1339 
1340 /* @obsolete ajMatrixfCvt
1341 ** @rename ajMatrixfGetCvt
1342 */
1343 
ajMatrixfCvt(const AjPMatrixf thys)1344 __deprecated AjPSeqCvt ajMatrixfCvt(const AjPMatrixf thys)
1345 {
1346     if(thys)
1347 	return thys->Cvt;
1348 
1349     return NULL;
1350 }
1351 
1352 
1353 
1354 
1355 /* @obsolete ajMatrixChar
1356 ** @remove Use ajMatrixGetLabelNum
1357 */
1358 
ajMatrixChar(const AjPMatrix thys,ajint i,AjPStr * label)1359 __deprecated void ajMatrixChar(const AjPMatrix thys, ajint i, AjPStr *label)
1360 {
1361     (void) label;
1362     ajStrAssignS(label, ajMatrixGetLabelNum(thys, i));
1363     return;
1364 }
1365 
1366 
1367 
1368 
1369 /* @obsolete ajMatrixfChar
1370 ** @remove Use ajMatrixfGetLabelNum
1371 */
1372 
ajMatrixfChar(const AjPMatrixf thys,ajint i,AjPStr * label)1373 __deprecated void ajMatrixfChar(const AjPMatrixf thys, ajint i, AjPStr *label)
1374 {
1375     (void) label;
1376     ajStrAssignS(label, ajMatrixfGetLabelNum(thys, i));
1377     return;
1378 }
1379 
1380 
1381 
1382 
1383 /* @obsolete ajMatrixName
1384 ** @rename ajMatrixGetName
1385 */
1386 
ajMatrixName(const AjPMatrix thys)1387 __deprecated const AjPStr ajMatrixName(const AjPMatrix thys)
1388 {
1389     return ajMatrixGetName(thys);
1390 }
1391 
1392 
1393 
1394 
1395 /* @obsolete ajMatrixfName
1396 ** @rename ajMatrixfGetName
1397 */
1398 
ajMatrixfName(const AjPMatrixf thys)1399 __deprecated  const AjPStr ajMatrixfName(const AjPMatrixf thys)
1400 {
1401     return ajMatrixfGetName(thys);
1402 }
1403 
1404 #endif /* AJ_COMPILE_DEPRECATED */
1405