Beispiel #1
0
    def _main(self):
        alifile = '/home/allis/Documents/INMI/Fervidicoccales-signature/arb-silva.de_2016-04-06_id331139.fasta'
        group_names = ['Fervidicoccales', 'Acidilobales', 'Desulfurococcales', 'Thermoproteales:Sulfolobales', 'Other']
        predefined_positions = [34, 501, 544, 1244, 1293]
        ref_name = 'Escherichia'
        reference = None
        groups = ListDB()
        with user_message('Loadding initial alignment...', '\n'):
            ali = AlignmentUtils.load_first(alifile)
            if not ali: return 1
        with user_message('Sorting alignment into subgroups...', '\n'):
            for rec in ali:
                if ref_name in rec.description:
                    reference = rec
                    continue
                found = False
                for g in group_names:
                    for k in g.split(':'):
                        if k in rec.description:
                            groups[g] = rec
                            found = True
                            break
                if not found: groups['Other'] = rec
            groups = dict((n, AlignmentExt(groups[n])) for n in groups)
        ali_len = ali.get_alignment_length()
        predefined_positions = [self._col_index(i, reference) for i in predefined_positions]

        print ('\nReference sequence:\n>%s\n%s' %
               (reference.description, str(reference.seq).replace('.', '').replace('-', '')))
        print '\nAlignment: %d seqs, %d columns' % (len(ali), ali_len)
        print print_table([(g, '%d sequences' % len(groups[g])) for g in group_names])
        print

        main_group = group_names[0]
        main_ali = groups[main_group]
        others = group_names[1:]
        for ci in xrange(ali_len):
            main_letter = self.LetterStats(main_ali[:,ci])
            predef = ci in predefined_positions
            if predef or main_letter.freq_no_gaps >= 0.95 and main_letter.freq > 0.5:
                other_letters = [self.LetterStats(groups[g][:,ci]) for g in others]
                if predef or any(l.letter != main_letter.letter for l in other_letters):
                    print ('------------------ E.coli position: %d ---------------------' %
                           (self._ref_index(ci, reference)+1))
                    print print_table([(main_group, str(main_letter))]+
                                      [(g, str(l)) for g, l in zip(others, other_letters)])
                    print
        print 'Done'
Beispiel #2
0
 def _main(self):
     min_prod = 400
     silva_db = '/home/allis/Documents/INMI/SILVA-DB/SILVA_123_SSURef_Nr99_tax_silva.fasta'
     alifile = '/home/allis/Documents/INMI/SunS-metagenome/Bathy/BA2_SunS_16S.aln.fasta'
     add_filename = FilenameParser.strip_ext(alifile)+'.with_additions.fasta'
     outgroups = ['Thermococcus_chitonophagus', 'SMTZ1-55', 'contig72135_1581_sunspring_meta']
     add = ['KF836721.1.1270','EU635905.1.1323']
     exclude = []#['Thermococcus_chitonophagus', 'SMTZ1-55', 'BA1-16S', 'contig72135_1581_sunspring_meta']
     #load alignment
     if os.path.isfile(add_filename): 
         alifile = add_filename
         add_filename = ''
     with user_message('Loadding initial alignment...', '\n'):
         orig_ali = AlignmentUtils.load_first(alifile)
         if not orig_ali: return 1
     #load homologs
     if add_filename:
         with user_message('Loadding additional sequences...', '\n'):
             add_seqs = []
             db = SeqView()
             if db.load(silva_db):
                 for sid in add:
                     seq = db.get(sid)
                     if seq: add_seqs.append(seq)
                     else: print '%s not found in %s' % (sid, silva_db)
         #realign data if needed
         if add_seqs:
             with user_message('Realigning data...', '\n'):
                 add_filename = FilenameParser.strip_ext(alifile)+'.with_additions.fasta'
                 AlignmentUtils.align(list(orig_ali)+add_seqs, add_filename)
                 orig_ali = AlignmentUtils.load_first(add_filename)
                 if not orig_ali: return 2
     #process the alignment
     ali = orig_ali.remove(*exclude).trim()
     for out in outgroups:
         if not ali.index(out):
             print '%s not found in the alignment' % out
             return 3
     ali.sort(key=lambda r: 'zzzzzzzz' if r.id in outgroups else r.id)
     ali_len = ali.get_alignment_length()
     AlignmentUtils.save(ali, '/home/allis/Documents/INMI/SunS-metagenome/Bathy/BA2_SunS_16S.aln.trimmed.fasta')
     args = dict(plen = (20,40),
                 max_mismatches = 8,
                 min_match_mismatches = 1,
                 first_match_mismatches = 1,
                 first_may_match = 1,
                 AT_first=True,
                 outgroup=len(outgroups))
     fprimers = self._find_primers(ali, **args)
     rprimers = self._find_primers(ali.reverse_complement(), **args)
     pairs = []
     for i, (fs, fp) in enumerate(fprimers):
         start = fs
         fprimer = Primer.from_sequences(fp[:-1], 1, 'SSBaF%d' % fs)
         for _j, (rs, rp) in enumerate(rprimers):
             end = ali_len-rs
             if end-start <= min_prod: continue
             pairs.append((fprimer, Primer.from_sequences(rp[:-1], 1, 'SSBaR%d' % (ali_len-rs+1))))
     if not pairs:
         print '\nNo suitable primer pairs found'
         return 3
     added = set()
     for i, (fp, rp) in enumerate(pairs):
         print '\npair %d' % (i+1)
         print '%s: %s' % (fp.id, fp)
         print '%s: %s' % (rp.id, rp)
         if fp.id not in added:
             orig_ali.append(fp.master_sequence+'-'*(orig_ali.get_alignment_length()-len(fp)))
             added.add(fp.id)
         if rp.id not in added:
             orig_ali.append(copy_attrs(rp.master_sequence,
                                        rp.master_sequence.reverse_complement())+
                             '-'*(orig_ali.get_alignment_length()-len(rp)))
             added.add(rp.id)
     print
     orig_ali = AlignmentUtils.align(orig_ali)
     AlignmentUtils.save(orig_ali, '/home/allis/Documents/INMI/SunS-metagenome/Bathy/BA2_SunS_16S.with_primers.aln.fasta')
     print 'Done'
Beispiel #3
0
 def _main(self):
     min_prod = 400
     silva_db = '/home/allis/Documents/INMI/SILVA-DB/SILVA_123_SSURef_Nr99_tax_silva.fasta'
     alifile = '/home/allis/Documents/INMI/SunS-metagenome/Bathy/BA2_SunS_16S.aln.fasta'
     add_filename = FilenameParser.strip_ext(
         alifile) + '.with_additions.fasta'
     outgroups = [
         'Thermococcus_chitonophagus', 'SMTZ1-55',
         'contig72135_1581_sunspring_meta'
     ]
     add = ['KF836721.1.1270', 'EU635905.1.1323']
     exclude = [
     ]  #['Thermococcus_chitonophagus', 'SMTZ1-55', 'BA1-16S', 'contig72135_1581_sunspring_meta']
     #load alignment
     if os.path.isfile(add_filename):
         alifile = add_filename
         add_filename = ''
     with user_message('Loadding initial alignment...', '\n'):
         orig_ali = AlignmentUtils.load_first(alifile)
         if not orig_ali: return 1
     #load homologs
     if add_filename:
         with user_message('Loadding additional sequences...', '\n'):
             add_seqs = []
             db = SeqView()
             if db.load(silva_db):
                 for sid in add:
                     seq = db.get(sid)
                     if seq: add_seqs.append(seq)
                     else: print '%s not found in %s' % (sid, silva_db)
         #realign data if needed
         if add_seqs:
             with user_message('Realigning data...', '\n'):
                 add_filename = FilenameParser.strip_ext(
                     alifile) + '.with_additions.fasta'
                 AlignmentUtils.align(
                     list(orig_ali) + add_seqs, add_filename)
                 orig_ali = AlignmentUtils.load_first(add_filename)
                 if not orig_ali: return 2
     #process the alignment
     ali = orig_ali.remove(*exclude).trim()
     for out in outgroups:
         if not ali.index(out):
             print '%s not found in the alignment' % out
             return 3
     ali.sort(key=lambda r: 'zzzzzzzz' if r.id in outgroups else r.id)
     AlignmentUtils.save(
         ali,
         '/home/allis/Documents/INMI/SunS-metagenome/Bathy/BA2_SunS_16S.aln.trimmed.fasta'
     )
     args = dict(plen=(20, 40),
                 max_mismatches=8,
                 min_match_mismatches=1,
                 first_match_mismatches=1,
                 first_may_match=1,
                 AT_first=True,
                 outgroup=len(outgroups))
     fprimers = PrimerFinder.find_discriminating_primers(ali, **args)
     rprimers = PrimerFinder.find_discriminating_primers(ali,
                                                         reverse=True,
                                                         **args)
     pairs = PrimerFinder.compile_pairs(fprimers, rprimers, min_prod,
                                        'SSBa')
     if not pairs:
         print '\nNo suitable primer pairs found'
         return 3
     PrimerFinder.print_pairs(pairs)
     orig_ali = PrimerFinder.add_pairs_to_alignment(pairs, orig_ali)
     AlignmentUtils.save(
         orig_ali,
         '/home/allis/Documents/INMI/SunS-metagenome/Bathy/BA2_SunS_16S.with_primers.aln.fasta'
     )
     print 'Done'