Wednesday, February 3, 2010

ฉบับที่ 6 (กุมภาพันธ์ 2010)

Opening

วันเวลาช่างผ่านไปเร็วเหลือเกินครับ เผลอแผลบเดียวถึงคราวที่ THAI BIOINFORMATICS ฉบับใหม่ต้องออกวางแผงออนไลน์เสียแล้ว หลายคนคงจะปรับตัวเข้ากับการทำงานเต็มรูปแบบหลังจากที่ได้พักยาวช่วงปลายปีกันไปแล้วนะครับ ถ้าใครยังไม่เข้าที่ก็เดินเครื่องกันหน่อยนะครับ

หลายเดือนที่ผ่านมา เพื่อนสมาชิกและผู้อ่านบางคนติงมาว่า ตัวหนังสือในเวบไม่แสดงผลเป็นภาษาไทยบ้าง หรือขนาดตัวอักษรเล็กไปบ้าง ทางทีมงานก็ได้เข้ามาแก้ไขปัญหาตรงจุดนี้ โดยออกวารสารในรูปแบบ PDF ให้ท่านทั้งหลายได้ download กลับไปอ่านกันครับ และเราก็ถือโอกาสนี้ เปิดตัวทีมงานในเบื้องต้นไปพร้อมกันเลย และไม่เพียงเท่านี้เรายังมีอะไรใหม่ๆ เพิ่มเติมให้ท่านผู้อ่านด้วยครับ สิ่งแรกก็คือ ภาพดอกไม้สวยๆ จากฝีมือของ ดร. แมกนุส ลีเด็น (Dr. Magnus Lidén) นักวิจัยจากมหาวิทยาลัยอุปซอลา (Uppsala University) ประเทศสวีเดนและยังเป็นหัวหน้า Botanic Garden ของมหาวิทยาลัยอีกด้วย ทีแรก ผมก็ไม่แน่ใจว่า ดร. แมกนุส จะสนใจเข้ามาร่วมทำวารสาร THAI BIOINFORMATICS กับพวกเราหรือเปล่า เพราะที่เราไว้ ส่วนมากก็จัดทำกันเป็นภาษาไทย แต่พอผมได้คุยให้ฟังและส่งต้นฉบับให้ดู ปรากฏว่า ดร. แมกนุส กลับยินดีช่วยเหลืออย่างเต็มที่ อันนี้ผมถือว่าท่านให้เกียรติกับ e-magazine ของเราเป็นอย่างมาก

นอกจากนี้ เรายังมี คอลัมน์ใหม่ที่เปิดประเดิมในฉบับนี้เป็นฉบับแรก นั่นคือ Talk to... ครับ อันที่จริง ทีมงานได้คุยกันมานานกว่า 2 เดือนแล้วว่าอยากให้มีการสัมภาษณ์นักวิจัยที่ทำงานวิจัยด้าน bioinformatics หรือเกี่ยวข้องกับ bioinformatics ไม่ว่าจะมากหรือน้อยก็ตาม แล้วก็นำมาเล่าสู่กันฟังน่าจะเป็นเรื่องราวที่เต็มเปี่ยมไปด้วยคุณประโยชน์และความสนุกสนาน เพราะผมเชื่อว่าการเรียนรู้จากประสบการณ์ของบุคคลอื่นเป็นการเรียนรู้ที่มีค่ามหาศาลมาก พวกเราขบคิดกันหลายตลบว่าใครควรจะเข้ามาทำงานตรงจุดนี้ ในที่สุดเราก็ได้ผู้ที่มีความเหมาะสมในการทำคอลัมน์นี้ นั่นก็คือน้องก้อย (จิตสุพางค์ รอดบำเรอ) ครับ ด้วยความน่ารัก ช่างพูดและเป็นกันเองของน้องก้อย ทำให้การสัมภาษณ์บุคคลต่างๆ เป็นไปได้ง่ายและเป็นธรรมชาติมาก ผมและทีมงานก็อยากจะขอเป็นกำลังใจให้น้องก้อยและคอลัมน์ Talk to... อยู่กับ THAI BIOINFORMATICS ไปตราบนานเท่านานนะครับ

สุดท้ายครับ พวกเรากำลังจะมีการเปลี่ยนแปลงครั้งใหญ่เกี่ยวกับเวบไซต์ และรูปแบบการทำ e-Magazine ทำให้ช่วงนี้อาจมีข้อติดขัดบางประการอยู่บ้าง ก็ขออภัยมา ณ ที่นี้ด้วยครับ ผมหวังว่าผู้อ่านทุกคนจะยังคงเป็นแฟนและให้กำลังใจพวกเราต่อไปนะครับ

Monday, February 1, 2010

Highlight

Sequence Analysis: from sequence to information
ประเวช อรรจวัฒนวงศ์

พบกันอีกครั้งกับ Highlight เดือนกุมภาพันธ์นะครับ ฉบับนี้ผมจะพาท่านผู้อ่านมาทำความรู้จักกับ bioinformatics ในยุคบุกเบิกกันครับ เราจะย้อนไปตั้งต้นเรื่องราวกันในยุคก่อนที่จะมี bioinformatics สักเล็กน้อย สมัยนั้นยังไม่มีโปรแกรมคอมพิวเตอร์เพื่อการวิเคราะห์ข้อมูลด้านชีววิทยามากมายเหมือนในปัจจุบัน อันที่จริงต้องบอกว่า แค่จะหามาใช้สักโปรแกรมหนึ่งก็ต้องพลิกแผ่นดินหากันเลยทีเดียวมิหนำซ้ำอินเตอร์เน็ตก็ช้า และยังไม่เป็นที่นิยมอีกด้วย คิดดูสิครับ สภาพแวดล้อมแบบนั้น จะให้มี bioinformatics เกิดขึ้นมาได้อย่างไร เอาเป็นว่าผมจะเล่าประวัติของ bioinformatics แบบสังเขปให้ฟังครับว่าเกิดขึ้นมาได้อย่างไร

ผมขอเริ่มเรื่องราวทั้งหมดในปี ค.ศ. 1955 เมื่อนักชีวเคมีชาวอังกฤษชื่อ เฟเดอริก แซงเกอร์ (Frederick Sanger) ได้นำเสนอเทคนิคการทำ protein sequencing และตีพิมพ์ protein sequence ของ insulin gene เป็นครั้งแรกในโลก จากผลงานชิ้นสำคัญนี้ ทำให้เขาได้รับรางวัลโนเบลสาขาเคมีในปี ค.ศ. 1958 และงานของเขายังกระตุ้นวงการชีววิทยาในยุคนั้นให้มีการทำ protein sequence มากขึ้น แต่เนื่องจากขั้นตอนการทำ protein sequencing ไม่ง่ายเลย และความยาวของ protein sequence ที่อ่านได้ก็สั้นมาก (คืออ่านได้ความยาวเพียง 4-10 ตำแหน่งเท่านั้น) อีกทั้งค่าใช้จ่ายในการทำก็สูงมาก และในเวลาเพียงไม่นาน กระแสความสนใจจากนักวิทยาศาสตร์ก็จางลง แม้แต่ในปัจจุบัน การทำ protein sequencing ก็ยังคงไม่เป็นที่นิยม ด้วยข้อจำกัดเดียวกับที่มีในอดีตนั่นแหละครับ

ในปี ค.ศ. 1958 กระทรวงกลาโหมของสหรัฐอเมริกาได้มีการจัดตั้งองค์กรที่มีชื่อว่า Advanced Research Projects Agency (ARPA) เพื่อค้นคว้าหาเทคโนโลยีทางการทหารใหม่ๆ ด้าน computer network จนกระทั่งในปี ค.ศ. 1969 ARPANET ประสบความสำเร็จในการสร้าง network เพื่อเชื่อมโยงคอมพิวเตอร์จากมหาวิทยาลัยสแตนฟอร์ด (Stanford) มหาวิทยาลัยแคลิฟอร์เนีย ซานตาบาบารา (UCSB) มหาวิทยาลัยอูธา (University of Utah) และมหาวิทยาลัยแคลลิฟอเนีย ลอสแองเจลลิส (UCLA) เข้าด้วยกัน กลายเป็นจุดเริ่มต้นของระบบอินเตอร์เน็ตที่เราใช้งานอยู่ในปัจจุบัน

ปี ค.ศ. 1972 โปรแกรมเมอร์ชาวอเมริกัน ชื่อ เรย์มอนด์ ซามูเอล ทอมลินสัน (Raymond Samuel Tomlinson) ได้สร้าง e-mail ขึ้นมาใช้งานเป็นครั้งแรกในระบบเครือข่ายของ ARPANET ตั้งแต่ช่วงนั้นเป็นต้นมา วงการคอมพิวเตอร์มีการเปลี่ยนแปลงอย่างมากมายและรวดเร็ว เพียง 1 ปีต่อมา (ค.ศ. 1973) ethernet ก็เกิดขึ้น จากงานวิทยานิพนธ์ของนายโรเบิร์ต เม็ทแคลฟี (Robert Metcalfe) ซึ่งได้จบการศึกษาระดับปริญญาเอกจากหาวิทยาลัยฮาร์เวิร์ด (Harvard University) และปีต่อมา (ค.ศ. 1974) ระบบอินเตอร์เน็ตก็ถูกสร้างขึ้น

กลับมาที่วงการชีววิทยาบ้างนะครับ ปี ค.ศ. 1977 แซงเกอร์ (นักชีวเคมีชาวอังกฤษ คนเดิม) ก็ประสบความสำเร็จในการทำ DNA sequencing แต่ความสำเร็จในครั้งนี้ต่างจากอดีต เพราะเทคนิคใหม่นี้กลับได้รับความนิยมเป็นอย่างมากจากนักวิจัยทั่วโลก จนถือว่านี่เป็นจุดเปลี่ยนของวงการชีววิทยาให้เข้าสู่ยุค sequencing ก็ว่าได้ ความสำเร็จดังกล่าว ทำให้ ศาสตรจารย์ เฟเดอริก แซงเกอร์ ได้รับรางวัลโนเบลสาขาเคมีเป็นครั้งที่ 2 ในปี ค.ศ. 1980

ค.ศ. 1984 นักชีวเคมีชาวสหรัฐอเมริกา ชื่อ แครี่ มูลลิส (Kary Mullis) ได้เสนอเทคนิคการเพิ่มจำนวนชิ้น DNA ที่เรียกกันว่า polymerase chain reaction หรือเรียกย่อว่า PCR ด้วยเทคนิคที่น่าทึ่งนี้ ทำให้งานวิจัยทางอณูชีววิทยาสะดวกรวดเร็วมากขึ้น และเมื่อนำเทคนิค PCR มาประยุกต์เข้ากับ DNA sequencing ก็จะกลายเป็นเทคนิคที่เรียกว่า cycle sequencing ซึ่งจะช่วยให้ DNA sequencing มีความสะดวกมากขึ้น สุดท้าย แครี่ มูลลิส ก็ได้รับรางวัลโนเบลสาขาเคมีในปี ค.ศ. 1993

จะเห็นว่า การกำเนิดขึ้นของ bioinformatics ไม่ได้มาจากความบังเอิญ หากแต่อาศัยรากฐานที่มั่นคงทางทั้งทางด้านชีววิทยาและคอมพิวเตอร์ เมื่อเวลาที่เหมาะสมมาถึง bioinformatics ก็เกิดขึ้นในวงการวิทยาศาสตร์โลก

เมื่อการทำ DNA sequencing เป็นเรื่องง่ายขึ้น และข้อมูลจาก DNA sequences ก็ซ่อนความลับทางธรรมชาติของสิ่งมีชีวิตไว้มากมายอย่างน่ามหัศจรรย์ ทำให้นักวิจัยจำนวนมากใช้เทคโนโลยีนี้ในงานวิจัยของตนเอง เวลาผ่านไปไม่นาน DNA sequence ที่เหล่านักวิจัยทั่วโลกสร้างขึ้นก็มีจำนวนนับไม่ถ้วน ปัญหาพื้นฐานของการทำงานวิจัยก็เกิดขึ้นว่า เราจะเก็บข้อมูลของ sequence อย่างไร ให้ค้นหาง่ายและสะดวกต่อการนำมาวิเคราะหข้อมูลต่อไป ทำอย่างไร เราจะสามารถนำข้อมูล sequence ของกลุ่มวิจัยอื่นมาเปรียบเทียบกับงานของเรา และปัญหาอื่นๆ อีกมากมายก็ติดตามมาอย่างต่อเนื่อง

Sequence Databases

ปัญหาเหล่านี้ฟังแล้วน่าหนักอก และแสนจะน่าเบื่อหน่ายสำหรับนักชีววิทยา แต่กลายเป็นปัญหาน่าสนใจของนักคอมพิวเตอร์ไปเสียนี่ ด้วยความรู้ทางด้านฐานข้อมูล ทำให้การจัดการกับข้อมูลจำนวนมากเป็นเรื่องที่ไม่ยากเกินไปนัก

ด้วยเหตุนี้ องค์กรต่างๆ ก็จัดทำฐานข้อมูลขึ้นเพื่อเก็บ DNA sequence และบางองค์กรก็ยอมให้บุคคลทั่วไป เข้าไป download ข้อมูลไปใช้ศึกษาได้ด้วย ต่อมาเมื่อข้อมูลมีมากขึ้นๆ องค์กรที่สามารถจัดการกับข้อมูลอันมหาศาลเหล่านี้ก็มีน้อยลง จนปัจจุบันเรามีฐานข้อมูลหลักทางด้าน sequence อยู่หลายแห่งด้วยกัน เช่น GenBank เป็นฐานข้อมูลทางชีววิทยาของสหรัฐอเมริกา EMBL (European Molecular Biology Laboratory) เป็นฐานข้อมูลของยุโรป และ DDBJ (DNA Database of Japan) เป็นฐานข้อมูลของญี่ปุ่น ฐานข้อมูลทั้ง 3 แห่งนี้เป็นฐานข้อมูลขนาดใหญ่ที่เกิดขึ้นมาตั้งแต่ยุคแรกๆ ของ bioinformatics และปัจจุบันการค้นหาข้อมูลในฐานข้อมูลทั้ง 3 นี้สามารถทำได้โดยผ่านทางเวบไซต์ของ National Center of Biotechnology Information หรือที่เราเรียกย่อว่า NCBI ถ้ามีโอกาสผมจะได้เล่ารายละเอียดเกี่ยวกับ biodatabases ให้ฟังนะครับ

Sequence Alignment

นอกจากการจัดเก็บข้อมูลอย่างมีประสิทธิภาพแล้ว การวิเคราะห์ข้อมูลก็เป็นปัญหาชวนปวดหัวของนักชีววิทยาเช่นกัน ลองนึกภาพดูสิครับ ถ้ามี DNA sequences 2 เส้นขนาดสั้นๆ เอาเป็นว่าสักประมาณ 60 เบส เราคงสามารถนำมาเทียบกันด้วยตา และใช้ดินสอเขียนลงบนกระดาษได้อย่างไม่น่าจะยุ่งยากนัก แต่ถ้ามี sequences เยอะขึ้น และความยาวก็เพิ่มเป็นหลายพันเบสล่ะ คงไม่สนุกแน่ถ้าไม่มีเครื่องมือทางคอมพิวเตอร์เข้ามาช่วย

ขั้นตอนการวิเคราะห์ sequence ที่เราจะต้องทำเป็นอันดับแรก ก็คือ การเปรียบเทียบว่า sequences 2 เส้นมีลำดับเบสเหมือนหรือต่างกันอย่างไร เราเรียกวิธีการเปรียบเทียบนี้ว่า sequence alignment

เราอาจเรียกได้ว่า sequence alignment เป็นหัวใจหลักของการศึกษา sequence analysis เลยก็ว่าได้ นั่นเป็นเพราะการวิเคราะห์ sequence ไม่ว่าจะเป็นการวิเคราะห์ที่ง่ายไปจนถึงขั้นที่มีความซับซ้อนมากก็ต้องเริ่มต้นจาก sequence alignment ทั้งสิ้น

ปัจจุบัน การทำ sequence alignment ได้รับการพัฒนาให้ก้าวหน้าและซับซ้อนขึ้นมาก และวิธีการต่างๆ ถูกนำมาสร้างเป็นโปรแกรมสำเร็จรูปมากมาย เช่น MAFF-T (อ่านว่า แมฟ-ที), T-Coffee, K-align, Muscle, ProbCon เป็นต้น แต่การทำงานของโปรแกรมเหล่านี้ก็ยังไม่ถูกต้องอย่างสมบูรณ์ ทำให้การพัฒนา algorithm ของการทำ alignment ยังคงเป็นหัวข้อวิจัยทาง bioinformatics ที่นักวิจัยทั่วโลกให้ความสนใจอยู่ ถึงแม้ว่าจะไม่มากเท่าในอดีตก็ตาม

Phylogenetic Reconstruction

ปัญหาพื้นฐานของการศึกษาชีววิทยา คงจะหนีไม่พ้นคำถามทางด้านวิวัฒนาการ สิ่งมีชีวิตจำนวนมากมีโครงสร้างที่บอบบาง ยากต่อการคงสภาพไว้หลังจากตายไปแล้ว เช่น แบคทีเรีย ไวรัส รา หรือแม้แต่เนื้อเยื่อของสัตว์หรือพืช ดังนั้นการศึกษาวิวัฒนาการจากซากดึกดำบรรพ์จึงจำกัดด้วยจำนวนตัวอย่าง แต่การศึกษาทางอณูชีววิทยาสามารถนำ protein sequence และ DNA sequence มาเปรียบเทียบกันและคำนวณแบบแผนวิวัฒนาการออกมา จากนั้นจึงนำไปสร้างเป็นแผนภาพเรียกว่า phylogenetic tree

Phylogenetic analysis จัดเป็นอีกสาขาหนึ่งของ sequence analysis ที่มีผู้สนใจศึกษามาก และยังคงมีแนวโน้มมากขึ้นในอนาคตด้วย งานวิจัยด้านนี้ได้แก่ การวัดอัตราการกลายพันธุ์ (mutation rate) อัตราการวิวัฒนาการ (evolutionary rate) ของสิ่งมีชีวิตจาก sequence หรือการคำนวณเวลาที่สิ่งมีชีวิตใช้ในการวิวัฒนาการจาก sequence ซึ่งเราเรียกว่า molecular clock ก็เป็นงานวิจัยที่ได้รับความสนใจมากเช่นกัน

Pattern Recognition

การหา motif หรือ domain บางอย่างใน DNA หรือ protein sequences จัดเป็นหัวข้อวิจัยที่น่าสนใจต่อการศึกษาโครงสร้างและหน้าที่ของ DNA และโปรตีนเป็นอย่างมาก งานวิจัยที่ bioinformatician ทำการศึกษากันมากก็คงจะเป็นการทำนายตำแหน่งของ promoter, ribosome binding site, immunogenicity site เป็นต้น

การทำนาย motif เหล่านี้ไม่ใช่เรื่องง่ายเลย ถึงแม้ปัจจุบันจะมีโปรแกรมคอมพิวเตอร์ที่มีคุณภาพสูงมากในการค้นหา แต่ความถูกต้องแม่นยำของวิธีการและโปรแกรม ก็ยังต้องการการพัฒนาขึ้นไปอีก งานวิจัยด้านนี้ อาจจะไม่ได้รับความสนใจมากนัก เนื่องจากวิธีการทำนาย จำเป็นต้องอาศัยเทคนิคทางคณิตศาสตร์และสถิติที่ค่อนข้างซับซ้อน

อย่างไรก็ตาม pattern recognition บางชนิด กลับมีกระแสความนิยมมากขึ้น นับตั้งแต่ Human genome project ได้รับความสนใจจากนักวิจัยทั่วโลก single nucleotide polymorphism หรือ SNP (อ่านว่า สนิป) ก็กลายเป็นงานวิจัยที่สำคัญที่ต้องการองค์ความรู้ทั้งทางด้าน population genetics และ bioinformatics

นอกจากงานวิจัยหลักๆ ที่ผมได้เล่าให้ฟังไปแล้ว ก็ยังมีงานวิจัยอื่นๆ ที่อยู่ในขอบข่ายของ sequence analysis อีกมากมายครับ ถ้ามีโอกาสคงได้นำมาเล่าให้ฟังต่อไป แต่ว่าหัวใจหลักของ sequence analysis คือการค้นหา information ที่สำคัญทางชีววิทยาจาก sequences นั่นเอง ผมคิดว่าหลายคนที่ไม่เคยรู้จัก sequence analysis มาก่อนเลย คงพอที่จะเห็นภาพแบบกว้างๆ แล้วนะครับว่า bioinformatics สาขานี้เป็นอย่างไร

Python Programming

Condition Statement and Function
ประพัฒน์ สุริยผล

จากโจทย์ครั้งที่แล้ว ที่ให้เขียนโปรแกรมโดยให้แปลงส่วนที่คำนวณพื้นที่ทั้งหมด ให้แยกออกมาเป็นฟังก์ชันเพื่อให้โปรแกรมอ่านง่ายและแก้ไขได้ง่ายขึ้น เมื่อเสร็จสิ้นในส่วนนี้ โปรแกรมควรจะสามารถคำนวณพื้นที่สี่เหลี่ยม สามเหลี่ยม วงกลมและสี่เหลี่ยมคางหมูได้

มีคนส่งโปรแกรมมาให้ดูหลากหลายทีเดียว ในครั้งนี้ขอยกตัวอย่างมาให้ 6 แบบ เพื่อให้ดูว่า โปรแกรมโจทย์เดียวกัน สามารถเขียนออกมาได้หลายแบบพอสมควร บางโปรแกรมก็ทำงานไม่ถูกต้อง อาจจะลอง cut & paste ไปทดสอบรันดูได้ครับจากหัวข้อที่แล้ว ลองดูสิว่า โปรแกรมข้างล่างนี้ โปรแกรมไหนทำงานได้ถูกต้องตามที่ต้องการ โปรแกรมไหนมีข้อผิดพลาด ควรแก้ไขอย่างไร

แบบที่ 1

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is", base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is", 0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area of radius", radius, "is", 3.14159*(radius_int**2)

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_base_top = raw_input("Please enter parallel_base_top -> ")
____parallel_base_bottom = raw_input("Please enter parallel_base_bottom -> ")
____height = raw_input("Please enter height -> ")
____parallel_base_top_int = int(parallel_base_top)
____parallel_base_bottom_int = int(parallel_base_bottom)
____height_int = int(height)
____print "Trapezoids area of parallel base top and bottom", parallel_base_top,\
__________"and", parallel_base_bottom, "and height", height, "is",\
__________1.0/2.0*(parallel_base_top_int +parallel_base_bottom_int)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and \
____choice != "C" and choice != "c" and choice != "Z" and choice != "z":

แบบที่ 2

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle\nTriangle area = 0.5*base*height"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle\nCircle area = 3.14159 * radius^2"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area with radius", radius, "is", 3.14159 *radius_int**2

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids" # = 0.5 *\
__________(parallelbase1+parallelbase2)* height
____parallelbase1 = raw_input("Please enter parallel base1 -> ")
____parallelbase2 = raw_input("Please enter parallel base2 -> ")
____height = raw_input("Please enter height -> ")
____parallelbase1_int = int(parallelbase1)
____parallelbase2_int = int(parallelbase2)
____height_int = int(height)
____print "Trapozoids area of parallel base1", parallelbase1, "parallel base2",\
__________parallelbase2, "and height", height, "is", 0.5*(parallelbase1_int +\
__________parallelbase2_int)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and\
____choice != "C" and choice != "c" and choice != "z":
____print "Please type R,T,C or Z to choose the type of area to calculate"

แบบที่ 3

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle\nTriangle area = 0.5*base*height"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle\nCircle area = 3.14159 * radius^2"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area with radius", radius, "is", 3.14159 *radius_int**2

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids" # = 0.5 *\
__________(parallelbase1+parallelbase2)* height
____parallelbase1 = raw_input("Please enter parallel base1 -> ")
____parallelbase2 = raw_input("Please enter parallel base2 -> ")
____height = raw_input("Please enter height -> ")
____parallelbase1_int = int(parallelbase1)
____parallelbase2_int = int(parallelbase2)
____height_int = int(height)
____print "Trapozoids area of parallel base1", parallelbase1, "parallel base2",\
__________parallelbase2, "and height", height, "is", 0.5*(parallelbase1_int +\
__________parallelbase2_int)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and\
____choice != "C" and choice != "c" and choice != "z":
____print "Please type R,T,C or Z to choose the type of area to calculate"

แบบที่ 4

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base=", base, "and height=", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base=", base, "and height=", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area of radius=", radius, "is", 3.14159*(radius_int*radius_int)

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_base1 = raw_input("Please enter parallel base 1-> ")
____parallel_base2 = raw_input("Please enter parallel base 2-> ")
____height = raw_input("Please enter height -> ")
____parallel_base_int1 = int(parallel_base1)
____parallel_base_int2 = int(parallel_base2)
____height_int = int(height)
____print "Trapezoids area of parallel base1=", parallel_base_int1,\
__________"parallel base2=", parallel_base_int2, "and height=", height,"is",\
__________0.5*(parallel_base_int1+parallel_base_int2)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateTriangleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
else:
____print "Please type R,T,C or Z to choose the type of area to calculate"

แบบที่ 5

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area of radius", radius , "is",3.14159*(radius_int**2)

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_base1 = raw_input("Please enter parallel base1 -> ")
____parallel_base2 = raw_input("Please enter parallel base2 -> ")
____height = raw_input("Please enter height -> ")
____parallel_base_int1 = int(parallel_base1)
____parallel_base_int2 = int(parallel_base2)
____height_int = int(height)
____print "Trapezoids area of parallel_base1", parallel_base1,"Trapezoids area of\
__________parallel_base2", parallel_base2, "and height", height, "is",\
__________float(0.5*parallel_base_int1+parallel_base_int2*height_int)

def xxx():
____print "Please type R,T or C to choose the type of area to calculate"
choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and\
___choice != "C" and choice != "c" and choice != "Z" and choice != "z" :
____xxx()

แบบที่ 6

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________base_int*height_int*0.5

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radious = raw_input("Please enter radious -> ")
____radious_int = int(radious)
____print "Circle area of radious", radious, "is", 3.14159*radious_int*radious_int

def calculateTrapezoidArea():
____print "Program to calculate Area of TrapeZoid"
____parallel_line1 = raw_input("Please enter length of 1st parallel line -> ")
____parallel_line2 = raw_input("Please enter length of 2nd parallel line -> ")
____height = raw_input("Please enter height -> ")
____parallel_line1_int = int(parallel_line1)
____parallel_line2_int = int(parallel_line2)
____height_int = int(height)
____print "Trapezoid area of 1st parallel line", parallel_line1, ", 2nd parallel\
__________line", parallel_line2, "and height", height, "is",\
__________(parallel_line1_int+parallel_line2_int)*height_int*0.5

i = 0
while i == 0:
____choice = raw_input("Which type of area you would like to\
________________________calculate (R,T,Z or C)? -> ")
________if choice == "R" or choice == "r":
____________calculateRectangleArea()
____________i += 1
________if choice == "T" or choice == "t":
____________calculateTriangleArea()
____________i += 1
________if choice == "C" or choice == "c":
____________calculateCircleArea()
____________i += 1
________if choice == "Z" or choice == "z":
____________calculateTrapezoidArea()
____________i += 1
________if choice != "R" and choice != "r" and choice != "T" and choice != "t"\
___________and choice != "C" and choice != "c" and choice != "Z" and choice != "z":
____________print "Please type R,T or C to choose the type of area to calculate."

If-Elif-Else structure

จากตัวอย่างครั้งที่แล้ว เราจะเห็นโครงสร้างในส่วนของ if มากขึ้น

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and \
___choice != "C" and choice != "c" and choice != "Z" and choice != "z":
____print "Please type R,T or C to choose the type of area to calculate"

สิ่งที่น่าสนใจใน code ข้างบน คือ การใช้ backslash (\) ในการเชื่อมบรรทัดที่ค่อนข้างยาว ทำให้อ่าน code ได้ง่ายขึ้น อันนี้เป็น trick หนึ่งที่น่าสนใจและเป็นประโยชน์

เมื่อเราดึงส่วนของโปรแกรมที่เป็นรายละเอียดออกไปเป็นฟังก์ชันแล้ว เราจะเห็นว่าทุกครั้งที่เราเพิ่มตัวเลือกเข้าไป เราจำเป็นจะต้องแก้ไข code 2 ที่ คือ

1.ในส่วนที่จะเรียกใช้ฟังก์ชันที่จำเพาะสำหรับตัวเลือกนั้น ๆ และไปเพิ่มในหัวข้อ if สุดท้าย

ถ้าหากเรามีตัวเลือกเยอะๆ คำสั่ง if ที่บรรทัดสุดท้ายคงจะยาวมากๆ แต่เราสามารถแก้ไขปัญหานี้ด้วย รูปแบบ if-elif-else ดังนี้

if choice == "R" or choice == "r":
____calculateREctangleArea()
elif choice == "T" or choice == "t":
____calculateTriangleArea()
.
.
.
else:
____print "Please type R, T, C or Z to choose the type of area to calculate"

ด้วยคำสั่งแบบนี้ เมื่อโปรแกรมตรวจสอบเงื่อนไขแรกแล้ว ไม่ถูกต้อง ก็จะมาทำงานที่เงื่อนไขถัดไปใน elif ไปเรื่อยๆ จนกระทั่งถึง elif ตัวสุดท้าย ถ้าหากเงื่อนไขยังคงไม่ถูกต้อง โปรแกรมก็จะมาทำงานในส่วนที่อยู่ใน else block เพราะฉะนั้น เราไม่ต้องใส่เงื่อนไขยาวๆ ที่ if คำสั่งสุดท้ายอีกต่อไป เพราะว่าเมื่อโปรแกรมหาคำสั่งที่เหมาะสมเจอ (if condition เป็นจริง) ก็จะไปทำงานตามที่ต้องการ และกระโดดไปทำงานที่คำสั่งถัดไปที่อยู่นอกโครงสร้าง if-elif-else เลย แต่ถ้าหากคำสั่งที่เหมาะสมไม่เจอ ก็จะไปทำงานที่ส่วน else

แบบฝึกหัด

1. นำโครงสร้าง if-elif-else มาใช้ในโปรแกรม
2. นำ backslash (\) มาช่วยแบ่งบรรทัด ในกรณีที่คำสั่งในบรรทัดนั้นยาวเกินไป