File: alignments.rst

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (843 lines) | stat: -rw-r--r-- 27,067 bytes parent folder | download
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
Collections and Alignments
--------------------------

.. authors, Gavin Huttley, Kristian Rother, Patrick Yannul, Tom Elliott, Jan Kosinski

For loading collections of unaligned or aligned sequences see :ref:`load-seqs`.

Basic Collection objects
^^^^^^^^^^^^^^^^^^^^^^^^

Constructing a ``SequenceCollection`` or ``Alignment`` object from strings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> dna  = {'seq1': 'ATGACC',
    ...         'seq2': 'ATCGCC'}
    >>> seqs = LoadSeqs(data=dna, moltype=DNA)
    >>> print type(seqs)
    <class 'cogent.core.alignment.Alignment'>
    >>> seqs = LoadSeqs(data=dna, moltype=DNA, aligned=False)
    >>> print type(seqs)
    <class 'cogent.core.alignment.SequenceCollection'>

Converting a ``SequenceCollection`` to FASTA format
"""""""""""""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent import LoadSeqs
    >>> seq = LoadSeqs('data/test.paml', aligned=False)
    >>> fasta_data = seq.toFasta()
    >>> print fasta_data
    >DogFaced
    GCAAGGAGCCAGCAGAACAGATGGGTTGAAACTAAGGAAACATGTAATGATAGGCAGACT
    >HowlerMon
    GCAAGGAGCCAACATAACAGATGGGCTGAAAGTGAGGAAACATGTAATGATAGGCAGACT
    >Human
    GCAAGGAGCCAACATAACAGATGGGCTGGAAGTAAGGAAACATGTAATGATAGGCGGACT
    >Mouse
    GCAGTGAGCCAGCAGAGCAGATGGGCTGCAAGTAAAGGAACATGTAACGACAGGCAGGTT
    >NineBande
    GCAAGGCGCCAACAGAGCAGATGGGCTGAAAGTAAGGAAACATGTAATGATAGGCAGACT

Adding new sequences to an existing collection or alignment
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

New sequences can be either appended or inserted using the ``addSeqs`` method. More than one sequence can be added at the same time. Note that ``addSeqs`` does not modify the existing collection/alignment, it creates new one.

Appending the sequences
"""""""""""""""""""""""

``addSeqs`` without additional parameters will append the sequences to the end of the collection/alignment. 

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA------'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> print aln
    >seq1
    ATGAA------
    >seq2
    ATG-AGTGATG
    >seq3
    AT--AG-GATG
    <BLANKLINE>
    >>> new_seqs = LoadSeqs(data= [('seq0', 'ATG-AGT-AGG'),
    ...                            ('seq4', 'ATGCC------')], moltype=DNA)
    >>> new_aln = aln.addSeqs(new_seqs)
    >>> print new_aln
    >seq1
    ATGAA------
    >seq2
    ATG-AGTGATG
    >seq3
    AT--AG-GATG
    >seq0
    ATG-AGT-AGG
    >seq4
    ATGCC------
    <BLANKLINE>

.. note:: The order is not preserved if you use ``toFasta`` method, which sorts sequences by name.

Inserting the sequences
"""""""""""""""""""""""

Sequences can be inserted into an alignment at the specified position using either the ``before_name`` or ``after_name`` arguments.

.. doctest::

   >>> new_aln = aln.addSeqs(new_seqs, before_name='seq2')
   >>> print new_aln
   >seq1
   ATGAA------
   >seq0
   ATG-AGT-AGG
   >seq4
   ATGCC------
   >seq2
   ATG-AGTGATG
   >seq3
   AT--AG-GATG
   <BLANKLINE>
   >>> new_aln = aln.addSeqs(new_seqs, after_name='seq2')
   >>> print new_aln
   >seq1
   ATGAA------
   >seq2
   ATG-AGTGATG
   >seq0
   ATG-AGT-AGG
   >seq4
   ATGCC------
   >seq3
   AT--AG-GATG
   <BLANKLINE>

Inserting sequence(s) based on their alignment to a reference sequence
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

Already aligned sequences can be added to an existing ``Alignment`` object and aligned at the same time using the ``addFromReferenceAln`` method. The alignment is performed based on their alignment to a reference sequence (which must be present in both alignments). The method assumes the first sequence in ``ref_aln.Names[0]`` is the reference.

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA------'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> ref_aln = LoadSeqs(data= [('seq3', 'ATAGGATG'),
    ...                           ('seq0', 'ATG-AGCG'),
    ...                           ('seq4', 'ATGCTGGG')], moltype=DNA)
    >>> new_aln = aln.addFromReferenceAln(ref_aln)
    >>> print new_aln
    >seq1
    ATGAA------
    >seq2
    ATG-AGTGATG
    >seq3
    AT--AG-GATG
    >seq0
    AT--G--AGCG
    >seq4
    AT--GC-TGGG
    <BLANKLINE>

``addFromReferenceAln`` has the same arguments as ``addSeqs`` so ``before_name`` and ``after_name`` can be used to insert the new sequences at the desired position.

.. note:: This method does not work with the ``DenseAlignment`` class.

Removing all columns with gaps in a named sequence
++++++++++++++++++++++++++++++++++++++++++++++++++

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA---TG-'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> new_aln = aln.getDegappedRelativeTo('seq1')
    >>> print new_aln
    >seq1
    ATGAATG
    >seq2
    ATG-AAT
    >seq3
    AT--AAT
    <BLANKLINE>

The elements of a collection or alignment
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Accessing individual sequences from a collection or alignment by name
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

Using the ``getSeq`` method allows for extracting an unaligned sequence from a collection or alignment by name.

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA------'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> seq = aln.getSeq('seq1')
    >>> seq.Name
    'seq1'
    >>> type(seq)
    <class 'cogent.core.sequence.DnaSequence'>
    >>> seq.isGapped()
    False

Alternatively, if you want to extract the aligned (i.e., gapped) sequence from an alignment, you can use ``getGappedSeq``.

.. doctest::

    >>> seq = aln.getGappedSeq('seq1')
    >>> seq.isGapped()
    True
    >>> print seq
    ATGAA------

To see the names of the sequences in a sequence collection, you can use either the ``Names`` attribute or ``getSeqNames`` method.

.. doctest::

    >>> aln.Names
    ['seq1', 'seq2', 'seq3']
    >>> aln.getSeqNames()
    ['seq1', 'seq2', 'seq3']

Slice the sequences from an alignment like a list
"""""""""""""""""""""""""""""""""""""""""""""""""

The usual approach is to access a ``SequenceCollection`` or ``Alignment`` object as a dictionary, obtaining the individual sequences using the titles as "keys" (above).  However, one can also iterate through the collection like a list.

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> fn = 'data/long_testseqs.fasta'
    >>> seqs = LoadSeqs(fn, moltype=DNA, aligned=False)
    >>> my_seq = seqs.Seqs[0]
    >>> my_seq[:24]
    DnaSequence(TGTGGCA... 24)
    >>> str(my_seq[:24])
    'TGTGGCACAAATACTCATGCCAGC'
    >>> type(my_seq)
    <class 'cogent.core.sequence.DnaSequence'>
    >>> aln = LoadSeqs(fn, moltype=DNA, aligned=True)
    >>> aln.Seqs[0][:24]
    [0:24]/2532 of DnaSequence(TGTGGCA... 2532)
    >>> print aln.Seqs[0][:24]
    TGTGGCACAAATACTCATGCCAGC

Getting a subset of sequences from the alignment
""""""""""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs('data/test.paml', moltype=DNA)
    >>> aln.Names
    ['NineBande', 'Mouse', 'Human', 'HowlerMon', 'DogFaced']
    >>> new = aln.takeSeqs(['Human', 'HowlerMon'])
    >>> new.Names
    ['Human', 'HowlerMon']

Note the subset contain references to the original sequences, not copies.

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs('data/test.paml', moltype=DNA)
    >>> seq = aln.getSeq('Human')
    >>> new = aln.takeSeqs(['Human', 'HowlerMon'])
    >>> id(new.getSeq('Human')) == id(aln.getSeq('Human'))
    True

Alignments
^^^^^^^^^^

Creating an ``Alignment`` object from a ``SequenceCollection``
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent.core.alignment import Alignment
    >>> seq = LoadSeqs('data/test.paml', aligned=False)
    >>> aln = Alignment(seq)
    >>> fasta_1 = seq.toFasta()
    >>> fasta_2 = aln.toFasta()
    >>> assert fasta_1 == fasta_2

Handling gaps
"""""""""""""

Remove all gaps from an alignment in FASTA format
+++++++++++++++++++++++++++++++++++++++++++++++++

This necessarily returns a ``SequenceCollection``.

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs("data/primate_cdx2_promoter.fasta")
    >>> degapped = aln.degap()
    >>> print type(degapped)
    <class 'cogent.core.alignment.SequenceCollection'>

.. TODO the following should be preceded by a section describing the writeToFile method and format argument

Writing sequences to file
"""""""""""""""""""""""""

Both collection and alignment objects have a ``writeToFile`` method. The output format is inferred from the filename suffix,

.. doctest::
    
    >>> from cogent import LoadSeqs, DNA
    >>> dna  = {'seq1': 'ATGACC',
    ...         'seq2': 'ATCGCC'}
    >>> aln = LoadSeqs(data=dna, moltype=DNA)
    >>> aln.writeToFile('sample.fasta')

or by the ``format`` argument.

.. doctest::
    
    >>> aln.writeToFile('sample', format='fasta')

.. now clean the files up

.. doctest::
    :hide:
    
    >>> from cogent.util.misc import remove_files
    >>> remove_files(['sample', 'sample.fasta'], error_on_missing=False)

Converting an alignment to FASTA format
"""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent.core.alignment import Alignment
    >>> seq = LoadSeqs('data/long_testseqs.fasta')
    >>> aln = Alignment(seq)
    >>> fasta_align = aln.toFasta()

Converting an alignment into Phylip format
""""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent.core.alignment import Alignment
    >>> seq = LoadSeqs('data/test.paml')
    >>> aln = Alignment(seq)
    >>> phylip_file, name_dictionary = aln.toPhylip()

Converting an alignment to a list of strings
""""""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent.core.alignment import Alignment
    >>> seq = LoadSeqs('data/test.paml')
    >>> aln = Alignment(seq)
    >>> string_list = aln.todict().values()

Slicing an alignment
^^^^^^^^^^^^^^^^^^^^

By rows (sequences)
"""""""""""""""""""

An ``Alignment`` can be sliced

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> fn = 'data/long_testseqs.fasta'
    >>> aln = LoadSeqs(fn, moltype=DNA, aligned=True)
    >>> print aln[:24]
    >Human
    TGTGGCACAAATACTCATGCCAGC
    >HowlerMon
    TGTGGCACAAATACTCATGCCAGC
    >Mouse
    TGTGGCACAGATGCTCATGCCAGC
    >NineBande
    TGTGGCACAAATACTCATGCCAAC
    >DogFaced
    TGTGGCACAAATACTCATGCCAAC
    <BLANKLINE>

but a ``SequenceCollection`` cannot be sliced

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> fn = 'data/long_testseqs.fasta'
    >>> seqs = LoadSeqs(fn, moltype=DNA, aligned=False)
    >>> print seqs[:24]
    Traceback (most recent call last):
    TypeError: 'SequenceCollection' object is not subscriptable

Getting a single column from an alignment
"""""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent.core.alignment import Alignment
    >>> seq = LoadSeqs('data/test.paml')
    >>> aln = Alignment(seq)
    >>> column_four = aln[3]

Getting a region of contiguous columns
""""""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent.core.alignment import Alignment
    >>> aln = LoadSeqs('data/long_testseqs.fasta')
    >>> region = aln[50:70]

Iterating over alignment positions
""""""""""""""""""""""""""""""""""

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/primate_cdx2_promoter.fasta')
    >>> col = aln[113:115].iterPositions()
    >>> type(col)
    <type 'generator'>
    >>> list(col)
    [['A', 'A', 'A'], ['T', '-', '-']]

Getting codon 3rd positions from an alignment
"""""""""""""""""""""""""""""""""""""""""""""

We'll do this by specifying the position indices of interest, creating a sequence ``Feature`` and using that to extract the positions.

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs(data={'seq1': 'ATGATGATG---',
    ...                      'seq2': 'ATGATGATGATG'})
    >>> range(len(aln))[2::3]
    [2, 5, 8, 11]
    >>> indices = [(i, i+1) for i in range(len(aln))[2::3]]
    >>> indices
    [(2, 3), (5, 6), (8, 9), (11, 12)]
    >>> pos3 = aln.addFeature('pos3', 'pos3', indices)
    >>> pos3 = pos3.getSlice()
    >>> print pos3
    >seq2
    GGGG
    >seq1
    GGG-
    <BLANKLINE>

.. _filter-positions:

Filtering positions
"""""""""""""""""""

Eliminating columns with non-nucleotide characters
++++++++++++++++++++++++++++++++++++++++++++++++++

We sometimes want to eliminate ambiguous or gap data from our alignments. We show how to exclude alignment columns by the characters they contain. In the first instance we do this just for single nucleotide columns, then for trinucleotides (equivalent for handling codons).

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAAGGTG---'),
    ...                       ('seq2', 'ATGAAGGTGATG'),
    ...                       ('seq3', 'ATGAAGGNGATG')], moltype=DNA)

We now just define a one-line function that returns ``True`` if the passed data contains only nucleotide characters, ``False`` otherwise. The function works by converting the aligned column into a ``set`` and checking it is equal to, or a subset of, all nucleotides. This function, which works for nucleotides or codons, has the effect of eliminating the (nucleotide/trinucleotide) columns with the 'N' and '-' characters.

.. doctest::

    >>> just_nucs = lambda x: set(''.join(x)) <= set('ACGT')

We apply to nucleotides,

.. doctest::

    >>> nucs = aln.filtered(just_nucs)
    >>> print nucs
    >seq1
    ATGAAGGG
    >seq2
    ATGAAGGG
    >seq3
    ATGAAGGG
    <BLANKLINE>

We can also do this in a more longwinded but clearer fashion with a named multi-line function:

.. doctest::

    >>> def just_nucs(x, allowed = 'ACGT'):
    ...     for char in ''.join(x): # ensure char is a str with length 1
    ...         if not char in allowed:
    ...             return False
    ...     return True
    ...
    >>> nucs = aln.filtered(just_nucs)
    >>> nucs
    3 x 8 dna alignment: seq1[ATGAAGGG], seq2[ATGAAGGG], seq3[ATGAAGGG]
    >>> print nucs
    >seq1
    ATGAAGGG
    >seq2
    ATGAAGGG
    >seq3
    ATGAAGGG
    <BLANKLINE>

Applying the same filter to trinucleotides (specified by setting ``motif_length=3``).

.. doctest::

    >>> trinucs = aln.filtered(just_nucs, motif_length=3)
    >>> print trinucs
    >seq1
    ATGAAG
    >seq2
    ATGAAG
    >seq3
    ATGAAG
    <BLANKLINE>

Getting all variable positions from an alignment
++++++++++++++++++++++++++++++++++++++++++++++++

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/long_testseqs.fasta')
    >>> just_variable_aln = aln.filtered(lambda x: len(set(x)) > 1)
    >>> print just_variable_aln[:10]
    >Human
    AAGCAAAACT
    >HowlerMon
    AAGCAAGACT
    >Mouse
    GGGCCCAGCT
    >NineBande
    AAATAAAACT
    >DogFaced
    AAACAAAATA
    <BLANKLINE>

Getting all constant positions from an alignment
++++++++++++++++++++++++++++++++++++++++++++++++

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/long_testseqs.fasta')
    >>> just_constant_aln = aln.filtered(lambda x: len(set(x)) == 1)
    >>> print just_constant_aln[:10]
    >Human
    TGTGGCACAA
    >HowlerMon
    TGTGGCACAA
    >Mouse
    TGTGGCACAA
    >NineBande
    TGTGGCACAA
    >DogFaced
    TGTGGCACAA
    <BLANKLINE>

Getting all variable codons from an alignment
+++++++++++++++++++++++++++++++++++++++++++++

This is exactly the same as before, with a new keyword argument

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/long_testseqs.fasta')
    >>> variable_codons = aln.filtered(lambda x: len(set(x)) > 1,
    ...                                motif_length=3)
    >>> print just_variable_aln[:9]
    >Human
    AAGCAAAAC
    >HowlerMon
    AAGCAAGAC
    >Mouse
    GGGCCCAGC
    >NineBande
    AAATAAAAC
    >DogFaced
    AAACAAAAT
    <BLANKLINE>

Filtering sequences
"""""""""""""""""""

Extracting sequences by sequence identifier into a new alignment object
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

You can use ``takeSeqs`` to extract some sequences by sequence identifier from an alignment to a new alignment object:

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/long_testseqs.fasta')
    >>> aln.takeSeqs(['Human','Mouse'])
    2 x 2532 text alignment: Human[TGTGGCACAAA...], Mouse[TGTGGCACAGA...]

Alternatively, you can extract only the sequences which are not specified by passing ``negate=True``:

.. doctest::

    >>> aln.takeSeqs(['Human','Mouse'],negate=True)
    3 x 2532 text alignment: NineBande[TGTGGCACAAA...], HowlerMon[TGTGGCACAAA...], DogFaced[TGTGGCACAAA...]

Extracting sequences using an arbitrary function into a new alignment object
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

You can use ``takeSeqsIf`` to extract sequences into a new alignment object based on whether an arbitrary function applied to the sequence evaluates to True. For example, to extract sequences which don't contain any N bases you could do the following:

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAAGGTG---'),
    ...                       ('seq2', 'ATGAAGGTGATG'),
    ...                       ('seq3', 'ATGAAGGNGATG')], moltype=DNA)
    >>> def no_N_chars(s):
    ...     return 'N' not in s
    >>> aln.takeSeqsIf(no_N_chars)
    2 x 12 dna alignment: seq1[ATGAAGGTG--...], seq2[ATGAAGGTGAT...]

You can additionally get the sequences where the provided function evaluates to False:

.. doctest::

    >>> aln.takeSeqsIf(no_N_chars,negate=True)
    1 x 12 dna alignment: seq3[ATGAAGGNGAT...]

Computing alignment statistics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Computing motif probabilities from an alignment
"""""""""""""""""""""""""""""""""""""""""""""""

The method ``getMotifProbs`` of ``Alignment`` objects returns the probabilities for all motifs of a given length. For individual nucleotides:

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> aln = LoadSeqs('data/primate_cdx2_promoter.fasta', moltype=DNA)
    >>> motif_probs = aln.getMotifProbs()
    >>> print motif_probs
    {'A': 0.24...

For dinucleotides or longer, we need to pass in an ``Alphabet`` with the appropriate word length. Here is an example with trinucleotides:

.. doctest::

    >>> from cogent import LoadSeqs, DNA
    >>> trinuc_alphabet = DNA.Alphabet.getWordAlphabet(3)
    >>> aln = LoadSeqs('data/primate_cdx2_promoter.fasta', moltype=DNA)
    >>> motif_probs = aln.getMotifProbs(alphabet=trinuc_alphabet)
    >>> for m in sorted(motif_probs, key=lambda x: motif_probs[x],
    ...                 reverse=True):
    ...     print m, motif_probs[m]
    ...
    CAG 0.0374581939799
    CCT 0.0341137123746
    CGC 0.0301003344482...

The same holds for other arbitrary alphabets, as long as they match the alignment ``MolType``.

Some calculations in cogent require all non-zero values in the motif probabilities, in which case we use a pseudo-count. We illustrate that here with a simple example where T is missing. Without the pseudo-count, the frequency of T is 0.0, with the pseudo-count defined as 1e-6 then the frequency of T will be slightly less than 1e-6.

.. doctest::

    >>> aln = LoadSeqs(data=[('a', 'AACAAC'),('b', 'AAGAAG')], moltype=DNA)
    >>> motif_probs = aln.getMotifProbs()
    >>> assert motif_probs['T'] == 0.0
    >>> motif_probs = aln.getMotifProbs(pseudocount=1e-6)
    >>> assert 0 < motif_probs['T'] <= 1e-6

It is important to notice that motif probabilities are computed by treating sequences as non-overlapping tuples. Below is a very simple pair of identical sequences where there are clearly 2 'AA' dinucleotides per sequence but only the first one is 'in-frame' (frame width = 2).

We then create a dinucleotide ``Alphabet`` object and use this to get dinucleotide probabilities. These frequencies are determined by breaking each aligned sequence up into non-overlapping dinucleotides and then doing a count. The expected value for the 'AA' dinucleotide in this case will be 2/8 = 0.25.

.. doctest::

    >>> seqs = [('a', 'AACGTAAG'), ('b', 'AACGTAAG')]
    >>> aln = LoadSeqs(data=seqs, moltype=DNA)
    >>> dinuc_alphabet = DNA.Alphabet.getWordAlphabet(2)
    >>> motif_probs = aln.getMotifProbs(alphabet=dinuc_alphabet)
    >>> assert motif_probs['AA'] == 0.25

What about counting the total incidence of dinucleotides including those not in-frame?  A naive application of the Python string object's count method will not work as desired either because it "returns the number of non-overlapping occurrences".

.. doctest::

    >>> seqs = [('my_seq', 'AAAGTAAG')]
    >>> aln = LoadSeqs(data=seqs, moltype=DNA)
    >>> my_seq = aln.getSeq('my_seq')
    >>> my_seq.count('AA')
    2
    >>> 'AAA'.count('AA')
    1
    >>> 'AAAA'.count('AA')
    2

To count all occurrences of a given dinucleotide in a DNA sequence, one could use a standard Python approach such as list comprehension:

.. doctest::

    >>> from cogent import Sequence, DNA
    >>> seq = Sequence(moltype=DNA, seq='AAAGTAAG')
    >>> seq
    DnaSequence(AAAGTAAG)
    >>> di_nucs = [seq[i:i+2] for i in range(len(seq)-1)]
    >>> sum([nn == 'AA' for nn in di_nucs])
    3

Working with alignment gaps
"""""""""""""""""""""""""""

Filtering extracted columns for the gap character
+++++++++++++++++++++++++++++++++++++++++++++++++

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/primate_cdx2_promoter.fasta')
    >>> col = aln[113:115].iterPositions()
    >>> c1, c2 = list(col)
    >>> c1, c2
    (['A', 'A', 'A'], ['T', '-', '-'])
    >>> filter(lambda x: x == '-', c1)
    []
    >>> filter(lambda x: x == '-', c2)
    ['-', '-']

Calculating the gap fraction
++++++++++++++++++++++++++++

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs('data/primate_cdx2_promoter.fasta')
    >>> for column in aln[113:150].iterPositions():
    ...     ungapped = filter(lambda x: x == '-', column)
    ...     gap_fraction = len(ungapped) * 1.0 / len(column)
    ...     print gap_fraction
    0.0
    0.666666666667
    0.0
    0.0...

Extracting maps of aligned to unaligned positions (i.e., gap maps)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

It's often important to know how an alignment position relates to a position in one or more of the sequences in the alignment. The ``gapMaps`` method of the individual sequences is useful for this. To get a map of sequence to alignment positions for a specific sequence in your alignment, do the following:

.. doctest::

    >>> from cogent import LoadSeqs
    >>> aln = LoadSeqs(data= [('seq1', 'ATGAAGG-TG--'),
    ...                       ('seq2', 'ATG-AGGTGATG'),
    ...                       ('seq3', 'ATGAAG--GATG')], moltype=DNA)
    >>> seq_to_aln_map = aln.getGappedSeq('seq1').gapMaps()[0]

It's now possible to look up positions in the ``seq1``, and find out what they map to in the alignment:

.. doctest::

    >>> seq_to_aln_map[3]
    3
    >>> seq_to_aln_map[8]
    9

This tells us that in position 3 in ``seq1`` corresponds to position 3 in ``aln``, and that position 8 in ``seq1`` corresponds to position 9 in ``aln``.

Notice that we grabbed the first result from the call to ``gapMaps``. This is the sequence position to alignment position map. The second value returned is the alignment position to sequence position map, so if you want to find out what sequence positions the alignment positions correspond to (opposed to what alignment positions the sequence positions correspond to) for a given sequence, you would take the following steps:

.. doctest::

    >>> aln_to_seq_map = aln.getGappedSeq('seq1').gapMaps()[1]
    >>> aln_to_seq_map[3]
    3
    >>> aln_to_seq_map[8]
    7

If an alignment position is a gap, and therefore has no corresponding sequence position, you'll get a ``KeyError``.

.. doctest::

   >>> seq_pos = aln_to_seq_map[7]
   Traceback (most recent call last):
   KeyError: 7

.. note:: The first position in alignments and sequences is always numbered position 0.

Filtering alignments based on gaps
++++++++++++++++++++++++++++++++++

.. note:: An alternate, computationally faster, approach to removing gaps is to use the ``filtered`` method as discussed in :ref:`filter-positions`.

The ``omitGapRuns`` method can be applied to remove long stretches of gaps in an alignment. In the following example, we remove sequences that have more than two adjacent gaps anywhere in the aligned sequence.

.. doctest::

    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA---TG-'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> print aln.omitGapRuns(2).toFasta()
    >seq2
    ATG-AGTGATG
    >seq3
    AT--AG-GATG

If instead, we just wanted to remove positions from the alignment which are gaps in more than a certain percentage of the sequences, we could use the ``omitGapPositions`` function. For example:

.. doctest::

    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA---TG-'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> print aln.omitGapPositions(0.40).toFasta()
    >seq1
    ATGA--TG-
    >seq2
    ATGAGGATG
    >seq3
    AT-AGGATG

You'll notice that the 4th and 7th columns of the alignment have been removed because they contained 66% gaps -- more than the allowed 40%. 

If you wanted to remove sequences which contain more than a certain percent gap characters, you could use the ``omitGapSeqs`` method. This is commonly applied to filter partial sequences from an alignment. 

    >>> aln = LoadSeqs(data= [('seq1', 'ATGAA------'),
    ...                       ('seq2', 'ATG-AGTGATG'),
    ...                       ('seq3', 'AT--AG-GATG')], moltype=DNA)
    >>> filtered_aln = aln.omitGapSeqs(0.50)
    >>> print filtered_aln.toFasta()
    >seq2
    ATG-AGTGATG
    >seq3
    AT--AG-GATG

Note that following this call to ``omitGapSeqs``, the 4th column of ``filtered_aln`` is 100% gaps. This is generally not desirable, so a call to ``omitGapSeqs`` is frequently followed with a call to ``omitGapPositions`` with no parameters -- this defaults to removing positions which are all gaps:

    >>> print filtered_aln.omitGapPositions().toFasta()
    >seq2
    ATGAGTGATG
    >seq3
    AT-AG-GATG