vcfconcat.py 33.8 KB
Newer Older
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
"""
Concatenates all SNPs from a VCF file in either FASTA or aln (clustal) format
"""
import os, sys
import copy
import argparse
import multiprocessing
from pprint      import pprint as pp
from collections import defaultdict, Counter

sys.path.insert(0, '.')
import vcfmerger
import editdist
from treemanager import fixsppname

Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
17
18
#GZ=SL2.40ch06g50000_000100001_000150000.vcf.gz.raw.vcf.gz; FA=$GZ.SL2.40ch06.fasta; ../vcfconcat.py -f -RIL -Rg -Rd -i $GZ; ../FastTreeMP -fastest -gamma -nt -bionj -boot 100 -log $FA.log -out $FA.tree $FA; ../FastTreeMP -nt -makematrix $FA > $FA.matrix; ./newick_to_png.py --infile $FA.tree
#FA=SL2.40ch06g50000_000100001_000150000.vcf.gz.SL2.40ch06.fasta; ../FastTreeMP -fastest -gamma -nt -bionj -boot 100 -log $FA.log -out $FA.tree $FA; ../FastTreeMP -nt -makematrix $FA > $FA.matrix; ./newick_to_png.py --infile $FA.tree
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32



def main(args):
    methods = {
        'median' : grouper_median,
        'linkage': grouper_linkage
    }


    dflmethod = methods.keys()[0]


    parser = argparse.ArgumentParser(description='Concatenate SNPs as a single sequence for each species.')
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    parser.add_argument('-c', '--chrom' , '--chromosome', dest='chromosome' , default=None     , action='store'      , nargs='?',                         type=str  , help='Chromosome to filter [all]')
    parser.add_argument('-I', '--ignore', '--skip'      , dest='ignore'     , default=[]       , action='append'     , nargs='*',                         type=str  , help='Chromosomes to skip')
    parser.add_argument('-s', '--start'                 , dest='start'      , default=None     , action='store'      , nargs='?',                         type=int  , help='Chromosome start position to filter [0]')
    parser.add_argument('-e', '--end'                   , dest='end'        , default=None     , action='store'      , nargs='?',                         type=int  , help='Chromosome end position to filter [-1]')
    parser.add_argument('-t', '--threads'               , dest='threads'    , default=0        , action='store'      , nargs='?',                         type=int  , help='Number of threads [num chromosomes]')
    parser.add_argument('-a', '--clustal'               , dest='fasta'      ,                    action='store_false',                                                help='Output in clustal .aln format [default: fasta format]')
    parser.add_argument('-r', '--noref'                 , dest='noref'      ,                    action='store_false',                                                help='Do not print reference [default: true]')
    parser.add_argument('-n', '--ref-name'              , dest='refname'    , default='ref'    , action='store'      , nargs='?',                         type=str  , help='Reference name [default: ref]')
    parser.add_argument('-R', '--RIL'                   , dest='RIL'        ,                    action='store_true' ,                                                help='RIL mode: false]')
    parser.add_argument('-Rm','--RIL-mads'              , dest='RILmads'    , default=0.25     , action='store'      , nargs='?',                         type=float, help='RIL percentage of Median Absolute Deviation to use (smaller = more restrictive): 0.25]')
    parser.add_argument('-Rs','--RIL-minsim'            , dest='RILminsim'  , default=0.75     , action='store'      , nargs='?',                         type=float, help='RIL percentage of nucleotides identical to reference to classify as reference: 0.75]')
    parser.add_argument('-Rg','--RIL-greedy'            , dest='RILgreedy'  ,                    action='store_true' ,                                                help='RIL greedy convert nucleotides to either the reference sequence or the alternative sequence: false]')
    parser.add_argument('-Rd','--RIL-delete'            , dest='RILdelete'  ,                    action='store_true' ,                                                help='RIL delete invalid sequences: false]')
    parser.add_argument('-M' ,'--RIL-method'            , dest='groupMethod', default=dflmethod, action='store'      , nargs='?', choices=methods.keys(), type=str  , help='Clustering method for RIL selection of good and bad sequences [' + ','.join(methods.keys()) + ']')

    parser.add_argument('-i' , '--input'                , dest='input'      , default=None     ,                       nargs='?',                         type=str  , help='Input file')
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
49
50
51
52
53
54
55
56
57
58
    #parser.add_argument('input'                        ,                    default=None     , action='store'      , nargs='?', metavar='input file', type=str  , help='Input file')

    options = parser.parse_args(args)

    print options

    indexFile    = None
    parallel     = False

    config = {
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
59
        'format'       : 'fasta',
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
60
61
62
63
64
65
66
        'ignore'       : [],
        'inchr'        : None,
        'inend'        : None,
        'infhd'        : None,
        'infile'       : None,
        'instart'      : None,
        'noref'        : True,
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
67
        'refname'      : None,
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        'ouchr'        : None,
        'oufhd'        : None,
        'RIL'          : False,
        'RILmads'      : 0.25,
        'RILminsim'    : 0.75,
        'RILgreedy'    : False,
        'RILdelete'    : False,
        'sppmaxlength' : None,
        'threads'      : None,
    }




    config['infile'     ] = options.input
    config['ignore'     ] = options.ignore
    config['inchr'      ] = options.chromosome
    config['inend'      ] = options.end
    config['instart'    ] = options.start
    config['noref'      ] = options.noref
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
88
    config['refname'    ] = options.refname
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
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
    config['threads'    ] = options.threads
    config['RIL'        ] = options.RIL
    config['RILmads'    ] = options.RILmads
    config['RILminsim'  ] = options.RILminsim
    config['RILgreedy'  ] = options.RILgreedy
    config['RILdelete'  ] = options.RILdelete
    config['groupMethod'] = options.groupMethod



    if config['groupMethod'] not in methods:
        print "%s not a valid method" % config['groupMethod']
        sys.exit(1)

    config[ 'grouper' ] = methods[ config['groupMethod'] ]



    if options.input is None:
        print "no input file defined"
        sys.exit(1)

    if not os.path.exists(options.input):
        print "input file %s does not exists" % options.input
        sys.exit(1)


Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
116
117
    if not options.fasta:
        config['format'  ]   = 'aln'
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
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


    if ( config['instart'] is not None ) and ( config['inend'] is not None ):
        if config['inend'] <= config['instart']:
            print "end position smaller than start position %d < %d" % (config['inend'] < config['instart'])
            sys.exit(1)

    parallel = False
    if ( config['inchr'] is None ) and ( config['instart'] is None ) and ( config['inend'] is None ):
        parallel = True


    indexFile = config['infile'] + ".idx"

    print "Input File             : %s"              % config['infile']
    print "Index File             : %s (exists: %s)" % ( indexFile, os.path.exists(indexFile) )
    print "Print Reference        : %s"              % ( str(options.noref)                   )

    if not os.path.exists( indexFile ):
        vcfmerger.makeIndexFile( indexFile, config['infile'] )

    config['idx'] = vcfmerger.readIndex(indexFile)

    if config['inchr'] is not None:
        if config['inchr'] not in config['idx']:
            print "requested chromosome %s does not exists" % config['inchr']
            sys.exit(1)
        config['insekpos'] = config['idx'][config['inchr']]


    readSources(config)


    if parallel:
        parallelize(config)
    else:
        readData(config)

    return config


def getOptionInFile(pars, opts):
    """
    Returns input file from argument
    """
    infile = opts.iinput if opts.input is None else opts.iinput

    if infile is None:
        print "no input file given"
        pars.print_help()
        sys.exit(1)


    if isinstance(infile, list):
        if len(infile) > 1:
            print "more than one file given"
            print infile
            pars.print_help()
            sys.exit(1)

        infile = infile[0]
    else:
        infile = infile

    return infile


def parallelize(config):
    """
    Runs in parallel
    """
    print "parallelizing"
    if config['threads' ] == 0:
        config['threads' ] = len( config['idx'] )

    print "num threads %d" % config['threads' ]
    #config['thread' ] = 1
    #sys.exit(0)

    pool    = multiprocessing.Pool(processes=config['threads' ])
    #pool    = multiprocessing.Pool(processes=1)
    results = []

    for chrom, pos in sorted(config['idx'].items(), key=lambda item: item[1]):
        config['inchr'   ] = chrom
        config['insekpos'] = pos

        if chrom in config['ignore' ]:
            continue

        print "parallelizing :: chrom %s" % chrom
        results.append( [chrom, pool.apply_async( readData, [ copy.copy( config ) ] )] )

    while len(results) > 0:
        for info in results:
            try:
                #print "getting result"
                chrom, res = info
                res.get( 5 )
                results.remove( info )
                print "getting result %s. OK" % chrom

            except multiprocessing.TimeoutError:
                #print "getting result FAILED. waiting"
                pass


def readSources(config):
    """
    Read list of VCF files
    """
    print "reading sources"
    print "opening",
    with vcfmerger.openvcffile(config['infile'], 'r') as infhd:
        print "ok"

        print "reading"

        header    = ""

        sources   = {}
        names     = None
        for line in infhd:
            line = line.strip()
            if len(line) == 0: continue
            if line[0]   == '#':
                header += line + "\n"
                if line.startswith('##sources='):
246
                    names = line[10:].split('|')
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
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
                    for name in names:
                        sources[name] = []
                    #pp(sources)

                elif line.startswith('##numsources='):
                    numsources = int(line[13:])
                    if numsources != len(sources):
                        print "error parsing sources", numsources, len(sources),sources, len(names), names
                        sys.exit(1)
                    else:
                        print "num sources:",numsources

                continue
            break


        sppmaxlength = 0
        for spp in sorted(sources):
            sppname = fixsppname( spp )
            if len(sppname) > sppmaxlength: sppmaxlength = len(sppname)

        sppmaxlength += 2
        sppmaxlength  = "%-"+str(sppmaxlength)+"s"

        config['sppmaxlength'] = sppmaxlength
        config['sources'     ] = sources
        config['names'       ] = names
        config['header'      ] = header


def readData(config):
    """
    read data from specific VCF file
    """
    print "reading data"
    config['infhd']  = vcfmerger.openvcffile(config['infile'], 'r')
    config['ouchr']  = None
    config['oufhd']  = None

    runName = "all"

    if config['idx'] is not None:
        print "reading data :: has idx"
        if config['inchr'] is not None:
            print "reading data :: has idx :: seeking chrom %s" % config['inchr']

            config['infhd'].seek( config['insekpos'] )
            runName = config['inchr']

            print "reading data :: has idx :: seeking chrom %s. DONE" % config['inchr']

        else:
            print "reading data :: has idx :: error seeking"
            sys.exit(1)




    print "reading data :: %s" % config['inchr']

    inchr      = config['inchr'      ]
    instart    = config['instart'    ]
    inend      = config['inend'      ]

    config['coords' ] = set()

    refs       = []
    lastChro   = None
    numSnps    = -1
    numSnpsVal = -1
    lastPos    = -1
    lastPosVal = -1
    finalChro  = False
    for line in config['infhd']:
        line = line.strip()
        if len(line) == 0: continue
        if line[0]   == '#':
            continue


        cols     = line.split("\t")


        if len(cols) != 10:
            print line
            sys.exit(1)



        chro =     cols[0]
        posi = int(cols[1])
        src  =     cols[3]
        dst  =     cols[4]
340
        spps =     cols[9].split("|")
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
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



        if lastChro != chro:
            print chro
            if lastChro is not None:
                if inchr is not None:
                    if inchr != lastChro:
                        print "reading data :: %s :: %s :: skipping exporting" % (runName, lastChro)
                        lastChro = chro
                        continue
                    else:
                        finalChro = True

                parse(config, refs, lastChro)
                if finalChro: break

            print "reading data :: %s :: %s :: empting" % (runName, lastChro)
            refs     = []
            lastChro = chro
            lastPos  = -1
            numSnps  = -1

            for spp in config['sources']:
                config['sources'][spp] = []

        if chro in config['ignore' ]:
            continue

        if lastPos != posi:
            if numSnps % 100000 == 0:
                print "reading data :: %s :: %s %12d" % (runName, chro, numSnps)

            numSnps += 1

            lastPos = posi

        if ( inchr is not None ):
            if ( inchr   != chro )                           :
                #sys.stdout.write(".")
                continue
            if ( instart is not None ) and ( posi < instart ):
                #sys.stdout.write("<")
                #print "%d<%d" % (posi, instart),
                continue
            if ( inend   is not None ) and ( posi > inend   ):
                #sys.stdout.write(">")
                #print "%d>%d" % (posi, instart),
                continue


        if lastPosVal != posi:
            if numSnpsVal % 1000 == 0:
                print "reading data :: %s :: %s %12d valid" % (runName, chro, numSnpsVal)

            numSnpsVal += 1

            for spp in config['sources']:
                config['sources'][spp].append(None)

            refs.append(src)
            lastPosVal = posi



        for spp in spps:
            config['sources'][spp][numSnps] = dst
            config['coords' ].add( posi )

        #print '.',


    if ( inchr is None ) or ( inchr == lastChro ):
        parse(config, refs, lastChro)


    if  config['oufhd'] is not None:
        config['oufhd'].write('\n\n')
        config['oufhd'].close()
        config['oufhd'] = None

    print "reading data :: %s :: finished" % runName

    return 0


def parse(config, refs, chro):
    """
    Export alignment either in FASTA or ALN (clustal)
    """
    print "  exporting: chro %s" % chro

    sources = config['sources']
    names   = config['names'  ]

    for spp in sources:
        #print "  parsing %s :: %s" % (chro, spp)

        poses = sources[spp]

        for pos in xrange(len(poses)):
            nuc = poses[pos]

            if nuc is None:
                nuc = refs[pos]

            poses[pos] = nuc


    sourcesStrs = {}
    for spp in names:
        sourcesStrs[spp] = "".join( sources[spp] )

    refsStrs    = "".join( refs )
    emptyStr    = "N"*len(sourcesStrs[spp])


    if config[ 'RIL' ]:
        print 'RIL'
        refsStrs, sourcesStrs, emptyStr = RIL( config, refsStrs, sourcesStrs, emptyStr )




    if config['format'] == 'aln':
        for frag in xrange(0, len(refs), 60):
            #print frag,

            if config['noref'   ]:
                refsfrag = refsStrs[frag:frag+60]
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
471
                printfilealn( config, config['refname'], refsfrag, chro )
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

            for spp in sorted( sourcesStrs ):
                poses     = sourcesStrs[spp]
                posesfrag = poses[frag:frag+60]

                sppname   = fixsppname( spp )
                printfilealn( config, sppname, posesfrag, chro )

            config['oufhd'].write('\n\n')

    elif config['format'] == 'fasta':
        for spp in sorted( sourcesStrs ):
            sppname   = fixsppname( spp )
            poses     = sourcesStrs[spp]
            printfilefasta(config, sppname, poses   , chro)

        if config['noref'   ]:
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
489
            printfilefasta(config, config['refname'], refsStrs, chro)
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
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



    printfilecoords(config, chro)

    if  config['oufhd'] is not None:
        config['oufhd'].write('\n\n')
        config['oufhd'].close()
        config['oufhd'] = None


def RIL( config, refsStrs, sourcesStrs, emptyStr ):
    high, low, valids, invalids     = config[ 'grouper' ]( config, refsStrs, sourcesStrs, emptyStr )

    #valids, invalids                = filterSimilarity( valids, invalids, config, refsStrs, sourcesStrs, emptyStr )

    refsStrs, sourcesStrs, emptyStr = filter( high, low, valids, invalids, config, refsStrs, sourcesStrs, emptyStr )

    return ( refsStrs, sourcesStrs, emptyStr )


def grouper_median( config, refsStrs, sourcesStrs, emptyStr ):
    distMatrix  = {}

    mindist     = sys.maxint
    maxdist     = 0
    dists       = []

    # CALCULATE PAIRWISE, MIN AND MAX DISTANCES
    sourceskeys = sourcesStrs.keys()
    for spp1 in sourceskeys:
        #print "spp1", spp1p, spp1n
        spp1seq = sourcesStrs[ spp1 ]
        dist    = editdist.distance( refsStrs, spp1seq )

        if dist > 0:
            dists.append( dist )

        distMatrix[ spp1 ] = dist
        if dist < mindist:
            mindist = dist

        if dist > maxdist:
            maxdist = dist

    dists.sort()

    print 'MIN DIST      ', mindist
    print 'MAX DIST      ', maxdist
    print 'DISTS         ', len(dists), " ".join(["%3d" % x for x in dists])

    median        = dists[ int((len(dists) / 2) + 0.5) ]
    vals          = [ x for x in dists if x <= median ]

    print 'MEDIAN        ', median

    delta         = maxdist - median

    print 'DELTA         ', delta

    threshold_low = mindist + delta
    threshold_hig = median  - delta

    print 'THRESHOLD LOW ', threshold_low
    print 'THRESHOLD HIGH', threshold_hig

    good_low      = [ k for k in distMatrix if distMatrix[k] <= threshold_low ]
    good_hig      = [ k for k in distMatrix if distMatrix[k] >= threshold_hig ]
    invalids      = [ k for k in distMatrix if distMatrix[k] > threshold_low and distMatrix[k] < threshold_hig ]
    valids        = good_low + good_hig

    good_low.sort()
    good_hig.sort()
    invalids.sort()
    valids.sort()

    print 'GOOD LOW      ', len(good_low), good_low
    print 'GOOD HIGH     ', len(good_hig), good_hig
    print 'INVALIDS      ', len(invalids), invalids
    print 'VALIDS        ', len(valids  ), valids

    #print 'DISTS         ', len(dists), " ".join(["%3d" % x for x in dists])
    #
    #median_global = dists[ int((len(dists) / 2) + 0.5) ]
    #vals_low      = [ x for x in dists if x <= median_global ]
    #vals_hig      = [ x for x in dists if x >  median_global ]
    #
    #print 'MEDIAN GLOBAL ', median_global
    #print 'VALS LOW      ', len(vals_low), " ".join(["%3d" % x for x in vals_low])
    #print 'VALS HIGH     ', len(vals_hig), " ".join(["%3d" % x for x in vals_hig])
    #
    #median_low    = vals_low[ int((len(vals_low) / 2) + 0.5) ]
    #median_hig    = vals_hig[ int((len(vals_hig) / 2) + 0.5) ]
    #
    #print 'MEDIAN LOW    ', median_low
    #print 'MEDIAN HIGH   ', median_hig
    #
    #delta_low     = median_low - mindist
    #delta_hig     = maxdist    - median_hig
    #
    #print 'DELTA LOW     ', delta_low
    #print 'DELTA HIGH    ', delta_hig
    #
    #threshold_low = median_low + delta_low
    #threshold_hig = median_hig - delta_hig
    #
    #print 'THRESHOLD LOW ', threshold_low
    #print 'THRESHOLD HIGH', threshold_hig
    #
    #good_low      = [ k for k in distMatrix if distMatrix[k] <= median_low ]
    #good_hig      = [ k for k in distMatrix if distMatrix[k] >= median_hig ]
    #bad           = [ k for k in distMatrix if distMatrix[k] > median_low and distMatrix[k] < median_hig ]
    #
    #print 'GOOD LOW      ', len(good_low), good_low
    #print 'GOOD HIGH     ', len(good_hig), good_hig
    #print 'BAD           ', len(bad     ), bad

    return ( good_hig, good_low, valids, invalids )



def grouper_linkage( config, refsStrs, sourcesStrs, emptyStr ):
    #entropy_cutoff = config[ 'RILentro' ]
    #
    ##print "len names", len(names)
    #model = bioMixture.getModel(3, len(refsStrs))
    ##print model
    #sourcesKeys = sourcesStrs.keys()
    #dataset = [list(sourcesStrs[x]) for x in sourcesKeys ]
    ##print dataset
    #data    = mixture.DataSet()
    #data.fromList( dataset, IDs=sourcesKeys )
    ##print data
    #model.EM(data, 40, entropy_cutoff)
    #c = model.classify( data, labels=sourcesKeys, entropy_cutoff=entropy_cutoff )#, silent=1)
    #print len(c), c
    #clusters = defaultdict( list )
    #for spppos, clusternum in enumerate(c):
    #    clusters[ clusternum ].append( sourcesKeys[ spppos ] )
    #
    #for key in clusters:
    #    spps = clusters[ key ]
    #
    #    dists = []
    #    for x in range(1, len(spps)):
    #        dists.append( editdist.distance( sourcesStrs[ spps[0] ], sourcesStrs[ spps[x] ] ) )
    #    print 'key', key, 'dists', dists


    tolerance   = config[ 'RILmads'   ]

    distMatrix  = {}

    mindist     = sys.maxint
    maxdist     = 0


    # CALCULATE PAIRWISE, MIN AND MAX DISTANCES
    sourceskeys = sourcesStrs.keys()
    for spp1p, spp1n in enumerate( sourceskeys ):
        #print "spp1", spp1p, spp1n

        for spp2n in sourceskeys[spp1p+1:]:
            #print " spp2", sourceskeys.index(spp2n), spp2n

            sppKey = tuple(set(sorted( [ spp1n, spp2n ] )))

            dist = editdist.distance( sourcesStrs[ spp1n ], sourcesStrs[ spp2n ] )

            #print " sppKey", sppKey, 'dist', dist

            distMatrix[ sppKey ] = dist
            if dist < mindist:
                mindist = dist

            if dist > maxdist:
                maxdist = dist


    # CALCULATE DELTA OF DISTANCE FOR SCALE
    deltadist   = (maxdist - mindist) * 1.0
    print 'min', mindist, 'max', maxdist, 'delta', deltadist


    # NORMALIZE THE DATA
    for sppKey in distMatrix:
        #print "norma %-60s %4d" % ( str(sppKey), distMatrix[ sppKey ] ),
        if deltadist == 0:
            distMatrix[ sppKey ] = 1.0

        else:
            distMatrix[ sppKey ] = ( distMatrix[ sppKey ] - mindist ) / deltadist # convert to percentage
        #print "%.4f" % distMatrix[ sppKey ]


    # SEPARATE IN TWO GROUPS: REFERENCE (d<0.5) AND WILD (d>=0.5)
    tolvalsmin = []
    tolvalsmax = []
    for sppKey in distMatrix:
        spp1 = sppKey[ 0 ]
        spp2 = sppKey[ 1 ]
        dist = distMatrix[ sppKey ]

        if dist >= 0.5:
            tolvalsmax.append( dist )

        else:
            tolvalsmin.append( dist )



    tolvalsmin.sort()
    tolvalsmax.sort()


    # CALCULATE MIDPOINTS FOR EACH GROUP TO CALCULATE MEDIAN
    #print 'tol vals', tolvals
    tolvalsminmidpoint = int( (len(tolvalsmin) / 2) + 0.5)
    tolvalsmaxmidpoint = int( (len(tolvalsmax) / 2) + 0.5)
    print 'tol val min midpoint', tolvalsminmidpoint, 'tol val min len', len( tolvalsmin )
    print 'tol val max midpoint', tolvalsmaxmidpoint, 'tol val max len', len( tolvalsmax )


    # CALCULATE MEDIAN FOR EACH GROUP
    tolvalsminmedian   = tolvalsmin[ tolvalsminmidpoint ]
    tolvalsmaxmedian   = tolvalsmax[ tolvalsmaxmidpoint ]
    print "dist min median", tolvalsminmedian
    print "dist max median", tolvalsmaxmedian


    # CALCULATE MAD
    tolvalsminMADs     = [ abs(x - tolvalsminmedian) for x in tolvalsmin ]
    tolvalsmaxMADs     = [ abs(x - tolvalsmaxmedian) for x in tolvalsmax ]
    tolvalsminMADs.sort()
    tolvalsmaxMADs.sort()
    #for y, x in enumerate(tolvalsmin):
    #    print 'min pos %4d val %.5f diff %.5f' % ( y, x, abs(x - tolvalsminmedian) )
    #for y, x in enumerate(tolvalsmax):
    #    print 'max pos %4d val %.5f diff %.5f' % ( y, x, abs(x - tolvalsmaxmedian) )


    #print 'tol mads', tolMADs
    tolvalsminMAD    = tolvalsminMADs[ tolvalsminmidpoint ]
    tolvalsmaxMAD    = tolvalsmaxMADs[ tolvalsmaxmidpoint ]

    print 'MAD min before', tolvalsminMAD
    print 'MAD max before', tolvalsmaxMAD
    print 'tolerance     ', tolerance
    if tolerance > 0:
        tolvalsminMAD    = tolvalsminMAD * tolerance
        tolvalsmaxMAD    = tolvalsmaxMAD * tolerance

        print 'MAD min tolerance', tolvalsminMAD
        print 'MAD max tolerance', tolvalsmaxMAD

    tolvalsmaxMAD = 1 - tolvalsmaxMAD
    print 'MAD min final', tolvalsminMAD
    print 'MAD max final', tolvalsmaxMAD



    # DEFINE WHICH INDIVIDUALS PASS THRESHOLD OF SIMILARITY
    setsSim = defaultdict( set )
    setsDis = defaultdict( set )
    for sppKey in distMatrix:
        spp1 = sppKey[ 0 ]
        spp2 = sppKey[ 1 ]
        dist = distMatrix[ sppKey ]
        #print "%.4f" % distMatrix[ sppKey ],

        if   dist <= tolvalsminMAD:
            setsSim[ spp1 ].add( spp2 )
            setsSim[ spp2 ].add( spp1 )
            #print '+'

        elif dist >= tolvalsmaxMAD:
            setsDis[ spp1 ].add( spp2 )
            setsDis[ spp2 ].add( spp1 )
            #print '-'

        else:
            #print
            pass

    print 'len sim', len(setsSim), 'dis', len(setsDis)
    similar    = set()
    dissimilar = set()


    # GENERATE FINAL LISTS
    # LIST OF SIMILAR TO REFERENCE
    for spp in setsSim:
        sppset = setsSim[ spp ]
        #print "spp SIM %-30s %s" % ( spp, str( sppset ) )
        similar = similar.union( sppset )

    # LIST OF DISSIMILAR TO REFERENCE
    for spp in setsDis:
        sppset = setsDis[ spp ]
        #print "spp DIS %-30s %s" % ( spp, str( sppset ) )
        dissimilar = dissimilar.union( sppset )


    # MERGE SIMILAR AND DISSIMILAR LISTS INTO VALID LIST
    # ADD REMAINING SPECIES INTO INVALID SPECIES
    shared    = dissimilar.intersection( similar    )
    simonly   = similar.difference(      dissimilar )
    disonly   = dissimilar.difference(   similar    )

    valids    = set().union(similar, dissimilar)
    invalids  = set(sourceskeys).difference(valids)

    print "Similar  ", len(sorted(list( similar    ))), sorted(list( similar    ))
    print "Disimilar", len(sorted(list( dissimilar ))), sorted(list( dissimilar ))
    print
    print "Shared   ", len(sorted(list( shared     ))), sorted(list( shared     ))
    print "Sim Only ", len(sorted(list( simonly    ))), sorted(list( simonly    ))
    print "Dis Only ", len(sorted(list( disonly    ))), sorted(list( disonly    ))
    print
    print "Valids   ", len(sorted(list( valids     ))), sorted(list( valids     ))
    print "Invalids ", len(sorted(list( invalids   ))), sorted(list( invalids   ))

    #Similar   31 ['609', '610', '612', '614', '615', '623', '634', '644', '649', '651', '654', '660', '665', '666', '667', '668', '670', '674', '676', '679', '685', '688', '692', '693', '694', '697', '702', '706', '710', 'Slycopersicum MoneyMaker', 'Spimpinellifolium LYC2740']
    #Disimilar 45 ['609', '610', '612', '614', '615', '618', '623', '625', '626', '634', '644', '649', '651', '653', '654', '659', '660', '665', '666', '667', '668', '670', '674', '676', '679', '684', '685', '688', '691', '692', '693', '694', '696', '697', '702', '705', '706', '707', '710', '711', 'Slycopersicum MoneyMaker', 'Spimpinellifolium LA1578', 'Spimpinellifolium LA1584', 'Spimpinellifolium LYC2740', 'Spimpinellifolium LYC2798']
    #
    #Shared    31 ['609', '610', '612', '614', '615', '623', '634', '644', '649', '651', '654', '660', '665', '666', '667', '668', '670', '674', '676', '679', '685', '688', '692', '693', '694', '697', '702', '706', '710', 'Slycopersicum MoneyMaker', 'Spimpinellifolium LYC2740']
    #Sim Only  0 []
    #Dis Only  14 ['618', '625', '626', '653', '659', '684', '691', '696', '705', '707', '711', 'Spimpinellifolium LA1578', 'Spimpinellifolium LA1584', 'Spimpinellifolium LYC2798']
    #
    #Valids    45 ['609', '610', '612', '614', '615', '618', '623', '625', '626', '634', '644', '649', '651', '653', '654', '659', '660', '665', '666', '667', '668', '670', '674', '676', '679', '684', '685', '688', '691', '692', '693', '694', '696', '697', '702', '705', '706', '707', '710', '711', 'Slycopersicum MoneyMaker', 'Spimpinellifolium LA1578', 'Spimpinellifolium LA1584', 'Spimpinellifolium LYC2740', 'Spimpinellifolium LYC2798']
    #Invalids  20 ['601', '603', '608', '611', '619', '622', '624', '630', '631', '639', '643', '646', '648', '656', '658', '669', '675', '678', '682', '701']

    return ( sorted(list(dissimilar)), sorted(list(similar)), sorted(list( valids )), sorted(list( invalids)) )



def filterSimilarity( high, low, valids, invalids, config, refsStrs, sourcesStrs, emptyStr ):
    minsim      = config[ 'RILminsim' ]

    # CALCULATE PERCENTAGE OF SIMILARITY TO REFERENCE
    refsims = {}

    for spp in valids:
        sppseq = sourcesStrs[ spp ]
        refsim = 0

        for nucPos, nucSeq in enumerate( sppseq ):
            refSeq = refsStrs[ nucPos ]

            if refSeq == nucSeq:
                refsim += 1

        refsims[ spp ] = ( float( refsim ) / float( len( sppseq ) ) )


    #print 'REF', refsStrs
    #print 'ALT', altsStrs


    # REPLACE SEQUENCES WITH EITHER REFERENCE OR ALTERNATIVE
    modsCount = defaultdict( int )
    for spp in sourcesStrs:
        if spp in refsims:
            if refsims[ spp ] > minsim:
                modsCount[ 'REF' ] += 1

            else:
                modsCount[ 'ALT' ] += 1
                invalids[ spp ] = valids[ spp ]
                del valids[ spp ]


    #for sppPos, sppNucs in enumerate( nucs ):
        #print sppNucs, list(sppNucs), dict(sppNucs), sppNucs.values()

    for key in modsCount:
        print "count", key, modsCount[ key ]
    print "count NNN", len( invalids )

    return ( valids, invalids, config, refsStrs, sourcesStrs, emptyStr )


def filter( high, low, valids, invalids, config, refsStrs, sourcesStrs, emptyStr ):
    greedy      = config[ 'RILgreedy' ]

    # IF IN GREEDY MODE, FIX NUCLEOTIDES FROM VALIDs
    if greedy:
        # COUNT NUCLEOTIDES IN EACH POSITION
        nucs = []
        for spp in sourcesStrs:
            sppseq = sourcesStrs[ spp ]
            for nucPos, nuc in enumerate( sppseq ):
                if len( nucs ) <= nucPos:
                    nucs.append( Counter() )
                nucs[ nucPos ].update( [ nuc ] )

        # GENERATE ALTERNATIVE STRING
        altsStrs  = ""
        refsStrsN = ""
        emptyStr  = ""
        for nucPos, nucRef in enumerate( refsStrs ):
            nucData = list( nucs[ nucPos ] )

            if len( nucData ) != 2:
                print "BAD BAD BAD SEQUENCE. MORE THAN TWO VARIANTES TO THE SAME POSITION", nucData
                altsStrs  += '-'
                refsStrsN += '-'
                emptyStr  += '-'

            else:
                #print 'pos %3d data %-10s ref %s' % ( nucPos, str(nucData), nucRef ),
                nucData.remove( nucRef )
                nucAlt     = nucData[0]
                #print 'alt', nucAlt
                altsStrs  += nucAlt
                refsStrsN += nucRef
                emptyStr  += 'N'

        refsStrs = refsStrsN




        for spp in sourcesStrs:
            if spp in low:
                sourcesStrs[ spp ]  = refsStrs
            #print " fixing spp %-30s to REF" % ( spp )
            #print " fixing spp %-30s to ALT" % ( spp )
            #sourcesStrs[ spp ]  = refsStrs
            #sourcesStrs[ spp ]  = altsStrs
            elif spp in high:
                sourcesStrs[ spp ]  = altsStrs



        if config['RILdelete']:
            for spp in sourcesStrs.keys():
                #sppseq = sourcesStrs[spp]
                #= "".join( sources[spp] )
                if spp in invalids:
                    #print "spp", spp, 'is invalid. converting to NNN'
                    del sourcesStrs[spp]





    # CONVERT UNCLUSTERED SEQUENCES TO N
    for spp in sourcesStrs:
        #sppseq = sourcesStrs[spp]
        #= "".join( sources[spp] )
        if spp in invalids:
            #print "spp", spp, 'is invalid. converting to NNN'
            sourcesStrs[spp] = emptyStr



    return refsStrs, sourcesStrs, emptyStr





def printfilename(config, chro, coords=False):
    inchr   = config['inchr'  ]
    instart = config['instart']
    inend   = config['inend'  ]

    posstr = ''

    if (instart is not None) or (inend is not None):
        if (instart is not None) and (inend is not None):
            posstr = '_%06d-%06d' % (instart, inend)

        elif (instart is not None):
            posstr = '_%06d-end' % (instart)

        elif (inend is not None):
            posstr = '_000000-%06d' % (inend)

    ext = '.aln'

    if config['format'] == 'fasta':
        ext = '.fasta'

    if coords:
        ext += '.coords'

    outfile = config['infile'] + '.' + chro + posstr + ext

    return outfile


def printfileopen(config, spp, chro, header=None):
    """
    Save to file
    """
    if config['ouchr'] != chro:
        print "    printing aln chromosome '%s'" % chro
        if config['oufhd'] is not None:
            config['oufhd'].write('\n\n')
            config['oufhd'].close()
            config['oufhd'] = None
        config['ouchr'] = chro

    outfile = printfilename(config, chro)

    if config['oufhd'] is None:
        print "      opening %s" % outfile
        config['oufhd']   = open(outfile, 'w')

For faster browsing, not all history is shown. View entire blame