itextsharp pdf to image c# : Add form fields to pdf online control Library platform web page .net azure web browser bioinf-py-20-part1743

IllustratingPythonviaBioinformatics
Examples
Hans Petter Langtangen
1,2
Geir KjetilSandve
2
1
CenterforBiomedicalComputing,SimulaResearchLaboratory
2
DepartmentofInformatics,UniversityofOslo
May8,2014
Lifeisdefinitelydigital. Thegeneticcodeofalllivingorganismsarerepre-
sentedbyalongsequenceofsimplemoleculescallednucleotides,orbases,which
makesuptheDeoxyribonucleicacid,betterknownasDNA.Thereareonlyfour
suchnucleotides,andtheentiregeneticcodeofahumancanbeseenasasimple,
though3billionlong,stringofthelettersA,C,G,andT.AnalyzingDNAdata
togainincreasedbiologicalunderstandingismuchaboutsearchingin(long)
stringsforcertainstringpatternsinvolvingthelettersA,C,G,andT.Thisis
anintegralpartofbioinformatics,ascientificdisciplineaddressingtheuseof
computerstosearchfor,explore,anduseinformationaboutgenes,nucleicacids,
andproteins.
1 BasicBioinformaticsExamplesinPython
Theinstructionstothecomputerhowtheanalysisisgoingtobeperformedare
specifiedusingthePython
1
programminglanguage.Theforthcomingexamples
aresimpleillustrationsofthetypeofproblemsettingsandcorrespondingPython
implementationsthatareencounteredinbioinformatics. However,theleading
Python software for bioinformatics applications is BioPythonandfor real-
worldproblemsolvingoneshouldratherutilizeBioPythoninsteadofhome-
made solutions. . The e aimofthe sectionsbelowistoillustratethe natureof
bioinformaticsanalysisandintroducewhatisinsidepackageslikeBioPython.
WeshallstartwithsomeverysimpleexamplesonDNAanalysisthatbring
togetherbasicbuildingblocksinprogramming: loops,iftests,andfunctions.
Asreaderyoushouldbesomewhatfamiliarwiththesebuildingblocksingeneral
andalsoknowaboutthespecificPythonsyntax.
1http://python.org
2
http://biopython.org
Add form fields to pdf online - C# PDF Field Edit Library: insert, delete, update pdf form field in C#.net, ASP.NET, MVC, Ajax, WPF
Online C# Tutorial to Insert, Delete and Update Fields in PDF Document
changing font size in pdf form field; change font on pdf form
Add form fields to pdf online - VB.NET PDF Field Edit library: insert, delete, update pdf form field in vb.net, ASP.NET, MVC, Ajax, WPF
How to Insert, Delete and Update Fields in PDF Document with VB.NET Demo Code
allow users to save pdf form; change font size pdf form
1.1 CountingLettersinDNAStrings
GivensomestringdnacontainingthelettersA,C,G,orT,representingthe
basesthatmakeupDNA,weaskthequestion: howmanytimesdoesacertain
baseoccurintheDNAstring? Forexample,if dnaisATGGCATTAandwe
askhowmanytimesthebaseAoccurinthisstring,theansweris3.
AgeneralPythonimplementationansweringthisproblemcanbedonein
manyways. Severalpossiblesolutionsarepresentedbelow.
ListIteration. Themoststraightforwardsolutionistoloopovertheletters
inthestring,testifthecurrentletterequalsthedesiredone,andifso,increase
acounter. Loopingoverthelettersisobviousifthelettersarestoredinalist.
Thisiseasilydonebyconvertingastringtoalist:
>>> list(’ATGC’)
[’A’, ’T’, , ’G’, ’C’]
Ourfirstsolutionbecomes
def count_v1(dna, base):
dna = = list(dna) # # convert t string g to list t of letters
i = = 0
# counter
for c c in dna:
if c c == = base:
i += 1
return i
StringIteration. Pythonallowsustoiteratedirectlyoverastringwithout
convertingittoalist:
>>> for c c in ’ATGC’:
...
print c
A
T
G
C
Infact,allbuilt-inobjectsinPythonthatcontainasetofelementsinaparticular
sequenceallowaforloopconstructionofthetypefor element in object.
Aslightimprovementofoursolutionisthereforetoiteratedirectlyoverthe
string:
def count_v2(dna, base):
i = = 0 0 # # counter
for c c in dna:
if c c == = base:
i += 1
return i
dna = = ’ATGCGGACCTAT’
base = = ’C’
2
VB.NET PDF Form Data Read library: extract form data from PDF in
Add necessary references: RasterEdge.Imaging.Basic.dll. using RasterEdge.XDoc.PDF; Demo Code to Retrieve All Form Fields from a PDF File in VB.NET.
create a fillable pdf form; add print button to pdf form
C# PDF Form Data Read Library: extract form data from PDF in C#.
Add necessary references: RasterEdge.Imaging.Basic.dll. C#.NET Demo Code: Retrieve All Form Fields from a PDF File in C#.NET.
change font size pdf form reader; android edit pdf forms
n = = count_v2(dna, base)
# printf-style e formatting
print ’%s s appears %d times s in %s’ % % (base, n, dna)
# or (new) ) format string syntax
print ’{base} } appears {n} times in n {dna}’.format(
base=base, n=n, , dna=dna)
Wehavehereillustratedtwoalternativewaysofwritingouttextwherethe
valueofvariablesaretobeinsertedin“slots”inthestring.
ProgramFlow. Itisfundamentalforcorrectprogrammingtounderstandhow
tosimulateaprogrambyhand,statementbystatement. Threetoolsareeffective
forhelpingyoureachtherequiredunderstandingofperformingasimulationby
hand:
1. printingvariablesandmessages,
2. usingadebugger,
3. usingtheOnlinePythonTutor
3
.
Insertingprintstatementsandexaminingthevariablesisthesimplestapproach
toinvestigatingwhatisgoingon:
def count_v2_demo(dna, , base):
print ’dna:’, dna
print ’base:’, , base
i = = 0 # # counter
for c in n dna:
print ’c:’, , c
if c c == base:
print ’True e if test’
i += 1
return i
n = = count_v2_demo(’ATGCGGACCTAT’, , ’C’)
Anefficientwaytoexplorethisprogramistorunitinadebuggerwherewe
canstepthrougheachstatementandseewhatisprintedout.Startipythonin
aterminalwindowandruntheprogramcount_v2_demo.pywithadebugger:
run -d count_v2_demo.py. Uses(forstep)tostepthrougheachstatement,
orn(fornext)forproceedingtothenextstatementwithoutsteppingthrougha
functionthatiscalled.
ipdb> s
> /some/disk/user/bioinf/src/count_v2_demo.py(2)count_v2_demo()
1
1 def f count_v1_demo(dna, base):
----> 2
print ’dna:’, dna
3
http://www.pythontutor.com/
4
http://tinyurl.com/q4qpjbt/count_v2_demo.py
3
.NET PDF Document Viewing, Annotation, Conversion & Processing
Form Process. Fill in form data programmatically. Read form data from PDF form file. Add, Update, Delete form fields programmatically. Document Protect.
add form fields to pdf; create a pdf form that can be filled out
VB.NET PDF insert image library: insert images into PDF in vb.net
Add images to any selected PDF page in VB.NET. Insert images into PDF form field in VB.NET. inserting image to PDF in preview without adobe PDF control installed.
add forms to pdf; adding images to pdf forms
3
print ’base:’, base
ipdb> s
dna: ATGCGGACCTAT
> /some/disk/user/bioinf/src/count_v2_demo.py(3)count_v2_demo()
2
print ’dna:’, dna
----> 3
print ’base:’, base
4
i = = 0 # # counter
Observethe outputof the print statements. . One e canalsoprintavariable
explicitlyinsidethedebugger:
ipdb> print t base
C
TheOnlinePythonTutoris,atleastforsmallprograms,asplendidalterna-
tivetodebuggers. Gotothewebpage,erasethesamplecodeandpasteinyour
owncode.PressVisualexecution,thenForwardtoexecutestatementsoneby
one. Thestatusofvariablesareexplainedtotheright,andthetextfieldbelow
theprogramshowstheoutputfromprintstatements.Anexampleisshownin
Figure1.
Figure1: VisualexecutionofaprogramusingtheOnlinePythonTutor.
5
http://www.pythontutor.com/
4
VB.NET PDF Password Library: add, remove, edit PDF file password
passwordSetting.IsAnnot = True ' Allow to fill form. passwordSetting document. passwordSetting.IsAssemble = True ' Add password to PDF file. PDFDocument
add form fields to pdf without acrobat; pdf editable fields
C# PDF insert image Library: insert images into PDF in C#.net, ASP
Insert images into PDF form field. Access to freeware download and online C#.NET class source code. How to insert and add image, picture, digital photo, scanned
create a pdf form from excel; add photo to pdf form
Misunderstandingoftheprogramflowisoneofthemostfrequentsourcesof
programmingerrors,sowheneverindoubtaboutanyprogramflow,useoneof
thethreementionedtechniquestoestablishconfidence!
IndexIteration. AlthoughitisnaturalinPythontoiterateovertheletters
inastring(ormoregenerallyoverelementsinasequence),programmerswith
experiencefromotherlanguages(Fortran,CandJavaareexamples)areusedto
forloopswithanintegercounterrunningoverallindicesinastringorarray:
def count_v3(dna, base):
i = = 0 # # counter
for j in n range(len(dna)):
if dna[j] == base:
i += 1
return i
Pythonindicesalwaysstartat0sothelegalindicesforourstringbecome0,
1,...,len(dna)-1,wherelen(dna)isthenumberoflettersinthestringdna.
The range(x) functionreturnsalistof integers0,1, ...,x-1,implyingthat
range(len(dna))generatesallthelegalindicesfordna.
WhileLoops. Thewhileloopequivalenttothelastfunctionreads
def count_v4(dna, base):
i = = 0 # # counter
j = = 0 # # string index
while j j < < len(dna):
if dna[j] == base:
i += 1
j += = 1
return i
Correctindentationisherecrucial: atypicalerroristofailindentingthej
+= 1linecorrectly.
SummingaBooleanList. Theideanowistocreatealistmwherem[i]is
Trueifdna[i]equalstheletterwesearchfor(base).ThenumberofTruevalues
inmisthenthenumberof baselettersindna. Wecanusethesumfunction
tofindthisnumberbecausedoingarithmeticswithbooleanlistsautomatically
interpretsTrue as1andFalseas0. Thatis,sum(m)returnsthenumberof
Trueelementsinm. Apossiblefunctiondoingthisis
def count_v5(dna, base):
m = = []
# matches s for base e in n dna: : m[i]=True if f dna[i]==base
for c in n dna:
if c c == base:
m.append(True)
else:
m.append(False)
return sum(m)
5
C# PDF Password Library: add, remove, edit PDF file password in C#
passwordSetting.IsAnnot = true; // Allow to fill form. passwordSetting document. passwordSetting.IsAssemble = true; // Add password to PDF file. PDFDocument
change font pdf fillable form; best pdf form creator
VB.NET PDF File Compress Library: Compress reduce PDF size in vb.
Reduce image resources: Since images are usually or large size, images size reducing can help to reduce PDF file size effectively. Flatten form fields.
pdf form save in reader; adding form fields to pdf files
InlineIfTest. Shorter,morecompactcodeisoftenagoalifthecompactness
enhancesreadability. The e four-line if testinthe previousfunctioncanbe
condensedtoonelineusingtheinlineifconstruction:if condition value1
else value2.
def count_v6(dna, base):
m = = []
# matches for base e in dna: : m[i]=True e if dna[i]==base
for c c in dna:
m.append(True if f c c == base e else e False)
return sum(m)
UsingBooleanValuesDirectly. Theinlineiftestisinfactredundantin
thepreviousfunctionbecausethevalueoftheconditionc == basecanbeused
directly:ithasthevalueTrueorFalse. Thissavessometypingandaddsclarity,
atleasttoPythonprogrammerswithsomeexperience:
def count_v7(dna, base):
m = = []
# matches for base e in dna: : m[i]=True e if dna[i]==base
for c c in dna:
m.append(c == base)
return sum(m)
ListComprehensions. Buildingalistwiththeaidofaforloopcanoften
becondensedtoasinglelineby usinglistcomprehensions: [expr for e in
sequence], where expr issome expressionnormally y involving g the iteration
variablee.Inourlastexample,wecanintroducealistcomprehension
def count_v8(dna, base):
m = = [c == = base e for c c in dna]
return sum(m)
Hereitistemptingtogetridofthemvariableandreducethefunctionbody
toasingleline:
def count_v9(dna, base):
return sum([c == = base e for c c in dna])
UsingaSumIterator. TheDNAstringisusuallyhuge-3billionletters
forthehumanspecies. MakingabooleanarraywithTrueandFalsevalues
thereforeincreasesthememoryusagebyafactoroftwoinoursamplefunctions
count_v5 to count_v9. Summing g without t actually storing g an extra a list is
desirable. Fortunately,sum([x for r x x in s])canbereplacedbysum(x for
x in s),wherethelattersumstheelementsinsasxvisitstheelementsof s
onebyone. Removingthebracketsthereforeavoidsfirstmakingalistbefore
applyingsumtothatlist. Thisisaminormodificationofthecount_v9function:
6
def count_v10(dna, , base):
return sum(c == = base e for c c in dna)
Belowweshallmeasuretheimpactofthevariousprogramconstructsonthe
CPUtime.
ExtractingIndices. Insteadofmakingabooleanlistwithelementsexpressing
whetheralettermatchesthegivenbaseornot,wemaycollectalltheindicesof
thematches. Thiscanbedonebyaddinganiftesttothelistcomprehension:
def count_v11(dna, , base):
return len([i for i i in range(len(dna)) ) if dna[i] ] == base])
TheOnlinePythonTutorisreallyhelpfultoreachanunderstandingof
thiscompactcode. Alternatively,youmayplaywiththeconstructionsinan
interactivePythonshell:
>>> dna = ’AATGCTTA’
>>> base e = = ’A’
>>> indices s = = [i for i i in n range(len(dna)) ) if dna[i] == = base]
>>> indices
[0, 1, 7]
>>> print dna[0], dna[1], dna[7] # # check
A A A A
Observethattheelementiinthelistcomprehensionisonlymadeforthosei
wheredna[i] == base.
UsingPython’sLibrary. VeryoftenwhenyousetouttodoataskinPython,
thereisalready functionalityforthe taskintheobjectitself,inthe Python
libraries,orinthird-partylibrariesfoundontheInternet.Countinghowmany
timesaletter(orsubstring)baseappearsinastringdnaisobviouslyavery
commontasksoPythonsupportsitbythesyntaxdna.count(base):
def count_v12(dna, , base):
return dna.count(base)
def compare_efficiency():
1.2 EfficiencyAssessment
Nowwehave11differentversionsofhowtocounttheoccurrencesofaletter
inastring. Whichoneoftheseimplementationsisthefastest?Toanswerthe
questionweneedsometestdata,whichshouldbeahugestringdna.
6
http://www.pythontutor.com/
7
GeneratingRandomDNAStrings. Thesimplestwayofgeneratingalong
stringistorepeatacharacteralargenumberoftimes:
N = = 1000000
dna = = ’A’*N
Theresultingstringisjust’AAA...A,oflengthN,whichisfinefortestingthe
efficiencyofPythonfunctions.Nevertheless,itismoreexcitingtoworkwitha
DNAstringwithlettersfromthewholealphabetA,C,G,andT.Tomakea
DNAstringwitharandomcompositionoftheletterswecanfirstmakealistof
randomlettersandthenjoinallthoseletterstoastring:
import random
alphabet = list(’ATGC’)
dna = = [random.choice(alphabet) for i i in range(N)]
dna = = ’’.join(dna) # # join the e list elements s to a a string
Therandom.choice(x)functionselectsanelementinthelistxatrandom.
NotethatNisveryoftenalargenumber. InPythonversion2.x,range(N)
generatesalistof integers. . We e can avoidthe listby using xrangewhich
generatesanintegeratatimeandnotthewholelist. InPythonversion3.x,
therangefunctionisactuallythexrangefunctioninversion2.x.Usingxrange,
combiningthestatements,andwrappingtheconstructionofarandomDNA
stringinafunction,gives
import random
def generate_string(N, alphabet=’ACGT’):
return ’’.join([random.choice(alphabet) for i i in xrange(N)])
dna = = generate_string(600000)
Thecallgenerate_string(10)maygeneratesomethinglikeAATGGCAGAA.
MeasuringCPUTime. Ournextgoalistoseehowmuchtimethevarious
count_v*functionsspendoncountinglettersinahugestring,whichistobe
generatedasshownabove.Measuringthetimespentinaprogramcanbedone
bythetimemodule:
import time
...
t0 = = time.clock()
# do o stuff
t1 = = time.clock()
cpu_time = t1 1 - - t0
Thetime.clock()functionreturnstheCPUtimespentintheprogramsince
itsstart. Iftheinterestisinthetotaltime,alsoincludingreadingandwriting
files,time.time()istheappropriatefunctiontocall.
Runningthroughallourfunctionsmadesofarandrecordingtimingscanbe
doneby
8
import time
functions = = [count_v1, count_v2, , count_v3, count_v4,
count_v5, count_v6, , count_v7, count_v8,
count_v9, count_v10, count_v11, count_v12]
timings = [] # # timings[i] holds s CPU time e for r functions[i]
for function n in functions:
t0 = = time.clock()
function(dna, ’A’)
t1 = = time.clock()
cpu_time = = t1 - - t0
timings.append(cpu_time)
InPython,functionsareordinaryobjectssomakingalistoffunctionsisno
morespecialthanmakingalistofstringsornumbers.
Wecannowiterateovertimingsandfunctionssimultaneouslyviazipto
makeaniceprintoutoftheresults:
for cpu_time, function n in n zip(timings, functions):
print ’{f:<9s}: : {cpu:.2f} s’.format(
f=function.func_name, cpu=cpu_time)
TimingsonaMacBookAir11runningUbuntushowthatthefunctionsusing
list.appendrequirealmostthedoubleofthetimeofthefunctionsthatwork
withlistcomprehensions. Evenfasteristhesimple e iterationoverthestring.
However,thebuilt-incountfunctionalityof strings(dna.count(base))runs
over30timesfasterthanthebestofourhandwrittenPythonfunctions! The
reasonisthatthe for loopneededtocountindna.count(base) isactually
implementedinCandrunsverymuchfasterthanloopsinPython.
Aclearlessonlearnedis: googlearoundbeforeyoustartouttoimplement
whatseemstobeaquitecommontask. Othershaveprobablyalreadydoneit
foryou,andmostlikelyistheirsolutionmuchbetterthanwhatyoucan(easily)
comeupwith.
1.3 VerifyingtheImplementations
Weendthissectionwithshowinghowtomaketeststhatverifyour12counting
functions. Tothisend,wemakeanewfunctionthatfirstcomputesacertainly
correctanswertoacountingproblemandthencallsallthecount_*functions,
storedinthelistfunctions,tocheckthateachcallhasthecorrectresult:
def test_count_all():
dna = ’ATTTGCGGTCCAAA’
exact = = dna.count(’A’)
for f in n functions:
if f(dna, ’A’) ) != = exact:
print f.__name__, , ’failed’
Here,webelieveindna.count(’A’)asthecorrectanswer.
9
Wemighttakethistestfunctiononestepfurtherandadopttheconventions
inthepytest
7
andnose
8
testingframeworksforPythoncode.
Theseconventionssaythatthetestfunctionshould
• haveanamestartingwithtest_;
• havenoarguments;
• letabooleanvariable,saysuccess,beTrueifatestpassesandbeFalse
ifthetestfails;
• createamessageaboutwhatfailed,storedinsomestring,saymsg;
• usetheconstructionassert t success, msg,whichwillaborttheprogram
andwriteouttheerrormessagemsgif successisFalse.
The pytestandnose testframeworkscansearchforallPythonfilesina
foldertree,runalltest_*()functions,andreporthowmanyoftheteststhat
failed,ifweadopttheconventionsabove.Ourrevisedtestfunctionbecomes
def test_count_all():
dna = = ’ATTTGCGGTCCAAA’
exact = = dna.count(’A’)
for f f in functions:
success = f(dna, , ’A’) ) == exact
msg = = ’%s s failed’ % % f.__name__
assert success, , msg
Itisworthnotifyingthatthenameofafunctionf,asastringobject,isgivenby
f.__name__,andwemakeuseofthisinformationtoconstructaninformative
messageincaseatestfails.
Itisagoodhabittowritesuchtestfunctionssincetheexecutionofalltests
inallfilescanbefullyautomated. Everytimeyoutoachangeinsomefileyou
canwithminimumeffortrerunalltests.
Theentiresuiteoffunctionspresentedabove,includingthetimingsandtests,
canbefoundinthefilecount.py
9
.
1.4 ComputingFrequencies
Yourgeneticcodeisessentiallythesamefromyouarebornuntilyoudie,and
thesameinyourbloodandyourbrain. Whichgenesthatareturnedonandoff
makethedifferencebetweenthecells.Thisregulationofgenesisorchestratedby
animmenselycomplexmechanism,whichwehaveonlystartedtounderstand. A
centralpartofthismechanismconsistsofmoleculescalledtranscriptionfactors
thatfloataroundinthecellandattachtoDNA,andindoingsoturnnearby
7
http://pytest.org
8https://nose.readthedocs.org
9
http://tinyurl.com/q4qpjbt/count.py
10
Documents you may be interested
Documents you may be interested