Commit 78c4a4d8 authored by Eric Coissac's avatar Eric Coissac

A first functionnal version of the normalize_plastid.sh script.

parent 9bad3443
#!/usr/bin/env python
import sys
data = open(sys.argv[1])
repeats = open(sys.argv[2])
chloro = {'LSC' : [], 'SSC' : [] }
chlorosize =0
for line in data:
parts = line.strip().split()
if len(parts) >= 4:
single = parts[0]
begin = int(parts[1])
end = int(parts[2])
direction = int(parts[3])
if direction==0:
direction=-1
if end > chlorosize:
extsize = end - chlorosize
chloro['LSC'].extend([0] * extsize)
chloro['SSC'].extend([0] * extsize)
chlorosize=len(chloro['LSC'])
begin-=1
chr = chloro[single]
for p in range(begin,end):
chr[p]+=direction
maxSSC = float(max(abs(n) for n in chloro['SSC']))
maxLSC = float(max(abs(n) for n in chloro['LSC']))
chloro['SSC']=[n / maxSSC for n in chloro['SSC']]
chloro['LSC']=[n / maxLSC for n in chloro['LSC']]
scoreMax=0
imax = len(chloro['LSC'])
for line in repeats:
parts = line.strip().split()
pos1 = int(parts[1]) -1
len1 = int(parts[3])
pos2 = int(parts[2]) -1
len2 = int(parts[4])
c_begin = min(pos1 + len1,imax)
c_end = min(pos2,imax)
o_max = min(pos1 ,imax)
o_min = min(pos2 + len2, imax)
c_lsc = sum(abs(chloro['LSC'][n]) for n in range(c_begin,c_end))
c_ssc = sum(abs(chloro['SSC'][n]) for n in range(c_begin,c_end))
o_lsc = sum(abs(chloro['LSC'][n]) for n in range(0,o_max))
o_ssc = sum(abs(chloro['SSC'][n]) for n in range(0,o_max))
o_lsc += sum(abs(chloro['LSC'][n]) for n in range(o_min,len(chloro['LSC'])))
o_ssc += sum(abs(chloro['SSC'][n]) for n in range(o_min,len(chloro['SSC'])))
c = float(c_lsc + c_ssc)
o = float(o_lsc + o_ssc)
if c > 0:
c_lsc /= c
c_ssc /= c
if o > 0:
o_lsc /= o
o_ssc /= o
score = ((c_lsc - c_ssc) ** 2 + (o_lsc - o_ssc) ** 2) / 2.0
# print >>sys.stderr,"c.lsc = %f c.ssc = %f o.lsc = %f o.ssc = %f score = %6.4f (len=%d)" % (c_lsc,c_ssc,o_lsc,o_ssc,score,len1)
if (score > scoreMax):
scoreMax = score
pos1Max = pos1
pos2Max = pos2
len1Max = len1
len2Max = len2
c_begin = min(pos1Max + len1Max,imax)
c_end = min(pos2Max,imax)
o_max = min(pos1Max,imax)
o_min = min(pos2Max + len2Max,imax)
c_lsc = sum(chloro['LSC'][n] for n in range(c_begin,c_end))
c_ssc = sum(chloro['SSC'][n] for n in range(c_begin,c_end))
o_lsc = sum(chloro['LSC'][n] for n in range(0,o_max))
o_ssc = sum(chloro['SSC'][n] for n in range(0,o_max))
o_lsc += sum(chloro['LSC'][n] for n in range(o_min,len(chloro['LSC'])))
o_ssc += sum(chloro['SSC'][n] for n in range(o_min,len(chloro['SSC'])))
if abs(c_lsc) > abs(c_ssc):
center = "LSC"
dcenter= "+" if c_lsc > 0 else "-"
else:
center = "SSC"
dcenter= "+" if c_ssc > 0 else "-"
if abs(o_lsc) > abs(o_ssc):
out = "LSC"
dout = "+" if o_lsc > 0 else "-"
else:
out = "SSC"
dout = "+" if o_ssc > 0 else "-"
sys.stdout.write("%s %s %s %s %d %d %d %d %6.5f\n" % (center,
dcenter,
out,
dout,
pos1Max + 1,
len1Max,
pos2Max + 1,
len2Max,
scoreMax))
#for p in range(chlorosize):
# sys.stdout.write("%d %d %d\n" % (p,chloro['SSC'][p],chloro['LSC'][p]))
\ No newline at end of file
......@@ -62,10 +62,58 @@ function logwarning {
# Sequence related functions
# Counts how many sequences are stored in a fasta file
# - $1 : The fasta file to count
function fastaCount {
grep '^>' $1 | wc -l
}
# compute the sequence length from a fasta sequence
# - $1 : The fasta file to cut
function seqlength {
cat $1 | \
wc |\
awk -v t="`head -1 $1 | wc -c`" '{print $3 - t - $1 + 1}'
}
# extract a subseq from a fasta sequence
# - $1 : The fasta file to cut
# - $2 : First position of the subsequence (first position is numered 1),
# - $3 : End of the subsequence (included in the subsequence)
function cutseq {
awk -v from=$2 -v end=$3 'function printfasta(seq) { \
seqlen=length(seq); \
for (i=1; i <= seqlen; i+=60) \
print substr(seq,i,60); \
} \
\
/^>/ {print $0} \
! /^>/ {seq=seq$0} \
END {printfasta(substr(seq,from,end-from+1))}' $1
}
# Joins a set of sequences stored in a fasta file into
# a single sequence
# - $1 : The fasta file containing the sequences to join
function joinfasta {
awk '(NR==1 && /^>/) {print $0} \
! /^>/ {print $0}' $1 | \
formatfasta
}
function formatfasta {
awk 'function printfasta(seq) { \
seqlen=length(seq); \
for (i=1; i <= seqlen; i+=60) \
print substr(seq,i,60); \
} \
/^>/ { print $0 } \
! /^>/ { seq=seq $0 } \
END { printfasta(seq)}' $1
}
#
#
########################
......@@ -115,3 +163,4 @@ export PATH
export LANG=C
export LC_ALL=C
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment