Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from __future__ import unicode_literals 

# we start with basic alignment functions 

def globalign( 

seqA, 

seqB, 

M, # length of seqA 

N, # length of seqB 

gop, 

scale, 

scorer 

): 

""" 

Carry out global alignment of two sequences. 

 

Parameters 

---------- 

seqA, seqB : list 

The sequences to be aligned, passed as lists. 

M, N : int 

The length of the two sequences. 

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

 

Returns 

------- 

alignment : tuple 

The aligned sequences and the similarity score. 

 

Notes 

----- 

This algorithm carries out classical Needleman-Wunsch alignment 

(:evobib:`Needleman1970`). 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.semi_globalign 

~lingpy.algorithm.cython.talign.localign 

~lingpy.algorithm.cython.talign.dialign 

 

""" 

 

# declare integers 

# [autouncomment] cdef int i,j 

 

# declare floats 

# [autouncomment] cdef float gapA,gapB,match,sim 

 

# declare lists 

almA = [] 

almB = [] 

 

# create matrix and traceback 

matrix = [[0.0 for i in range(M+1)] for j in range(N+1)] 

traceback = [[0 for i in range(M+1)] for j in range(N+1)] 

 

# modify matrix and traceback 

traceback[0][0] = 1 

for i in range(1,M+1): 

matrix[0][i] = matrix[0][i-1] + gop * scale 

traceback[0][i] = 2 

for i in range(1,N+1): 

matrix[i][0] = matrix[i-1][0] + gop * scale 

traceback[i][0] = 3 

 

# start the loop 

for i in range(1,N+1): 

for j in range(1,M+1): 

 

# calculate costs for gapA 

if traceback[i-1][j] == 3: 

gapA = matrix[i-1][j] + gop * scale 

else: 

gapA = matrix[i-1][j] + gop 

 

# calculate costs for gapB 

if traceback[i][j-1] == 2: 

gapB = matrix[i][j-1] + gop * scale 

else: 

gapB = matrix[i][j-1] + gop 

 

# get the score 

match = matrix[i-1][j-1] + scorer[seqA[j-1],seqB[i-1]] 

 

# determine minimal cost 

if gapA > match and gapA >= gapB: 

matrix[i][j] = gapA 

traceback[i][j] = 3 

elif match >= gapB: 

matrix[i][j] = match 

traceback[i][j] = 1 

else: 

matrix[i][j] = gapB 

traceback[i][j] = 2 

 

# get the similarity 

sim = matrix[N][M] 

 

# carry out the traceback 

while i > 0 or j > 0: 

if traceback[i][j] == 3: 

almA += ['-'] 

almB += [seqB[i-1]] 

i -= 1 

elif traceback[i][j] == 1: 

almA += [seqA[j-1]] 

almB += [seqB[i-1]] 

i -= 1 

j -= 1 

else: 

almA += [seqA[j-1]] 

almB += ['-'] 

j -= 1 

 

# turn alignments back 

almA,almB = almA[::-1],almB[::-1] 

 

# return alignments 

return almA,almB,sim 

 

def semi_globalign( 

seqA, 

seqB, 

M, # length of seqA 

N, # length of seqB 

gop, 

scale, 

scorer 

): 

""" 

Carry out semi-global alignment of two sequences. 

 

Parameters 

---------- 

seqA, seqB : list 

The sequences to be aligned, passed as lists. 

M, N : int 

The length of the two sequences. 

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

 

Returns 

------- 

alignment : tuple 

The aligned sequences and the similarity score. 

 

Notes 

----- 

This algorithm carries out semi-global alignment  

(:evobib:`Durbin2002`). 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.globalign 

~lingpy.algorithm.cython.talign.localign 

~lingpy.algorithm.cython.talign.dialign 

 

""" 

 

# declare integers 

# [autouncomment] cdef int i,j 

 

# declare floats 

# [autouncomment] cdef float gapA,gapB,match,sim 

 

# declare lists 

almA = [] 

almB = [] 

 

# create matrix and traceback 

matrix = [[0.0 for i in range(M+1)] for j in range(N+1)] 

traceback = [[0 for i in range(M+1)] for j in range(N+1)] 

 

# modify matrix and traceback 

traceback[0][0] = 1 

for i in range(1,M+1): 

traceback[0][i] = 2 

for i in range(1,N+1): 

traceback[i][0] = 3 

 

# start the loop 

for i in range(1,N+1): 

for j in range(1,M+1): 

 

# calculate costs for gapA 

if j == M: 

gapA = matrix[i-1][j] 

elif traceback[i-1][j] == 3: 

gapA = matrix[i-1][j] + gop * scale 

else: 

gapA = matrix[i-1][j] + gop 

 

# calculate costs for gapB 

if i == N: 

gapB = matrix[i][j-1] 

elif traceback[i][j-1] == 2: 

gapB = matrix[i][j-1] + gop * scale 

else: 

gapB = matrix[i][j-1] + gop 

 

# calculate costs for match 

 

# get the score 

match = matrix[i-1][j-1] + scorer[seqA[j-1],seqB[i-1]] 

 

# determine minimal cost 

if gapA > match and gapA >= gapB: 

matrix[i][j] = gapA 

traceback[i][j] = 3 

elif match >= gapB: 

matrix[i][j] = match 

traceback[i][j] = 1 

else: 

matrix[i][j] = gapB 

traceback[i][j] = 2 

 

# get the similarity 

sim = matrix[N][M] 

 

# carry out the traceback 

while i > 0 or j > 0: 

if traceback[i][j] == 3: 

almA += ['-'] 

almB += [seqB[i-1]] 

i -= 1 

elif traceback[i][j] == 1: 

almA += [seqA[j-1]] 

almB += [seqB[i-1]] 

i -= 1 

j -= 1 

else: 

almA += [seqA[j-1]] 

almB += ['-'] 

j -= 1 

 

# turn alignments back 

almA,almB = almA[::-1],almB[::-1] 

 

# return alignments 

return almA,almB,sim 

 

def localign( 

seqA, 

seqB, 

M, # length of seqA 

N, # length of seqB 

gop, 

scale, 

scorer 

): 

""" 

Carry out semi-global alignment of two sequences. 

 

Parameters 

---------- 

seqA, seqB : list 

The sequences to be aligned, passed as lists. 

M, N : int 

The length of the two sequences. 

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

 

Returns 

------- 

alignment : tuple 

The aligned sequences and the similarity score. 

 

Notes 

----- 

This algorithm carries out local alignment  

(:evobib:`Smith1981`). 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.globalign 

~lingpy.algorithm.cython.talign.semi_globalign 

~lingpy.algorithm.cython.talign.dialign 

""" 

 

# declare integers 

# [autouncomment] cdef int i,j,k,l 

 

# declare floats 

# [autouncomment] cdef float gapA,gapB,match,sim 

 

# declare char-character 

# [autouncomment] cdef str x 

 

# declare lists 

almA = [] 

almB = [] 

 

# create matrix and traceback 

matrix = [[0.0 for i in range(M+1)] for j in range(N+1)] 

traceback = [[0 for i in range(M+1)] for j in range(N+1)] 

 

# set similarity to zero 

sim = 0.0 

 

# start the loop 

for i in range(1,N+1): 

for j in range(1,M+1): 

 

# calculate costs for gapA 

if traceback[i-1][j] == 3: 

gapA = matrix[i-1][j] + gop * scale 

else: 

gapA = matrix[i-1][j] + gop 

 

# calculate costs for gapB 

if traceback[i][j-1] == 2: 

gapB = matrix[i][j-1] + gop * scale 

else: 

gapB = matrix[i][j-1] + gop 

 

# calculate costs for match 

 

# get the score 

match = matrix[i-1][j-1] + scorer[seqA[j-1],seqB[i-1]] 

 

# determine minimal cost 

if gapA >= match and gapA >= gapB and gapA >= 0.0: 

matrix[i][j] = gapA 

traceback[i][j] = 3 

elif match >= gapB and match >= 0.0: 

matrix[i][j] = match 

traceback[i][j] = 1 

elif gapB >= 0.0: 

matrix[i][j] = gapB 

traceback[i][j] = 2 

else: 

matrix[i][j] = 0.0 

traceback[i][j] = 0 

 

if matrix[i][j] >= sim: 

sim = matrix[i][j] 

k = i 

l = j 

 

# get the similarity 

sim = matrix[k][l] 

 

# reset i,j 

i,j = k,l 

 

# append stuff to almA and almB 

almA += [[x for x in seqA[j:]]] 

almB += [[x for x in seqB[i:]]] 

 

# append empty seq for alms to almA and almB 

almA += [[]] 

almB += [[]] 

 

while traceback[i][j] != 0: 

if traceback[i][j] == 3: 

 

almA[1] += ['-'] 

almB[1] += [seqB[i-1]] 

i -= 1 

elif traceback[i][j] == 1: 

almA[1] += [seqA[j-1]] 

almB[1] += [seqB[i-1]] 

i -= 1 

j -= 1 

 

elif traceback[i][j] == 2: 

almA[1] += [seqA[j-1]] 

almB[1] += ['-'] 

j -= 1 

else: 

break 

 

# revert the alms 

almA[1] = almA[1][::-1] 

almB[1] = almB[1][::-1] 

 

# append the rest 

almA += [[x for x in seqA[0:j]]] 

almB += [[x for x in seqB[0:i]]] 

 

# return alignments 

return almA[::-1],almB[::-1],sim 

 

def dialign( 

seqA, 

seqB, 

M, # length of seqA 

N, # length of seqB 

scale, 

scorer 

): 

""" 

Carry out dialign alignment of two sequences. 

 

Parameters 

---------- 

seqA, seqB : list 

The sequences to be aligned, passed as lists. 

M, N : int 

The length of the two sequences. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

 

Returns 

------- 

alignment : tuple 

The aligned sequences and the similarity score. 

 

Notes 

----- 

This algorithm carries out dialign alignment  

(:evobib:`Morgenstern1996`). 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.globalign 

~lingpy.algorithm.cython.talign.semi_globalign 

~lingpy.algorithm.cython.talign.localign 

""" 

 

# declare integers 

# [autouncomment] cdef int i,j,k,l,o,p 

 

# declare floats 

# [autouncomment] cdef float gapA,gapB,match,sim,tmp_match 

 

# declare lists 

almA = [] 

almB = [] 

 

# create matrix and traceback 

matrix = [[0.0 for i in range(M+1)] for j in range(N+1)] 

traceback = [[0 for i in range(M+1)] for j in range(N+1)] 

 

# modify matrix and traceback 

traceback[0][0] = 1 

for i in range(1,M+1): 

traceback[0][i] = 2 

for i in range(1,N+1): 

traceback[i][0] = 3 

 

# start the loop 

for i in range(1,N+1): 

for j in range(1,M+1): 

 

# calculate costs for gapA 

gapA = matrix[i-1][j] 

 

# calculate costs for gapB 

gapB = matrix[i][j-1] 

 

# calculate costs for match 

sim = 0.0 

o = 1 

for k in range(min(i,j)): 

match = matrix[i-k-1][j-k-1] 

for l in range(k,-1,-1): 

# get temporary match 

match += scorer[seqA[j-l-1],seqB[i-l-1]] 

 

p = k+1 

if match > sim: 

sim = match 

o = p 

 

# determine minimal cost 

if gapA > match and gapA >= gapB: 

matrix[i][j] = gapA 

traceback[i][j] = 3 

elif match >= gapB: 

matrix[i][j] = match 

traceback[i][j] = 1 

else: 

matrix[i][j] = gapB 

traceback[i][j] = 2 

 

# get the similarity 

sim = matrix[N][M] 

 

# carry out the traceback 

while i > 0 or j > 0: 

if traceback[i][j] == 3: 

almA += ['-'] 

almB += [seqB[i-1]] 

i -= 1 

elif traceback[i][j] == 1: 

almA += [seqA[j-1]] 

almB += [seqB[i-1]] 

i -= 1 

j -= 1 

else: 

almA += [seqA[j-1]] 

almB += ['-'] 

j -= 1 

 

# turn alignments back 

almA,almB = almA[::-1],almB[::-1] 

 

# return alignments 

return almA,almB,sim 

 

def align_pair( 

seqA, 

seqB, 

gop, 

scale, 

scorer, 

mode, 

distance = 0 

): 

""" 

Align a pair of sequences. 

 

Parameters 

---------- 

seqA, seqB : list 

The sequences to be aligned, passed as lists. 

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

mode : { "global", "local", "overlap", "dialign" } 

Select the mode for the alignment analysis ("overlap" refers to 

semi-global alignments). 

distance : (default=0) 

Select whether you want distances or similarities to be returned (0 

indicates similarities, 1 indicates distances, 2 indicates both). 

 

Returns 

------- 

alignment : tuple 

The aligned sequences and the similarity score. 

 

Notes 

----- 

This is a utility function that allows calls any of the four classical 

alignment functions (:py:class:`lingpy.algorithm.cython.talign.globalign` 

:py:class:`lingpy.algorithm.cython.talign.semi_globalign`, 

:py:class:`lingpy.algorithm.cython.talign.lotalign`, 

:py:class:`lingpy.algorithm.cython.talign.dialign`,) and their secondary counterparts. 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.align_pairwise 

~lingpy.algorithm.cython.talign.align_pairs 

 

Returns 

------- 

alignment : tuple 

The aligned sequences and the similarity or distance scores, or both. 

 

""" 

# define basic types 

# [autouncomment] cdef int i 

# [autouncomment] cdef list almA,almB 

# [autouncomment] cdef float sim,dist,simA,simB 

 

# get length of seqA,seqB 

M = len(seqA) 

N = len(seqB) 

 

# determine the mode 

if mode == "global": 

 

# carry out the alignment 

almA,almB,sim = globalign( 

seqA, 

seqB, 

M, 

N, 

gop, 

scale, 

scorer 

) 

 

elif mode == "local": 

 

# carry out the alignment 

almA,almB,sim = localign( 

seqA, 

seqB, 

M, 

N, 

gop, 

scale, 

scorer 

) 

 

elif mode == "overlap": 

 

# carry out the alignment 

almA,almB,sim = semi_globalign( 

seqA, 

seqB, 

M, 

N, 

gop, 

scale, 

scorer 

) 

 

elif mode == "dialign": 

almA,almB,sim = dialign( 

seqA, 

seqB, 

M, 

N, 

scale, 

scorer 

) 

 

# calculate distance, if this is needed 

if distance > 0: 

simA = sum([scorer[seqA[i],seqA[i]] for i in range(M)]) 

simB = sum([scorer[seqB[i],seqB[i]] for i in range(N)]) 

 

dist = 1 - ( ( 2 * sim ) / ( simA + simB ) ) 

if distance == 1: 

return almA,almB,dist 

else: 

return almA,almB,sim,dist 

else: 

return almA,almB,sim 

 

def align_pairwise( 

seqs, 

gop, 

scale, 

scorer, 

mode 

): 

""" 

Align all sequences pairwise. 

 

Parameters 

---------- 

seqs : list 

The sequences to be aligned, passed as lists. 

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

mode : { "global", "local", "overlap", "dialign" } 

Select the mode for the alignment analysis ("overlap" refers to 

semi-global alignments). 

 

Returns 

------- 

alignments : list 

A of tuples, containing the aligned sequences, the similarity 

and the distance scores. 

 

Notes 

----- 

This function aligns all possible pairs between the sequences you pass to 

it. It is important for multiple alignment, where it can be used to 

construct the guide tree. 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.align_pair 

~lingpy.algorithm.cython.talign.align_pairs 

""" 

# define basic stuff 

alignments = [] 

lS = len(seqs) 

 

# [autouncomment] cdef int i,j,k,lenA,lenB 

# [autouncomment] cdef list almA,almB,seqA,seqB 

# [autouncomment] cdef float sim,simA,simB,dist 

 

# get self-scores 

sims = [0.0 for i in range(lS)] 

lens = [0 for i in range(lS)] 

 

for i in range(lS): 

seqA = seqs[i] 

k = len(seqA) 

sim = sum([scorer[seqA[j],seqA[j]] for j in range(k)]) 

lens[i] = k 

sims[i] = sim 

 

if mode == "global": 

# start loop 

for i in range(lS): 

for j in range(lS): 

if i < j: 

seqA,seqB = seqs[i],seqs[j] 

simA,simB = sims[i],sims[j] 

lenA,lenB = lens[i],lens[j] 

almA,almB,sim = globalign( 

seqA, 

seqB, 

lenA, 

lenB, 

gop, 

scale, 

scorer 

) 

 

# get the distance 

dist = 1 - ( 2 * sim / ( simA + simB ) ) 

 

# append it to list 

alignments.append( 

(almA,almB,sim,dist) 

) 

elif i == j: 

seqA = seqs[i] 

alignments.append( 

(seqA,seqA,sims[i],0.0) 

) 

 

elif mode == "local": 

# start loop 

for i in range(lS): 

for j in range(lS): 

if i < j: 

seqA,seqB = seqs[i],seqs[j] 

simA,simB = sims[i],sims[j] 

lenA,lenB = lens[i],lens[j] 

 

# check for secondary structures 

almA,almB,sim = localign( 

seqA, 

seqB, 

lenA, 

lenB, 

gop, 

scale, 

scorer 

) 

 

# get the distance 

dist = 1 - ( 2 * sim / ( simA + simB ) ) 

 

# append it to list 

alignments.append( 

(almA,almB,sim,dist) 

) 

elif i == j: 

seqA = seqs[i] 

alignments.append( 

(seqA,seqA,sims[i],0.0) 

) 

 

elif mode == "overlap": 

# start loop 

for i in range(lS): 

for j in range(lS): 

if i < j: 

seqA,seqB = seqs[i],seqs[j] 

simA,simB = sims[i],sims[j] 

lenA,lenB = lens[i],lens[j] 

 

almA,almB,sim = semi_globalign( 

seqA, 

seqB, 

lenA, 

lenB, 

gop, 

scale, 

scorer 

) 

 

# get the distance 

dist = 1 - ( 2 * sim / ( simA + simB ) ) 

 

# append it to list 

alignments.append( 

(almA,almB,sim,dist) 

) 

elif i == j: 

seqA = seqs[i] 

alignments.append( 

(seqA,seqA,sims[i],0.0) 

) 

 

elif mode == "dialign": 

# start loop 

for i in range(lS): 

for j in range(lS): 

if i < j: 

seqA,seqB = seqs[i],seqs[j] 

simA,simB = sims[i],sims[j] 

lenA,lenB = lens[i],lens[j] 

 

almA,almB,sim = dialign( 

seqA, 

seqB, 

lenA, 

lenB, 

scale, 

scorer 

) 

 

# get the distance 

dist = 1 - ( 2 * sim / ( simA + simB ) ) 

 

# append it to list 

alignments.append( 

(almA,almB,sim,dist) 

) 

elif i == j: 

seqA = seqs[i] 

alignments.append( 

(seqA,seqA,sims[i],0.0) 

) 

 

return alignments 

 

def align_pairs( 

seqs, 

gop, 

scale, 

scorer, 

mode, 

distance = 0 

): 

""" 

Align multiple sequence pairs. 

 

Parameters 

---------- 

seqs : list 

The sequences to be aligned, passed as lists. 

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale. 

scorer : { dict, ~lingpy.algorithm.cython.misc.ScoreDict } 

The scoring dictionary containing scores for all possible segment 

combinations in the two sequences. 

mode : { "global", "local", "overlap", "dialign" } 

Select the mode for the alignment analysis ("overlap" refers to 

semi-global alignments). 

distance : (default=0) 

Indicate whether distances or similarities should be returned. 

 

Returns 

------- 

alignments : list 

A of tuples, containing the aligned sequences, and the similarity 

or the distance scores. 

 

Notes 

----- 

This function aligns all pairs which are passed to 

it.  

 

See also 

-------- 

~lingpy.algorithm.cython.talign.align_pair 

~lingpy.algorithm.cython.talign.align_pairwise 

 

""" 

# basic defs 

# [autouncomment] cdef int i,j,M,N,lP 

# [autouncomment] cdef list seqA,seqB,almA,almB 

# [autouncomment] cdef float sim 

alignments = [] 

 

# get basic params 

lP = len(seqs) 

 

# check for restricted prostrings 

 

# carry out alignments 

for i in range(lP): 

# get sequences 

seqA,seqB = seqs[i][0],seqs[i][1] 

 

# get length of seqs 

M,N = len(seqA),len(seqB) 

 

if mode == "global": 

almA,almB,sim = globalign( 

seqA, 

seqB, 

M, 

N, 

gop, 

scale, 

scorer 

) 

elif mode == "local": 

almA,almB,sim = localign( 

seqA, 

seqB, 

M, 

N, 

gop, 

scale, 

scorer 

) 

 

elif mode == "overlap": 

almA,almB,sim = semi_globalign( 

seqA, 

seqB, 

M, 

N, 

gop, 

scale, 

scorer 

) 

 

elif mode == "dialign": 

almA,almB,sim = dialign( 

seqA, 

seqB, 

M, 

N, 

scale, 

scorer 

) 

 

# calculate distances if option is chose 

if distance > 0: 

simA = sum([scorer[seqA[i],seqA[i]] for i in range(M)]) 

simB = sum([scorer[seqB[i],seqB[i]] for i in range(N)]) 

 

dist = 1 - ( ( 2 * sim ) / ( simA + simB ) ) 

if distance == 1: 

alignments.append((almA,almB,dist)) 

else: 

alignments.append((almA,almB,sim,dist)) 

else: 

alignments.append((almA,almB,sim)) 

 

return alignments 

 

# specific methods for the alignment of profiles 

def align_profile( 

profileA, 

profileB, 

gop, 

scale, 

scorer, 

mode, 

gap_weight 

): 

""" 

Align two profiles using the basic modes. 

 

Parameters 

---------- 

profileA, profileB : list 

Two-dimensional for each of the profiles.  

gop : int 

The gap opening penalty. 

scale : float 

The gap extension scale by which consecutive gaps are reduced. LingPy 

uses a scale rather than a constant gap extension penalty.  

scorer : { dict, :py:class:`lingpy.algorithm.cython.misc.ScoreDict` } 

The scoring function which needs to provide scores for all 

segments in the two profiles. 

mode : { "global", "overlap", "dialign" } 

Select one of the four basic modes for alignment analyses. 

gap_weight : float 

This handles the weight that is given to gaps in a column. If you set 

it to 0, for example, this means that all gaps will be ignored when 

determining the score for two columns in the profile. 

 

Notes 

----- 

This function computes alignments of two profiles of multiple sequences 

(see :evobib:`Durbin2002` for details on profiles) 

and is important for multiple alignment analyses. 

 

Returns 

------- 

alignment : tuple 

The aligned profiles, and the overall similarity of the profiles. 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.score_profile 

~lingpy.algorithm.cython.talign.swap_score_profile 

""" 

 

# basic defs 

# [autouncomment] cdef int i,j,k,l,M,N,O,P 

# [autouncomment] cdef float sim,count 

# [autouncomment] cdef str charA,charB 

# [autouncomment] cdef list listA,listB,almA,almB 

 

M = len(profileA) 

N = len(profileB) 

O = len(profileA[0]) 

P = len(profileB[0]) 

 

tmp_scorer = {} 

 

listA = [i for i in range(M)] 

listB = [i for i in range(N)] 

 

for i in range(M): 

for j in range(N): 

sim = 0.0 

count = 0.0 

for k in range(O): 

for l in range(P): 

charA = profileA[i][k] 

charB = profileB[j][l] 

if charA != 'X' and charB != 'X': 

sim += scorer[charA,charB] 

count += 1.0 

else: 

count += gap_weight 

tmp_scorer[i,j] = sim / count 

 

if mode == "global": 

almA,almB,sim = globalign( 

listA, 

listB, 

M, 

N, 

gop, 

scale, 

tmp_scorer 

) 

elif mode == "overlap": 

almA,almB,sim = semi_globalign( 

listA, 

listB, 

M, 

N, 

gop, 

scale, 

tmp_scorer 

) 

elif mode == "dialign": 

almA,almB,sim = dialign( 

listA, 

listB, 

M, 

N, 

scale, 

tmp_scorer 

) 

 

return almA,almB,sim 

 

def score_profile( 

colA, 

colB, 

scorer, 

gop, 

gap_weight = 0.0 

): 

""" 

Basic function for the scoring of profiles. 

 

Parameters 

---------- 

colA, colB : list 

The two columns of a profile. 

scorer : { dict, :py:class:`lingpy.algorithm.cython.misc.ScoreDict` } 

The scoring function which needs to provide scores for all 

segments in the two profiles. 

gap_weight : (default=0.0) 

This handles the weight that is given to gaps in a column. If you set 

it to 0, for example, this means that all gaps will be ignored when 

determining the score for two columns in the profile. 

 

Notes 

----- 

This function handles how profiles are scored. 

 

Returns 

------- 

score : float 

The score for the profile 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.align_profile 

~lingpy.algorithm.cython.talign.swap_score_profile 

""" 

# basic definitions 

# [autouncomment] cdef int i,j 

# [autouncomment] cdef str charA,charB 

 

# define the initial score 

score = 0.0 

 

# set a counter 

counter = 0 

 

# iterate over all chars 

for i,charA in enumerate(colA): 

for j,charB in enumerate(colB): 

if charA != 'X' and charB != 'X': 

score += scorer[charA,charB] 

counter += 1.0 

elif charA == 'X' and charB == 'X': 

counter += gap_weight 

else: 

score += float(gop) 

counter += 1.0 

return score / counter 

 

def swap_score_profile( 

colA, 

colB, 

scorer, 

gap_weight = 0.0, 

swap_penalty = -1 

): 

""" 

Basic function for the scoring of profiles in swapped sequences. 

 

Parameters 

---------- 

colA, colB : list 

The two columns of a profile. 

scorer : { dict, :py:class:`lingpy.algorithm.cython.misc.ScoreDict` } 

The scoring function which needs to provide scores for all 

segments in the two profiles. 

gap_weight : (default=0.0) 

This handles the weight that is given to gaps in a column. If you set 

it to 0, for example, this means that all gaps will be ignored when 

determining the score for two columns in the profile. 

swap_penalty : (default=-5) 

The swap penalty applied to swapped columns. 

 

Notes 

----- 

This function handles how profiles with swapped segments are scored. 

 

Returns 

------- 

score : float 

The score for the profile. 

 

See also 

-------- 

~lingpy.algorithm.cython.talign.align_profile 

~lingpy.algorithm.cython.talign.score_profile 

 

""" 

# basic definitions 

# [autouncomment] cdef int i,j 

# [autouncomment] cdef str charA,charB 

 

# define the initial score 

score = 0.0 

 

# set a counter 

counter = 0 

 

# iterate over all chars 

for i,charA in enumerate(colA): 

for j,charB in enumerate(colB): 

if charA != 'X' and charB != 'X' and charA != '+' and charB != '+': 

score += scorer[charA,charB] 

counter += 1.0 

elif charA == '+' or charB == '+': 

if charA == '+' and charB == '+': 

score += 0.0 

counter += 1.0 

elif charA == 'X' or charB == 'X': 

score += swap_penalty # this is the swap cost 

counter += 1.0 

else: 

score += -1000000 

counter += 1.0 

else: 

counter += gap_weight 

 

return score / counter