Back to index

python-biopython  1.60
PrimerSearch.py
Go to the documentation of this file.
00001 """Code to interact with the primersearch program from EMBOSS.
00002 """
00003 
00004 
00005 class InputRecord(object):
00006     """Represent the input file into the primersearch program.
00007 
00008     This makes it easy to add primer information and write it out to the
00009     simple primer file format.
00010     """ 
00011     def __init__(self):
00012         self.primer_info = []
00013 
00014     def __str__(self):
00015         output = ""
00016         for name, primer1, primer2 in self.primer_info:
00017             output += "%s %s %s\n" % (name, primer1, primer2)
00018         return output
00019 
00020     def add_primer_set(self, primer_name, first_primer_seq, 
00021                        second_primer_seq):
00022         """Add primer information to the record.
00023         """
00024         self.primer_info.append((primer_name, first_primer_seq,
00025                                  second_primer_seq))
00026            
00027 class OutputRecord(object):
00028     """Represent the information from a primersearch job.
00029 
00030     amplifiers is a dictionary where the keys are the primer names and
00031     the values are a list of PrimerSearchAmplifier objects.
00032     """
00033     def __init__(self):
00034         self.amplifiers = {}
00035 
00036 class Amplifier(object):
00037     """Represent a single amplification from a primer.
00038     """
00039     def __init__(self):
00040         self.hit_info = ""
00041         self.length = 0
00042 
00043 def read(handle):
00044     """Get output from primersearch into a PrimerSearchOutputRecord
00045     """
00046     record = OutputRecord()
00047 
00048     for line in handle:
00049         if not line.strip():
00050             continue
00051         elif line.startswith("Primer name"):
00052             name = line.split()[-1]
00053             record.amplifiers[name] = []
00054         elif line.startswith("Amplimer"):
00055             amplifier = Amplifier()
00056             record.amplifiers[name].append(amplifier)
00057         elif line.startswith("\tSequence: "):
00058             amplifier.hit_info = line.replace("\tSequence: ", "")
00059         elif line.startswith("\tAmplimer length: "):
00060             length = line.split()[-2]
00061             amplifier.length = int(length)
00062         else:
00063             amplifier.hit_info += line
00064 
00065     for name in record.amplifiers:
00066         for amplifier in record.amplifiers[name]:
00067             amplifier.hit_info = amplifier.hit_info.rstrip()
00068 
00069     return record