def load_chromosome(chr_name): """Load a chromosome and all of its segments. """ cur_chromosome = BasicChromosome.Chromosome(chr_name) chr_segment_info = all_chr_info[chr_name] for seg_info_num in range(len(chr_segment_info)): label, fill_color, scale = chr_segment_info[seg_info_num] # make the top and bottom telomeres if seg_info_num == 0: cur_segment = BasicChromosome.TelomereSegment() elif seg_info_num == len(chr_segment_info) - 1: cur_segment = BasicChromosome.TelomereSegment(1) # otherwise, they are just regular segments else: cur_segment = BasicChromosome.ChromosomeSegment() if label != "": cur_segment.label = label if fill_color is not None: cur_segment.fill_color = fill_color cur_segment.scale = scale cur_chromosome.add(cur_segment) # scale by the size of chromosome 2 cur_chromosome.scale_num = 19 return cur_chromosome
def load_random_chromosome(chr_name): """Generate a chromosome with random information about it. """ cur_chromosome = BasicChromosome.Chromosome(chr_name) num_segments = random.randrange(num_possible_segments) for seg in range(num_segments): # make the top and bottom telomeres if seg == 0: cur_segment = BasicChromosome.TelomereSegment() elif seg == num_segments - 1: cur_segment = BasicChromosome.TelomereSegment(1) # otherwise, they are just regular segments else: cur_segment = BasicChromosome.ChromosomeSegment() color_chance = random.random() if color_chance <= color_prob: fill_color = random.choice(color_choices) cur_segment.fill_color = fill_color id_chance = random.random() if id_chance <= id_prob: id = get_random_id() cur_segment.label = id cur_chromosome.add(cur_segment) return cur_chromosome, num_segments
def test_widget(self): """Try widget derived functionality. """ test_widget = BasicChromosome.ChromosomeSegment() expected_string = "chr_percent = 0.25" # trick to write the properties to a string save_stdout = sys.stdout new_stdout = cStringIO.StringIO() sys.stdout = new_stdout test_widget.dumpProperties() properties = new_stdout.getvalue() sys.stdout = save_stdout assert properties.find(expected_string) >= 0, \ "Unexpected results from dumpProperties: \n %s" % properties properties = test_widget.getProperties() assert properties.has_key("label_size") \ and properties["label_size"] == 6, \ "Unexpected results from getProperties: %s" % properties test_widget.setProperties({"start_x_position": 12}) assert test_widget.start_x_position == 12, \ "setProperties doesn't seem to work right: %s" \ % test_widget.start_x_position
def drawchrom(cvsfile, write_func, *args): """Draw CE chromosome tool. Doesn't need any parameters. """ from Bio.Graphics import BasicChromosome from reportlab.lib.colors import gray, black entries = [("chrI", 15072419), ("chrII", 15279316), ("chrIII", 13783681), ("chrIV", 17493784), ("chrV", 20919398), ("chrX", 17718852)] max_length = max([x[1] for x in entries]) chr_diagram = BasicChromosome.Organism() for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) #Set the length, adding and extra 20 percent for the tolomeres: cur_chromosome.scale_num = max_length * 1.1 # Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = 0.05 * max_length start.fill_color = black cur_chromosome.add(start) #Add a body - using bp as the scale length here. body = BasicChromosome.ChromosomeSegment() body.fill_color = gray body.scale = length cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = 0.05 * max_length end.fill_color = black cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) chr_diagram.draw("simple_chrom.pdf", "Caenorhabditis elegans")
def test_widget(self): """Try widget derived functionality. """ test_widget = BasicChromosome.ChromosomeSegment() expected_string = "chr_percent = 0.25" # trick to write the properties to a string save_stdout = sys.stdout new_stdout = StringIO() sys.stdout = new_stdout test_widget.dumpProperties() properties = new_stdout.getvalue() sys.stdout = save_stdout self.assertTrue( expected_string in properties, "Unexpected results from dumpProperties: \n %s" % properties) properties = test_widget.getProperties() self.assertEqual( properties["label_size"], 6, "Unexpected results from getProperties: %s" % properties) test_widget.setProperties({"start_x_position": 12}) self.assertEqual( test_widget.start_x_position, 12, "setProperties doesn't seem to work right: %s" % test_widget.start_x_position)
def draw_chromosome(sequence): entries = [("Legionella Pneumophilia")] max_len = 30432563 telomere_length = 1000000 chr_diagram = BasicChromosome.Organism() chr_diagram.page_size = (29.7*cm, 21*cm) #A4 landscape for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) cur_chromosome.scale_num = max_len + 2 * telomere_length start = BasicChromosome.TelomereSegment() start.scale = telomere_length cur_chromosome.add(start) body = BasicChromosome.ChromosomeSegment() body.scale = length cur_chromosome.add(body) end = BasicChromosome.TelomereSegement(inverted=True) end.scale = telomere_length cur_chromosome.add(end) chr_diagram.add(cur_chromosome) chr_diagram.draw("Chromosome.pdf", "Legionella Pneumophilia")
def load_chrom(chr_name): """ Generate a chromosome with information """ cur_chromosome = BasicChromosome.Chromosome(chr_name[0]) chr_segment_info = chr_name[1] for seg_info_num in range(len(chr_segment_info)): label, color, scale = chr_segment_info[seg_info_num] # make the top and bottom telomeres if seg_info_num == 0: cur_segment = BasicChromosome.TelomereSegment() elif seg_info_num == len(chr_segment_info) - 1: cur_segment = BasicChromosome.TelomereSegment(1) # otherwise, they are just regular segments else: cur_segment = BasicChromosome.ChromosomeSegment() cur_segment.label = label cur_segment.label_size = 12 cur_segment.fill_color = color cur_segment.scale = scale cur_chromosome.add(cur_segment) cur_chromosome.scale_num = max(END) + (max(END) * .04) return cur_chromosome
def get(self): #查找和查询 s=entity.hosInfo(self.db) #cent_code='004' offset = int(self.get_argument('o',default='1')) rowcount = int(self.get_argument('r',default='10')) offset=(offset-1)*rowcount no=self.get_argument("no",default='') file_id=self.get_argument("file_id",default='') cur=self.db.getCursor() if no=='1': sql="select a.path from public.file a where a.id=%s "%(file_id) cur.execute(sql) row = cur.fetchone() rowdata={} rowdata['rows']=row print(row) filename="/home/ubuntu/pythonff/mdt/mdt/mdtproject/trunk/app/"+row[0] imgfile=filename[:-2]+"svg" imgfile1=filename[:-2]+"1.svg" print(filename) print(imgfile) record = SeqIO.read(filename, "genbank") gd_diagram = GenomeDiagram.Diagram(record.id) gd_track_for_features = gd_diagram.new_track(1, name="Annotated Features") gd_feature_set = gd_track_for_features.new_set() for feature in record.features: if feature.type != "gene": #Exclude this feature continue if len(gd_feature_set) % 2 == 0: color = colors.blue else: color = colors.lightblue gd_feature_set.add_feature(feature, sigil="ARROW", color=color, label=True, label_size = 14, label_angle=0) #I want to include some strandless features, so for an example #will use EcoRI recognition sites etc. for site, name, color in [("GAATTC","EcoRI",colors.green), ("CCCGGG","SmaI",colors.orange), ("AAGCTT","HindIII",colors.red), ("GGATCC","BamHI",colors.purple)]: index = 0 while True: index = record.seq.find(site, start=index) if index == -1 : break feature = SeqFeature(FeatureLocation(index, index+len(site))) gd_feature_set.add_feature(feature, color=color, name=name, label=True, label_size = 10, label_color=color) index += len(site) gd_diagram.draw(format="linear", pagesize='A4', fragments=4, start=0, end=len(record)) #gd_diagram.write("plasmid_linear_nice.pdf", "PDF") #gd_diagram.write("plasmid_linear_nice.eps", "EPS") gd_diagram.write(imgfile, "SVG") gd_diagram.draw(format="circular", circular=True, pagesize=(20*cm,20*cm), start=0, end=len(record), circle_core = 0.5) #gd_diagram.write("plasmid_circular_nice.pdf", "PDF") #gd_diagram.write("plasmid_circular_nice.eps", "EPS") gd_diagram.write(imgfile1, "SVG") elif no=='2': q=0 pdffile="/home/ubuntu/pythonff/mdt/mdt/mdtproject/trunk/app/uploads/tm/"+file_id+".pdf" pdf="uploads/tm/"+file_id+".pdf" file_id=file_id.split(',') sql1="where a.id=%s "%(file_id[q]) for i in range(len(file_id)-1): sql1=sql1+"or a.id=%s "%(file_id[q+1]) q=q+1 sql="select a.path,a.file_name from public.file a %s "%(sql1) cur.execute(sql) row = cur.fetchall() print(row) rowdata={} rowdata['rows']=pdf q=0 a=[] entriess = [] entries = [] for i in range(len(row)): filepath="/home/ubuntu/pythonff/mdt/mdt/mdtproject/trunk/app/"+row[q][0] filename=row[q][1] entriess.append((filename,filepath)) q=q+1 for(name,path) in entriess: record=SeqIO.read(path,"fasta") a.append(len(record)) entries.append((name,len(record))) max_len = max(a) telomere_length = 1000000 #For illustration chr_diagram = BasicChromosome.Organism() chr_diagram.page_size = (29.7*cm, 21*cm) #A4 landscape for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) #Set the scale to the MAXIMUM length plus the two telomeres in bp, #want the same scale used on all five chromosomes so they can be #compared to each other cur_chromosome.scale_num = max_len + 2 * telomere_length #Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = telomere_length cur_chromosome.add(start) #Add a body - using bp as the scale length here. body = BasicChromosome.ChromosomeSegment() body.scale = length cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = telomere_length cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) chr_diagram.draw(pdffile, "Arabidopsis thaliana") elif no=='3': q=0 pdffile="/home/ubuntu/pythonff/mdt/mdt/mdtproject/trunk/app/uploads/tm/"+file_id+".pdf" pdf="uploads/tm/"+file_id+".pdf" file_id=file_id.split(',') sql1="where a.id=%s "%(file_id[q]) for i in range(len(file_id)-1): sql1=sql1+"or a.id=%s "%(file_id[q+1]) q=q+1 sql="select a.path,a.file_name from public.file a %s "%(sql1) cur.execute(sql) row = cur.fetchall() print(row) rowdata={} rowdata['rows']=pdf q=0 a=[] entries = [] for i in range(len(row)): filepath="/home/ubuntu/pythonff/mdt/mdt/mdtproject/trunk/app/"+row[q][0] filename=row[q][1] entries.append((filename,filepath)) q=q+1 for(name,path) in entries: record=SeqIO.read(path,"genbank") a.append(len(record)) max_len=max(a) telomere_length = 1000000 #For illustration chr_diagram = BasicChromosome.Organism() chr_diagram.page_size = (29.7*cm, 21*cm) #A4 landscape for index, (name, filename) in enumerate(entries): record = SeqIO.read(filename,"genbank") length = len(record) features = [f for f in record.features if f.type=="tRNA"] #Record an Artemis style integer color in the feature's qualifiers, #1 = Black, 2 = Red, 3 = Green, 4 = blue, 5 =cyan, 6 = purple for f in features: f.qualifiers["color"] = [index+2] cur_chromosome = BasicChromosome.Chromosome(name) #Set the scale to the MAXIMUM length plus the two telomeres in bp, #want the same scale used on all five chromosomes so they can be #compared to each other cur_chromosome.scale_num = max_len + 2 * telomere_length #Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = telomere_length cur_chromosome.add(start) #Add a body - again using bp as the scale length here. body = BasicChromosome.AnnotatedChromosomeSegment(length, features) body.scale = length cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = telomere_length cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) chr_diagram.draw(pdffile, "Arabidopsis thaliana") self.response(rowdata)
def combcall2draw(cvsfile, write_func, *args): """User specifies several columns to consider, this tool will call regions where either of the column is above its threshold. """ argv = args[0] if len(argv) < 6: sys.stderr.write( "Need 6 extra arguments for 'combcall2draw', options <loc column> <score column1[,score column2,...]> <cutoff1[,cutoff2,cutoff3]> <min length> <max gap> <pdf filename>\ne.g. command: <0> <1,2,3> <0.5,0.6,0.7> <10000> <2000> <a.pdf>, means to use the first column as genome coordinations to call enriched regions from the combinition of #1, #2 and #3, the thresholds to call enriched region are 0.5 for column 1, 0.6 for column 2 and 0.7 for column 3, the minimum length of region is 10k, and the maximum gap to link two nearby regions is 2k. Then the figure will be saved in a.pdf.\n" ) sys.exit() cor_column = cvsfile.fieldnames[int(argv[0])] var_columns = map(lambda x: cvsfile.fieldnames[int(x)], argv[1].split(",")) cutoffs = map(float, argv[2].split(",")) min_len = int(argv[3]) max_gap = int(argv[4]) wtrack = WigTrackI( ) # combined track containing 1 if either of track is above cutoff add_func = wtrack.add_loc for l in cvsfile: cor = l.setdefault(cor_column, None) if not cor or cor == "NA": continue for i in range(len(var_columns)): var_column = var_columns[i] cutoff = cutoffs[i] var = l.setdefault(var_column, None) if var and var != "NA" and float(var) > cutoff: (chrom, start, end) = cor.split(".") add_func(chrom, int(start), 1.1) break wtrack.span = int(end) - int(start) bpeaks = wtrack.call_peaks(cutoff=1.0, min_length=min_len, max_gap=max_gap) #f = argv[5] fhd = open(argv[5].replace("pdf", "bed"), "w") fhd.write(bpeaks.tobed()) from Bio.Graphics import BasicChromosome from reportlab.lib.colors import gray, black, white entries = [("chrI", 15072419), ("chrII", 15279316), ("chrIII", 13783681), ("chrIV", 17493784), ("chrV", 20919398), ("chrX", 17718852)] max_length = max([x[1] for x in entries]) chr_diagram = BasicChromosome.Organism() for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) #Set the length, adding and extra 20 percent for the tolomeres: cur_chromosome.scale_num = max_length * 1.1 # Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = 0.05 * max_length start.fill_color = gray cur_chromosome.add(start) #Add a body - using bp as the scale length here. try: cpeaks = bpeaks.peaks[name] except: cpeaks = [] body_regions = [] last_pos = 0 for p in cpeaks: body_regions.append((p[0] - last_pos, white)) # outside regions body_regions.append((p[1] - p[0], black)) # enriched regions last_pos = p[1] assert p[1] < length body_regions.append((length - last_pos, white)) # last part for b, c in body_regions: body = BasicChromosome.ChromosomeSegment() body.fill_color = c body.scale = b cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = 0.05 * max_length end.fill_color = gray cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) chr_diagram.draw(argv[5], "Highlight regions in Caenorhabditis elegans")
def call1draw(cvsfile, write_func, *args): """Call regions, then plot it in chromosome figure. A combination of drawchrom and call1 """ argv = args[0] if len(argv) < 6: sys.stderr.write( "Need 6 extra arguments for 'call1draw', options <loc column> <score column> <cutoff> <min length> <max gap> <pdf filename>\ne.g. command: <0> <1> <0.5> <10000> <2000> <a.pdf>, means to use the first column as genome coordinations to call enriched regions from the second column, the threshold to call enriched region is 0.5, the minimum length of region is 10k, and the maximum gap to link two nearby regions is 2k. Then the figure will be saved in a.pdf.\n" ) sys.exit() cor_column = cvsfile.fieldnames[int(argv[0])] var_column = cvsfile.fieldnames[int(argv[1])] cutoff = float(argv[2]) min_len = int(argv[3]) max_gap = int(argv[4]) wtrack = WigTrackI() add_func = wtrack.add_loc for l in cvsfile: cor = l.setdefault(cor_column, None) var = l.setdefault(var_column, None) if cor and var and cor != "NA" and var != "NA": (chrom, start, end) = cor.split(".") add_func(chrom, int(start), float(var)) wtrack.span = int(end) - int(start) bpeaks = wtrack.call_peaks(cutoff=cutoff, min_length=min_len, max_gap=max_gap) fhd = open(argv[5].replace("pdf", "bed"), "w") fhd.write(bpeaks.tobed()) from Bio.Graphics import BasicChromosome from reportlab.lib.colors import gray, black, white entries = [("chrI", 15072419), ("chrII", 15279316), ("chrIII", 13783681), ("chrIV", 17493784), ("chrV", 20919398), ("chrX", 17718852)] max_length = max([x[1] for x in entries]) chr_diagram = BasicChromosome.Organism() for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) #Set the length, adding and extra 20 percent for the tolomeres: cur_chromosome.scale_num = max_length * 1.1 # Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = 0.05 * max_length start.fill_color = gray cur_chromosome.add(start) #Add a body - using bp as the scale length here. try: cpeaks = bpeaks.peaks[name] except: cpeaks = [] body_regions = [] last_pos = 0 for p in cpeaks: body_regions.append((p[0] - last_pos, white)) # outside regions body_regions.append((p[1] - p[0], black)) # enriched regions last_pos = p[1] assert p[1] < length body_regions.append((length - last_pos, white)) # last part for b, c in body_regions: body = BasicChromosome.ChromosomeSegment() body.fill_color = c body.scale = b cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = 0.05 * max_length end.fill_color = gray cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) chr_diagram.draw(argv[5], "%s regions in Caenorhabditis elegans" % (var_column))
chr_diagram.page_size = (29.7 * cm, 21 * cm) #A4 landscape for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) #Set the scale to the MAXIMUM length plus the two telomeres in bp, #want the same scale used on all five chromosomes so they can be #compared to each other cur_chromosome.scale_num = max_len + 2 * telomere_length #Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = telomere_length cur_chromosome.add(start) #Add a body - using bp as the scale length here. body = BasicChromosome.ChromosomeSegment() body.scale = length cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = telomere_length cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) os.chdir('C:\\Users\\qxs\\Documents\\Python Scripts') chr_diagram.draw("Brapa_chrom.pdf", "Brassica rapa") ###### simcoal2
def main(): from taolib.CoreLib.Parser.BedIO import parse_BED from Bio.Graphics import BasicChromosome if len(sys.argv) < 3: sys.stderr.write( "Draw Chromosome Figure\nneed 2 paras: %s <dbname> <color> <bed file>\n" % sys.argv[0]) sys.exit(1) try: entries = get_chrom_length(sys.argv[1]) except: error("Error!") sys.exit(1) col = getCol(sys.argv[2]) bpeaks = parse_BED(open(sys.argv[3])) pdffile = sys.argv[3] + ".pdf" max_length = max([x[1] for x in entries]) chr_diagram = BasicChromosome.Organism() for name, length in entries: cur_chromosome = BasicChromosome.Chromosome(name) cur_chromosome.title_size = 0.5 #Set the length, adding and extra 20 percent for the tolomeres: cur_chromosome.scale_num = max_length * 1.1 # Add an opening telomere start = BasicChromosome.TelomereSegment() start.scale = 0.05 * max_length start.fill_color = gray cur_chromosome.add(start) #Add a body - using bp as the scale length here. try: cpeaks = bpeaks.peaks[name] except: cpeaks = [] body_regions = [] last_pos = 0 for p in cpeaks: body_regions.append((p[0] - last_pos, white)) # outside regions body_regions.append((p[1] - p[0], col)) # enriched regions last_pos = p[1] assert p[1] < length body_regions.append((length - last_pos, white)) # last part for b, c in body_regions: body = BasicChromosome.ChromosomeSegment() body.fill_color = c body.scale = b cur_chromosome.add(body) #Add a closing telomere end = BasicChromosome.TelomereSegment(inverted=True) end.scale = 0.05 * max_length end.fill_color = gray cur_chromosome.add(end) #This chromosome is done chr_diagram.add(cur_chromosome) chr_diagram.draw(pdffile, "Highlight regions")