Showing posts with label programing. Show all posts
Showing posts with label programing. Show all posts

Wednesday, October 03, 2007

FASTA, betta, more

My girlfriend's sister asked for help. But all she got was a 16.

The purpose of this script is to get a file(name) as a first argument, a regexp as a second argument and then search trough the file and print out the whole sequence where any of the possible regexp values are found, being that the searched sequence itself must be identified with "<>" symbols and then do a little basic math with the elements we've found.

To try this out, save the following as (for instance) sequences.txt:

>50c's and 50t's
cccccccccccccccccccccccccccccccccccccccccccccccccctttttttttt
tttttttttttttttttttttttttttttttttttttttt
>10G's and 90A's
GGGGGGGGGGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>A random sequence
ccagtcctgtctaaggactttggttcatgcgttaatttcggttagccagtggcgcccacg
taacaaagtgcgaccgacctgcttccatagcattgaaatgtccttgattggagattttac
gcaggaggactgataagttcgggtctgaattgatgcagcgaacgttcatccaatactcag
acttgcactcagtcg
>A second random sequence
cgcatgacggccctcctcgacggttcattaaccttgcacaccaactgttcctcaaccgat
ctggtgtctttctcacttacatagcagttgctgtaccatttgatgggaacccgagatcac
cgggttatcgcgggagtttattccgaattgttctcggaaatgtggcgtcggcttgaattg
ggaataatacggatcttgacaagcacgatttcatccacaatgcggcacgagtaatcccct
tctggaaggatgcagaaaggacatatacaatgagctagccacgtggcgcataacgcagct
tggttagaaactaaatctatacaggaagatacagaatgggaacagtgtcgctcagtctag
ccagctagatccgcctttagtcgaccttaggggtaaggca

And the below code as scripty.pl:

print "\n\n *girfriends_sister_school_id* \*girfriends_sisters_friend_school_id*\n\n";
print "filename:\n";
$filename = ;
open(TEMPFILE, $filename) or die("Cannot open the file $filename");
print "Insert the regular expression to find:\n";
chomp($regexp=);
while () {

if ( /^>/xms) {
$paragraph = "";
$paragraph_head = $_
}
if ( /^$/xms) {
push @paragraphs, $paragraph;
} else {
$paragraph .= $_;
}
}
push @paragraphs, $paragraph;
foreach (@paragraphs) {
while ( $_ =~ /($regexp)/gi) {
$expressions{$1} += 1;
}
}
foreach $key (keys %expressions) {
print "\n\nPattern = $key\n";
foreach (@paragraphs) {
if ($_ =~ /$key/) {
$paragraph_with_patterns = $_;
$total = length$paragraph_with_patterns;
$counterA = 0;
$counterC = 0;
$counterT = 0;
$counterG = 0;
if ($key =~ /a/){
while ( $paragraph_with_patterns =~ /a/gi) {
$counterA += 1;
$sum1 = ($counterA/$total);
}
}
if ($key =~ /c/){
while ( $paragraph_with_patterns =~ /c/gi) {
$counterC += 1;
$sum2 = ($counterC/$total);
}
}
if ($key =~ /t/){
while ( $paragraph_with_patterns =~ /t/gi) {
$counterT += 1;
$sum3 = ($counterT/$total);
}
}
if ($key =~ /g/){
while ( $paragraph_with_patterns =~ /g/gi) {
$counterG += 1;
$sum4 = ($counterG/$total);
}
}
$paragraph_with_patterns =~ s/($key)/\<$1\>/gi;
print "$paragraph_with_patterns\n\n";
%sum = ();
%sum1 = ();
%sum2 = ();
%sum3 = ();
%sum4 = ();
if ($counterA >0){
%sum1 = ("A", $counterA);
}
if ($counterC >0){
%sum2 = ("C", $counterC);
}
if ($counterT >0){
%sum3 = ("T", $counterT);
}
if ($counterG >0){
%sum4 = ("G", $counterG);
}
%sum = (%sum1,%sum2,%sum3,%sum4);
foreach $key_id (sort hashValueDescendingNum (keys(%sum))) {
$div = ($sum{$key_id}/$total);
print "$key_id = $sum{$key_id} \/ $total = $div\n";
}
}
}
}
sub hashValueDescendingNum{
$sum{$b} <=> $sum{$a};
}
exit;

Then run perl scripty.pl . Put sequences.txt as the file and a[c|t]g as the regexp when prompted.

I had alot of fun doing this wich is a good thing since I didn't get paid.

And no, neither bio::perl, nor any other module for that matter, were options.

Finally, a major thank you must go out to sab for the priceless (as in: he didn't get paid either) help.

Tuesday, August 14, 2007

that's cool

Quicky:

Are you using OS X to develop the next big Rails app and need ImageMagick, RMagick and friends?

Well, just point your browser to rubyforge and download the rmagick-osx-installer package, unzip it and read the fine manual. If you have all the pre-requisites, "sudo run" the installer script and then... go out for a coffee.

When you're back you'll have the following log in your terminal windows:

libpng was installed successfully
libjpeg was installed successfully
ghostscript was installed successfully
ghostscript-fonts-std was installed successfully
FreeType was installed successfully
libwmf was installed successfully
ImageMagick was installed successfully
RMagick was installed successfully
Removing rm_install_tmp directory...Done

That simple.

Tuesday, July 03, 2007

Making the enterprise see red - part I

Ruby trough Rails has been a poster-child for the so called web 2.0, but in the enterprise level the adoption rate hasn't been all that remarkable.

Rather than go trough an extensive diatribe about how adoption rates in the enterprise for just about any open source technology is always necessarily slow, because no self respecting OSS programmer plays golf, I'll just list some of the perceived weaknesses and shortcoming usually attributed to Ruby.
- Contrary to Perl, it doesn't come installed by default on just about every Unix-like OS;
- The treathing model used is week;
- Rails doesn't scale;
- It's just a fad, and it will go away before it matures;
- Poor Unicode support;
- No single good IDE and Text Editors are soo 80's;
- Nobody wants to learn yet another language especially when there is close to 0 employability for it;

While most of these are (at least to some extent) true, there are many projects that aim at overcoming them. And Ruby itself is still just version 1.8.6 at this point.

One such project seems to be catching enough wind behind it so that it could drive Ruby into mass (wishful thinking here...) adoption. JRuby.

Now, one might expect that the "J" would stand for Japanese, but remarkably it stands for Java. Isn't that ironic? Ruby is sometimes hailed as "What programmers should be using to still have time for a personal life" contrary to Java's "Write once. Then again, Just one more time... Ok, call your mom and cancel dinner" motto.

Jruby (which just recently reached the 1.0 mark) is a pure Java implementation of Ruby, and it's developed by Thomas Enebo and Charles Nutter with Ola Bini also being credited here and there, and, to some extent, sponsored by SUN. It aims at being more that just another implementation of Ruby (not that there are many...) which means that while still programming in pure Ruby, a coder can now enjoy the following added benefits:
- Java's scaling capabilities since Ruby code now runs inside a JVM which means parallelization in multi-processor setups ;
- An optimized Database connectivity with JDBC;
- Same level of Java's Unicode support;
- Garbage Collection handled by Java. (don't we all appreciate that?);
- A great IDE (Netbeans 6. Packed with all typical goodies plus code completion and a Swing Form Builder, it just might be the VB6 of the XXI century), plus a pretty good one (XCode + a hack);
- Access to those millions of lines of code in Java classes;

And last but not least : Integration. This is the one that's going bring it home.

With Jruby and a rails plugin called GoldSpike (previously rails-integration) we're a simple rake command away from turning any Rails app into a standard WAR file, which in turn can be deployed in either Tomcat or Glassfish or presumably just about any other applet container.

What this means is, in an enterprise environment where you have all these layers (think MVC in different LAN segments) set up and where any innovation is foreseen as potentially disruptive if not outright forbidden, we can easily introduce Ruby and Rails code, and from a manager's point of view its a win-win situation where the developers get things done faster, and the implemented structure is maintained without any hiccups. And they still get Sun's level of support.

In parts II and III I'll be showing some code and examples of rapid deployment with JRuby.

Thursday, April 12, 2007

Old School To-Do List

I figure that if I write down my tasks, I'll be forced (by punishment of public embarrassment) to _really_ do them. This will hopefully get me out of LMP(*) and into GTD mode.

To Do:
- Install Edubuntu on a old iMac G3 to offer as a birthday present to my 6 year old cousin;
- Finnish reading Everyday Scripting with Ruby;
- Create a script to automate data extraction and transformation and creation of Excel graphics at work;
- Replace a myriad of Judoscript, Groovy, bash and Perl scripts with well documented Ruby scripts at work (It will look good in my resumé.. "the guy who introduced #a banking institution# to ruby");
- (finally) Take the admission exams for College;
- Help in the translation of www.ruby-lang.org to Portuguese;



(*)LMP .: Last Minute Panic - the major driving force for lazy creative geeks :o)

Friday, March 02, 2007

A week in song titles

Ramble On Led Zeppelin
BES Photo's winner this year was Daniel Blaufuks, a multimedia artist who also does some photos.

Not surprising at all. As someone way more "in the know" that I noted earlier, this was very likely the worst BES Photo ever.

If you take some time to go around a couple of blogs and Flickr pages, you'l find many talented photographers, but hey, their name isn't Blaufuks or Themlitz or Ideafix or any of the other weird names that seem to be a pre-requisite to enter the competition for Portuguese artists.

By the way, the photo on my blog's title is a self portrait by Kay whom i rank at nr. 2 on my favorite artists list, right after Dali and Trent Reznor (incidentally, if you didn't get this joke, you might be reading the wrong blog) and is here with her consent. The logo underneath it is from the "Art is Resistance" movement.

In Memoriam Rodrigo Leão & Vox Ensemble
I finally managed to give my Macbook a 1G memory upgrade, and it is worth every penny. If your stuck with the default 512 Mb, your missing out. Good does get better.

Simultaneously, a work colleague humped his (newer model) Macbook a full Ghz (to 2G) and notes that the performance improvements are even more noticeable. So todays Haiku is "Grab RAM if you can. OS X surfs graciously in deeper waters".

There There Radiohead
This week on Digg a story gained some momentum again (the original is kind of old) about how 199 out of 200 CS Majors can't program. This just proves that a degree can be a display of educational excellence, but doesn't hint at all on competence.

Dead cat on the line (Diamanda Galas cover)
I had almost forgotten about these...

Monday, February 26, 2007

Search and Zip (revised and improved)

This is a little vbs script I wrote the other day. It's purpose is to find all log files in a given directory, create a zip archive (using 7-Zip command line interface) of each individual file (created before today, so I don't zip anything currently in use) and delete the original file.

I hope somebody else finds it useful... Although I know that someone can probably out best me with a Perl one-liner..

Here goes!

'Function: Compress and delete log files
'Description: Finds all files with the extension ".log"
'from inside the folder(s) passed as arguments, create a ".zip"
'archive with the same name and delete the original file
'Example: cscript compress.vbs d:\logs "d:\big app logs"

strComputer = "."
str7z = "C:\7-Zip\7z.exe"
i=0

If WScript.Arguments.Count = 0 then
WScript.Echo "Directory path missing. Usage: cscript zippit.vbs dir1 ""dir 2"" (etc)"
Else
'For each folder passed as an argument
For Each strArgument in WScript.Arguments
strDir = WScript.Arguments.Item(i)
Set oShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'fiel collection inside strDir folder
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='"& strDir &"'} Where " _
& "ResultClass = CIM_DataFile")
'Process monitor; will report upon closed \ finished processes
Set colMonitoredProcesses = objWMIService. _
ExecNotificationQuery("select * from __instancedeletionevent " _
& "within 1 where TargetInstance isa 'Win32_Process'")

If colFiles.Count >0 Then

'for each file
For Each objFile In colFiles
'only logs mater.any other file is skiped

If WMIDateStringToDate(objFile.CreationDate) > now() And objFile.Extension = "txt" Then
'compression routine
oShell.Run ""& str7z &" a -tzip """& strDir &"""\"& objFile.FileName &".zip "_
& " """& strDir &"""\"& objFile.FileName &".txt"
WScript.Echo "Created the file "& strDir &"\"& objFile.FileName &".zip "

'log deletion routine
z = 0
'wainting Loop (sleep until zip is done)
Do While z = 0
Set objLatestProcess = colMonitoredProcesses.NextEvent
If objLatestProcess.TargetInstance.Name = "7z.exe" Then
'7z process ended; i can leave the waiting loop and delete the original file
objFile.Delete
WScript.Echo "Deleted the file "& strDir &"\"& objFile.FileName &"."& objFile.Extension &" "
z = 1
Exit Do
End If
Loop

End If
Next
End if

i = i+1
Next
End If

'change date format returned by wmi_script
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & _
Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate, _
13, 2))
End Function

WScript.Quit

'as with everything else on this blog.... Creative Commons apply to this code.

Wednesday, February 21, 2007

carla, you still owe me for this one!

A friend of mine called me recently at 1 a.m. and asked me for some coding help. I had insomnia so, i agreed to help.

Two hours of phone coding later this was the best i could do. Not bad, if you consider that all i had was a pen, some paper and her telling me if the code compiled successfully and if the result was what she expected.

By the way, if you know what kind of code this is and are looking for a job, send me your CV. I might know of a job opening for you.

local desconta, cod_subsrefeicao, N

desconta = .f.
cod_subsrefeicao = 107
N = 0

if txnlevel() = 0
select prre
scan
if prre.cr = 399 or (prre.cr <350 or >364)
desconta = .t.
N = N + prre.rqtt
end if
end scan
if N >0
goto top
scan
if prre.cr = cod_subsrefeicao
scatter name myPRRE
select prre
gather name myPRRE
replace prre.rqtt with rqtt
replace prre.ere with prre.ere * 1-(n/30)
replace prre.re with ((prre.re * N) /30)
replace prre.ervu with ((prre.ervu * N) /30)
REPLACE PRRE.RVU WITH ((PRRE.RVU * N) /30)
replace prre.Lordem with prre.Lordem * 100

scatter name myPRRE
select prre
append blank
gather name myPRRE
replace prre.rqtt with rqtt
replace prre.ere with ((prre.ere * N) /30) * -1
replace prre.re with ((prre.re * N) /30) * -1
replace prre.ervu with ((prre.ervu * N) /30) * -1
REPLACE PRRE.RVU WITH ((PRRE.RVU * N) /30) * -1
replcace prre.rem with "reembolso subsidio refeicÁ„o"
replace prre.Lordem with prre.Lordem * 100

m.mxsujirs = 0
m.mvirs = 0
declare a_diff (L),a_mesano(L),a_dtsuj(L)
a_diff = 0
a_mesano= 0
a_dtsuj= 0
do prantirs with pr.no, pr.data, m.eur_def
do prproc
do prprocf
exit
end if
end scan
else
goto top
scan
if prre.cr = cod_subsrefeicao
scatter name myPRRE
select prre
append blank
gather name myPRRE
replace prre.rqtt with rqtt
replace prre.ere with prre.ere * -1
replace prre.re with prre.re * -1
replace prre.ervu with prre.ervu * -1
REPLACE PRRE.RVU WITH PRRE.RVU * -1
replcace prre.rem with "reembolso subsidio refeicÁ„o"
replace prre.Lordem with prre.Lordem * 100

m.mxsujirs = 0
m.mvirs = 0
declare a_diff (L),a_mesano(L),a_dtsuj(L)
a_diff = 0
a_mesano= 0
a_dtsuj= 0
do prantirs with pr.no, pr.data, m.eur_def
do prproc
do prprocf
exit
end if
end scan
end if
end if
return prbairro