4
$\begingroup$

I have the following sets of data:

file1:

1   15776220    15776240    GTGACCAGCAGGTGTCTCTG    16855676    16855696    CTGTCCAGCAGAGGGCGGTG 

file2:

1   15776231    2   5008    G:5002  A:6 
1   15776239    3   5008    C:3358  A:14    G:1636

file3:

1   16855677    2   5008    A:5003  C:5
1   16855689    3   5008    G:4957  A:41    T:10

Most lines have 6 columns but a few lines also have 7 columns which have data that I want to preserve.

Can a script be written which does the following:

if $2 of file2 comes in between $2 and $3 of file1 (between the interval of $2 and $3) and $2 of file3 comes in between $5 and $6 of file1 (between the interval of $5 and $6), then the output would be $1,$2,$3,$4 from file1 and $2,$3,$4,$5,$6,$7 from file2 and $5,$6,$7 from file1 and $2,$3,$4,$5,$6,$7 from file 3, all in one line. So something like this:

1   15776220    15776240    GTGACCAGCAGGTGTCTCTG    15776231    2   5008    G:5002  A:6 16855676    16855696    CTGTCCAGCAGAGGGCGGTG    16855677    2   5008    A:5003  C:5

1   15776220    15776240    GTGACCAGCAGGTGTCTCTG    15776239    3   5008    C:3358  A:14    G:1636  16855676    16855696    CTGTCCAGCAGAGGGCGGTG    1   16855689    3   5008    G:4957  A:41    T:10

Something like bedtools does but I am not able to use bedtools one this since I don't have two columns in file2 and file3. The intersection is based on $2 in both the files, and all the other columns after it are just copied

(There could be many lines from file2 and file3 intersecting in the same line in file1,so that line could be repeated based on the number of intersections)

$\endgroup$
  • $\begingroup$ Can't you just munge file2 to add an end coordinate and then use bedtools intersect? For example awk '{$3 = $2+1 FS $3}' file2 > file2.modified, assuming the coordinates are 0-based. $\endgroup$ – Devon Ryan Jan 2 '18 at 14:42
  • $\begingroup$ Can you please clarify how this is related to your previous question? $\endgroup$ – heathobrien Jan 2 '18 at 14:51
  • $\begingroup$ @heathobrien it's an extension. When the OP asked the previous question, he hadn't realized there were certain issues in the input files. So I suggested he ask a new question showing the files he currently has after fixing these issue. This is now about getting information from three separate files and two formats (number of fields) per file. So quite a different issue. $\endgroup$ – terdon Jan 2 '18 at 16:34
4
$\begingroup$

An awk solution

awk '
    FILENAME == ARGV[1] {f2[$2] = $2 OFS $3 OFS $4 OFS $5 OFS $6 OFS $7; next}
    FILENAME == ARGV[2] {f3[$2] = $2 OFS $3 OFS $4 OFS $5 OFS $6 OFS $7; next}
    {
        for (key2 in f2) {
            for (key3 in f3) {
                if (($2 <= key2 && key2 <= $3) && ($5 <= key3 && key3 <= $6)) {
                    print $1,$2,$3,$4, f2[key2], $5,$6,$7, f3[key3]
                }
            }
        }
    }
' file2 file3 file1

outputs

1 15776220 15776240 GTGACCAGCAGGTGTCTCTG 15776231 2 5008 G:5002 A:6  16855676 16855696 CTGTCCAGCAGAGGGCGGTG 16855677 2 5008 A:5003 C:5
1 15776220 15776240 GTGACCAGCAGGTGTCTCTG 15776231 2 5008 G:5002 A:6  16855676 16855696 CTGTCCAGCAGAGGGCGGTG 16855689 3 5008 G:4957 A:41 T:10
1 15776220 15776240 GTGACCAGCAGGTGTCTCTG 15776239 3 5008 C:3358 A:14 G:1636 16855676 16855696 CTGTCCAGCAGAGGGCGGTG 16855677 2 5008 A:5003 C:5
1 15776220 15776240 GTGACCAGCAGGTGTCTCTG 15776239 3 5008 C:3358 A:14 G:1636 16855676 16855696 CTGTCCAGCAGAGGGCGGTG 16855689 3 5008 G:4957 A:41 T:10

For your sample data, all combinations of records in file2 and file3 meet the criteria for the file1 record.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.