def new_motif(sites): """Given sites, return motif object""" sites = listutils.nub_by(sequence.overlap_test, sites) seqs = [site.seq for site in sites] motif_ = motifs.Motif(instances=motifs.Instances(seqs)) motif_.pseudocounts = dict(A=0.25, C=0.25, G=0.25, T=0.25) return Motif(sites, motif_)
def permute(motif): """Permute the given motif by shuffling its columns""" cols = range(length(motif)) random.shuffle(cols) shuffled = [''.join(site[i] for i in cols) for site in seqs(motif)] _motif = motifs.Motif(instances=motifs.Instances(shuffled)) _motif.pseudocounts = pseudocounts(motif) return Motif(None, _motif)
def read(handle): """Parse the text output of the MEME program into a meme.Record object. Example: >>> from Bio.motifs import minimal >>> with open("motifs/meme.out") as f: ... record = minimal.read(f) ... >>> for motif in record: ... print(motif.name, motif.evalue) ... 1 1.1e-22 You can access individual motifs in the record by their index or find a motif by its name: Example: >>> from Bio import motifs >>> with open("motifs/minimal_test.meme") as f: ... record = motifs.parse(f, 'minimal') ... >>> motif = record[0] >>> print(motif.name) KRP >>> motif = record['IFXA'] >>> print(motif.name) IFXA This function wont retrieve instances, as there are none in minimal meme format. """ motif_number = 0 record = Record() _read_version(record, handle) _read_alphabet(record, handle) _read_background(record, handle) while True: for line in handle: if line.startswith('MOTIF'): break else: return record name = line.split()[1] motif_number += 1 length, num_occurrences, evalue = _read_motif_statistics(line, handle) counts = _read_lpm(line, handle) # {'A': 0.25, 'C': 0.25, 'T': 0.25, 'G': 0.25} motif = motifs.Motif(alphabet=record.alphabet, counts=counts) motif.background = record.background motif.length = length motif.num_occurrences = num_occurrences motif.evalue = evalue motif.name = name record.append(motif) assert len(record) == motif_number return record
def ic_at(motif, other, offset): """Return the total IC of two aligned motifs""" alignment_len = min(length(motif) - offset, length(other)) motif_seqs = [site[offset:alignment_len + offset] for site in seqs(motif)] other_seqs = [site[:alignment_len] for site in seqs(other)] # Create the motif and compute the IC amotif = motifs.Motif(instances=motifs.Instances(motif_seqs + other_seqs)) amotif.pseudocounts = dict(A=0.25, C=0.25, G=0.25, T=0.25) return amotif.pssm.mean()
def read(handle): """Read motifs in Cluster Buster position frequency matrix format from a file handle. Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html """ motif_nbr = 0 record = Record() nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []} motif_name = "" for line in handle: line = line.strip() if line: if line.startswith('>'): if motif_nbr != 0: motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_name = line[1:].strip() nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []} motif_nbr += 1 else: if line.startswith('#'): continue matrix_columns = line.split() if len(matrix_columns) == 4: [ nucleotide_counts[nucleotide].append( float(nucleotide_count)) for nucleotide, nucleotide_count in zip( ['A', 'C', 'G', 'T'], matrix_columns) ] motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts) motif.name = motif_name record.append(motif) return record
def read(handle): """Read motifs in Cluster Buster position frequency matrix format from a file handle. Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html """ motif_nbr = 0 record = Record() nucleotide_counts = {"A": [], "C": [], "G": [], "T": []} motif_name = "" for line in handle: line = line.strip() if line: if line.startswith(">"): if motif_nbr != 0: motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_name = line[1:].strip() nucleotide_counts = {"A": [], "C": [], "G": [], "T": []} motif_nbr += 1 else: if line.startswith("#"): continue matrix_columns = line.split() if len(matrix_columns) == 4: [ nucleotide_counts[nucleotide].append( float(nucleotide_count)) for nucleotide, nucleotide_count in zip( ["A", "C", "G", "T"], matrix_columns) ] motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) return record
def handle_motif(self, node): """Read the motif's name and column from the node and add the motif record.""" motif_name = self.get_text(node.getElementsByTagName("name")) nucleotide_counts = {"A": [], "C": [], "G": [], "T": []} for column in node.getElementsByTagName("column"): [nucleotide_counts[nucleotide].append(float(nucleotide_count)) for nucleotide, nucleotide_count in zip(["A", "C", "G", "T"], self.get_acgt(column))] motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name self.record.append(motif)
def pfm2pssm(pfm_file, pseudocount, alphabet, background=None): """ Convert load PFM and convert it to PSSM (take the log_odds) """ pfm = pd.read_table(pfm_file) pfm = pfm.drop(pfm.columns[0], 1).to_dict(orient='list') pfm = motifs.Motif(alphabet=alphabet, counts=pfm) pfm = pfm.counts.normalize(pseudocount) pssm = pfm.log_odds(background=background) pssm = matrix.ExtendedPositionSpecificScoringMatrix(pssm.alphabet, pssm) return pssm
def pwm2pssm(file, pseudocount): """ Convert load PWM and covernt it to PSSM (take the log_odds) """ pwm = pd.read_table(file) # Assuming we are doing RNA motif scanning. Need to replace U with T # as Biopython's motif scanner only does DNA pwm.rename(columns={'U': 'T'}, inplace=True) pwm = pwm.drop("Pos", 1).to_dict(orient='list') pwm = motifs.Motif(alphabet=IUPAC.IUPACUnambiguousDNA(), counts=pwm) pwm = pwm.counts.normalize(pseudocount) # Can optionally add background, but for now assuming uniform probability pssm = pwm.log_odds() # Replace negative infinity values with very low number #for letter, odds in pssm.iteritems(): #pssm[letter] = [-10**6 if x == -float("inf") else x for x in odds] return (pssm)
def _load_pwms(self): """Loads and returns position weight matrices. Returns: a dictionary of pwms, where the key is the CISBP id code """ pwms = {} dir_path = os.path.dirname(os.path.realpath(__file__)) for file in glob.glob(dir_path + "/data/cisbp_rna/pwms/*.txt"): pwm_id = os.path.splitext(os.path.basename(file))[0] try: pwm = pd.read_csv(file, sep="\t", header=0, index_col=0) # biopython can only handle DNA motifs so we replace U with T pwm.rename(columns = {"U":"T"}, inplace=True) pwm = motifs.Motif(alphabet=IUPAC.IUPACUnambiguousDNA(), counts=pwm.to_dict(orient="list")) pwm = pwm.counts.normalize(pseudocounts=0.00001) pwms[pwm_id] = pwm except: # some pwm files are empty - we skip these continue return pwms
def create_matrix_from_file(filename, factor): i = 1 with open(filename, "r") as f: for line in f: if 'Transcription Factor Name: ' + factor in line: while i < 5: i += 1 header = next(f) m = motifs.Motif() a = IUPAC.unambiguous_dna m.add_instance(Seq(header.strip(), a)) l = ('*') j = 1 while j != len(header.strip()): j += 1 l += '*' while header[0] != '\n': header = next(f) if header[0] != '\n': m.add_instance(Seq(header.strip(), a)) m.set_mask(l) if header[0] == '\n': return m
def read(handle): """Parses the text output of the MEME program into a meme.Record object. Example: >>> from Bio.motifs import meme >>> with open("meme.output.txt") as f: ... record = meme.read(f) >>> for motif in record: ... for instance in motif.instances: ... print(instance.motif_name, instance.sequence_name, instance.strand, instance.pvalue) """ motif_number = 0 record = Record() __read_version(record, handle) __read_alphabet(record, handle) __read_background(record, handle) while True: for line in handle: if line.startswith('MOTIF'): break else: return record name = line.split()[1] motif_number += 1 length, num_occurrences, evalue = __read_motif_statistics(line, handle) counts = __read_lpm(line, handle) #{'A': 0.25, 'C': 0.25, 'T': 0.25, 'G': 0.25} motif = motifs.Motif(alphabet=record.alphabet, counts=counts) motif.background = record.background motif.length = length motif.num_occurrences = num_occurrences motif.evalue = evalue motif.name = name record.append(motif) assert len(record) == motif_number return record
def _read_pfm_four_columns(handle): """Read motifs in Cluster Buster position frequency matrix format from a file handle. Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html #cisbp Pos A C G T 1 0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846 2 0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846 3 0.971153846153846 0.00961538461538462 0.00961538461538462 0.00961538461538462 4 0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846 5 0.00961538461538462 0.971153846153846 0.00961538461538462 0.00961538461538462 6 0.971153846153846 0.00961538461538462 0.00961538461538462 0.00961538461538462 7 0.00961538461538462 0.971153846153846 0.00961538461538462 0.00961538461538462 8 0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846 #c2h2 zfs Gene ENSG00000197372 Pos A C G T 1 0.341303 0.132427 0.117054 0.409215 2 0.283785 0.077066 0.364552 0.274597 3 0.491055 0.078208 0.310520 0.120217 4 0.492621 0.076117 0.131007 0.300256 5 0.250645 0.361464 0.176504 0.211387 6 0.276694 0.498070 0.197793 0.027444 7 0.056317 0.014631 0.926202 0.002850 8 0.004470 0.007769 0.983797 0.003964 9 0.936213 0.058787 0.002387 0.002613 10 0.004352 0.004030 0.002418 0.989200 11 0.013277 0.008165 0.001991 0.976567 12 0.968132 0.002263 0.002868 0.026737 13 0.397623 0.052017 0.350783 0.199577 14 0.000000 0.000000 1.000000 0.000000 15 1.000000 0.000000 0.000000 0.000000 16 0.000000 0.000000 1.000000 0.000000 17 0.000000 0.000000 1.000000 0.000000 18 1.000000 0.000000 0.000000 0.000000 19 0.000000 1.000000 0.000000 0.000000 20 1.000000 0.000000 0.000000 0.000000 #c2h2 zfs Gene FBgn0000210 Motif M1734_0.90 Pos A C G T 1 0.25 0.0833333 0.0833333 0.583333 2 0.75 0.166667 0.0833333 0 3 0.833333 0 0 0.166667 4 1 0 0 0 5 0 0.833333 0.0833333 0.0833333 6 0.333333 0 0 0.666667 7 0.833333 0 0 0.166667 8 0.5 0 0.333333 0.166667 9 0.5 0.0833333 0.166667 0.25 10 0.333333 0.25 0.166667 0.25 11 0.166667 0.25 0.416667 0.166667 # flyfactorsurvey (cluster buster) >AbdA_Cell_FBgn0000014 1 3 0 14 0 0 0 18 16 0 0 2 18 0 0 0 1 0 0 17 0 0 6 12 15 1 2 0 # homer >ATGACTCATC AP-1(bZIP)/ThioMac-PU.1-ChIP-Seq(GSE21512)/Homer 6.049537 -1.782996e+03 0 9805.3,5781.0,3085.1,2715.0,0.00e+00 0.419 0.275 0.277 0.028 0.001 0.001 0.001 0.997 0.010 0.002 0.965 0.023 0.984 0.003 0.001 0.012 0.062 0.579 0.305 0.054 0.026 0.001 0.001 0.972 0.043 0.943 0.001 0.012 0.980 0.005 0.001 0.014 0.050 0.172 0.307 0.471 0.149 0.444 0.211 0.195 # hocomoco > AHR_si 40.51343240527031 18.259112547756697 56.41253757072521 38.77363485291994 10.877470982533044 11.870876719950774 34.66312982331297 96.54723985087516 21.7165707818416 43.883079837598544 20.706746561638717 67.6523201955933 2.5465132509466635 1.3171620263517245 145.8637051322628 4.231336967110781 0.0 150.35847450464382 1.4927836298652875 2.1074592421627525 3.441039751299748 0.7902972158110341 149.37613720253387 0.3512432070271259 0.0 3.441039751299748 0.7024864140542533 149.81519121131782 0.0 0.0 153.95871737667187 0.0 43.07922333291745 66.87558226865211 16.159862546986584 27.844049228115868 # neph UW.Motif.0001 atgactca 0.772949 0.089579 0.098612 0.038860 0.026652 0.004653 0.025056 0.943639 0.017663 0.023344 0.918728 0.040264 0.919596 0.025414 0.029759 0.025231 0.060312 0.772259 0.104968 0.062462 0.037406 0.020643 0.006667 0.935284 0.047316 0.899024 0.026928 0.026732 0.948639 0.019497 0.005737 0.026128 # tiffin T A G C 30 0 28 40 0 0 0 99 0 55 14 29 0 99 0 0 20 78 0 0 0 52 7 39 19 46 11 22 0 60 38 0 0 33 0 66 73 0 25 0 99 0 0 0 """ record = Record() motif_name = None motif_nbr = 0 motif_nbr_added = 0 default_nucleotide_order = ["A", "C", "G", "T"] nucleotide_order = default_nucleotide_order nucleotide_counts = {"A": [], "C": [], "G": [], "T": []} for line in handle: line = line.strip() if line: columns = line.split() nbr_columns = len(columns) if line.startswith("#"): # Skip comment lines. continue elif line.startswith(">"): # Parse ">AbdA_Cell_FBgn0000014" and "> AHR_si" like lines and put the part after ">" as motif name. if motif_nbr != 0 and motif_nbr_added != motif_nbr: # Add the previous motif to the record. motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_nbr_added = motif_nbr # Reinitialize variables for the new motif. motif_name = line[1:].strip() nucleotide_order = default_nucleotide_order elif columns[0] == "Gene": # Parse "Gene ENSG00000197372" like lines and put the gene name as motif name. if motif_nbr != 0 and motif_nbr_added != motif_nbr: # Add the previous motif to the record. motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_nbr_added = motif_nbr # Reinitialize variables for the new motif. motif_name = columns[1] nucleotide_order = default_nucleotide_order elif columns[0] == "Motif": # Parse "Motif M1734_0.90" like lines. if motif_nbr != 0 and motif_nbr_added != motif_nbr: # Add the previous motif to the record. motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_nbr_added = motif_nbr # Reinitialize variables for the new motif. motif_name = columns[1] nucleotide_order = default_nucleotide_order elif columns[0] == "Pos": # Parse "Pos A C G T" like lines and change nucleotide order if necessary. if nbr_columns == 5: # If the previous line was not a "Gene ENSG00000197372" like line, a new motif starts here. if motif_nbr != 0 and motif_nbr_added != motif_nbr: # Add the previous motif to the record. motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_nbr_added = motif_nbr nucleotide_order = default_nucleotide_order if set(columns[1:]) == set(default_nucleotide_order): nucleotide_order = columns[1:] elif columns[0] in default_nucleotide_order: # Parse "A C G T" like lines and change nucleotide order if necessary. if nbr_columns == 4: nucleotide_order = default_nucleotide_order if set(columns) == set(default_nucleotide_order): nucleotide_order = columns else: # Parse matrix columns lines and use the correct nucleotide order. if nbr_columns == 4: matrix_columns = columns elif nbr_columns == 5: matrix_columns = columns[1:] else: continue if motif_nbr == motif_nbr_added: # A new motif matrix starts here, so reinitialize variables for the new motif. nucleotide_counts = {"A": [], "C": [], "G": [], "T": []} motif_nbr += 1 [ nucleotide_counts[nucleotide].append( float(nucleotide_count)) for nucleotide, nucleotide_count in zip( nucleotide_order, matrix_columns) ] else: # Empty lines can be separators between motifs. if motif_nbr != 0 and motif_nbr_added != motif_nbr: # Add the previous motif to the record. motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) motif_nbr_added = motif_nbr # Reinitialize variables for the new motif. motif_name = None nucleotide_order = default_nucleotide_order # nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []} if motif_nbr != 0 and motif_nbr_added != motif_nbr: motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) motif.name = motif_name record.append(motif) return record
def _read_pfm_four_rows(handle): """Read motifs in position frequency matrix format from a file handle. Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html #hdpi A 0 5 6 5 1 0 C 1 1 0 0 0 4 G 5 0 0 0 3 0 T 0 0 0 1 2 2 # yetfasco A 0.5 0.0 0.0 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.5 0.0 0.0833333334583333 T 0.0 0.0 0.0 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.0 0.0 0.0833333334583333 G 0.0 1.0 0.0 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.0 1.0 0.249999999875 C 0.5 0.0 1.0 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.5 0.0 0.583333333208333 #flyfactorsurvey ZFP finger A | 92 106 231 135 0 1 780 28 0 700 739 94 60 127 130 C | 138 82 129 81 774 1 3 1 0 6 17 49 193 122 148 G | 270 398 54 164 7 659 1 750 755 65 1 41 202 234 205 T | 290 204 375 411 9 127 6 11 36 20 31 605 335 307 308 # scertf pcm A | 9 1 1 97 1 94 T | 80 1 97 1 1 2 C | 9 97 1 1 1 2 G | 2 1 1 1 97 2 # scertf pfm A | 0.090 0.010 0.010 0.970 0.010 0.940 C | 0.090 0.970 0.010 0.010 0.010 0.020 G | 0.020 0.010 0.010 0.010 0.970 0.020 T | 0.800 0.010 0.970 0.010 0.010 0.020 #idmmpmm > abd-A 0.218451749734889 0.0230646871686108 0.656680805938494 0.898197242841994 0.040694591728526 0.132953340402969 0.74907211028632 0.628313891834571 0.0896076352067868 0.317338282078473 0.321580063626723 0.0461293743372216 0.0502386002120891 0.040694591728526 0.0284994697773065 0.0339342523860021 0.455991516436904 0.0691940615058324 0.0108695652173913 0.0217391304347826 0.0284994697773065 0.0284994697773065 0.016304347826087 0.160127253446448 0.235949098621421 0.590402969247084 0.0108695652173913 0.0339342523860021 0.880567338282079 0.797852598091198 0.206124072110286 0.17762460233298 # JASPAR >MA0001.1 AGL3 A [ 0 3 79 40 66 48 65 11 65 0 ] C [94 75 4 3 1 2 5 2 3 3 ] G [ 1 0 3 4 1 0 5 3 28 88 ] T [ 2 19 11 50 29 47 22 81 1 6 ] or:: >MA0001.1 AGL3 0 3 79 40 66 48 65 11 65 0 94 75 4 3 1 2 5 2 3 3 1 0 3 4 1 0 5 3 28 88 2 19 11 50 29 47 22 81 1 6 """ record = Record() name_pattern = re.compile(r"^>\s*(.+)\s*") row_pattern_with_nucleotide_letter = re.compile( r"\s*([ACGT])\s*[[]*[|]*\s*([0-9.\s]+)\s*[]]*\s*") row_pattern_without_nucleotide_letter = re.compile(r"\s*([0-9.\s]+)\s*") motif_name = None nucleotide_counts = {} row_count = 0 nucleotides = ["A", "C", "G", "T"] for line in handle: line = line.strip() name_match = name_pattern.match(line) row_match_with_nucleotide_letter = row_pattern_with_nucleotide_letter.match( line) row_match_without_nucleotide_letter = row_pattern_without_nucleotide_letter.match( line) if name_match: motif_name = name_match.group(1) elif row_match_with_nucleotide_letter: (nucleotide, counts_str) = row_match_with_nucleotide_letter.group(1, 2) current_nucleotide_counts = counts_str.split() nucleotide_counts[nucleotide] = [ float(current_nucleotide_count) for current_nucleotide_count in current_nucleotide_counts ] row_count += 1 if row_count == 4: motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) if motif_name: motif.name = motif_name record.append(motif) motif_name = None nucleotide_counts = {} row_count = 0 elif row_match_without_nucleotide_letter: current_nucleotide_counts = row_match_without_nucleotide_letter.group( 1).split() nucleotide_counts[nucleotides[row_count]] = [ float(current_nucleotide_count) for current_nucleotide_count in current_nucleotide_counts ] row_count += 1 if row_count == 4: motif = motifs.Motif(alphabet="GATC", counts=nucleotide_counts) if motif_name: motif.name = motif_name record.append(motif) motif_name = None nucleotide_counts = {} row_count = 0 return record
def build(name, freq): m = motifs.Motif(counts=freq) m.name = name return m