File: simutil.c

package info (click to toggle)
ncbi-tools6 6.1.20090809-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 222,952 kB
  • ctags: 92,767
  • sloc: ansic: 1,297,253; cpp: 6,248; xml: 3,390; sh: 2,981; csh: 488; makefile: 455; perl: 391; lisp: 81
file content (1528 lines) | stat: -rw-r--r-- 34,379 bytes parent folder | download | duplicates (13)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
static char const rcsid[] = "$Id: simutil.c,v 6.8 2003/05/30 17:25:38 coulouri Exp $";

/*  simutil.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name:  simutil.c
*
* Author:  Jinghui Zhang
*
* Version Creation Date: 5/24/95
*
*
* File Description:  functions for using the sim/sim2 algorithm
*
* Modifications:
* --------------------------------------------------------------------------
* Date     Name        Description of modification
*
* $Log: simutil.c,v $
* Revision 6.8  2003/05/30 17:25:38  coulouri
* add rcsid
*
* Revision 6.7  2003/05/13 16:02:54  coulouri
* make ErrPostEx(SEV_FATAL, ...) exit with nonzero status
*
* Revision 6.6  2000/04/28 19:10:47  kans
* check_strand_mol protected against bsp == NULL
*
* Revision 6.5  1999/12/17 20:47:06  egorov
* Fix 'gcc -Wall' warnings
*
* Revision 6.4  1999/03/12 18:38:02  kans
* fixed ErrPostEx problems
*
* Revision 6.3  1999/03/11 23:09:19  kans
* casts for sscanf, sprintf
*
* Revision 6.2  1998/07/21 23:11:52  zjing
* add Uint4 cast to SIZE_MAX
*
 * Revision 5.7  1997/04/11  18:41:46  zjing
 * .
 *
 * Revision 5.6  1997/04/11  18:11:34  zjing
 * fix a bug create_edit_script
 *
 * Revision 5.5  1997/03/13  21:01:28  zjing
 * get rid of new variable
 *
 * Revision 5.4  1996/12/23  18:01:01  zjing
 * .
 *
 * Revision 5.3  1996/06/19  14:45:59  zjing
 * move the function AddAlignInfoToSeqAnnot from pobutil to simutil
 *
 * Revision 4.7  1996/05/06  14:39:20  zjing
 * fix ALU filtering
 *
 * Revision 4.6  1996/04/18  22:33:44  zjing
 * change ck_name_same
 *
 * Revision 4.5  1996/02/21  22:08:40  zjing
 * protection for bizzare sequence
 *
 * Revision 4.4  1995/11/08  17:16:44  zjing
 * .
 *
 * Revision 4.3  1995/10/19  17:06:05  zjing
 * fix in make_sim_seq
 *
 * Revision 4.2  1995/09/28  01:58:49  zjing
 * fix in the function convert
 *
*
*
* ==========================================================================
*/


#ifndef _SIMUTIL_
#include <simutil.h>
#endif


static Int4 get_sim_num(FloatHi num)
{
	FloatHi temp;

	temp = (FloatHi)DIGIT * num;
	return (Int4)temp;
}

Boolean DefaultSimPam(SimPamPtr spp)
{
	if(spp == NULL)
		return FALSE;
	spp->cutoff = SIM2_cutoff;
	spp->mismatch = get_sim_num(SIM2_mismatch);
	spp->gap_open = get_sim_num(SIM2_gap_open);
	spp->gap_ext = get_sim_num(SIM2_gap_ext);
	spp->mismatch_2 = get_sim_num(SIM2_mismatch_2);
	spp->gap_open_2 = get_sim_num(SIM2_gap_open_2);
	spp->gap_ext_2 = get_sim_num(SIM2_gap_ext_2);
	spp->word = SIM2_word;
	return TRUE;
}
	
Boolean DefaultPSimPam(PSimPamPtr pspp)
{
	if(pspp == NULL)
		return FALSE;
	pspp->M_val = DEFAULT_M;
	pspp->I_val = DEFAULT_I;
	pspp->V_val = DEFAULT_V;
	pspp->O = DEFAULT_O;
	pspp->E = DEFAULT_E;
	pspp->pam_file = NULL;
	pspp->def_gap = TRUE;
	return TRUE;
}

/**********************************************************************
*
*	write_sim_output(align, out_name)
*	Create a Seq-annot for the list of align and save the Seq-annot
*	to the file out_name. 
*	FREE the Seq-align and returns a NULL pointer
*
**********************************************************************/
SeqAlignPtr write_sim_output(SeqAlignPtr align, CharPtr out_name)
{
	SeqAnnotPtr annot;
	AsnIoPtr aip;

	if(align)
	{
		annot = SeqAnnotNew();
		annot->type = 2;
		annot->data = align;
		aip = AsnIoOpen(out_name, "w");
		SeqAnnotAsnWrite(annot, aip, NULL);
		AsnIoClose(aip);
		SeqAnnotFree(annot);
		
	}
	
	return NULL;
}


/********************************************************************
*
*	check_strand_mol(loc, is_dna)
*	if the bioseq for loc is DNA, *is_dna = TRUE
*	return TRUE for computing both strand, FALSE for one strand
*
********************************************************************/
Boolean check_strand_mol (SeqLocPtr loc, BoolPtr is_dna)
{
	BioseqPtr bsp;
	
	bsp = BioseqFindCore(SeqLocId(loc));
	*is_dna = (bsp == NULL || bsp->mol != Seq_mol_aa);
	if(SeqLocStrand (loc) == Seq_strand_both)
	{
		if(*is_dna)
			return TRUE;
		else
			Change_Loc_Strand(loc, Seq_strand_plus);
	}

	return FALSE;
}


/******************************************************************
****
*       Change_Loc_Strand(loc, strand) 
*	Change the loc strand to strnad. 
*	only works for SEQLOC_INT. 
*	return FALSE for fail
*
*******************************************************************
***/
Boolean Change_Loc_Strand(SeqLocPtr loc, Uint1 strand)
{

  SeqIntPtr sip;

	if(loc->choice != SEQLOC_INT)
		return FALSE;
        sip = loc->data.ptrvalue;
        sip->strand = strand;
        loc->data.ptrvalue = sip;
	return TRUE;
}



/***********************************************************************
*
*	make_sim_seq(slp, t_seq)
*	return the instatiated sequence made from slp for sim2/sim search. 
*	sequence starts from 1 of the array)
*	if(t_seq !=NULL), load data to t_seq, return t_seq
*	else create a new array.
*	t_seq is used for the saving of time in computing both strand
*
***********************************************************************/
CharPtr make_sim_seq(SeqLocPtr slp, Boolean is_sim2, CharPtr t_seq)
{
        CharPtr seq;
        Int4 length;
        SeqPortPtr spp;
        Uint1 residue;
        Int4 i;
        BioseqPtr bsp; 
        Uint1 code; 
	SeqIntPtr sint;
	Char buf[21];
 
                 
                if(slp->choice != SEQLOC_WHOLE && slp->choice != SEQLOC_INT)
                        return NULL;
                bsp = BioseqFindCore(SeqLocId(slp));
		if(bsp == NULL)
			return NULL;
		if(bsp->mol == Seq_mol_aa)
			code = Seq_code_ncbieaa;
		else
			code = Seq_code_iupacna;
                length = SeqLocLen(slp);
		if(t_seq)
			seq = t_seq;
		else
                	seq = MemNew((size_t)(length+2) * sizeof(Char));
                spp = SeqPortNewByLoc(slp, code);
		/* spp->do_virtual = FALSE; */

		i =1;
                while((residue = SeqPortGetResidue(spp)) != SEQPORT_EOF)
		{
		   if(residue != SEQPORT_EOS && residue != SEQPORT_VIRT)
		   {
			if(IS_ALPHA(residue))
			{
                        	seq[i] = (Char)residue;
                        	if(bsp->mol != Seq_mol_aa && is_sim2) 
                        	{
                                	if (StringChr("ACGT", seq[i]) == NULL)
                                        	seq[i] = 'A';
                        	} 
                        	++i;
			}
			else
			{
				/*printf("residue is %d\n", residue);*/
				seq[i++] = 'A';
			}
		   }
                }
                seq[i] = '\0';
                SeqPortFree(spp);
		if( i != length+1)
		{
			SeqIdWrite(SeqLocId(slp), buf, PRINTID_FASTA_LONG, 20);
			ErrPostEx(SEV_WARNING, 0, 0, "the expected "
			"length %ld is not the same as the real length %ld for %s", (long) length, (long) (i+1), buf);
			if(i < length +1 && slp->choice == SEQLOC_INT)
			{
				sint = slp->data.ptrvalue;
				if(sint->strand == Seq_strand_minus)
					sint->from += (length + 1 - i);
				else
					sint->to -= (length + 1 -i);
			}
			/* if(t_seq == NULL)
				MemFree(seq);
			return NULL; */
		}
                return seq;
 
}



/*************************************************************************
***
*	convert(): convert the x, y into the value for dsp->starts in Dense
*	seg. 
*	if strand == Seq_strand_minus, take the end point of the second 
*	strand as the start instead of the first strand
*	return Score Pointer
**************************************************************************
***/
#define gap(k) ((k) <= 0 ? 0 : (g+hh*(k)))       /* k-symbol indel cost */
static Int4 g, hh;


static ScorePtr load_score(FloatHi score, ScorePtr PNTR scp_head)
{
	ScorePtr scp, curr;
	
  	scp = ScoreNew();
   	scp->choice = 2;                /**it is a real number**/
   	scp->value.realvalue = score;
   	scp->next = NULL;

	if(*scp_head == NULL)
		*scp_head = scp;
	else
	{
		curr = *scp_head;
		while(curr->next != NULL)
			curr = curr->next;
		curr->next = scp;
	}

	return (*scp_head);
}

static ScorePtr convert(Int4Ptr x, Int4Ptr y, Int4Ptr start, Int4Ptr length, Int4 n, SeqLocPtr locx, SeqLocPtr locy, Int2Ptr numseg, FloatHiPtr SegScores)
{
  Int2 i, j, k;
  Int4 len_x, len_y, len_offset;
  ScorePtr scp_head = NULL;
  Uint1 strand;
  Int4 temp;
  Int4 x_start, x_stop, y_start, y_stop;

   strand = SeqLocStrand(locy);
   for(i=0; i<n; ++i){
     x[i] = x[i] -1 + SeqLocStart(locx) ;
     if(strand == Seq_strand_minus)
	y[i] = SeqLocStop(locy) -  (y[i] -1);
     else
        y[i] = y[i] -1 + SeqLocStart(locy) ;
   }

   if(strand == Seq_strand_minus)
   {
	for( i=0; i<n/2; ++i)
	{
		temp = y[2*i];
		y[2*i]= y[2*i+1];
		y[2*i+1] = temp;
	}
   }

   for( i =0, j=0; i<n/2; ++i)
   {
	x_start = x[2*i];
	x_stop = x[2*i+1];
	y_start = y[2*i];
	y_stop = y[2*i+1];

	start[2*j] = x_start;
	start[2*j+1] = y_start;
	length[j] = x_stop - x_start +1;
	if(SegScores != NULL)
		load_score(SegScores[i], &scp_head);
	++j;

	k = i+1;	/*the next segment*/
	if(k < n/2)	/*not the last segment*/
	{
		len_x = x[2*k] - (x_stop +1);
		if(strand == Seq_strand_minus)
			len_y = (y_start -1) - y[2*k+1];
		else
			len_y = y[2*k] - (y_stop +1);
		len_offset = MIN(len_x, len_y);
		if(len_offset > 0)
		{
			x[2*k] -= len_offset;
			if(strand == Seq_strand_minus)
				y[2*k +1] += len_offset;
			else
				y[2*k] -= len_offset;
		}
		if(len_x > len_y)
		{
			start[2*j] = x_stop +1;
			start[2*j+1] = -1;
			length[j] = len_x - len_y;
			++j;
			if(SegScores != NULL)
				load_score(-(FloatHi) gap(len_x - len_y)/DIGIT, &scp_head);
		}
		if(len_y > len_x)
		{
			start[2*j] = -1;
			if(strand == Seq_strand_minus)
				start[2*j+1] = y[2*k+1]+1;
			else
				start[2*j+1] = y_stop +1;	
			length[j] = len_y - len_x;
			++j;
			if(SegScores != NULL)
				load_score(-(FloatHi) gap(len_y - len_x)/DIGIT, &scp_head);
		}
	}
    }
	*numseg = j;
	return scp_head;
}
			

static void create_new_script PROTO((Int4 len, Uint1 type, EditScriptPtr PNTR head, EditScriptPtr PNTR prev));
static EditScriptPtr convert_array_to_EditScript(Int4Ptr x, Int4Ptr y, Int2 n)
{
	EditScriptPtr prev = NULL, head = NULL;
	Int2 i;
	Int4 gap_size, num;
	Uint1 gap_type;

	gap_size = 0;
	num = 0;
	for(i = 0; i<n/2; ++i)
	{
		gap_size = 0;
		if(i > 0)
		{
			if(x[2*i] > x[2*i-1] +1)
			{
				gap_size = x[2*i] - (x[2*i-1] +1);
				gap_type = DEL;
			}
			else if(y[2*i] > y[2*i-1] +1)
			{
				gap_size = y[2*i] - (y[2*i-1] +1);
				gap_type = INS;
			}
		}

		if(gap_size > 0)
		{
			if(num > 0)
			{
				create_new_script(num, SUB, &head, &prev);
				num = 0;
			}
			create_new_script(gap_size, gap_type, &head, &prev);
		}

		num += x[2*i +1] - x[2*i] +1;
		if(i == n/2-1)
			create_new_script(num, SUB, &head, &prev);

	}
	return head;
	
			
}

/***********************************************************************
*
*	make_align(x, y, n, score, SegScores, loc1, loc2)
*	x, y is the two arrays storing the start, stop of the aligned seg
*	for the two sequences
*	score: the score of the alignment
*	SegScores: the scores for each aligned seg
*	loc1: the Seq-loc of the first sequence
*	loc2: the Seq-loc of the second sequence
*
*	return Seq-align made from the above data
*
************************************************************************/
SeqAlignPtr make_align(Int4Ptr x, Int4Ptr y, Int2 n, FloatHi score, FloatHiPtr SegScores, SeqLocPtr loc1, SeqLocPtr loc2)
{
  ScorePtr scp;
  SeqAlignPtr sap;
  Uint1 strand;
  EditScriptPtr esp;

	strand = SeqLocStrand(loc2);
	esp = convert_array_to_EditScript(x, y, n);
	sap = ConvertEditScript(esp, loc1, loc2, x[0]-1, y[0]-1, FALSE);
	Script_free(esp);

	scp = ScoreNew();
	scp->choice = 2;		/**it is a real number**/
	scp->value.realvalue = score;
	scp->next = NULL;
	sap->score = scp;
	sap->next = NULL;
	return sap;
}
		
		
/**********************************************************************
*
*	link_align(new, head)
*	link the new align to the end of head align. return the 
*	start of the linked chain
*
**********************************************************************/
SeqAlignPtr link_align(SeqAlignPtr a_new, SeqAlignPtr head)
{
	SeqAlignPtr curr;

	curr = head;
        if(curr!= NULL){
                while(curr->next != NULL)
                        curr= curr->next;
                curr->next = a_new;
		return head;
        }
	else
        	return a_new;

}


/***********************************************************************
*
*	free_align_list(head)
*	Free all the Seq-align linked to head
*	return NULL
*
**********************************************************************/
SeqAlignPtr free_align_list(SeqAlignPtr head)
{
	SeqAlignPtr next;

	while(head)
	{
		next = head->next;
		head->next = NULL;
		SeqAlignFree(head);
		head = next;
	}
	return NULL;
}


/*****************************************************************
*
*	get_align_score(sap, score)
*	get the score from the alignment. return FALSE if there is 
*	no score.
*	sap: Seq-align
*	score: for laoding the score
*
*****************************************************************/
Boolean get_align_score(SeqAlignPtr sap, FloatHiPtr score)
{

        ScorePtr scp;

        if(sap->score == NULL)
                return FALSE;
        scp = sap->score;
        *score = scp->value.realvalue;
        return TRUE;
}


/*******************************************************************
*
*	sort_align_list(align)
*	sort the list of Seq-align to the descending order of alignment
*	score
*
*******************************************************************/
SeqAlignPtr sort_align_list(SeqAlignPtr align)
{
	SeqAlignPtr cp1, cp2;
	FloatHi score1, score2;
  	Uint1 ttype, tsegtype;
  	Int2 tdim;
  	ScorePtr tscore;
  	Pointer tsegs;


        for (cp1 = align; cp1; cp1 = cp1->next) {
           for (cp2 = cp1->next; cp2; cp2 = cp2->next) {
                get_align_score(cp1, &score1);
                get_align_score(cp2, &score2);
                if (score2 > score1) { /* swap */
                   /* save cp1 */
                   ttype = cp1->type;
                   tsegtype = cp1->segtype;
                   tdim = cp1->dim;
                   tscore = cp1->score;
                   tsegs = cp1->segs;
 
                   /* cp1 <- cp2 */
                   cp1->type = cp2->type;
                   cp1->segtype = cp2->segtype;
                   cp1->dim = cp2->dim;
                   cp1->score = cp2->score;
                   cp1->segs = cp2->segs;
 
                   /* restore cp1 to cp2 */
                   cp2->type = ttype;
                   cp2->segtype = tsegtype;
                   cp2->dim = tdim;
                   cp2->score = tscore;
                   cp2->segs = tsegs;
                }
           }
        }

	return align;
}


/******************************************************************
*
*	get_top_K_alignment(align, K, cut_off)
*	get the top K SCORED alignment and all the scores should be
*	above the cutoff
*	align	Seq-align
*	K	the number of alignment to keep
*	cutoff	the cut off score
*
*******************************************************************/
SeqAlignPtr get_top_K_alignment(SeqAlignPtr align, Int4 K, FloatHi cut_off)
{
	Boolean check_score;
	Int2 i;
	SeqAlignPtr curr, prev;
	FloatHi score;

	check_score = (cut_off > 0.0);

        align = sort_align_list(align);
        /* save the k best alignments and free the others */
        i = 0;
	curr = align;
	prev = NULL;
        while (i<K && curr) {
	   if(check_score)
	   {
           	get_align_score(curr, &score);
           	if (score < cut_off) break;
	   }
           ++i;
	   prev = curr;
           curr= curr->next;
        }
 
        if (curr) { /* free some alignments */
	   if(prev != NULL)
		prev->next = NULL;
	   else
		align = NULL;
	   free_align_list(curr);
	}
	
	return align;
}


/* fatal - print message and die */
void fatal(CharPtr msg)
{
        Message(MSG_FATAL, "%s\n", msg);
}

/* fatalf - format message, print it, and die */
void fatalf(CharPtr msg, CharPtr val)
{
        Message(MSG_FATAL, msg, val);
}

/* ckopen - open file; check for success */
FILE *ckopen(CharPtr name, CharPtr mode)
{
        FILE *fp;
 
        if ((fp = FileOpen(name, mode)) == NULL) {
                Message(MSG_ERROR,"Can not open %Fs", name);
                fatalf("Cannot open %s.", name);
        }
        return(fp);
}        
 
/* ckalloc - allocate space; check for success */
CharPtr ckalloc(Int4 amount)
{
        CharPtr p;

                   
        if ((Uint4)amount > (Uint4)SIZE_MAX)
        {
                ErrPostEx(SEV_FATAL, 1,0,"Sorry. Can't allocate that much (%ld), %ld", (long) amount, (long) SIZE_MAX);
		exit(1);
        }
 
        if ((p = MemNew( (size_t) amount)) == NULL)
                fatal("Ran out of memory.");
        return(p);
}
 
/* strsame - tell whether two strings are identical */
Int4 strsame(CharPtr s, CharPtr t)
{
        return(strcmp(s, t) == 0);
}


	
				
/********************************************************************
*
*	LoadNewEnds()
*	load the related RecordEnds info to the structrue and link it 
*	to the header of the list. (This is only for convienience)
*
*********************************************************************/
ValNodePtr LoadNewEnds(Int4 start, Int4 stop, Uint1 strand, FloatHi score, FloatHi zscore, CharPtr seq_name, Boolean sort_zscore, ValNodePtr PNTR ends_list)
{
	ValNodePtr c_new, prev, curr, next;
	RecordEndsPtr rep, newrep;
	FloatHi tscore, cscore;


	newrep = MemNew(sizeof(RecordEnds));
	newrep->start = start;
	newrep->stop = stop;
	newrep->strand = strand;
	newrep->score = score;
	newrep->zscore = zscore;
	newrep->gscore = 0.0;
	newrep->count = 1;
	StringCpy(newrep->name, seq_name);
	c_new = ValNodeNew(NULL);
	c_new->data.ptrvalue = newrep;

	if(*ends_list == NULL)
	{
		*ends_list = c_new;
		return c_new;
	}

	prev = NULL;
	tscore = (sort_zscore ? (newrep->zscore) : score);
	for(curr = *ends_list; curr != NULL; curr = curr->next)
	{
		rep = (RecordEndsPtr)(curr->data.ptrvalue);
		cscore = (sort_zscore ? (rep->zscore) : (rep->score));
		if(tscore > cscore)
		{
			if(prev == NULL)
			{
				next = curr;
				*ends_list = c_new;
			}
			else
			{
				next = prev->next;
				prev->next = c_new;
			}
			c_new->next = next;
			return (*ends_list);
		}
		prev = curr;
	}
	prev->next = c_new;
	return (*ends_list);
}


/*******************************************************************
*
*	getp_top_K(head, K)
*	get only the top K of the list, free the others. 
*	The top K is simplily determined by the order in the list
*
********************************************************************/
ValNodePtr get_top_K(ValNodePtr head, Int4 K)
{
	Int4 i;
	ValNodePtr curr, list;

	for(i =0, list=head; i<K && list!=NULL; list = list->next)
	{
		++i;
		if(K ==i)
		{
			curr = list->next;
			list->next = NULL;
			ValNodeFreeData(curr);
			return head;
		}
	}
	return head;
}

static FloatHi re_cal_score(RecordEndsPtr o_rep, RecordEndsPtr n_rep)
{
	Int4 o_len, n_len;
	FloatHi temp;

	o_len = o_rep->stop - o_rep->start +1;
	n_len = n_rep->stop - n_rep->start +1;

	temp = (FloatHi)(o_len) * o_rep->score + (FloatHi)(n_len) * n_rep->score;
	return temp/(FloatHi)(o_len+n_len);
}

static Boolean merge_list(Int4 o_start, Int4 o_stop, Int4 n_start, Int4 n_stop)
{
	/*Int4 min_start, min_stop, min_len;*/

	if(o_stop < n_start || o_start > n_stop)
		return FALSE;
	else
		return TRUE;
	/*if(!(o_stop < n_start || o_start > n_stop))
	{
		min_start = MAX(o_start, n_start);
		min_stop = MIN(o_stop, n_stop);
		min_len = min_stop - min_start;
		if((FloatHi)(min_len)/(FloatHi)(o_stop-o_start) > 0.6)
			return TRUE;
		if((FloatHi)(min_len)/(FloatHi)(n_stop-n_start) > 0.6)
			return TRUE;
	}
	return FALSE;*/
}
		

static Boolean is_alu_seq(CharPtr name)
{
	if(!StrStr(name, "MER"))
		if(!StrStr(name, "L1"))
			return TRUE;
	return FALSE;
}
static Boolean ck_name_same(CharPtr name1, CharPtr name2)
{
	/* if(StringCmp(name1, name2) == 0)
		return TRUE;
	
	if(is_alu_seq(name1) && is_alu_seq(name2))
	{
		StringCpy(name1, "Alu");
		return TRUE;
	}
	else
		return FALSE; */
	return (StringCmp(name1, name2) == 0);
}


/********************************************************************
*
*	CleanNewList(newlist)
*	merge any overlappign RecordEnds in the current list
*
********************************************************************/
ValNodePtr CleanNewList(ValNodePtr newlist)
{
	ValNodePtr c_new, prev;
	RecordEndsPtr o_rep, n_rep;
	Int4 n_start, n_stop, o_start, o_stop;
	Uint1 n_strand, o_strand;
	Boolean found;

	newlist = SortEndsList(newlist);
	prev = NULL;
	for(c_new = newlist; c_new != NULL; )
	{
		if(prev == NULL)
		{
			prev = c_new;
			c_new = c_new->next;
		}
		else
		{
			n_rep = (RecordEndsPtr)(c_new->data.ptrvalue);	
			n_start = n_rep->start;
			n_stop = n_rep->stop;
			n_strand = n_rep->strand;

			o_rep = (RecordEndsPtr)(prev->data.ptrvalue);	
			o_start = o_rep->start;
			o_stop = o_rep->stop;
			o_strand = o_rep->strand;
			found = FALSE;
			if((n_strand == o_strand) && ck_name_same(o_rep->name, n_rep->name))
			{
			   if(merge_list(o_start, o_stop, n_start, n_stop))
			   {
				prev->next = c_new->next;
				c_new->next = NULL;
				o_rep->start = MIN(o_start, n_start);
				o_rep->stop = MAX(o_stop, n_stop);
				o_rep->zscore = MAX(o_rep->zscore, n_rep->zscore);
				o_rep->score = MAX(o_rep->score, n_rep->score);
				ValNodeFreeData(c_new);
				c_new = prev->next;
				found = TRUE;
			    }
			}
			if(!found)
			{
				prev = c_new;
				c_new = c_new->next;
			}
		}
	}

	return newlist;
}


/******************************************************************
*
*	merge_two_list(oldlist, newlist)
*	merge any overlapping piece of RecordEnds in the two list
*	link the new ends to the oldlist
*	resort the oldlist with the region
*	return the head of the oldlist
*
*******************************************************************/	
ValNodePtr merge_two_list(ValNodePtr oldlist, ValNodePtr newlist)
{
	ValNodePtr c_old, c_new, prev, next;
	RecordEndsPtr o_rep, n_rep;
	Int4 n_start, n_stop, o_start, o_stop;
	Uint1 n_strand, o_strand;
	Boolean found;

	newlist = CleanNewList(newlist);
				
	if(oldlist == NULL)
		return newlist;

	
	prev = NULL;
	for(c_new = newlist; c_new != NULL; )
	{
		n_rep = (RecordEndsPtr)(c_new->data.ptrvalue);	
		n_start = n_rep->start;
		n_stop = n_rep->stop;
		n_strand = n_rep->strand;
		found = FALSE;
		for(c_old = oldlist; c_old!=NULL && !found; c_old=c_old->next)
		{
			o_rep = (RecordEndsPtr)(c_old->data.ptrvalue);	
			o_start = o_rep->start;
			o_stop = o_rep->stop;
			o_strand = o_rep->strand;
			if((n_strand == o_strand) && ck_name_same(o_rep->name, n_rep->name))
			{
			   if(merge_list(o_start, o_stop, n_start, n_stop))
			   {
				found = TRUE;
				o_rep->start = MIN(o_start, n_start);
				o_rep->stop = MAX(o_stop, n_stop);
				/*o_rep->score = re_cal_score(o_rep, n_rep);*/
				o_rep->zscore = MAX(o_rep->zscore, n_rep->zscore);
				o_rep->score = MAX(o_rep->score, n_rep->score);
				++(o_rep->count);
				next = c_new->next;
				if(prev == NULL)
					newlist = next;
				else
					prev->next = next;
				c_new->next=NULL;
				ValNodeFreeData(c_new);
				c_new = next;
			   }
			}
		}
		if(!found)
		{
			prev = c_new;
			c_new = c_new->next;
		}
	}

	if(oldlist == NULL)
		oldlist = newlist;
	else
	{
		c_old = oldlist;
		while(c_old->next != NULL)
			c_old = c_old->next;
		c_old->next = newlist;
	}
	/*ValNodeLink(&oldlist, newlist);*/
	return SortEndsList(oldlist);
}
	

		

			
	
static int LIBCALLBACK ValNodeCompProc (VoidPtr ptr1, VoidPtr ptr2)
{
  ValNodePtr  vnp1, vnp2;
  RecordEndsPtr rep1, rep2;

  if (ptr1 != NULL && ptr2 != NULL) {
    vnp1 = *((ValNodePtr PNTR) ptr1);
    vnp2 = *((ValNodePtr PNTR) ptr2);
    if (vnp1 != NULL && vnp2 != NULL) {
	rep1 = vnp1->data.ptrvalue;
	rep2 = vnp2->data.ptrvalue;
      if (rep1 != NULL && rep2 != NULL) {
        if (rep1->start > rep2->start) {
          return 1;
        } else if (rep1->start < rep2->start) {
          return -1;
        } else if (rep1->stop > rep2->stop) {
          return 1;
        } else if (rep1->stop < rep2->stop) {
          return -1;
        } else {
          return 0;
        }
      } else {
        return 0;
      }
    } else {
      return 0;
    }
  } else {
    return 0;
  }
}


/**********************************************************************
*
*	SortEndsList(vnp)
*	sort the list of RecordEnds to the ascending order of the 
*	LOCATION
*
**********************************************************************/
ValNodePtr SortEndsList(ValNodePtr vnp)
{
	ValNodePtr tmp, PNTR head;
	Int4 num, i;

	for(tmp =vnp, num=0; tmp!=NULL; tmp=tmp->next)
		++num;
		 
	if(num >1 && vnp)
	{
		head = MemNew((size_t)(num+1) * sizeof (ValNodePtr));
		i=0;
		tmp = vnp;
		while(tmp != NULL && i< num)
		{
			head[i] = tmp;
			tmp = tmp->next;
			++i;
		}

		HeapSort(head, (size_t)num, sizeof(ValNodePtr), ValNodeCompProc);
		for(i =0; i<num; ++i)
		{
			tmp = head[i];
			tmp->next = head[i+1];
		}
		vnp = head[0];
		MemFree(head);
	}
	return vnp;

}


/***********************************************************************
*
*	parse_buf_val(buf, seq_name, start, stop, strand)
*	parse the information stored in the text (buf) into the data 
*	that can be used to make a Seq-loc
*	
***********************************************************************/
static Boolean parse_buf_val(CharPtr buf, CharPtr seq_name, Int4Ptr start, Int4Ptr stop, Uint1Ptr strand)
{
	CharPtr curr;
	Int2 i=0;
	long t_start =-1, t_stop =-1;

	*start = 0;
	*stop = -1;
	*strand = Seq_strand_both;
	if(!StrStr(buf, "|"))
		StringCpy(seq_name, buf);
	else
	{
		i =0;
		curr = StrTok(buf, "|");
		while(curr != NULL)
		{
			switch (i)
			{
				case 0:
					StringCpy(seq_name, curr);
					break;
				case 1:
					sscanf(curr, "%ld", &t_start);
					break;
				case 2:
					sscanf(curr, "%ld", &t_stop);
					break;
				case 3:
					if(curr !=NULL)
					{
						if(*curr == '+')
							*strand = Seq_strand_plus;
						if(*curr == '-')
							*strand = Seq_strand_minus;
					}
					break;
				default:
					break;
			}
			curr = StrTok(NULL, "|");
			++i;
		}
	}
	if(t_start > 0)
		--t_start;
	if(t_stop > 0)
		--t_stop;
	if(t_start >=0 && t_stop >=0)
	{
		*start = (Int4) MIN(t_start, t_stop);
		*stop = (Int4) MAX(t_start, t_stop);
	}


	return TRUE;

}


static Boolean get_local_seq_name(CharPtr seq_name, CharPtr name)
{
	Int4 len, i;
	CharPtr str;

	len = StringLen(seq_name);
	str = seq_name + len -1;
	i = 0;
	while(*str != '.')
	{
		++i;
		if(i == len)
			return FALSE;
		--str;
	}

	StringNCpy(name, seq_name, (len-i-1));
	name[len-i-1] = '\0';

	return TRUE;
}

/******************************************************************
*
*	prepare_align_data(buf, bsp)
*
*	convert data in buf in the format used by SIM ans SIM2 to a 
*	Seq-loc in a Bioseq
*	buf: a special formatted buffer which can be converted into 
*	a Seq-loc
*	bsp: store the Bioseq data
*	return the Seq-loc made from the buf. NULL if the format is 
*	not correct or the Bioseq is NULL
*
******************************************************************/
SeqLocPtr prepare_align_data(CharPtr buf, BioseqPtr PNTR bsp)
{
	Char seq_name[100], name[100];
	Int4 gi;
	BioseqPtr cbsp;
	SeqIdPtr sip;
	Uint1 k;

	ObjectIdPtr obj_id;
	TextSeqIdPtr tsip;
	Int4 start, stop;
	Int4 temp;
	Uint1 strand;


	parse_buf_val(buf, seq_name, &start, &stop, &strand);
	if(!StrStr(seq_name, "."))	/*no extension, assume GenBank entry*/
	{
		gi = atol(seq_name);
		if(gi == 0)	/*not a gi, assumes it is an accession*/
		{
			sip = ValNodeNew(NULL);
			tsip = TextSeqIdNew();
			for(k = SEQID_GENBANK; k<= SEQID_SWISSPROT; ++k)
			{
				sip->choice = k;
				sip->data.ptrvalue = tsip;
				if(tsip->accession != NULL)
					tsip->accession = MemFree(tsip->accession);
				if(tsip->name != NULL)
					tsip->name = MemFree(tsip->name);
				tsip->accession = StringSave(seq_name);
				cbsp = BioseqLockById(sip);
				if(cbsp == NULL)
				{
					MemFree(tsip->accession);
					tsip->accession = NULL;
					tsip->name = StringSave(seq_name);
					cbsp = BioseqLockById(sip);
				}
				if(cbsp != NULL)
					break;
			}
		}
		else
		{
			sip = ValNodeNew(NULL);
			sip->choice = SEQID_GI;
			sip->data.intvalue = gi;
			cbsp = BioseqLockById(sip);
		}
	}
	else	/*treat it as a local file*/
	{
		get_local_seq_name(seq_name, name);
		sip = ValNodeNew(NULL);
		sip->choice = SEQID_LOCAL;
		obj_id = ObjectIdNew();
		obj_id->str = StringSave(name);
		sip->data.ptrvalue = obj_id;
		cbsp = BioseqLockById(sip);
	}

	SeqIdFree(sip);
	if(cbsp == NULL)
		return NULL;

	*bsp = cbsp;
	if(stop == -1)
		stop = cbsp->length -1;
	if(stop < start)
	{
		temp = start;
		start = stop;
		stop = temp;
	}
#ifdef WIN16	/*check the interval with MS_WIN*/
		if((stop - start) > MAX_WINLEN)
		{
			Message(MSG_OK, "The alignment length is chopped to %ld", MAX_WINLEN);
			stop = start + MAX_WINLEN-1;
		}
#endif

	sip = SeqIdFindBest(cbsp->id, SEQID_GI);
	if(sip == NULL || sip->choice != SEQID_GI)
		sip = SeqIdFindBest(cbsp->id, SEQID_LOCAL);
	if(sip == NULL)
		sip = cbsp->id;
	return SeqLocIntNew(start, stop, strand, sip);
}



static void create_new_script(Int4 len, Uint1 type, EditScriptPtr PNTR head, 
	EditScriptPtr PNTR prev)
{
	EditScriptPtr curr;

	curr = MemNew(sizeof(EditScript));
	curr->op_type = type;
	curr->num = ABS(len);

	if(*head == NULL)
		*head = curr;
	else
		(*prev)->next = curr;
	*prev = curr;
}

EditScriptPtr CreateEditScript (Int4Ptr S, Int4 M, Int4 N)
{
	Int4 i, j;
	Int4 num;
	EditScriptPtr prev, head;

	head = NULL;
	prev = NULL;
	i = j = 0;
	num = 0;
	while(i < M || j < N)
	{
		if(*S == 0)
		{
			++i;
			++j;
			++num;
		}
		else
		{
			if(num > 0)
			{
				create_new_script(num, SUB, &head, &prev); 
				num = 0;
			}
			if(*S > 0)
			{
				create_new_script(*S, INS, &head, &prev);
				j += *S;
			}
			else
			{
				create_new_script(*S, DEL, &head, &prev);
				i+= ABS(*S);
			}
		}

		++S;
	}

	return head;
}


EditScriptPtr Script_free(EditScriptPtr head)
{
	EditScriptPtr tp, tp1;

	tp = head;
	while (tp != NULL) {
	   tp1 = tp->next;
	   MemFree(tp);
	   tp = tp1;
	}
	return NULL;
}


static Int4 get_start_pos(SeqLocPtr slp)
{
	if(SeqLocStrand(slp) == Seq_strand_minus)
		return (-SeqLocStop(slp));
	else
		return SeqLocStart(slp);
}

static Int4 get_current_pos(Int4Ptr pos, Int4 length)
{
	Int4 val;
	if(*pos < 0)
		val = -(*pos + length -1);
	else
		val = *pos;
	*pos += length;
	return val;
}

static Boolean is_end_seg(EditScriptPtr curr, EditScriptPtr head)
{
	EditScriptPtr next, prev;
	Boolean prev_sub_seg = FALSE;

	if(curr == head || curr->next == NULL)
		return TRUE;
	for(prev = head; prev != curr; prev = prev->next)
	{
		if(prev->op_type == SUB)
		{
			prev_sub_seg = TRUE;
			break;
		}
	}
	if(prev_sub_seg == FALSE)
		return TRUE;

	if(curr != NULL && curr->op_type != SUB)
	{
		next = curr->next;
		while(next)
		{
			if(next->op_type == SUB)
				return FALSE;
			next = next->next;
		}
	}
	return TRUE;
}

		

SeqAlignPtr ConvertEditScript(EditScriptPtr esp, SeqLocPtr slp1, SeqLocPtr slp2, Int4 offset_1, Int4 offset_2, Boolean show_end_gap)
{
	/*ScorePtr scp;	 */
	Int2 numseg = 0, i;
	EditScriptPtr curr;

	Int4Ptr starts, length;
	Int4 start_1, start_2;
	DenseSegPtr dsp;
	SeqAlignPtr sap;

	Uint1Ptr a_strands;
	Boolean is_end;

	if(esp == NULL || slp1 == NULL || slp2 == NULL)
		return NULL;

	for(curr = esp; curr != NULL; curr = curr->next)
	{
		if(show_end_gap)
			++numseg;
		else
		{
			if(!is_end_seg(curr, esp) || curr->op_type == SUB)
				++numseg;
		}
	}
	if(numseg == 0)
	{
		Message(MSG_POSTERR, "Only contains gaps");
		return NULL;
	}
		

	starts = MemNew((size_t)(2*numseg) * sizeof(Int4));
	a_strands = MemNew((size_t)(2*numseg) * sizeof(Uint1));
	length = MemNew((size_t)numseg * sizeof(Int4));

	start_1 = get_start_pos(slp1) + offset_1;
	start_2 = get_start_pos(slp2) + offset_2;
	i = 0;
	for(curr = esp; curr != NULL; curr = curr->next)
	{
		if(show_end_gap)
			is_end = FALSE;
		else
			is_end = is_end_seg(curr, esp);
		switch(curr->op_type)
		{
			case SUB:
				starts[2*i] = get_current_pos(&start_1, curr->num);
				starts[2*i+1] = get_current_pos(&start_2, curr->num);
				break;

			case DEL:
				if(is_end)
					get_current_pos(&start_1, curr->num);
				else
				{
					starts[2*i] = get_current_pos(&start_1, curr->num);
					starts[2*i+1] = -1;
				}
				break;

			case INS:
				if(is_end)
					get_current_pos(&start_2, curr->num);
				else
				{
					starts[2*i] = -1;
					starts[2*i+1] = get_current_pos(&start_2, curr->num);
				}
				break;
			default:
				break;
		}

		if(!is_end || curr->op_type == SUB)
		{
			length[i] = curr->num;
			a_strands[2*i] = SeqLocStrand(slp1);
			a_strands[2*i+1] = SeqLocStrand(slp2);
			++i;
		}
	}


	sap = SeqAlignNew();

	sap->type = 3; /**the type in this SeqAlignObject are partial**/
	sap->segtype =2; /**use denseg to store the alignment**/
	sap->dim =2; /**only two dimention alignment**/

	/*scp = ScoreNew();
	scp->choice = 2;		
	scp->value.realvalue = score;
	scp->next = NULL;
	sap->score = scp;*/
  
	/**make the Denseg Object for SeqAlign**/
	dsp = DenseSegNew();
	dsp->dim = 2;
	dsp->numseg = numseg;
	dsp->ids = SeqIdDup(SeqLocId(slp1));
	dsp->ids->next = SeqIdDup(SeqLocId(slp2));
	dsp->starts = starts;
	dsp->strands = a_strands;
	dsp->lens = length;
	sap->segs = dsp;
	sap->next = NULL;

	return sap;
}