Exemplo n.º 1
0
 def setUp(self):
     ref_seq = Seq.Seq('GA' * 9 + 'GC' * 6 + 'GT' * 15)
     query_seq = 'AA' * 4 + 'GA' * 5 + 'AC' + 'GC' * 5 + 'GT' * 15
     aln_string = '>seq1\n{0}'.format(query_seq)
     self.aln = Alignment(helpers.parse_fasta(aln_string).values(),
                          reference_sequence=ref_seq)
     self.mutation_patterns = [mut_pattern.GA, mut_pattern.GM]
Exemplo n.º 2
0
 def setUp(self):
     aln_string = """
     >seq1
     GTCAGTCAGTCAGTCACCCC
     >seq2
     GTCAGTCAGTCAGTCACCCC
     >seq3
     ATCAATCAGTCAATCACCCC
     """
     self.seqs = helpers.parse_fasta(aln_string)
     self.aln = Alignment(self.seqs.values())
Exemplo n.º 3
0
 def setUp(self):
     aln_string = """
     >seq1
     GGTGACGCT
     >seq2
     AGTAACGCT
     >seq3
     GGTAACACT
     """
     ref_seq = Seq.Seq('GGTGACGCT')
     self.aln = Alignment(helpers.parse_fasta(aln_string).values(),
                          reference_sequence=ref_seq)
Exemplo n.º 4
0
 def setUp(self):
     ref_seq = helpers.parse_fasta("""
     >all
     GGGGGGGGGTGTGTGTGT""")
     self.aln = Alignment(helpers.parse_fasta("""
     >seq1
     GGGGGGGGGTGTGTGTGT
     >seq2
     AGAGAGAGGTGTGTGTGT
     >seq3
     GGGGGGGGGTATATATAT
     """).values(),
                          reference_sequence=ref_seq['all'])
Exemplo n.º 5
0
 def setUp(self):
     aln_string = """
     >seq1
     GTCAGTCAGTCAGTCA
     GTCAGTCAGTCAGTCA
     >seq2
     GTCAGTCAGTCAGTCA
     GTCAGTCAGTCAGTCA
     >seq3
     ATCAATCAGTCAATCG
     ATCAATCAGTCAATCG"""
     self.seqs = helpers.parse_fasta_list(aln_string)
     self.aln = Alignment(self.seqs)
Exemplo n.º 6
0
def split(args):
    hm_col_reader = csv.DictReader(args.columns)
    hm_columns = map(lambda x: int(x['column']), hm_col_reader)
    hm_columns = list(set(hm_columns))

    seq_records = SeqIO.parse(args.alignment, 'fasta')
    aln = Alignment(seq_records)
    aln.split_hypermuts(hm_columns=hm_columns)

    fn_base = path.join(args.out_dir, args.prefix)
    hm_pos_handle = open(fn_base + '.pos.fasta', 'w')
    hm_neg_handle = open(fn_base + '.neg.fasta', 'w')

    AlignIO.write(aln.hm_pos_aln, hm_pos_handle, 'fasta')
    AlignIO.write(aln.hm_neg_aln, hm_neg_handle, 'fasta')

    for handle in [args.alignment, args.columns, hm_pos_handle, hm_neg_handle]:
        handle.close()
Exemplo n.º 7
0
def analyze(args):
    import logging
    logging.captureWarnings(True)
    # Fetch sequence records and analysis patterns
    seq_records = SeqIO.to_dict(SeqIO.parse(args.alignment, 'fasta'))
    patterns = [mut_pattern.patterns[p] for p in args.patterns]
    pattern_names = [p.name for p in patterns]
    prefix = path.join(args.out_dir, args.prefix)
    analysis_settings = dict(rpr_cutoff=args.rpr_cutoff,
                             significance_level=args.significance_level,
                             quants=args.quants,
                             pos_quants_only=args.pos_quants_only,
                             caller=args.caller,
                             prior=args.prior,
                             cdfs=args.cdfs,
                             quadr_maxiter=args.quadr_maxiter,
                             optim_maxiter=args.optim_maxiter)

    # Need to think about how best to fork things here; for instance, might make sense to let the user specify
    # the initial clusters for whatever reason... However, specifying the reference sequences shouldn't make
    # any sense there
    if args.reference_sequences:
        reference_sequences = SeqIO.to_dict(
            SeqIO.parse(args.reference_sequences, 'fasta'))
    else:
        reference_sequences = None

    # This lets the cluster map be optional, so that this script can be used
    # for naive hm filtering/analysis
    cluster_map = load_cluster_map(
        args.cluster_map,
        cluster_col=args.cluster_col) if args.cluster_map else None
    alignments = AlignmentSet(seq_records,
                              cluster_map,
                              consensus_threshold=args.consensus_threshold,
                              reference_sequences=reference_sequences)

    # Create the analysis generator
    analysis = alignments.multiple_context_analysis(patterns,
                                                    **analysis_settings)

    if args.cluster_threshold:
        for hm_it in range(args.cluster_iterations - 1):
            print " ..On hm/cluster iteration", hm_it
            # Grab the HM columns from the most recent analysis and split out the pos sites
            hm_columns = []
            for result in analysis:
                hm_columns += result['call']['mut_columns']
            hm_neg_aln = Alignment(
                seq_records.values()).split_hypermuts(hm_columns).hm_neg_aln
            # Cluster with the specified settings
            clustering = alnclst.Clustering(hm_neg_aln, args.cluster_threshold,
                                            args.consensus_threshold)
            clustering = clustering.recenter(args.recentering_iterations)
            clustering.merge_small_clusters(args.min_per_cluster)
            cluster_map = parse_clusters(clustering.mapping_iterator(),
                                         cluster_key=0,
                                         sequence_key=1)
            # Create the Alignment set
            clustered_alignment = AlignmentSet(
                seq_records,
                cluster_map,
                consensus_threshold=args.consensus_threshold)
            analysis = clustered_alignment.multiple_context_analysis(
                patterns, **analysis_settings)
        # write out the final clusters
        clusterout_handle = file(prefix + '.clst.csv', 'w')
        clustering.write(clusterout_handle)

    if args.interactive:
        local = copy.copy(locals())
        import hyperfreq
        local.update(
            dict(hyperfreq=hyperfreq,
                 Alignment=Alignment,
                 AlignmentSet=AlignmentSet,
                 mut_pattern=mut_pattern,
                 write_analysis=write_analysis))
        code.interact(local=local)

    # Write the final analysis to file
    write_analysis(analysis,
                   prefix,
                   pattern_names,
                   args.quants,
                   args.cdfs,
                   call_only=args.call_only)
    if args.write_references:
        write_reference_seqs(alignments, prefix)

    # Closing files
    args.alignment.close()
    if args.cluster_map:
        args.cluster_map.close()